diff --git a/backend/src/controller/AniListController.ts b/backend/src/controller/AniListController.ts new file mode 100644 index 0000000..e1e357b --- /dev/null +++ b/backend/src/controller/AniListController.ts @@ -0,0 +1,21 @@ +import {NextFunction, Request, Response} from 'express'; + +export default class AniListController { + async graphql(req: Request, res: Response, next: NextFunction): Promise { + try { + const fromApi = await fetch('https://graphql.anilist.co/graphql', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(req.body), + }); + + const fromApiText = await fromApi.text(); + res.status(fromApi.status).send(fromApiText); + } catch (e) { + console.error(e); + } + next(); + } +} \ No newline at end of file diff --git a/backend/src/router/AniListRouter.ts b/backend/src/router/AniListRouter.ts index 5333c6c..1005f8e 100644 --- a/backend/src/router/AniListRouter.ts +++ b/backend/src/router/AniListRouter.ts @@ -1,24 +1,9 @@ -import {NextFunction, Request, Response, Router} from 'express'; +import {Router} from 'express'; +import AniListController from '../controller/AniListController'; export default function aniListRouter(): Router { + const controller = new AniListController(); const router = Router(); - router.post('/graphql', async (req: Request, res: Response, next: NextFunction) => { - try { - const fromApi = await fetch('https://graphql.anilist.co/graphql', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(req.body), - }); - - const fromApiText = await fromApi.text() - res.status(fromApi.status).send(fromApiText); - } catch (e) { - console.error(e); - } - next(); - }); - + router.post('/graphql', controller.graphql.bind(controller)); return router; }