mirror of
https://github.com/tcgdex/javascript-sdk.git
synced 2025-07-14 19:05:11 +00:00
fix: use URL
to build the URL
This commit is contained in:
@ -326,19 +326,22 @@ export default class TCGdex {
|
|||||||
* format the final URL
|
* format the final URL
|
||||||
*/
|
*/
|
||||||
private getFullURL(
|
private getFullURL(
|
||||||
url: Array<string | number>,
|
path: Array<string | number>,
|
||||||
searchParams?: Array<{ key: string, value: string | number | boolean }>
|
searchParams?: Array<{ key: string, value: string | number | boolean }>
|
||||||
): string {
|
): string {
|
||||||
// Normalize path
|
// build base path
|
||||||
let path = url.map(this.encode).join('/')
|
const url = new URL(`${this.getEndpoint()}/${this.getLang()}`)
|
||||||
|
|
||||||
|
// set url path
|
||||||
|
url.pathname = `${url.pathname}/${path.join('/')}`
|
||||||
|
|
||||||
// handle the Search Params
|
// handle the Search Params
|
||||||
if (searchParams) {
|
for (const param of searchParams ?? []) {
|
||||||
path += '?' + searchParams.map((it) => `${this.encode(it.key)}=${this.encode(it.value)}`).join('&')
|
url.searchParams.append(param.key, param.value.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
// return with the endpoint and all the shit
|
// return with the endpoint and all the shit
|
||||||
return `${this.getEndpoint()}/${this.getLang()}/${path}`
|
return url.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
private async actualFetch<T = object>(path: string): Promise<T | undefined> {
|
private async actualFetch<T = object>(path: string): Promise<T | undefined> {
|
||||||
@ -376,26 +379,6 @@ export default class TCGdex {
|
|||||||
this.cache.set(path, json, this.cacheTTL)
|
this.cache.set(path, json, this.cacheTTL)
|
||||||
return json as T
|
return json as T
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* encode a string to be used in an url
|
|
||||||
* @param str the string to encode to URL
|
|
||||||
* @returns the encoded string
|
|
||||||
*/
|
|
||||||
private encode(str: string | number | boolean): string {
|
|
||||||
return encodeURI(
|
|
||||||
str
|
|
||||||
// Transform numbers to string
|
|
||||||
.toString()
|
|
||||||
// replace this special character with an escaped one
|
|
||||||
.replace('?', '%3F')
|
|
||||||
// normalize the string
|
|
||||||
.normalize('NFC')
|
|
||||||
// remove some special chars
|
|
||||||
// eslint-disable-next-line no-misleading-character-class
|
|
||||||
.replace(/["'\u0300-\u036f]/gu, '')
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// export the old interfaces
|
// export the old interfaces
|
||||||
|
Reference in New Issue
Block a user