backend: separate anilist router into router and controller

This commit is contained in:
wea_ondara
2023-11-25 17:06:36 +01:00
parent 941d0d28a4
commit 9c0b266cbb
2 changed files with 25 additions and 19 deletions

View File

@@ -0,0 +1,21 @@
import {NextFunction, Request, Response} from 'express';
export default class AniListController {
async graphql(req: Request, res: Response, next: NextFunction): Promise<void> {
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();
}
}

View File

@@ -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;
}