Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 9 additions & 26 deletions src/tcgdex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,19 +326,22 @@
* format the final URL
*/
private getFullURL(
url: Array<string | number>,
path: Array<string | number>,
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<T = object>(path: string): Promise<T | undefined> {
Expand All @@ -361,7 +364,7 @@
const json = JSON.stringify(await resp.json())
throw new Error(json)
} catch {
throw new Error('TCGdex Server responded with an invalid error :(')

Check failure on line 367 in src/tcgdex.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

__tests__/basic.test.ts > test real variant endpoint item

Error: TCGdex Server responded with an invalid error :( ❯ TCGdex.actualFetch src/tcgdex.ts:367:11 ❯ SimpleEndpoint.get src/endpoints/SimpleEndpoint.ts:14:15 ❯ __tests__/basic.test.ts:137:4

Check failure on line 367 in src/tcgdex.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

__tests__/basic.test.ts > test real variant endpoint list

Error: TCGdex Server responded with an invalid error :( ❯ TCGdex.actualFetch src/tcgdex.ts:367:11 ❯ SimpleEndpoint.list src/endpoints/SimpleEndpoint.ts:22:10 ❯ __tests__/basic.test.ts:128:4

Check failure on line 367 in src/tcgdex.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

__tests__/basic.test.ts > test real variant endpoint list

Error: TCGdex Server responded with an invalid error :( ❯ TCGdex.actualFetch src/tcgdex.ts:367:11 ❯ SimpleEndpoint.list src/endpoints/SimpleEndpoint.ts:22:10 ❯ __tests__/basic.test.ts:128:4
}
}

Expand All @@ -376,26 +379,6 @@
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
Expand Down
Loading