mirror of
https://github.com/tcgdex/javascript-sdk.git
synced 2025-04-22 10:42:10 +00:00
Added a User-Agent
Signed-off-by: Florian BOUILLON <florian.bouillon@delta-wings.net>
This commit is contained in:
parent
23da33f81f
commit
b845e36784
17
Request.ts
17
Request.ts
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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. */
|
||||
|
Loading…
x
Reference in New Issue
Block a user