mirror of
https://github.com/tcgdex/javascript-sdk.git
synced 2025-04-22 10:42:10 +00:00
Reworked Request (#10)
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
parent
3684c61812
commit
941fd925b8
@ -1,60 +1,30 @@
|
||||
// Temporary disable this as it will be rebuilt to make it like the php-sdk one
|
||||
// eslint-disable-next-line max-classes-per-file
|
||||
import TCGdex from './tcgdex'
|
||||
|
||||
export class Request<T = any> {
|
||||
export default class Request {
|
||||
|
||||
// 1 hour of TTL by default
|
||||
public static ttl = 1000 * 60 * 60
|
||||
|
||||
private response?: T
|
||||
private static cache: Record<string, {response: any, time: number}> = {}
|
||||
|
||||
private fetched?: Date
|
||||
|
||||
public constructor(
|
||||
// url is public for quick url test
|
||||
public url: string
|
||||
) {}
|
||||
|
||||
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 &&
|
||||
now.getTime() - this.fetched.getTime() < Request.ttl
|
||||
) {
|
||||
return this.response
|
||||
}
|
||||
|
||||
// Fetch Response
|
||||
const unfetch = TCGdex.fetch
|
||||
const resp = await unfetch(this.url, {
|
||||
headers: {
|
||||
'user-agent': `@tcgdex/javascript-sdk/${TCGdex.VERSION}`
|
||||
public static async fetch<T>(url: string): Promise<T | undefined> {
|
||||
let request = this.cache[url]
|
||||
const now = new Date().getTime()
|
||||
if (!request || now - request.time > this.ttl) {
|
||||
const unfetch = TCGdex.fetch
|
||||
const resp = await unfetch(url, {
|
||||
headers: {
|
||||
'user-agent': `@tcgdex/javascript-sdk/${TCGdex.VERSION}`
|
||||
}
|
||||
})
|
||||
if (resp.status !== 200) {
|
||||
return undefined
|
||||
}
|
||||
})
|
||||
if (resp.status !== 200) {
|
||||
return undefined
|
||||
|
||||
this.cache[url] = { response: await resp.json(), time: now }
|
||||
request = this.cache[url]
|
||||
}
|
||||
this.response = await resp.json()
|
||||
this.fetched = now
|
||||
return this.response
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default class RequestWrapper {
|
||||
|
||||
private static cache: Array<Request<any>> = []
|
||||
|
||||
public static getRequest<T>(url: string): Request<T> {
|
||||
let request = this.cache.find((req) => req.url === url) as Request<T>|undefined
|
||||
if (!request) {
|
||||
request = new Request<T>(url)
|
||||
this.cache.push(request)
|
||||
}
|
||||
return request
|
||||
return request.response
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -179,7 +179,7 @@ export default class TCGdex {
|
||||
// eslint-disable-next-line no-misleading-character-class
|
||||
.replace(/["'\u0300-\u036f]/gu, '')
|
||||
)).join('/')
|
||||
return RequestWrapper.getRequest<T>(`${BASE_URL}/${this.getLang()}/${path}`).get()
|
||||
return RequestWrapper.fetch<T>(`${BASE_URL}/${this.getLang()}/${path}`)
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user