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,35 +1,18 @@
|
|||||||
// 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'
|
import TCGdex from './tcgdex'
|
||||||
|
|
||||||
export class Request<T = any> {
|
export default class Request {
|
||||||
|
|
||||||
// 1 hour of TTL by default
|
// 1 hour of TTL by default
|
||||||
public static ttl = 1000 * 60 * 60
|
public static ttl = 1000 * 60 * 60
|
||||||
|
|
||||||
private response?: T
|
private static cache: Record<string, {response: any, time: number}> = {}
|
||||||
|
|
||||||
private fetched?: Date
|
public static async fetch<T>(url: string): Promise<T | undefined> {
|
||||||
|
let request = this.cache[url]
|
||||||
public constructor(
|
const now = new Date().getTime()
|
||||||
// url is public for quick url test
|
if (!request || now - request.time > this.ttl) {
|
||||||
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 unfetch = TCGdex.fetch
|
||||||
const resp = await unfetch(this.url, {
|
const resp = await unfetch(url, {
|
||||||
headers: {
|
headers: {
|
||||||
'user-agent': `@tcgdex/javascript-sdk/${TCGdex.VERSION}`
|
'user-agent': `@tcgdex/javascript-sdk/${TCGdex.VERSION}`
|
||||||
}
|
}
|
||||||
@ -37,24 +20,11 @@ export class Request<T = any> {
|
|||||||
if (resp.status !== 200) {
|
if (resp.status !== 200) {
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
this.response = await resp.json()
|
|
||||||
this.fetched = now
|
this.cache[url] = { response: await resp.json(), time: now }
|
||||||
return this.response
|
request = this.cache[url]
|
||||||
}
|
}
|
||||||
|
return request.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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -179,7 +179,7 @@ export default class TCGdex {
|
|||||||
// eslint-disable-next-line no-misleading-character-class
|
// eslint-disable-next-line no-misleading-character-class
|
||||||
.replace(/["'\u0300-\u036f]/gu, '')
|
.replace(/["'\u0300-\u036f]/gu, '')
|
||||||
)).join('/')
|
)).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