zyachel
2023-04-15 21:02:10 +05:30
parent 18ca98fd4a
commit 75732e0086
21 changed files with 2150 additions and 2 deletions

View File

@ -0,0 +1,28 @@
import * as cheerio from 'cheerio';
import RawName from 'src/interfaces/misc/rawName';
import axiosInstance from 'src/utils/axiosInstance';
import cleanName from 'src/utils/cleaners/name';
import { AppError } from 'src/utils/helpers';
const name = async (nameId: string) => {
try {
// getting data
const res = await axiosInstance(`/name/${nameId}`);
const $ = cheerio.load(res.data);
const rawData = $('script#__NEXT_DATA__').text();
// cleaning it a bit
const parsedRawData: RawName = JSON.parse(rawData);
const cleanData = cleanName(parsedRawData);
// returning
return cleanData;
} catch (err: any) {
if (err.response?.status === 404) throw new AppError('not found', 404, err.cause);
console.warn(err);
throw new AppError('something went wrong', 500, err.cause);
}
};
export default name;