@@ -26,8 +26,9 @@ import graphQLOptions from './utils/graphql-options.mjs';
26
26
import cacheMachine from './utils/cache-machine.mjs' ;
27
27
import fetchWithTimeout from './utils/fetch-with-timeout.mjs' ;
28
28
29
- import { getNightbotResponse } from './plugins/plugin-nightbot.mjs' ;
29
+ import { getNightbotResponse , useNightbotOnUrl } from './plugins/plugin-nightbot.mjs' ;
30
30
import { getTwitchResponse } from './plugins/plugin-twitch.mjs' ;
31
+ import { getLiteApiResponse , useLiteApiOnUrl } from './plugins/plugin-lite-api.mjs' ;
31
32
32
33
let dataAPI ;
33
34
@@ -108,12 +109,15 @@ async function graphqlHandler(request, env, ctx) {
108
109
//console.log(`Skipping cache in ${ENVIRONMENT} environment`);
109
110
}
110
111
111
- // if an HTTP GraphQL server is configured, pass the request to that
112
+ // if an origin server is configured, pass the request
112
113
if ( env . USE_ORIGIN === 'true' ) {
113
114
try {
114
- const serverUrl = `https://api.tarkov.dev${ graphQLOptions . baseEndpoint } ` ;
115
- const queryResult = await fetchWithTimeout ( serverUrl , {
116
- method : request . method ,
115
+ const originUrl = new URL ( request . url ) ;
116
+ if ( env . ORIGIN_OVERRIDE ) {
117
+ originUrl . host = env . ORIGIN_OVERRIDE ;
118
+ }
119
+ const queryResult = await fetchWithTimeout ( originUrl , {
120
+ method : 'POST' ,
117
121
body : JSON . stringify ( {
118
122
query,
119
123
variables,
@@ -127,10 +131,10 @@ async function graphqlHandler(request, env, ctx) {
127
131
if ( queryResult . status !== 200 ) {
128
132
throw new Error ( `${ queryResult . status } ${ await queryResult . text ( ) } ` ) ;
129
133
}
130
- console . log ( 'Request served from graphql server' ) ;
134
+ console . log ( 'Request served from origin server' ) ;
131
135
return new Response ( await queryResult . text ( ) , responseOptions ) ;
132
136
} catch ( error ) {
133
- console . error ( `Error getting response from GraphQL server: ${ error } ` ) ;
137
+ console . error ( `Error getting response from origin server: ${ error } ` ) ;
134
138
}
135
139
}
136
140
@@ -206,47 +210,42 @@ export default {
206
210
const requestStart = new Date ( ) ;
207
211
const url = new URL ( request . url ) ;
208
212
209
- let response ;
210
-
211
213
try {
212
214
if ( url . pathname === '/twitch' ) {
213
- response = await getTwitchResponse ( env ) ;
215
+ const response = await getTwitchResponse ( env ) ;
214
216
if ( graphQLOptions . cors ) {
215
217
setCors ( response , graphQLOptions . cors ) ;
216
218
}
219
+ return response ;
217
220
}
218
221
219
222
if ( url . pathname === graphQLOptions . playgroundEndpoint ) {
220
223
//response = playground(request, graphQLOptions);
221
- response = graphiql ( graphQLOptions ) ;
222
- }
223
-
224
- if ( graphQLOptions . forwardUnmatchedRequestsToOrigin ) {
225
- return fetch ( request ) ;
224
+ return graphiql ( graphQLOptions ) ;
226
225
}
227
226
228
- if ( url . pathname === '/webhook/nightbot' ||
229
- url . pathname === '/webhook/stream-elements' ||
230
- url . pathname === '/webhook/moobot'
231
- ) {
232
- response = await getNightbotResponse ( request , url , env , ctx ) ;
227
+ if ( useNightbotOnUrl ( url ) ) {
228
+ return await getNightbotResponse ( request , url , env , ctx ) ;
229
+ }
230
+
231
+ if ( useLiteApiOnUrl ( url ) ) {
232
+ return await getLiteApiResponse ( request , url , env , ctx ) ;
233
233
}
234
234
235
235
if ( url . pathname === graphQLOptions . baseEndpoint ) {
236
- response = await graphqlHandler ( request , env , ctx ) ;
236
+ const response = await graphqlHandler ( request , env , ctx ) ;
237
237
if ( graphQLOptions . cors ) {
238
238
setCors ( response , graphQLOptions . cors ) ;
239
239
}
240
+ return response ;
240
241
}
241
242
242
- if ( ! response ) {
243
- response = new Response ( 'Not found' , { status : 404 } ) ;
244
- }
245
- console . log ( `Response time: ${ new Date ( ) - requestStart } ms` ) ;
246
- return response ;
243
+ return new Response ( 'Not found' , { status : 404 } ) ;
247
244
} catch ( err ) {
248
245
console . log ( err ) ;
249
246
return new Response ( graphQLOptions . debug ? err : 'Something went wrong' , { status : 500 } ) ;
247
+ } finally {
248
+ console . log ( `Response time: ${ new Date ( ) - requestStart } ms` ) ;
250
249
}
251
250
} ,
252
251
} ;
0 commit comments