From 2f0448059be622ea2d1d16df0bb16a74a811a10b Mon Sep 17 00:00:00 2001 From: Avior Date: Mon, 7 Jul 2025 17:26:05 +0200 Subject: [PATCH] fix: use `URL` to build the URL --- src/tcgdex.ts | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/src/tcgdex.ts b/src/tcgdex.ts index ca3802f..83319f8 100644 --- a/src/tcgdex.ts +++ b/src/tcgdex.ts @@ -326,19 +326,22 @@ export default class TCGdex { * format the final URL */ private getFullURL( - url: Array, + path: Array, searchParams?: Array<{ key: string, value: string | number | boolean }> ): string { - // Normalize path - let path = url.map(this.encode).join('/') + // build base path + const url = new URL(`${this.getEndpoint()}/${this.getLang()}`) + + // set url path + url.pathname = `${url.pathname}/${path.join('/')}` // handle the Search Params - if (searchParams) { - path += '?' + searchParams.map((it) => `${this.encode(it.key)}=${this.encode(it.value)}`).join('&') + for (const param of searchParams ?? []) { + url.searchParams.append(param.key, param.value.toString()) } // return with the endpoint and all the shit - return `${this.getEndpoint()}/${this.getLang()}/${path}` + return url.toString() } private async actualFetch(path: string): Promise { @@ -376,26 +379,6 @@ export default class TCGdex { this.cache.set(path, json, this.cacheTTL) 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