|
| 1 | +import { FastifyRequest, FastifyReply, FastifyInstance, RegisterOptions } from 'fastify'; |
| 2 | +import { ANIME } from '@consumet/extensions'; |
| 3 | + |
| 4 | +const routes = async (fastify: FastifyInstance, options: RegisterOptions) => { |
| 5 | + const anix = new ANIME.Anix(); |
| 6 | + |
| 7 | + fastify.get('/', (_, rp) => { |
| 8 | + rp.status(200).send({ |
| 9 | + intro: |
| 10 | + "Welcome to the Anix provider: check out the provider's website @ https://anix.sh", |
| 11 | + routes: ['/:query', '/recent-episodes', '/info/:id', '/watch/:id/:episodeId', '/servers/:id/:episodeId'], |
| 12 | + documentation: 'https://docs.consumet.org/#tag/anix', |
| 13 | + }); |
| 14 | + }); |
| 15 | + |
| 16 | + fastify.get('/:query', async (request: FastifyRequest, reply: FastifyReply) => { |
| 17 | + const query = (request.params as { query: string }).query; |
| 18 | + const { page = 1 } = request.query as { page?: number }; |
| 19 | + |
| 20 | + const res = await anix.search(query, page); |
| 21 | + |
| 22 | + reply.status(200).send(res); |
| 23 | + }); |
| 24 | + |
| 25 | + fastify.get( |
| 26 | + '/recent-episodes', |
| 27 | + async (request: FastifyRequest, reply: FastifyReply) => { |
| 28 | + const { page = 1 } = request.query as { page?: number }; |
| 29 | + |
| 30 | + try { |
| 31 | + const res = await anix.fetchRecentEpisodes(page); |
| 32 | + |
| 33 | + reply.status(200).send(res); |
| 34 | + } catch (err) { |
| 35 | + console.error(err); |
| 36 | + reply |
| 37 | + .status(500) |
| 38 | + .send({ message: 'Something went wrong. Contact developer for help.' }); |
| 39 | + } |
| 40 | + }, |
| 41 | + ); |
| 42 | + |
| 43 | + fastify.get('/info/:id', async (request: FastifyRequest, reply: FastifyReply) => { |
| 44 | + const id = decodeURIComponent((request.params as { id: string }).id); |
| 45 | + |
| 46 | + try { |
| 47 | + const res = await anix |
| 48 | + .fetchAnimeInfo(id) |
| 49 | + .catch((err) => reply.status(404).send({ message: err })); |
| 50 | + |
| 51 | + reply.status(200).send(res); |
| 52 | + } catch (err) { |
| 53 | + reply |
| 54 | + .status(500) |
| 55 | + .send({ message: 'Something went wrong. Contact developer for help.' }); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + fastify.get( |
| 60 | + '/watch/:id/:episodeId', |
| 61 | + async (request: FastifyRequest, reply: FastifyReply) => { |
| 62 | + const { id, episodeId } = request.params as { id: string; episodeId: string }; |
| 63 | + const { server } = request.query as { server?: string }; |
| 64 | + |
| 65 | + try { |
| 66 | + const res = await anix.fetchEpisodeSources(id, episodeId, server); |
| 67 | + |
| 68 | + reply.status(200).send(res); |
| 69 | + } catch (err) { |
| 70 | + console.error(err); |
| 71 | + reply |
| 72 | + .status(500) |
| 73 | + .send({ message: 'Something went wrong. Contact developer for help.' }); |
| 74 | + } |
| 75 | + }, |
| 76 | + ); |
| 77 | + |
| 78 | + fastify.get( |
| 79 | + '/servers/:id/:episodeId', |
| 80 | + async (request: FastifyRequest, reply: FastifyReply) => { |
| 81 | + const { id, episodeId } = request.params as { id: string; episodeId: string }; |
| 82 | + |
| 83 | + try { |
| 84 | + const res = await anix.fetchEpisodeServers(id, episodeId); |
| 85 | + |
| 86 | + reply.status(200).send(res); |
| 87 | + } catch (err) { |
| 88 | + console.error(err); |
| 89 | + reply |
| 90 | + .status(500) |
| 91 | + .send({ message: 'Something went wrong. Contact developer for help.' }); |
| 92 | + } |
| 93 | + }, |
| 94 | + ); |
| 95 | +}; |
| 96 | + |
| 97 | +export default routes; |
0 commit comments