-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
include fetch types and console-stamp
- Loading branch information
1 parent
63d871b
commit bae0d1b
Showing
8 changed files
with
220 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", () => { | ||
|
||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
// require( 'console-stamp' )( global.console ); | ||
|
||
global.console = require('console'); | ||
|