Socialify

Folder ..

Viewing index.ts
61 lines (51 loc) • 2.1 KB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { FastifyRequest, FastifyReply, FastifyInstance, RegisterOptions } from 'fastify';
import { PROVIDERS_LIST } from '@consumet/extensions';

import gogoanime from './gogoanime';
import animepahe from './animepahe';
import zoro from './zoro';
import nineanime from './9anime';
import animefox from './animefox';
import anify from './anify';
import crunchyroll from './crunchyroll';
import bilibili from './bilibili';
import marin from './marin';

const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
  await fastify.register(gogoanime, { prefix: '/gogoanime' });
  await fastify.register(animepahe, { prefix: '/animepahe' });
  await fastify.register(zoro, { prefix: '/zoro' });
  await fastify.register(nineanime, { prefix: '/9anime' });
  await fastify.register(animefox, { prefix: '/animefox' });
  await fastify.register(anify, { prefix: '/anify' });
  await fastify.register(crunchyroll, { prefix: '/crunchyroll' });
  await fastify.register(bilibili, { prefix: '/bilibili' });
  await fastify.register(marin, { prefix: '/marin' });

  fastify.get('/', async (request: any, reply: any) => {
    reply.status(200).send('Welcome to Consumet Anime 🗾');
  });

  fastify.get('/:animeProvider', async (request: FastifyRequest, reply: FastifyReply) => {
    const queries: { animeProvider: string; page: number } = {
      animeProvider: '',
      page: 1,
    };

    queries.animeProvider = decodeURIComponent(
      (request.params as { animeProvider: string; page: number }).animeProvider,
    );

    queries.page = (request.query as { animeProvider: string; page: number }).page;

    if (queries.page! < 1) queries.page = 1;

    const provider = PROVIDERS_LIST.ANIME.find(
      (provider: any) => provider.toString.name === queries.animeProvider,
    );

    try {
      if (provider) {
        reply.redirect(`/anime/${provider.toString.name}`);
      } else {
        reply
          .status(404)
          .send({ message: 'Provider not found, please check the providers list.' });
      }
    } catch (err) {
      reply.status(500).send('Something went wrong. Please try again later.');
    }
  });
};

export default routes;