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 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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. */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user