Skip to content

Commit 7f4c5e9

Browse files
authored
fix: use URL to build the URL (#300)
1 parent 6849546 commit 7f4c5e9

File tree

1 file changed

+9
-26
lines changed

1 file changed

+9
-26
lines changed

src/tcgdex.ts

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -326,19 +326,22 @@ export default class TCGdex {
326326
* format the final URL
327327
*/
328328
private getFullURL(
329-
url: Array<string | number>,
329+
path: Array<string | number>,
330330
searchParams?: Array<{ key: string, value: string | number | boolean }>
331331
): string {
332-
// Normalize path
333-
let path = url.map(this.encode).join('/')
332+
// build base path
333+
const url = new URL(`${this.getEndpoint()}/${this.getLang()}`)
334+
335+
// set url path
336+
url.pathname = `${url.pathname}/${path.join('/')}`
334337

335338
// handle the Search Params
336-
if (searchParams) {
337-
path += '?' + searchParams.map((it) => `${this.encode(it.key)}=${this.encode(it.value)}`).join('&')
339+
for (const param of searchParams ?? []) {
340+
url.searchParams.append(param.key, param.value.toString())
338341
}
339342

340343
// return with the endpoint and all the shit
341-
return `${this.getEndpoint()}/${this.getLang()}/${path}`
344+
return url.toString()
342345
}
343346

344347
private async actualFetch<T = object>(path: string): Promise<T | undefined> {
@@ -376,26 +379,6 @@ export default class TCGdex {
376379
this.cache.set(path, json, this.cacheTTL)
377380
return json as T
378381
}
379-
380-
/**
381-
* encode a string to be used in an url
382-
* @param str the string to encode to URL
383-
* @returns the encoded string
384-
*/
385-
private encode(str: string | number | boolean): string {
386-
return encodeURI(
387-
str
388-
// Transform numbers to string
389-
.toString()
390-
// replace this special character with an escaped one
391-
.replace('?', '%3F')
392-
// normalize the string
393-
.normalize('NFC')
394-
// remove some special chars
395-
// eslint-disable-next-line no-misleading-character-class
396-
.replace(/["'\u0300-\u036f]/gu, '')
397-
)
398-
}
399382
}
400383

401384
// export the old interfaces

0 commit comments

Comments
 (0)