Skip to content
Myr

Fix extending an object `retry` with a number dropping the other retry options

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 0655b9d · Browse files · 2 files+193 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

2 files+193 added−3 removed

  • tests:+184 added−0 removed
  • other:+9 added−3 removed

2 files+193 added−3 removed

modified:source/utils/merge.ts+9 added-3 removed
308 unmodified lines
309 }310311 // `retry` accepts a number as shorthand for `{limit: number}`. Expand it before312 // merging so extending a numeric `retry` with an object keeps the limit instead313 // of dropping it (e.g. `ky.create({retry: 3}).extend({retry: {methods: ['get']}})`).313 // of dropping it (e.g. `ky.create({retry: 3}).extend({retry: {methods: ['get']}})`),314 // and extending an object `retry` with a number keeps the other options315 // (e.g. `ky.create({retry: {methods: ['post']}}).extend({retry: 3})`).316 // Scoped to the root options level so it never rewrites nested user data that317 // happens to contain a `retry` key (e.g. a `json` request body).318 if (isRoot && key === 'retry' && !isReplace) {316 if (isRoot && key === 'retry' && isObject(value) && !isReplace && typeof returnValue[key] === 'number') {317 returnValue = {...returnValue, [key]: {limit: returnValue[key]}};319 if (isObject(value) && typeof returnValue[key] === 'number') {320 returnValue = {...returnValue, [key]: {limit: returnValue[key]}};321 } else if (typeof value === 'number' && isObject(returnValue[key])) {322 value = {limit: value};323 }324 }325326 if (!isReplace && isMergeable(returnValue[key]) && isMergeable(value)) {327 value = deepMergeInternal<unknown>(false, returnValue[key], value);
51 unmodified lines
modified:test/retry.ts+184 added
1import process from 'node:process';2import {setTimeout as delay} from 'node:timers/promises';3import test, {type ExecutionContext} from 'ava';4import ky, {5 replaceOption,6 NetworkError,7 TimeoutError,8 isKyError,9 isNetworkError,
1172 unmodified lines
1182 });1183 t.is(requestCount, 4);1184});11851186test('retry - extending an object `retry` with a number keeps the other options', async t => {1187 let requestCount = 0;11881189 const server = await createHttpTestServer(t);1190 server.post('/', async (_request, response) => {1191 requestCount++;1192 response.sendStatus(500);1193 });11941195 // `retry: 3` is shorthand for `{limit: 3}`, so extending an object with it1196 // should only change the limit and keep `methods` and `delay`.1197 const extended = ky.create({retry: {methods: ['post'], delay: () => 0}}).extend({retry: 3});11981199 await t.throwsAsync(extended.post(server.url).text(), {1200 message: /Internal Server Error/,1201 });1202 t.is(requestCount, 4);1203});12041205test('retry - numeric `retry` in per-request options keeps object defaults', async t => {1206 let requestCount = 0;12071208 const server = await createHttpTestServer(t);1209 server.post('/', async (_request, response) => {1210 requestCount++;1211 response.sendStatus(500);1212 });12131214 const instance = ky.create({retry: {methods: ['post'], delay: () => 0}});12151216 await t.throwsAsync(instance.post(server.url, {retry: 1}).text(), {1217 message: /Internal Server Error/,1218 });1219 t.is(requestCount, 2);1220});12211222test('retry - numeric `retry: 0` in per-request options disables retries over object defaults', async t => {1223 let requestCount = 0;12241225 const server = await createHttpTestServer(t);1226 server.post('/', async (_request, response) => {1227 requestCount++;1228 response.sendStatus(500);1229 });12301231 const instance = ky.create({retry: {methods: ['post'], delay: () => 0}});12321233 await t.throwsAsync(instance.post(server.url, {retry: 0}).text(), {1234 message: /Internal Server Error/,1235 });1236 t.is(requestCount, 1);1237});12381239test('retry - number, object, number chain keeps the object options and the last limit', async t => {1240 let normalizedRetry: {limit: number; methods: string[]} | undefined;12411242 const client = ky1243 .create({retry: 5})1244 .extend({retry: {methods: ['post']}})1245 .extend({retry: 1});12461247 await client.post('https://example.com', {1248 async fetch() {1249 return new Response('ok');1250 },1251 hooks: {1252 beforeRequest: [1253 ({options}) => {1254 normalizedRetry = {limit: options.retry.limit, methods: options.retry.methods};1255 },1256 ],1257 },1258 });12591260 t.deepEqual(normalizedRetry, {limit: 1, methods: ['post']});1261});12621263test('retry - extending an object `retry` with a number keeps custom status codes', async t => {1264 let requestCount = 0;12651266 const server = await createHttpTestServer(t);1267 server.get('/', async (_request, response) => {1268 requestCount++;1269 response.sendStatus(418);1270 });12711272 const extended = ky.create({retry: {statusCodes: [418], delay: () => 0}}).extend({retry: 2});12731274 await t.throwsAsync(extended(server.url).text(), {1275 message: /I'm a Teapot/,1276 });1277 t.is(requestCount, 3);1278});12791280test('retry - extending an object `retry` with a number keeps `shouldRetry`', async t => {1281 let requestCount = 0;1282 let shouldRetryCalls = 0;12831284 const server = await createHttpTestServer(t);1285 server.get('/', async (_request, response) => {1286 requestCount++;1287 response.sendStatus(500);1288 });12891290 const extended = ky.create({1291 retry: {1292 delay: () => 0,1293 shouldRetry() {1294 shouldRetryCalls++;1295 return false;1296 },1297 },1298 }).extend({retry: 3});12991300 await t.throwsAsync(extended(server.url).text(), {1301 message: /Internal Server Error/,1302 });1303 t.is(requestCount, 1);1304 t.is(shouldRetryCalls, 1);1305});13061307test('retry - `replaceOption` with a number replaces the whole object', async t => {1308 let requestCount = 0;13091310 const server = await createHttpTestServer(t);1311 server.post('/', async (_request, response) => {1312 requestCount++;1313 response.sendStatus(500);1314 });13151316 // Replacing drops the custom `methods`, so POST falls back to the default non-retriable behavior.1317 const extended = ky.create({retry: {methods: ['post'], delay: () => 0}}).extend({retry: replaceOption(3)});13181319 await t.throwsAsync(extended.post(server.url).text(), {1320 message: /Internal Server Error/,1321 });1322 t.is(requestCount, 1);1323});13241325test('retry - init hook sees a numeric `retry` merged into the object defaults', async t => {1326 let initRetry: unknown;13271328 const client = ky.create({retry: {methods: ['post']}}).extend({retry: 3});13291330 await client.post('https://example.com', {1331 async fetch() {1332 return new Response('ok');1333 },1334 hooks: {1335 init: [1336 options => {1337 initRetry = options.retry;1338 },1339 ],1340 },1341 });13421343 t.deepEqual(initRetry, {methods: ['post'], limit: 3});1344});13451346test('retry - invalid numeric `retry` extending an object still throws', t => {1347 const client = ky.create({retry: {methods: ['post']}}).extend({retry: -1});13481349 t.throws(() => {1350 void client.post('https://example.com');1351 }, {1352 instanceOf: TypeError,1353 message: '`retry.limit` must be a finite, non-negative integer',1354 });1355});13561357test('retry - numeric shorthand over an object does not rewrite nested user data with a `retry` key', async t => {1358 const server = await createHttpTestServer(t);1359 server.post('/', (request, response) => {1360 response.json({body: request.body});1361 });13621363 const client = ky.create({json: {retry: {foo: 'bar'}}}).extend({json: {retry: 3}});13641365 const {body} = await client.post(server.url).json<{body: {retry: unknown}}>();1366 t.is(body.retry, 3);1367});13681369test('retry - shorthand expansion does not rewrite nested user data with a `retry` key', async t => {1370 const server = await createHttpTestServer(t);1371 server.post('/', (request, response) => {1372 response.json({body: request.body});
2308 unmodified lines