@@ -2,7 +2,7 @@ import { parse as parseContentDisposition } from '@tinyhttp/content-disposition'
2
2
import { parse as parseCookie } from '@tinyhttp/cookie'
3
3
import FileSaver from 'file-saver'
4
4
import { FetchError , type FetchOptions , type FetchRequest , type FetchResponse , ofetch } from 'ofetch'
5
- import { stringify } from 'qs'
5
+ import { type IStringifyOptions , stringify } from 'qs'
6
6
import { type Lang } from '../composables/Lang'
7
7
import { isBlob , isError , isFormData , isRequest , isResponse , isString } from '../support/Utils'
8
8
@@ -20,6 +20,7 @@ export interface HttpOptions {
20
20
lang ?: Lang
21
21
payloadKey ?: string
22
22
headers ?: ( ) => Awaitable < Record < string , string > >
23
+ stringifyOptions ?: IStringifyOptions
23
24
}
24
25
25
26
export class Http {
@@ -29,6 +30,7 @@ export class Http {
29
30
private static lang : Lang | undefined = undefined
30
31
private static payloadKey = '__payload__'
31
32
private static headers : ( ) => Awaitable < Record < string , string > > = async ( ) => ( { } )
33
+ private static stringifyOptions : IStringifyOptions = { }
32
34
33
35
static config ( options : HttpOptions ) : void {
34
36
if ( options . baseUrl ) {
@@ -49,6 +51,9 @@ export class Http {
49
51
if ( options . headers ) {
50
52
Http . headers = options . headers
51
53
}
54
+ if ( options . stringifyOptions ) {
55
+ Http . stringifyOptions = options . stringifyOptions
56
+ }
52
57
}
53
58
54
59
private async ensureXsrfToken ( ) : Promise < string | undefined > {
@@ -69,7 +74,8 @@ export class Http {
69
74
private async buildRequest ( url : string , _options : FetchOptions = { } ) : Promise < [ string , FetchOptions ] > {
70
75
const { method, params, query, ...options } = _options
71
76
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 } )
73
79
74
80
return [
75
81
`${ url } ${ queryString ? `?${ queryString } ` : '' } ` ,
@@ -195,16 +201,10 @@ export class Http {
195
201
export function isFetchError ( e : unknown ) : e is FetchError {
196
202
return (
197
203
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 ) } : ` ) ) ) )
208
208
)
209
209
}
210
210
0 commit comments