request-options.ts
This file
Path
test/request-options.ts
48 lines · 1.6 KiB · TypeScript
Last changed by
Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177
History of this fileTake it
Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177
lines 1–48why, one step back1import test from 'ava';2import ky from '../source/index.js';34test('undefined request options clear inherited defaults without changing the parent', async t => {5 const url = 'https://example.com';6 const parent = ky.create({7 method: 'POST',8 body: 'parent',9 credentials: 'include',10 cache: 'no-store',11 async fetch(request) {12 return Response.json({body: await request.text(), credentials: request.credentials, cache: request.cache});13 },14 });15 const resetOptions = {body: undefined, credentials: undefined, cache: undefined};16 const extended = parent.extend(resetOptions);17 const expected = {body: '', credentials: 'same-origin', cache: 'default'};1819 t.deepEqual(await parent(url, resetOptions).json(), expected);20 t.deepEqual(await extended(url).json(), expected);21 t.deepEqual(await extended(new Request(url, {22 method: 'POST',23 body: 'input',24 credentials: 'omit',25 cache: 'reload',26 })).json(), {body: 'input', credentials: 'omit', cache: 'reload'});27 t.deepEqual(await parent(url).json(), {body: 'parent', credentials: 'include', cache: 'no-store'});28});2930test('init hooks can clear inherited request options', async t => {31 const response = await ky.post('https://example.com', {32 body: 'parent',33 credentials: 'include',34 cache: 'no-store',35 hooks: {36 init: [options => {37 options.body = undefined;38 options.credentials = undefined;39 options.cache = undefined;40 }],41 },42 async fetch(request) {43 return Response.json({body: await request.text(), credentials: request.credentials, cache: request.cache});44 },45 }).json();4647 t.deepEqual(response, {body: '', credentials: 'same-origin', cache: 'default'});48});