2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-17 19:21:29 +03:00

fix: correct isCancel type to narrow to CanceledError<T> (#10952)

* fix(index.d.cts): correct isCancel type to narrow to CanceledError<T> instead of Cancel

* test(types): cover CJS isCancel narrowing

* test(types): assert isCancel preserves generic

---------

Co-authored-by: Jason Saayman <jasonsaayman@gmail.com>
This commit is contained in:
Mohammad Faiz
2026-05-28 23:08:30 +05:30
committed by GitHub
parent fa01b9255d
commit a2390a5c05
4 changed files with 25 additions and 1 deletions
+1
View File
@@ -11,6 +11,7 @@
- **AxiosHeaders:** Silently skip empty response header names emitted by some React Native Android responses instead of throwing. (**#6959**, **#10875**)
- **Config Security:** Ignore inherited `params` and `paramsSerializer` values when resolving request config, preventing prototype-pollution gadgets from changing serialized URLs. (**#10922**)
- **Types:** Add the missing readonly `name: 'CanceledError'` declaration to CommonJS `CanceledError` typings to match the ESM declarations. (**#10922**)
- **Types:** Correct the CommonJS `isCancel` type guard to narrow cancellation errors to `CanceledError<T>`, matching the ESM declaration. (**#10952**)
- **HTTP Adapter - Auth on Redirect:** HTTP Basic credentials supplied via `config.auth` are now restored on same-origin redirects, fixing a regression caused by `follow-redirects` >= 1.15.8 that broke `POST` requests answered with a 303 Location. Cross-origin redirects continue to drop credentials, preserving the existing T-R2 mitigation in `THREATMODEL.md`. (**#6929**)
- **HTTP Adapter - Socket Path:** Ignore inherited `socketPath` and `allowedSocketPaths` config values when building Node.js requests, preventing prototype-pollution SSRF via Unix sockets. (**#10901**)
- **React Native FormData:** Clear the default `Content-Type` header for React Native `FormData` requests so Android can build multipart bodies with the correct boundary. (**#10898**)
+1 -1
View File
@@ -696,7 +696,7 @@ declare namespace axios {
CanceledError: typeof CanceledError;
HttpStatusCode: typeof HttpStatusCode;
readonly VERSION: string;
isCancel(value: any): value is Cancel;
isCancel<T = any>(value: any): value is CanceledError<T>;
all<T>(values: Array<T | Promise<T>>): Promise<T[]>;
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
isAxiosError<T = any, D = any>(payload: any): payload is AxiosError<T, D>;
@@ -0,0 +1,12 @@
import axios = require('axios');
declare const thrown: unknown;
if (axios.isCancel<{ ok: true }>(thrown)) {
const canceled: InstanceType<typeof axios.CanceledError> = thrown;
const data: { ok: true } | undefined = thrown.response?.data;
// @ts-expect-error -- The generic response data must not be widened to any.
const wrongData: { ok: false } | undefined = thrown.response?.data;
console.log(canceled.message, data, wrongData);
}
@@ -25,4 +25,15 @@ describe('module cjs typings compatibility', () => {
cleanupTempFixture(fixturePath);
}
});
it('narrows isCancel to CanceledError in commonjs typings', () => {
const sourcePath = path.join(repoRoot, 'tests/module/cjs/tests/helpers/cjs-is-cancel-typing.ts');
const fixturePath = createTempFixture(suiteRoot, 'typings-cjs-is-cancel', sourcePath, tsconfig);
try {
runCommand('node', [tscBin, '--noEmit', '-p', 'tsconfig.json'], { cwd: fixturePath });
} finally {
cleanupTempFixture(fixturePath);
}
});
});