Don't produce URL with "?" alone (#706)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nguyễn Hồng Quân · committed by GitHub · parent 0630c2c · Browse files · 3 files·+58 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+58 added−3 removed
- tests:+29 added−1 removed
- other:+29 added−2 removed
Provenance
- Commit
17ca389- Tree
bc8477e- Author
Nguyễn Hồng Quân <ng.hong.quan@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
0630c2c
3 files changed+58 added−3 removed
modified:source/core/Ky.ts+2 added-2 removed
12 unmodified lines13 import {normalizeRequestMethod, normalizeRetryOptions} from '../utils/normalize.js';14 import timeout, {type TimeoutOptions} from '../utils/timeout.js';15 import delay from '../utils/delay.js';16 import {type ObjectEntries} from '../utils/types.js';17-import {findUnknownOptions} from '../utils/options.js';17+import {findUnknownOptions, hasSearchParameters} from '../utils/options.js';18 import {19 maxSafeTimeout,20 responseTypes,21 stop,164 unmodified lines186 }187 188 this.request = new globalThis.Request(this._input, this._options);189 190- if (this._options.searchParams) {190+ if (hasSearchParameters(this._options.searchParams)) {191 // eslint-disable-next-line unicorn/prevent-abbreviations192 const textSearchParams = typeof this._options.searchParams === 'string'193 ? this._options.searchParams.replace(/^\?/, '')194 : new URLSearchParams(this._options.searchParams as unknown as SearchParamsInit).toString();137 unmodified linesmodified:source/utils/options.ts+27 added
1234567 unmodified lines141516171819202122232425262728293031323334353637383940414243import {kyOptionKeys, requestOptionsRegistry} from '../core/constants.js';import type {SearchParamsOption} from '../types/options.js';
export const findUnknownOptions = ( request: Request, options: Record<string, unknown>,7 unmodified lines }
return unknownOptions;};
export const hasSearchParameters = (search: SearchParamsOption): boolean => { if (search === undefined) { return false; }
// The `typeof array` still gives "object", so we need different checking for array. if (Array.isArray(search)) { return search.length > 0; }
if (search instanceof URLSearchParams) { return search.size > 0; }
// Record if (typeof search === 'object') { return Object.keys(search).length > 0; }
if (typeof search === 'string') { return search.trim().length > 0; }
return Boolean(search);};modified:test/fetch.ts+29 added-1 removed
2 unmodified lines34567789101110 unmodified lines22232425262728293031323334353637383940414243444546474849505152535455565750 unmodified lines2 unmodified lines
const fixture = 'https://example.com/unicorn';
test('fetch option takes a custom fetch function', async t => { t.plan(6); t.plan(10);
const customFetch: typeof fetch = async input => { if (!(input instanceof Request)) { throw new TypeError('Expected to have input as request');10 unmodified lines }).text(), `${fixture}?foo=bar`, ); t.is( await ky(fixture, { fetch: customFetch, searchParams: {}, }).text(), `${fixture}`, ); t.is( await ky(fixture, { fetch: customFetch, searchParams: [], }).text(), `${fixture}`, ); t.is( await ky(fixture, { fetch: customFetch, searchParams: new URLSearchParams(), }).text(), `${fixture}`, ); t.is( await ky(fixture, { fetch: customFetch, searchParams: ' ', }).text(), `${fixture}`, ); t.is( await ky(`${fixture}#hash`, { fetch: customFetch, searchParams: 'foo', }).text(),50 unmodified lines