Socialify

Folder ..

Viewing ann.ts
48 lines (40 loc) • 1.4 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
import { NEWS, Topics } from '@consumet/extensions';
import { FastifyRequest, FastifyReply, FastifyInstance, RegisterOptions } from 'fastify';

const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
  const ann = new NEWS.ANN();

  fastify.get('/', (_, rp) => {
    rp.status(200).send({
      intro:
        "Welcome to the Anime News Network provider: check out the provider's website @ https://www.animenewsnetwork.com/",
      routes: ['/recent-feeds', '/info'],
      documentation: 'https://docs.consumet.org/#tag/animenewsnetwork',
    });
  });

  fastify.get('/recent-feeds', async (req: FastifyRequest, reply: FastifyReply) => {
    let { topic } = req.query as { topic?: Topics };

    try {
      const feeds = await ann.fetchNewsFeeds(topic);
      reply.status(200).send(feeds);
    } catch (e) {
      reply.status(500).send({
        message: (e as Error).message,
      });
    }
  });

  fastify.get('/info', async (req: FastifyRequest, reply: FastifyReply) => {
    const { id } = req.query as { id: string };

    if (typeof id === 'undefined')
      return reply.status(400).send({
        message: 'id is required',
      });

    try {
      const info = await ann.fetchNewsInfo(id);
      reply.status(200).send(info);
    } catch (error) {
      reply.status(500).send({
        message: (error as Error).message,
      });
    }
  });
};

export default routes;