1
1
import { FastifyRequest , FastifyReply , FastifyInstance , RegisterOptions } from 'fastify' ;
2
2
import { ANIME } from '@consumet/extensions' ;
3
+ import { StreamingServers } from '@consumet/extensions/dist/models' ;
3
4
4
5
const routes = async ( fastify : FastifyInstance , options : RegisterOptions ) => {
5
6
const anix = new ANIME . Anix ( ) ;
@@ -26,10 +27,16 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
26
27
'/recent-episodes' ,
27
28
async ( request : FastifyRequest , reply : FastifyReply ) => {
28
29
const { page = 1 } = request . query as { page ?: number } ;
29
-
30
+ const type = ( request . query as { type ?: number } ) . type ;
31
+
30
32
try {
31
- const res = await anix . fetchRecentEpisodes ( page ) ;
32
-
33
+ let res ;
34
+ if ( typeof type === 'undefined' ) {
35
+ res = await anix . fetchRecentEpisodes ( page ) ;
36
+ } else {
37
+ res = await anix . fetchRecentEpisodes ( page , type ) ;
38
+ }
39
+
33
40
reply . status ( 200 ) . send ( res ) ;
34
41
} catch ( err ) {
35
42
console . error ( err ) ;
@@ -60,11 +67,20 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
60
67
'/watch/:id/:episodeId' ,
61
68
async ( request : FastifyRequest , reply : FastifyReply ) => {
62
69
const { id, episodeId } = request . params as { id : string ; episodeId : string } ;
63
- const { server } = request . query as { server ?: string } ;
70
+ const server = ( request . query as { server : string } ) . server as StreamingServers ;
71
+ const type = ( request . query as { type : string } ) . type ?? 'sub' ;
64
72
65
- try {
66
- const res = await anix . fetchEpisodeSources ( id , episodeId , server ) ;
73
+ if ( typeof id === 'undefined' )
74
+ return reply . status ( 400 ) . send ( { message : 'id is required' } ) ;
67
75
76
+ if ( typeof episodeId === 'undefined' )
77
+ return reply . status ( 400 ) . send ( { message : 'episodeId is required' } ) ;
78
+
79
+ try {
80
+ const res = await anix
81
+ . fetchEpisodeSources ( id , episodeId , server , type )
82
+ . catch ( ( err ) => reply . status ( 404 ) . send ( { message : err } ) ) ;
83
+
68
84
reply . status ( 200 ) . send ( res ) ;
69
85
} catch ( err ) {
70
86
console . error ( err ) ;
@@ -79,10 +95,26 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
79
95
'/servers/:id/:episodeId' ,
80
96
async ( request : FastifyRequest , reply : FastifyReply ) => {
81
97
const { id, episodeId } = request . params as { id : string ; episodeId : string } ;
98
+ const type = ( request . query as { type ?: string } ) . type ;
82
99
83
- try {
84
- const res = await anix . fetchEpisodeServers ( id , episodeId ) ;
100
+ if ( typeof id === 'undefined' )
101
+ return reply . status ( 400 ) . send ( { message : 'id is required' } ) ;
85
102
103
+ if ( typeof episodeId === 'undefined' )
104
+ return reply . status ( 400 ) . send ( { message : 'episodeId is required' } ) ;
105
+
106
+ try {
107
+ let res ;
108
+ if ( typeof type === 'undefined' ) {
109
+ res = await anix
110
+ . fetchEpisodeServers ( id , episodeId )
111
+ . catch ( ( err ) => reply . status ( 404 ) . send ( { message : err } ) ) ; ;
112
+ } else {
113
+ res = await anix
114
+ . fetchEpisodeServerType ( id , episodeId , type )
115
+ . catch ( ( err ) => reply . status ( 404 ) . send ( { message : err } ) ) ; ;
116
+ }
117
+
86
118
reply . status ( 200 ) . send ( res ) ;
87
119
} catch ( err ) {
88
120
console . error ( err ) ;
0 commit comments