From 0ed990931ec3a5622ff0c1675cbcdb3d54d8d7ed Mon Sep 17 00:00:00 2001 From: Grant Birkinbine Date: Mon, 18 Apr 2022 21:07:13 -0400 Subject: [PATCH 1/5] init twitch endpoint --- custom-endpoints/twitch.js | 3 +++ index.js | 5 +++++ package-lock.json | 4 ++-- 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 custom-endpoints/twitch.js diff --git a/custom-endpoints/twitch.js b/custom-endpoints/twitch.js new file mode 100644 index 00000000..6f2b1723 --- /dev/null +++ b/custom-endpoints/twitch.js @@ -0,0 +1,3 @@ +module.exports = async (request) => { + return new Response(`hello world`); +}; diff --git a/index.js b/index.js index 54351992..57fe1710 100644 --- a/index.js +++ b/index.js @@ -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); @@ -151,6 +152,10 @@ const handleRequest = async request => { return nightbot(request); } + if(url.pathname === '/twitch'){ + return twitch(request); + } + if (url.pathname === graphQLOptions.baseEndpoint) { const response = request.method === 'OPTIONS' ? new Response('', { status: 204 }) : await graphqlHandler(request, graphQLOptions); if (graphQLOptions.cors) { diff --git a/package-lock.json b/package-lock.json index 2d5e3341..a485e2f9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,9 +12,9 @@ "graphql": "^15.8.0" }, "devDependencies": { - "@cloudflare/wrangler": "^1.19.8", + "@cloudflare/wrangler": "^1.19.11", "newman": "^5.3.2", - "prettier": "^2.2.1" + "prettier": "^2.6.1" } }, "node_modules/@cloudflare/wrangler": { From 3a99f82d701f79e4394caed414606cf9ede09fce Mon Sep 17 00:00:00 2001 From: Grant Birkinbine Date: Mon, 18 Apr 2022 22:01:56 -0400 Subject: [PATCH 2/5] working twitch endpoint --- custom-endpoints/twitch.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/custom-endpoints/twitch.js b/custom-endpoints/twitch.js index 6f2b1723..713aa0b8 100644 --- a/custom-endpoints/twitch.js +++ b/custom-endpoints/twitch.js @@ -1,3 +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) => { - return new Response(`hello world`); + let twitchJson = await getTwitchData(); + + return new Response(twitchJson, { + headers: { + 'content-type': contentType, + }, + }); }; From ad97a33ca07ed1970c91fd03b7a14df3fe9c9b2c Mon Sep 17 00:00:00 2001 From: Grant Birkinbine Date: Mon, 18 Apr 2022 22:02:02 -0400 Subject: [PATCH 3/5] update docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 520e3575..b352827c 100644 --- a/README.md +++ b/README.md @@ -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: From 464c7fe924db969b53e49c88b2273886ca68611b Mon Sep 17 00:00:00 2001 From: Grant Birkinbine Date: Mon, 18 Apr 2022 22:02:07 -0400 Subject: [PATCH 4/5] newlines --- script/ci/wrangler-ci.toml | 2 +- wrangler.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/script/ci/wrangler-ci.toml b/script/ci/wrangler-ci.toml index 7b34e67d..40dfdf68 100644 --- a/script/ci/wrangler-ci.toml +++ b/script/ci/wrangler-ci.toml @@ -15,4 +15,4 @@ kv-namespaces = [ kv-namespaces = [ { binding = "ITEM_DATA", id = "2973a2dd070e4a348d87084171efe11a", preview_id = "2973a2dd070e4a348d87084171efe11a" }, { binding = "QUERY_CACHE", id = "7ae67967cff24ceaa31ca58c5a6da798", preview_id = "7ae67967cff24ceaa31ca58c5a6da798" } -] \ No newline at end of file +] diff --git a/wrangler.toml b/wrangler.toml index 7bbb2e5d..b206e4de 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -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" } -] \ No newline at end of file +] From 3a61ee7f013bf6b6f818ad06215edb931257daec Mon Sep 17 00:00:00 2001 From: Grant Birkinbine Date: Mon, 18 Apr 2022 22:51:08 -0400 Subject: [PATCH 5/5] fixing a bug found with cors --- index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 57fe1710..db7c553f 100644 --- a/index.js +++ b/index.js @@ -153,7 +153,12 @@ const handleRequest = async request => { } if(url.pathname === '/twitch'){ - return twitch(request); + 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) {