Skip to content

Commit f4613b3

Browse files
Babyyoda777riimuru
andauthored
feat(TMDB): Add /trending endpoint (#580)
* Add /trending path to TMDB --------- Co-authored-by: Marouane <57333995+riimuru@users.noreply.github.com>
1 parent 404f425 commit f4613b3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/routes/meta/tmdb.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
4040
reply.status(200).send(res);
4141
});
4242

43+
fastify.get('/trending', async (request: FastifyRequest, reply: FastifyReply) => {
44+
const validTimePeriods = new Set(['day', 'week'] as const);
45+
type validTimeType = typeof validTimePeriods extends Set<infer T> ? T : undefined
46+
47+
const type = (request.query as { type?: string }).type || 'all';
48+
let timePeriod = (request.query as { timePeriod?: validTimeType }).timePeriod || 'day';
49+
50+
// make day as default time period
51+
if (!validTimePeriods.has(timePeriod)) timePeriod = 'day';
52+
53+
const page = (request.query as { page?: number }).page || 1;
54+
55+
const tmdb = new META.TMDB(tmdbApi);
56+
57+
try {
58+
const res = await tmdb.fetchTrending(type, timePeriod, page);
59+
reply.status(200).send(res);
60+
} catch (err) {
61+
reply.status(500).send({ message: 'Failed to fetch trending media.' });
62+
}
63+
});
64+
4365
fastify.get(
4466
'/watch/:episodeId',
4567
async (request: FastifyRequest, reply: FastifyReply) => {

0 commit comments

Comments
 (0)