Skip to content

Commit c97eac1

Browse files
committed
Start to JS library
1 parent 8ad5de3 commit c97eac1

File tree

7 files changed

+553
-0
lines changed

7 files changed

+553
-0
lines changed

libraries/javascript/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore Node installs
2+
node_modules/
3+
4+
# Ignore secrets files
5+
*secrets*

libraries/javascript/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# JavaScript Library for IGDB
2+
3+
A JavaScript library for interacting with IGDB - a database of video game data. This implementation utilizes the Apicalypse module provided by IGDB to assist with request building.
4+
5+
## Testing Setup
6+
7+
You can test the library out-of-the-box with some simple setup.
8+
9+
1. Install the latest stable versions of Node and NPM.
10+
2. Clone/Download the project.
11+
3. From the root directory of the library, run `npm install`.
12+
4. Store client credentials (details below).
13+
5. From the project directory, run `node app.js`.
14+
6. Observe output data.
15+
16+
## Client Credentials
17+
18+
You will need to create a file for storing your client credentials. Typically these secrets would be handled through your server environment, but for basic testing you can create this secrets file to immediately test the library.
19+
20+
Reminder: DON'T COMMIT SECRETS TO VERSION CONTROL!
21+
22+
### Twitch Account for Authentication
23+
24+
IGDB is owned and operated by Twitch.tv. In order to authenticate with the api, you will need an active Twitch account and register a developer application with it to acquire a Client ID and Client Secret. See IGDB's documentation here for details: [IGDB Account Creation](https://api-docs.igdb.com/#account-creation)
25+
26+
### Create Secrets File
27+
28+
1. Create a `secrets.json` file at the project root.
29+
2. Add two fields called `clientId` and `clientSecret`.
30+
3. Set those values to their respective values from your Twitch Developer App.

libraries/javascript/app.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import IGDB_Helper from "./src/igdb/igdb_api.js";
2+
import TwitchAPI from "./src/twitch_oauth/twitch_oauth.js";
3+
import fs from 'fs';
4+
5+
function getCredentials() {
6+
console.log("Retrieving client ID and client secret");
7+
const secrets = fs.readFileSync("./secrets.json");
8+
const secretsJSON = JSON.parse(secrets);
9+
return secretsJSON;
10+
}
11+
12+
async function getAPIToken(clientId, clientSecret) {
13+
console.log("Requesting API token from Twitch");
14+
const twichApi = new TwitchAPI();
15+
const apiTokenResult = await twichApi.requestOauthToken(clientId, clientSecret);
16+
return apiTokenResult.access_token;
17+
}
18+
19+
async function getGameData(clientId, apiToken) {
20+
console.log("Pulling game data from IGDB");
21+
const igdb = new IGDB_Helper();
22+
const igdbRequest = igdb.createRequest(clientId, apiToken);
23+
24+
igdbRequest
25+
.fields("name, release_dates")
26+
.limit(1);
27+
28+
const responseData = await igdbRequest.request("/games")
29+
.then(async (result) => {
30+
if (result.status !== 200) {
31+
console.log("Unexpected response from IGDB! Received: " + await result.text());
32+
throw new Error("Received bad result from IGDB API!");
33+
}
34+
35+
return result.data;
36+
})
37+
.then((data) => {
38+
console.log("Successfully retrieved data from IGDB API.");
39+
return data;
40+
});
41+
42+
return responseData;
43+
}
44+
45+
const credentials = getCredentials();
46+
const token = await getAPIToken(credentials.clientId, credentials.clientSecret);
47+
const gameData = await getGameData(credentials.clientId, token);
48+
49+
const firstGame = gameData[0];
50+
const firstGameName = firstGame.name;
51+
let firstGameRelease = new Date(firstGame.release_dates[0]);
52+
firstGameRelease = firstGameRelease.toLocaleDateString();
53+
console.log(`Retrieved game ${firstGameName} released at ${firstGameRelease}`);

0 commit comments

Comments
 (0)