Skip to content

Commit 3af46ae

Browse files
authored
Merge pull request #335 from the-hideout/lite-api
Lite api
2 parents 07e6fe8 + f0fffa8 commit 3af46ae

File tree

7 files changed

+304
-55
lines changed

7 files changed

+304
-55
lines changed

graphql-yoga.mjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import graphqlUtil from './utils/graphql-util.mjs';
77
import graphQLOptions from './utils/graphql-options.mjs';
88

99
import useRequestTimer from './plugins/plugin-request-timer.mjs';
10-
import useHttpServer from './plugins/plugin-http-server.mjs';
10+
import useGraphQLOrigin from './plugins/plugin-graphql-origin.mjs';
1111
import useCacheMachine from './plugins/plugin-use-cache-machine.mjs';
1212
import useTwitch from './plugins/plugin-twitch.mjs';
1313
import useNightbot from './plugins/plugin-nightbot.mjs';
1414
import usePlayground from './plugins/plugin-playground.mjs';
1515
import useOptionMethod from './plugins/plugin-option-method.mjs';
16+
import useLiteApi from './plugins/plugin-lite-api.mjs';
1617

1718
let dataAPI, yoga;
1819

@@ -45,9 +46,10 @@ export default async function getYoga(env) {
4546
useOptionMethod(),
4647
useTwitch(env),
4748
usePlayground(),
48-
useNightbot(env),
49-
useHttpServer(env),
5049
useCacheMachine(env),
50+
useGraphQLOrigin(env),
51+
useNightbot(env),
52+
useLiteApi(env),
5153
],
5254
cors: {
5355
origin: graphQLOptions.cors.allowOrigin,

http/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.mjs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ import graphQLOptions from './utils/graphql-options.mjs';
2626
import cacheMachine from './utils/cache-machine.mjs';
2727
import fetchWithTimeout from './utils/fetch-with-timeout.mjs';
2828

29-
import { getNightbotResponse } from './plugins/plugin-nightbot.mjs';
29+
import { getNightbotResponse, useNightbotOnUrl } from './plugins/plugin-nightbot.mjs';
3030
import { getTwitchResponse } from './plugins/plugin-twitch.mjs';
31+
import { getLiteApiResponse, useLiteApiOnUrl } from './plugins/plugin-lite-api.mjs';
3132

3233
let dataAPI;
3334

@@ -108,12 +109,15 @@ async function graphqlHandler(request, env, ctx) {
108109
//console.log(`Skipping cache in ${ENVIRONMENT} environment`);
109110
}
110111

111-
// if an HTTP GraphQL server is configured, pass the request to that
112+
// if an origin server is configured, pass the request
112113
if (env.USE_ORIGIN === 'true') {
113114
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',
117121
body: JSON.stringify({
118122
query,
119123
variables,
@@ -127,10 +131,10 @@ async function graphqlHandler(request, env, ctx) {
127131
if (queryResult.status !== 200) {
128132
throw new Error(`${queryResult.status} ${await queryResult.text()}`);
129133
}
130-
console.log('Request served from graphql server');
134+
console.log('Request served from origin server');
131135
return new Response(await queryResult.text(), responseOptions);
132136
} catch (error) {
133-
console.error(`Error getting response from GraphQL server: ${error}`);
137+
console.error(`Error getting response from origin server: ${error}`);
134138
}
135139
}
136140

@@ -206,47 +210,42 @@ export default {
206210
const requestStart = new Date();
207211
const url = new URL(request.url);
208212

209-
let response;
210-
211213
try {
212214
if (url.pathname === '/twitch') {
213-
response = await getTwitchResponse(env);
215+
const response = await getTwitchResponse(env);
214216
if (graphQLOptions.cors) {
215217
setCors(response, graphQLOptions.cors);
216218
}
219+
return response;
217220
}
218221

219222
if (url.pathname === graphQLOptions.playgroundEndpoint) {
220223
//response = playground(request, graphQLOptions);
221-
response = graphiql(graphQLOptions);
222-
}
223-
224-
if (graphQLOptions.forwardUnmatchedRequestsToOrigin) {
225-
return fetch(request);
224+
return graphiql(graphQLOptions);
226225
}
227226

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);
233233
}
234234

235235
if (url.pathname === graphQLOptions.baseEndpoint) {
236-
response = await graphqlHandler(request, env, ctx);
236+
const response = await graphqlHandler(request, env, ctx);
237237
if (graphQLOptions.cors) {
238238
setCors(response, graphQLOptions.cors);
239239
}
240+
return response;
240241
}
241242

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 });
247244
} catch (err) {
248245
console.log(err);
249246
return new Response(graphQLOptions.debug ? err : 'Something went wrong', { status: 500 });
247+
} finally {
248+
console.log(`Response time: ${new Date() - requestStart} ms`);
250249
}
251250
},
252251
};

package-lock.json

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/plugin-http-server.mjs renamed to plugins/plugin-graphql-origin.mjs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1-
import graphQLOptions from '../utils/graphql-options.mjs';
1+
// Pass the request to an origin server if USE_ORIGIN is set to 'true'
2+
import fetchWithTimeout from '../utils/fetch-with-timeout.mjs';
23

3-
export default function useHttpServer(env) {
4+
export default function useGraphQLOrigin(env) {
45
return {
56
async onParams({params, request, setParams, setResult, fetchAPI}) {
6-
// if an HTTP GraphQL server is configured, pass the request to that
77
if (env.USE_ORIGIN !== 'true') {
88
return;
99
}
1010
try {
11-
const serverUrl = `https://api.tarkov.dev${graphQLOptions.baseEndpoint}`;
12-
const queryResult = await fetch(serverUrl, {
11+
const originUrl = new URL(request.url);
12+
if (env.ORIGIN_OVERRIDE) {
13+
originUrl.host = env.ORIGIN_OVERRIDE;
14+
}
15+
const queryResult = await fetchWithTimeout(originUrl, {
1316
method: request.method,
1417
body: JSON.stringify(params),
1518
headers: {
1619
'Content-Type': 'application/json',
1720
},
21+
timeout: 20000
1822
});
1923
if (queryResult.status !== 200) {
2024
throw new Error(`${queryResult.status} ${queryResult.statusText}: ${await queryResult.text()}`);
2125
}
22-
console.log('Request served from graphql server');
26+
console.log('Request served from origin server');
2327
setResult(await queryResult.json());
2428
request.cached = true;
2529
} catch (error) {
26-
console.error(`Error getting response from GraphQL server: ${error}`);
30+
console.error(`Error getting response from origin server: ${error}`);
2731
}
2832
},
2933
}

0 commit comments

Comments
 (0)