Added a User-Agent

Signed-off-by: Florian BOUILLON <florian.bouillon@delta-wings.net>
This commit is contained in:
Florian Bouillon 2021-05-31 12:48:25 +02:00
parent 23da33f81f
commit b845e36784
2 changed files with 10 additions and 8 deletions

View File

@ -1,4 +1,5 @@
import fetch from 'isomorphic-unfetch'
import pkg from './package.json'
export default class RequestWrapper {
private static cache: Array<Request<any>> = []
@ -15,16 +16,17 @@ export default class RequestWrapper {
export class Request<T = any> {
public static ttl = 1000 * 60 * 60 // 1 hour
private response?: T
private fetched?: Date
public url: string // url is public for quick url test
public constructor(url: string) {
this.url = url
}
public constructor(
public url: string // url is public for quick url test
) {}
public async get(): Promise<T | undefined> {
const now = new Date()
// if reponse was already fetched and TTL was not passed
if (
this.fetched &&
this.response &&
@ -36,15 +38,14 @@ export class Request<T = any> {
// Fetch Response
const resp = await fetch(this.url, {
headers: {
"Content-Type": "text/plain"
'user-agent': `${pkg.name}/${pkg.version}`
}
})
if (resp.status !== 200) {
return undefined
}
const response = await resp.json()
this.response = response
this.response = await resp.json()
this.fetched = now
return response
return this.response
}
}

View File

@ -41,6 +41,7 @@
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */