diff --git a/Request.ts b/Request.ts index 70bd8ed..7c1b98c 100644 --- a/Request.ts +++ b/Request.ts @@ -1,4 +1,5 @@ import fetch from 'isomorphic-unfetch' +import pkg from './package.json' export default class RequestWrapper { private static cache: Array> = [] @@ -15,16 +16,17 @@ export default class RequestWrapper { export class Request { 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 { 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 { // 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 } } diff --git a/tsconfig.json b/tsconfig.json index aab8c05..47982fd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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. */