Skip to content

Commit db4643a

Browse files
authored
feat(http): support configuring stringify options (#587)
1 parent 180babb commit db4643a

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

docs/network-requests/http.md

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ interface HttpOptions {
6565
* @default '__payload__'
6666
*/
6767
payloadKey?: string
68+
/**
69+
* The options for the stringify function to create a queryString.
70+
*
71+
* @default {}
72+
*/
73+
stringifyOptions?: import('qs').IStringifyOptions
6874
}
6975

7076
interface HttpClient {

lib/http/Http.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { parse as parseContentDisposition } from '@tinyhttp/content-disposition'
22
import { parse as parseCookie } from '@tinyhttp/cookie'
33
import FileSaver from 'file-saver'
44
import { FetchError, type FetchOptions, type FetchRequest, type FetchResponse, ofetch } from 'ofetch'
5-
import { stringify } from 'qs'
5+
import { type IStringifyOptions, stringify } from 'qs'
66
import { type Lang } from '../composables/Lang'
77
import { isBlob, isError, isFormData, isRequest, isResponse, isString } from '../support/Utils'
88

@@ -20,6 +20,7 @@ export interface HttpOptions {
2020
lang?: Lang
2121
payloadKey?: string
2222
headers?: () => Awaitable<Record<string, string>>
23+
stringifyOptions?: IStringifyOptions
2324
}
2425

2526
export class Http {
@@ -29,6 +30,7 @@ export class Http {
2930
private static lang: Lang | undefined = undefined
3031
private static payloadKey = '__payload__'
3132
private static headers: () => Awaitable<Record<string, string>> = async () => ({})
33+
private static stringifyOptions: IStringifyOptions = {}
3234

3335
static config(options: HttpOptions): void {
3436
if (options.baseUrl) {
@@ -49,6 +51,9 @@ export class Http {
4951
if (options.headers) {
5052
Http.headers = options.headers
5153
}
54+
if (options.stringifyOptions) {
55+
Http.stringifyOptions = options.stringifyOptions
56+
}
5257
}
5358

5459
private async ensureXsrfToken(): Promise<string | undefined> {
@@ -69,7 +74,8 @@ export class Http {
6974
private async buildRequest(url: string, _options: FetchOptions = {}): Promise<[string, FetchOptions]> {
7075
const { method, params, query, ...options } = _options
7176
const xsrfToken = ['POST', 'PUT', 'PATCH', 'DELETE'].includes(method || '') && (await this.ensureXsrfToken())
72-
const queryString = stringify({ ...params, ...query }, { encodeValuesOnly: true })
77+
78+
const queryString = stringify({ ...params, ...query }, { encodeValuesOnly: true, ...Http.stringifyOptions })
7379

7480
return [
7581
`${url}${queryString ? `?${queryString}` : ''}`,
@@ -195,16 +201,10 @@ export class Http {
195201
export function isFetchError(e: unknown): e is FetchError {
196202
return (
197203
e instanceof FetchError
198-
|| (isError(e)
199-
&& (isString((e as FetchError).request) || isRequest((e as FetchError).request))
200-
&& ((e as FetchError).response === undefined || isResponse((e as FetchError).response))
201-
&& e.message.startsWith(
202-
`[${
203-
((e as FetchError).request as Request | undefined)?.method || (e as FetchError).options?.method || 'GET'
204-
}] ${JSON.stringify(
205-
((e as FetchError).request as Request | undefined)?.url || String((e as FetchError).request) || '/'
206-
)}: `
207-
))
204+
|| (isError<FetchError>(e)
205+
&& (e.response === undefined || isResponse(e.response))
206+
&& ((isString(e.request) && e.message.startsWith(`[${e.options?.method || 'GET'}] ${JSON.stringify(e.request || '/')}: `))
207+
|| (isRequest(e.request) && e.message.startsWith(`[${e.request.method}] ${JSON.stringify(e.request.url)}: `))))
208208
)
209209
}
210210

lib/support/Utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function isDate(value: unknown): value is Date {
1616
return _isDate(value)
1717
}
1818

19-
export function isError(value: unknown): value is Error {
19+
export function isError<T extends Error = Error>(value: unknown): value is T {
2020
return _isError(value)
2121
}
2222

0 commit comments

Comments
 (0)