Skip to content

Commit

Permalink
include fetch types and console-stamp
Browse files Browse the repository at this point in the history
  • Loading branch information
ardc-shorn committed Jun 17, 2022
1 parent 63d871b commit bae0d1b
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 11 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
setupFiles: ["<rootDir>/src/test/setup-globals.js"],
};
169 changes: 169 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
"test": "jest"
},
"devDependencies": {
"console-stamp": "3.0.6",
"@types/jsonwebtoken": "8.5.8",
"jsonwebtoken": "8.5.1",
"@types/node-fetch": "2.6.2",
"node-fetch": "2.6.7",
"@types/jest": "28.1.1",
"jest": "28.1.1",
Expand Down
46 changes: 41 additions & 5 deletions src/RaidApi.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,48 @@
import fetch from "node-fetch";
import fetch, {Response, RequestInfo, RequestInit} from "node-fetch";

const apiBase = "https://api.raid.org.au/v1";

export async function getRoot(): Promise<any> {
const response = await fetch(`${apiBase}/`, {
return await apiFetchJson(`${apiBase}/`, {
method: "GET",
});
console.log("got", response);
}

const delay = 200;
let lastSent = new Date().getTime();

/*
I have no idea what this is going to do for parallel requests :|
IMPROVE: way too dumb, but it'll do for now
*/
async function apiFetchJson(
url: RequestInfo,
init?: RequestInit
): Promise<any>{
const nextAllowed = lastSent + delay;
const now = new Date().getTime();

return "";
}
if( now < nextAllowed ){
const sleepTime = nextAllowed - now;
console.log(`request rate-limit, sleep ${sleepTime}ms for ${url}`);
await sleep(sleepTime);
console.log(`request rate-limit, awake for ${url}`);
}

const response: Response = await fetch(url, init);
if( response.status === 429 ){
console.log("API 429", response);
throw new Error("API gave too many requests error");
}
if( !response.ok ){
console.log("API not ok", response);
throw new Error("API returned error - " + response.statusText);
}

return await response.json();
}

async function sleep(sleepMs: number){
return new Promise(resolve => setTimeout(resolve, sleepMs));
}
6 changes: 1 addition & 5 deletions test/Api.test.ts → src/test/Api.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getRoot} from "../src/RaidApi";
import {getRoot} from "../RaidApi";

const apiBase = "https://api.raid.org.au/v1";

Expand All @@ -7,10 +7,6 @@ describe("RaidApi", () => {
test("root responds 200", async () => {
console.log("starting test");

// const response = await fetch(`${apiBase}/`, {
// method: "GET",
// });

const response = await getRoot();
console.log("got", response);
});
Expand Down
2 changes: 1 addition & 1 deletion test/Empty.test.ts → src/test/Empty.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {doNothing} from "../src/empty";
import {doNothing} from "../empty";

describe("Empty", () => {

Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions src/test/setup-globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

// require( 'console-stamp' )( global.console );

global.console = require('console');

0 comments on commit bae0d1b

Please sign in to comment.