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
Provenance
- Commit
ee3dec1- Tree
1524ab7- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
0655b9d
2 files changed+193 added−3 removed
modified:source/utils/merge.ts+9 added-3 removed
308 unmodified lines309 }310 311 // `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 }325 326 if (!isReplace && isMergeable(returnValue[key]) && isMergeable(value)) {327 value = deepMergeInternal<unknown>(false, returnValue[key], value);51 unmodified linesmodified:test/retry.ts+184 added
1 import process from 'node:process';2 import {setTimeout as delay} from 'node:timers/promises';3 import test, {type ExecutionContext} from 'ava';4 import ky, {5+ replaceOption,6 NetworkError,7 TimeoutError,8 isKyError,9 isNetworkError,1172 unmodified lines1182 });1183 t.is(requestCount, 4);1184 });1185 1186+test('retry - extending an object `retry` with a number keeps the other options', async t => {1187+ let requestCount = 0;1188+1189+ const server = await createHttpTestServer(t);1190+ server.post('/', async (_request, response) => {1191+ requestCount++;1192+ response.sendStatus(500);1193+ });1194+1195+ // `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});1198+1199+ await t.throwsAsync(extended.post(server.url).text(), {1200+ message: /Internal Server Error/,1201+ });1202+ t.is(requestCount, 4);1203+});1204+1205+test('retry - numeric `retry` in per-request options keeps object defaults', async t => {1206+ let requestCount = 0;1207+1208+ const server = await createHttpTestServer(t);1209+ server.post('/', async (_request, response) => {1210+ requestCount++;1211+ response.sendStatus(500);1212+ });1213+1214+ const instance = ky.create({retry: {methods: ['post'], delay: () => 0}});1215+1216+ await t.throwsAsync(instance.post(server.url, {retry: 1}).text(), {1217+ message: /Internal Server Error/,1218+ });1219+ t.is(requestCount, 2);1220+});1221+1222+test('retry - numeric `retry: 0` in per-request options disables retries over object defaults', async t => {1223+ let requestCount = 0;1224+1225+ const server = await createHttpTestServer(t);1226+ server.post('/', async (_request, response) => {1227+ requestCount++;1228+ response.sendStatus(500);1229+ });1230+1231+ const instance = ky.create({retry: {methods: ['post'], delay: () => 0}});1232+1233+ await t.throwsAsync(instance.post(server.url, {retry: 0}).text(), {1234+ message: /Internal Server Error/,1235+ });1236+ t.is(requestCount, 1);1237+});1238+1239+test('retry - number, object, number chain keeps the object options and the last limit', async t => {1240+ let normalizedRetry: {limit: number; methods: string[]} | undefined;1241+1242+ const client = ky1243+ .create({retry: 5})1244+ .extend({retry: {methods: ['post']}})1245+ .extend({retry: 1});1246+1247+ 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+ });1259+1260+ t.deepEqual(normalizedRetry, {limit: 1, methods: ['post']});1261+});1262+1263+test('retry - extending an object `retry` with a number keeps custom status codes', async t => {1264+ let requestCount = 0;1265+1266+ const server = await createHttpTestServer(t);1267+ server.get('/', async (_request, response) => {1268+ requestCount++;1269+ response.sendStatus(418);1270+ });1271+1272+ const extended = ky.create({retry: {statusCodes: [418], delay: () => 0}}).extend({retry: 2});1273+1274+ await t.throwsAsync(extended(server.url).text(), {1275+ message: /I'm a Teapot/,1276+ });1277+ t.is(requestCount, 3);1278+});1279+1280+test('retry - extending an object `retry` with a number keeps `shouldRetry`', async t => {1281+ let requestCount = 0;1282+ let shouldRetryCalls = 0;1283+1284+ const server = await createHttpTestServer(t);1285+ server.get('/', async (_request, response) => {1286+ requestCount++;1287+ response.sendStatus(500);1288+ });1289+1290+ const extended = ky.create({1291+ retry: {1292+ delay: () => 0,1293+ shouldRetry() {1294+ shouldRetryCalls++;1295+ return false;1296+ },1297+ },1298+ }).extend({retry: 3});1299+1300+ await t.throwsAsync(extended(server.url).text(), {1301+ message: /Internal Server Error/,1302+ });1303+ t.is(requestCount, 1);1304+ t.is(shouldRetryCalls, 1);1305+});1306+1307+test('retry - `replaceOption` with a number replaces the whole object', async t => {1308+ let requestCount = 0;1309+1310+ const server = await createHttpTestServer(t);1311+ server.post('/', async (_request, response) => {1312+ requestCount++;1313+ response.sendStatus(500);1314+ });1315+1316+ // 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)});1318+1319+ await t.throwsAsync(extended.post(server.url).text(), {1320+ message: /Internal Server Error/,1321+ });1322+ t.is(requestCount, 1);1323+});1324+1325+test('retry - init hook sees a numeric `retry` merged into the object defaults', async t => {1326+ let initRetry: unknown;1327+1328+ const client = ky.create({retry: {methods: ['post']}}).extend({retry: 3});1329+1330+ 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+ });1342+1343+ t.deepEqual(initRetry, {methods: ['post'], limit: 3});1344+});1345+1346+test('retry - invalid numeric `retry` extending an object still throws', t => {1347+ const client = ky.create({retry: {methods: ['post']}}).extend({retry: -1});1348+1349+ 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+});1356+1357+test('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+ });1362+1363+ const client = ky.create({json: {retry: {foo: 'bar'}}}).extend({json: {retry: 3}});1364+1365+ const {body} = await client.post(server.url).json<{body: {retry: unknown}}>();1366+ t.is(body.retry, 3);1367+});1368+1369 test('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