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

Twitch stats #30

Merged
merged 5 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ query {
Prerequisites:

- Comment out the two `routes =` lines in the `wrangler.toml` file
- Run `wrangler login`
- Run `wrangler login` - (needed for k/v store and secrets)

Start the API server:

Expand Down
28 changes: 28 additions & 0 deletions custom-endpoints/twitch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// The Twitch API url to hit for EFT data
const url = "https://api.twitch.tv/helix/streams?game_id=491931&first=100"
// Twitch API headers for authentication
const init = {
headers: {
'Authorization': `Bearer ${TWITCH_TOKEN}`,
'Client-ID': TWITCH_CLIENT_ID,
},
};
// Response content-type headers
const contentType = 'application/json;charset=UTF-8';

// Make a request to the Twitch API to get data for EFT streams
async function getTwitchData() {
let response = await fetch(url, init);
let responseJson = JSON.stringify(await response.json(), null, 2);
return responseJson;
}

module.exports = async (request) => {
let twitchJson = await getTwitchData();

return new Response(twitchJson, {
headers: {
'content-type': contentType,
},
});
};
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const resolvers = require('./resolvers');
require('./loader');

const nightbot = require('./custom-endpoints/nightbot');
const twitch = require('./custom-endpoints/twitch');

const schema = buildSchema(typeDefs);

Expand Down Expand Up @@ -151,6 +152,15 @@ const handleRequest = async request => {
return nightbot(request);
}

if(url.pathname === '/twitch'){
const response = request.method === 'OPTIONS' ? new Response('', { status: 204 }) : await twitch(request);
if (graphQLOptions.cors) {
setCors(response, graphQLOptions.cors);
}

return response;
}

if (url.pathname === graphQLOptions.baseEndpoint) {
const response = request.method === 'OPTIONS' ? new Response('', { status: 204 }) : await graphqlHandler(request, graphQLOptions);
if (graphQLOptions.cors) {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion script/ci/wrangler-ci.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ kv-namespaces = [
kv-namespaces = [
{ binding = "ITEM_DATA", id = "2973a2dd070e4a348d87084171efe11a", preview_id = "2973a2dd070e4a348d87084171efe11a" },
{ binding = "QUERY_CACHE", id = "7ae67967cff24ceaa31ca58c5a6da798", preview_id = "7ae67967cff24ceaa31ca58c5a6da798" }
]
]
2 changes: 1 addition & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ routes = ["dev.api.tarkov.dev/graphql", "dev.api.tarkov.dev/graphql*", "dev.api.
kv-namespaces = [
{ binding = "ITEM_DATA", id = "2973a2dd070e4a348d87084171efe11a", preview_id = "2973a2dd070e4a348d87084171efe11a" },
{ binding = "QUERY_CACHE", id = "7ae67967cff24ceaa31ca58c5a6da798", preview_id = "7ae67967cff24ceaa31ca58c5a6da798" }
]
]