Socialify

Folder ..

Viewing index.ts
50 lines (41 loc) • 1.6 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
import { FastifyRequest, FastifyReply, FastifyInstance, RegisterOptions } from 'fastify';
import { PROVIDERS_LIST } from '@consumet/extensions';

import anilist from './anilist';
import anilistManga from './anilist-manga';
import mal from './mal';
import tmdb from './tmdb';
const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
  await fastify.register(anilist, { prefix: '/anilist' });
  await fastify.register(anilistManga, { prefix: '/anilist-manga' });
  await fastify.register(mal, { prefix: '/mal' });
  await fastify.register(tmdb, { prefix: '/tmdb' });

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

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

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

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

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

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

    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;