Skip to content

Commit

Permalink
Fix searchParams option TypeScript type (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgcrea committed May 14, 2021
1 parent bf5be2f commit 010b082
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions source/core/Ky.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {HTTPError} from '../errors/HTTPError.js';
import {TimeoutError} from '../errors/TimeoutError.js';
import type {Hooks} from '../types/hooks.js';
import type {Input, InternalOptions, NormalizedOptions, Options} from '../types/options.js';
import type {Input, InternalOptions, NormalizedOptions, Options, SearchParamsInit} from '../types/options.js';
import {ResponsePromise} from '../types/response.js';
import {deepMerge, mergeHeaders} from '../utils/merge.js';
import {normalizeRequestMethod, normalizeRetryOptions} from '../utils/normalize.js';
Expand Down Expand Up @@ -73,7 +73,7 @@ export class Ky {
// eslint-disable-next-line unicorn/prevent-abbreviations
const textSearchParams = typeof this._options.searchParams === 'string' ?
this._options.searchParams.replace(/^\?/, '') :
new URLSearchParams(this._options.searchParams).toString();
new URLSearchParams(this._options.searchParams as unknown as SearchParamsInit).toString();
// eslint-disable-next-line unicorn/prevent-abbreviations
const searchParams = '?' + textSearchParams;
const url = this.request.url.replace(/(?:\?.*?)?(?=#|$)/, searchParams);
Expand Down
8 changes: 7 additions & 1 deletion source/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import type {LiteralUnion, Required} from './common.js';
import type {Hooks} from './hooks.js';
import type {RetryOptions} from './retry.js';

// eslint-disable-next-line unicorn/prevent-abbreviations
export type SearchParamsInit = string | string[][] | Record<string, string> | URLSearchParams | undefined;

// eslint-disable-next-line unicorn/prevent-abbreviations
export type SearchParamsOption = SearchParamsInit | Record<string, string | number | boolean> | Array<Array<string | number | boolean>>;

export type HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete';

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

/**
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.
Expand Down

0 comments on commit 010b082

Please sign in to comment.