Skip to content
Myr

Add QUERY method support (#873)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sindre Sorhus · committed by GitHub · parent f37e82c · Browse files · 10 files+118 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

10 files+118 added−7 removed

  • tests:+102 added−0 removed
  • other:+16 added−7 removed

10 files+118 added−7 removed

modified:source/core/constants.ts+1 added-1 removed
34 unmodified lines
35export const supportsAbortSignal = typeof globalThis.AbortSignal === 'function' && typeof globalThis.AbortSignal.any === 'function';36export const supportsResponseStreams = typeof globalThis.ReadableStream === 'function';37export const supportsFormData = typeof globalThis.FormData === 'function';3839export const requestMethods = ['get', 'post', 'put', 'patch', 'head', 'delete'] as const;39export const requestMethods = ['get', 'post', 'put', 'patch', 'head', 'delete', 'query'] as const;4041const validate = <T extends Array<true>>() => undefined as unknown as T;42validate<[43 Expect<Equal<typeof requestMethods[number], RequestHttpMethod>>,
241 unmodified lines
modified:source/types/ky.ts+8 added
modified:source/types/options.ts+2 added-2 removed
6 unmodified lines
78// eslint-disable-next-line unicorn/prevent-abbreviations9export type SearchParamsOption = SearchParamsInit | Record<string, string | number | boolean | undefined> | Array<Array<string | number | boolean>>;1011export type RequestHttpMethod = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete';11export type RequestHttpMethod = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'query';12export type HttpMethod = LiteralUnion<RequestHttpMethod | 'options' | 'trace', string>;1314export type Input = string | URL | Request;15
383 unmodified lines
399export interface Options extends KyOptions, Omit<RequestInit, 'headers'> { // eslint-disable-line @typescript-eslint/consistent-type-definitions -- This must stay an interface so that it can be extended outside of Ky for use in `ky.create`.400 /**401 HTTP method used to make the request.402403 Internally, the standard methods (`GET`, `POST`, `PUT`, `PATCH`, `HEAD` and `DELETE`) are uppercased in order to avoid server errors due to case sensitivity.403 Internally, the standard methods (`GET`, `POST`, `PUT`, `PATCH`, `HEAD`, `DELETE`, and `QUERY`) are uppercased in order to avoid server errors due to case sensitivity.404 */405 method?: LiteralUnion<HttpMethod, string>;406407 /**
67 unmodified lines
modified:source/types/retry.ts+1 added-1 removed
21 unmodified lines
2223 /**24 The HTTP methods allowed to retry.2526 @default ['get', 'put', 'head', 'delete', 'options', 'trace']26 @default ['get', 'put', 'head', 'delete', 'options', 'trace', 'query']27 */28 methods?: HttpMethod[];2930 /**
145 unmodified lines
modified:source/utils/normalize.ts+1 added-1 removed
3 unmodified lines
45export const normalizeRequestMethod = (input: string): string =>6 requestMethods.includes(input as RequestHttpMethod) ? input.toUpperCase() : input;78const retryMethods: HttpMethod[] = ['get', 'put', 'head', 'delete', 'options', 'trace'];8const retryMethods: HttpMethod[] = ['get', 'put', 'head', 'delete', 'options', 'trace', 'query'];910const retryStatusCodes = [408, 413, 429, 500, 502, 503, 504];1112const retryAfterStatusCodes = [413, 429, 503];
41 unmodified lines
modified:test/browser.ts+27 added
106 unmodified lines
107108 t.deepEqual(results, ['rainbow', 'rainbow', 'rainbow', 'rainbow', 'rainbow', 'rainbow', 'rainbow']);109});110111defaultBrowsersTest('QUERY request', async (t: ExecutionContext, page: Page) => {112 t.plan(2);113114 server.get('/', (_request, response) => {115 response.end();116 });117118 server.all('/test', (request, response) => {119 t.is(request.method, 'QUERY');120 response.json(request.body);121 });122123 await page.goto(server.url);124 await addKyScriptToPage(page);125126 const json = {127 foo: true,128 };129130 const result = await page.evaluate(async ({url, json}) => globalThis.ky.query(`${url}/test`, {json}).json(), {131 url: server.url,132 json,133 });134135 t.deepEqual(result, json);136});137138defaultBrowsersTest('aborting a request', async (t: ExecutionContext, page: Page) => {139 server.get('/', (_request, response) => {140 response.end('meow');141 });
468 unmodified lines
modified:test/main.ts+20 added
111 unmodified lines
112113 t.is(await ky.delete(server.url).text(), 'DELETE');114});115116test('QUERY request', async t => {117 t.plan(3);118119 const server = await createHttpTestServer(t);120 server.all('/', (request, response) => {121 t.is(request.method, 'QUERY');122 t.is(request.headers['content-type'], 'application/json');123 response.json(request.body);124 });125126 const json = {127 foo: true,128 };129130 const responseJson = await ky.query<typeof json>(server.url, {json}).json();131132 expectTypeOf(responseJson).toEqualTypeOf<typeof json>();133 t.deepEqual(responseJson, json);134});135136test('POST JSON', async t => {137 t.plan(2);138139 const server = await createHttpTestServer(t);
2364 unmodified lines
modified:test/methods.ts+20 added
41 unmodified lines
42 }),43 );44});4546test('QUERY method is normalized', async t => {47 const server = await createHttpTestServer(t);48 server.all('/', (_request, response) => {49 response.end();50 });5152 t.plan(1);5354 await ky(server.url, {55 method: 'query',56 hooks: {57 beforeRequest: [58 ({options}) => {59 t.is(options.method, 'QUERY');60 },61 ],62 },63 });64});6566test.failing('custom method remains identical', async t => {67 const server = await createHttpTestServer(t);68 server.all('/', (_request, response) => {69 response.end();
28 unmodified lines
modified:test/retry.ts+35 added
87 unmodified lines
88 });89 t.is(requestCount, 1);90});9192test('QUERY retries by default', async t => {93 let requestCount = 0;94 const receivedBodies: unknown[] = [];95 const receivedMethods: string[] = [];9697 const server = await createHttpTestServer(t);98 server.all('/', (request, response) => {99 requestCount++;100 receivedMethods.push(request.method);101 receivedBodies.push(request.body);102103 if (requestCount === defaultRetryCount + 1) {104 response.json(request.body);105 } else {106 response.sendStatus(500);107 }108 });109110 const json = {111 foo: true,112 };113114 const result = await ky.query(server.url, {115 json,116 retry: {117 delay: () => 0,118 },119 }).json();120121 t.deepEqual(result, json);122 t.is(requestCount, defaultRetryCount + 1);123 t.deepEqual(receivedMethods, ['QUERY', 'QUERY', 'QUERY']);124 t.deepEqual(receivedBodies, [json, json, json]);125});126127test('respect Retry-After: 0 and retry immediately', async t => {128 const retryCount = 4;129 let requestCount = 0;130
2171 unmodified lines
modified:readme.md+3 added-2 removed
171 unmodified lines
172### ky.put(input, options?)173### ky.patch(input, options?)174### ky.head(input, options?)175### ky.delete(input, options?)176### ky.query(input, options?)177178Sets `options.method` to the method name and makes a request.179180⌨️ **TypeScript:** Accepts an optional type parameter for use with JSON responses (see [`ky()`](#kyinput-options)).
18 unmodified lines
199Default: `'get'`200201HTTP method used to make the request.202202Internally, the standard methods (`GET`, `POST`, `PUT`, `PATCH`, `HEAD` and `DELETE`) are uppercased in order to avoid server errors due to case sensitivity.203Internally, the standard methods (`GET`, `POST`, `PUT`, `PATCH`, `HEAD`, `DELETE`, and `QUERY`) are uppercased in order to avoid server errors due to case sensitivity.204205##### json206207Type: `object` and any other value accepted by [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
69 unmodified lines
277278Type: `object | number`\279Default:280- `limit`: `2`280- `methods`: `get` `put` `head` `delete` `options` `trace`281- `methods`: `get` `put` `head` `delete` `options` `trace` `query`282- `statusCodes`: [`408`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) [`413`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/413) [`429`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) [`500`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) [`502`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/502) [`503`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/503) [`504`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/504)283- `afterStatusCodes`: [`413`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/413), [`429`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429), [`503`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/503)284- `maxRetryAfter`: `Infinity`285- `backoffLimit`: `Infinity`
1583 unmodified lines