Preserve JSON types when cloning requests and responses
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent 51b3177 · Browse files · 6 files·+71 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+71 added−0 removed
- tests:+38 added−0 removed
- other:+33 added−0 removed
Provenance
- Commit
f4886ca- Tree
c8c78a8- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
51b3177
6 files changed+71 added−0 removed
modified:source/types/request.ts+1 added
1234export type KyRequest<T = unknown> = { clone: () => KyRequest<T>; json: <J = T>() => Promise<J>;} & Request;modified:source/types/response.ts+1 added
1234export type KyResponse<T = unknown> = { clone: () => KyResponse<T>; json: <J = T>() => Promise<J>;} & Response;modified:test/hooks.ts+24 added
62 unmodified lines63 });64 65 const requestFixtureUrl = 'about:blank';66 67+test('beforeRequest can inspect cloned JSON without consuming the outgoing body', async t => {68+ const body = {name: 'Ada'};69+ let inspected = false;70+ const result = await ky.post('https://example.com', {71+ json: body,72+ hooks: {73+ beforeRequest: [async ({request}) => {74+ const clone = request.clone();75+ const nestedClone = clone.clone();76+ t.deepEqual(await clone.json<{name: string}>(), body);77+ t.deepEqual(await nestedClone.json<{name: string}>(), body);78+ t.false(request.bodyUsed);79+ inspected = true;80+ }],81+ },82+ async fetch(input) {83+ t.true(inspected);84+ return Response.json(await (input as Request).json());85+ },86+ }).json();87+88+ t.deepEqual(result, body);89+});90+91 const createStreamBody = (text: string) => new ReadableStream<Uint8Array>({92 start(controller) {93 controller.enqueue(new TextEncoder().encode(text));94 controller.close();6465 unmodified linesmodified:test/main.ts+14 added
26 unmodified lines27 fetch: async () => new Response(''),28 }).json(undefined), {instanceOf: SyntaxError});29 });30 31+test('typed response clones can independently parse the same JSON body', async t => {32+ const response = await ky<{name: string}>('https://example.com', {33+ fetch: async () => Response.json({name: 'Ada'}),34+ });35+ const clone = response.clone();36+ const nestedClone = clone.clone();37+38+ t.false(response.bodyUsed);39+ t.deepEqual(await clone.json(), {name: 'Ada'});40+ t.false(response.bodyUsed);41+ t.deepEqual(await nestedClone.json(), {name: 'Ada'});42+ t.deepEqual(await response.json(), {name: 'Ada'});43+});44+45 type TestSchemaResult<Output> = {value: Output} | {issues: Array<{message: string}>};46 47 const createSchema = <Output>(48 validate: (value: unknown) => TestSchemaResult<Output> | Promise<TestSchemaResult<Output>>,3899 unmodified linesadded:test-d/request-clone.ts+19 added
12345678910111213141516171819import {expectTypeOf} from 'expect-type';import ky, {type KyRequest} from 'ky';
const request: KyRequest<{name: string}> = new Request('https://example.com');const clone = request.clone();
expectTypeOf(clone.json()).toEqualTypeOf<Promise<{name: string}>>();expectTypeOf(clone.clone().json()).toEqualTypeOf<Promise<{name: string}>>();expectTypeOf(clone.json<{id: number}>()).toEqualTypeOf<Promise<{id: number}>>();
void ky.post('https://example.com', { json: {name: 'Ada'}, hooks: { beforeRequest: [({request}) => { expectTypeOf(request.clone().json()).toEqualTypeOf<Promise<unknown>>(); expectTypeOf(request.clone().json<{name: string}>()).toEqualTypeOf<Promise<{name: string}>>(); }], },});added:test-d/response-clone.ts+12 added
123456789101112import {expectTypeOf} from 'expect-type';import ky from 'ky';
const response = await ky<{name: string}>('https://example.com');const clone = response.clone();
expectTypeOf(clone.json()).toEqualTypeOf<Promise<{name: string}>>();expectTypeOf(clone.clone().json()).toEqualTypeOf<Promise<{name: string}>>();expectTypeOf(clone.json<{id: number}>()).toEqualTypeOf<Promise<{id: number}>>();
const unknownResponse = await ky('https://example.com');expectTypeOf(unknownResponse.clone().json()).toEqualTypeOf<Promise<unknown>>();