File tree Expand file tree Collapse file tree 3 files changed +58
-3
lines changed Expand file tree Collapse file tree 3 files changed +58
-3
lines changed Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ import {normalizeRequestMethod, normalizeRetryOptions} from '../utils/normalize.
14
14
import timeout , { type TimeoutOptions } from '../utils/timeout.js' ;
15
15
import delay from '../utils/delay.js' ;
16
16
import { type ObjectEntries } from '../utils/types.js' ;
17
- import { findUnknownOptions } from '../utils/options.js' ;
17
+ import { findUnknownOptions , hasSearchParameters } from '../utils/options.js' ;
18
18
import {
19
19
maxSafeTimeout ,
20
20
responseTypes ,
@@ -187,7 +187,7 @@ export class Ky {
187
187
188
188
this . request = new globalThis . Request ( this . _input , this . _options ) ;
189
189
190
- if ( this . _options . searchParams ) {
190
+ if ( hasSearchParameters ( this . _options . searchParams ) ) {
191
191
// eslint-disable-next-line unicorn/prevent-abbreviations
192
192
const textSearchParams = typeof this . _options . searchParams === 'string'
193
193
? this . _options . searchParams . replace ( / ^ \? / , '' )
Original file line number Diff line number Diff line change 1
1
import { kyOptionKeys , requestOptionsRegistry } from '../core/constants.js' ;
2
+ import type { SearchParamsOption } from '../types/options.js' ;
2
3
3
4
export const findUnknownOptions = (
4
5
request : Request ,
@@ -14,3 +15,29 @@ export const findUnknownOptions = (
14
15
15
16
return unknownOptions ;
16
17
} ;
18
+
19
+ export const hasSearchParameters = ( search : SearchParamsOption ) : boolean => {
20
+ if ( search === undefined ) {
21
+ return false ;
22
+ }
23
+
24
+ // The `typeof array` still gives "object", so we need different checking for array.
25
+ if ( Array . isArray ( search ) ) {
26
+ return search . length > 0 ;
27
+ }
28
+
29
+ if ( search instanceof URLSearchParams ) {
30
+ return search . size > 0 ;
31
+ }
32
+
33
+ // Record
34
+ if ( typeof search === 'object' ) {
35
+ return Object . keys ( search ) . length > 0 ;
36
+ }
37
+
38
+ if ( typeof search === 'string' ) {
39
+ return search . trim ( ) . length > 0 ;
40
+ }
41
+
42
+ return Boolean ( search ) ;
43
+ } ;
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import ky from '../source/index.js';
4
4
const fixture = 'https://example.com/unicorn' ;
5
5
6
6
test ( 'fetch option takes a custom fetch function' , async t => {
7
- t . plan ( 6 ) ;
7
+ t . plan ( 10 ) ;
8
8
9
9
const customFetch : typeof fetch = async input => {
10
10
if ( ! ( input instanceof Request ) ) {
@@ -22,6 +22,34 @@ test('fetch option takes a custom fetch function', async t => {
22
22
} ) . text ( ) ,
23
23
`${ fixture } ?foo=bar` ,
24
24
) ;
25
+ t . is (
26
+ await ky ( fixture , {
27
+ fetch : customFetch ,
28
+ searchParams : { } ,
29
+ } ) . text ( ) ,
30
+ `${ fixture } ` ,
31
+ ) ;
32
+ t . is (
33
+ await ky ( fixture , {
34
+ fetch : customFetch ,
35
+ searchParams : [ ] ,
36
+ } ) . text ( ) ,
37
+ `${ fixture } ` ,
38
+ ) ;
39
+ t . is (
40
+ await ky ( fixture , {
41
+ fetch : customFetch ,
42
+ searchParams : new URLSearchParams ( ) ,
43
+ } ) . text ( ) ,
44
+ `${ fixture } ` ,
45
+ ) ;
46
+ t . is (
47
+ await ky ( fixture , {
48
+ fetch : customFetch ,
49
+ searchParams : ' ' ,
50
+ } ) . text ( ) ,
51
+ `${ fixture } ` ,
52
+ ) ;
25
53
t . is (
26
54
await ky ( `${ fixture } #hash` , {
27
55
fetch : customFetch ,
You can’t perform that action at this time.
0 commit comments