Skip to content

Commit 010b082

Browse files
authoredMay 14, 2021
Fix searchParams option TypeScript type (#344)
1 parent bf5be2f commit 010b082

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed
 

‎source/core/Ky.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {HTTPError} from '../errors/HTTPError.js';
22
import {TimeoutError} from '../errors/TimeoutError.js';
33
import type {Hooks} from '../types/hooks.js';
4-
import type {Input, InternalOptions, NormalizedOptions, Options} from '../types/options.js';
4+
import type {Input, InternalOptions, NormalizedOptions, Options, SearchParamsInit} from '../types/options.js';
55
import {ResponsePromise} from '../types/response.js';
66
import {deepMerge, mergeHeaders} from '../utils/merge.js';
77
import {normalizeRequestMethod, normalizeRetryOptions} from '../utils/normalize.js';
@@ -73,7 +73,7 @@ export class Ky {
7373
// eslint-disable-next-line unicorn/prevent-abbreviations
7474
const textSearchParams = typeof this._options.searchParams === 'string' ?
7575
this._options.searchParams.replace(/^\?/, '') :
76-
new URLSearchParams(this._options.searchParams).toString();
76+
new URLSearchParams(this._options.searchParams as unknown as SearchParamsInit).toString();
7777
// eslint-disable-next-line unicorn/prevent-abbreviations
7878
const searchParams = '?' + textSearchParams;
7979
const url = this.request.url.replace(/(?:\?.*?)?(?=#|$)/, searchParams);

‎source/types/options.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import type {LiteralUnion, Required} from './common.js';
22
import type {Hooks} from './hooks.js';
33
import type {RetryOptions} from './retry.js';
44

5+
// eslint-disable-next-line unicorn/prevent-abbreviations
6+
export type SearchParamsInit = string | string[][] | Record<string, string> | URLSearchParams | undefined;
7+
8+
// eslint-disable-next-line unicorn/prevent-abbreviations
9+
export type SearchParamsOption = SearchParamsInit | Record<string, string | number | boolean> | Array<Array<string | number | boolean>>;
10+
511
export type HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete';
612

713
export type Input = string | URL | Request;
@@ -99,7 +105,7 @@ export interface Options extends Omit<RequestInit, 'headers'> {
99105
100106
Accepts any value supported by [`URLSearchParams()`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams).
101107
*/
102-
searchParams?: string[][] | Record<string, string> | string | URLSearchParams;
108+
searchParams?: SearchParamsOption;
103109

104110
/**
105111
A prefix to prepend to the `input` URL when making the request. It can be any valid URL, either relative or absolute. A trailing slash `/` is optional and will be added automatically, if needed, when it is joined with `input`. Only takes effect when `input` is a string. The `input` argument cannot start with a slash `/` when using this option.

0 commit comments

Comments
 (0)
Please sign in to comment.