Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lite api #335

Merged
merged 11 commits into from
Sep 25, 2024
8 changes: 5 additions & 3 deletions graphql-yoga.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import graphqlUtil from './utils/graphql-util.mjs';
import graphQLOptions from './utils/graphql-options.mjs';

import useRequestTimer from './plugins/plugin-request-timer.mjs';
import useHttpServer from './plugins/plugin-http-server.mjs';
import useGraphQLOrigin from './plugins/plugin-graphql-origin.mjs';
import useCacheMachine from './plugins/plugin-use-cache-machine.mjs';
import useTwitch from './plugins/plugin-twitch.mjs';
import useNightbot from './plugins/plugin-nightbot.mjs';
import usePlayground from './plugins/plugin-playground.mjs';
import useOptionMethod from './plugins/plugin-option-method.mjs';
import useLiteApi from './plugins/plugin-lite-api.mjs';

let dataAPI, yoga;

Expand Down Expand Up @@ -45,9 +46,10 @@ export default async function getYoga(env) {
useOptionMethod(),
useTwitch(env),
usePlayground(),
useNightbot(env),
useHttpServer(env),
useCacheMachine(env),
useGraphQLOrigin(env),
useNightbot(env),
useLiteApi(env),
],
cors: {
origin: graphQLOptions.cors.allowOrigin,
Expand Down
6 changes: 3 additions & 3 deletions http/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 25 additions & 26 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import graphQLOptions from './utils/graphql-options.mjs';
import cacheMachine from './utils/cache-machine.mjs';
import fetchWithTimeout from './utils/fetch-with-timeout.mjs';

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

let dataAPI;

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

// if an HTTP GraphQL server is configured, pass the request to that
// if an origin server is configured, pass the request
if (env.USE_ORIGIN === 'true') {
try {
const serverUrl = `https://api.tarkov.dev${graphQLOptions.baseEndpoint}`;
const queryResult = await fetchWithTimeout(serverUrl, {
method: request.method,
const originUrl = new URL(request.url);
if (env.ORIGIN_OVERRIDE) {
originUrl.host = env.ORIGIN_OVERRIDE;
}
const queryResult = await fetchWithTimeout(originUrl, {
method: 'POST',
body: JSON.stringify({
query,
variables,
Expand All @@ -127,10 +131,10 @@ async function graphqlHandler(request, env, ctx) {
if (queryResult.status !== 200) {
throw new Error(`${queryResult.status} ${await queryResult.text()}`);
}
console.log('Request served from graphql server');
console.log('Request served from origin server');
return new Response(await queryResult.text(), responseOptions);
} catch (error) {
console.error(`Error getting response from GraphQL server: ${error}`);
console.error(`Error getting response from origin server: ${error}`);
}
}

Expand Down Expand Up @@ -206,47 +210,42 @@ export default {
const requestStart = new Date();
const url = new URL(request.url);

let response;

try {
if (url.pathname === '/twitch') {
response = await getTwitchResponse(env);
const response = await getTwitchResponse(env);
if (graphQLOptions.cors) {
setCors(response, graphQLOptions.cors);
}
return response;
}

if (url.pathname === graphQLOptions.playgroundEndpoint) {
//response = playground(request, graphQLOptions);
response = graphiql(graphQLOptions);
}

if (graphQLOptions.forwardUnmatchedRequestsToOrigin) {
return fetch(request);
return graphiql(graphQLOptions);
}

if (url.pathname === '/webhook/nightbot' ||
url.pathname === '/webhook/stream-elements' ||
url.pathname === '/webhook/moobot'
) {
response = await getNightbotResponse(request, url, env, ctx);
if (useNightbotOnUrl(url)) {
return await getNightbotResponse(request, url, env, ctx);
}

if (useLiteApiOnUrl(url)) {
return await getLiteApiResponse(request, url, env, ctx);
}

if (url.pathname === graphQLOptions.baseEndpoint) {
response = await graphqlHandler(request, env, ctx);
const response = await graphqlHandler(request, env, ctx);
if (graphQLOptions.cors) {
setCors(response, graphQLOptions.cors);
}
return response;
}

if (!response) {
response = new Response('Not found', { status: 404 });
}
console.log(`Response time: ${new Date() - requestStart} ms`);
return response;
return new Response('Not found', { status: 404 });
} catch (err) {
console.log(err);
return new Response(graphQLOptions.debug ? err : 'Something went wrong', { status: 500 });
} finally {
console.log(`Response time: ${new Date() - requestStart} ms`);
}
},
};
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import graphQLOptions from '../utils/graphql-options.mjs';
// Pass the request to an origin server if USE_ORIGIN is set to 'true'
import fetchWithTimeout from '../utils/fetch-with-timeout.mjs';

export default function useHttpServer(env) {
export default function useGraphQLOrigin(env) {
return {
async onParams({params, request, setParams, setResult, fetchAPI}) {
// if an HTTP GraphQL server is configured, pass the request to that
if (env.USE_ORIGIN !== 'true') {
return;
}
try {
const serverUrl = `https://api.tarkov.dev${graphQLOptions.baseEndpoint}`;
const queryResult = await fetch(serverUrl, {
const originUrl = new URL(request.url);
if (env.ORIGIN_OVERRIDE) {
originUrl.host = env.ORIGIN_OVERRIDE;
}
const queryResult = await fetchWithTimeout(originUrl, {
method: request.method,
body: JSON.stringify(params),
headers: {
'Content-Type': 'application/json',
},
timeout: 20000
});
if (queryResult.status !== 200) {
throw new Error(`${queryResult.status} ${queryResult.statusText}: ${await queryResult.text()}`);
}
console.log('Request served from graphql server');
console.log('Request served from origin server');
setResult(await queryResult.json());
request.cached = true;
} catch (error) {
console.error(`Error getting response from GraphQL server: ${error}`);
console.error(`Error getting response from origin server: ${error}`);
}
},
}
Expand Down
Loading
Loading