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

View File

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