[codex] fix transformRequest input typing#10745
Conversation
a381472 to
f5a1fa9
Compare
There was a problem hiding this comment.
2 issues found across 3 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="tests/module/esm/tests/ts.module.test.js">
<violation number="1" location="tests/module/esm/tests/ts.module.test.js:25">
P2: Custom agent: **Flag AI Slop and Fabricated Changes**
This test no longer validates repo-local axios resolution because it omits the local checkout symlink, leaving the PR’s behavior change untested.</violation>
</file>
<file name="tests/module/cjs/tests/ts-require-default.module.test.cjs">
<violation number="1" location="tests/module/cjs/tests/ts-require-default.module.test.cjs:26">
P1: Passing null removes the local checkout symlink, so this test no longer reliably validates the current axios repo.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
|
It seems to me that perhaps this PR is invalid. in short, the argument data cannot be of type of https://github.com/axios/axios/blob/main/lib/core/transformData.js#L22 utils.forEach(fns, function transform(fn) {
data = fn.call(config, data, headers.normalize(), response ? response.status : undefined);
});
export interface AxiosRequestTransformer<D = any> {
(this: InternalAxiosRequestConfig<D>, data: D, headers: AxiosRequestHeaders): any;argument has type of type of data must be equal to type of result of AxiosRequestTransformer (because I think the current typing is valid only for the first call to transformRequest, but if there is an array of them and the first one does, for example, convert an object to a string or to a Buffer, then the typings will not correspond to what will happen at runtime! |
|
Thanks for digging into this. You're right, I don't think that invalidates the PR, though. It just means the generic has a narrower scope than a fully sound pipeline type would. I added a JSDoc on both interfaces. It explains the pipeline semantics, calls out that D/T only describes the first call, warns that later stages may see a serialised form, and says when typing D is safe and when you need a runtime check. |
but this is precisely what makes this PR invalid!
But the interface and the code itself don't distinguish between the first call and subsequent calls. then it makes sense to simply not provide the API in order to create more than 1 transform request (but then backward compatibility will be lost)...
It turns out that the typings needed for compile-time do not describe what happens at runtime. |
|
Fair, I agree that is true. I will revert this one as agreed we gain nothing really from this. |
|
Reverted in #10810 |
From typings we get a well documented and type-safety API, this is valuable! |
@jasonsaayman We can make typing improvements that will match what happens at runtime like this (while maintaining backward compatibility): https://github.com/axios/axios/blob/v1.x/lib/adapters/http.js#L574 interface AxiosRequestTransformer {
(this: InternalAxiosRequestConfig, data: any, headers: AxiosRequestHeaders): any;
}
interface AxiosRequestConfig<D = any> {
transformRequest?: AxiosRequestTransformer | AxiosRequestTransformer[];======> interface FirstAxiosRequestTransformer<D> {
(this: InternalAxiosRequestConfig, data: D, headers: AxiosRequestHeaders): D | Stream | string | Buffer | ArrayBuffer;
}
interface AxiosRequestTransformer<D> {
(this: InternalAxiosRequestConfig, data: D | Stream | string | Buffer | ArrayBuffer, headers: AxiosRequestHeaders): D | Stream | string | Buffer | ArrayBuffer;
}
interface AxiosRequestConfig<D = any> {
transformRequest?: FirstAxiosRequestTransformer | [FirstAxiosRequestTransformer, ...AxiosRequestTransformer[]]; |
|
by the way @jasonsaayman import axios from 'axios';
const instance = axios.create({
baseURL: 'https://github.com/',
transformRequest: [
function (data, headers) {
return {};
},
],
});
instance.post('/', {});===> failed because https://github.com/axios/axios/blob/v1.x/lib/adapters/http.js#L574 ==> new AxiosError(
'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
AxiosError.ERR_BAD_REQUEST,
config
)Am I correct in understanding that the results of chain calls ==> type ResultOfChainAxiosRequestTransformer = Stream | string | Buffer | ArrayBuffer;
interface FirstAndOnlyAxiosRequestTransformer<D> {
(this: InternalAxiosRequestConfig, data: D, headers: AxiosRequestHeaders): ResultOfChainAxiosRequestTransformer;
}
interface FirstOfChainAxiosRequestTransformer<D> {
(this: InternalAxiosRequestConfig, data: D, headers: AxiosRequestHeaders): D | ResultOfChainAxiosRequestTransformer;
}
interface NotLastOfChainAxiosRequestTransformer<D> {
(this: InternalAxiosRequestConfig, data: D | ResultOfChainAxiosRequestTransformer, headers: AxiosRequestHeaders): D | ResultOfChainAxiosRequestTransformer;
}
interface LastOfChainAxiosRequestTransformer<D> {
(this: InternalAxiosRequestConfig, data: D | ResultOfChainAxiosRequestTransformer, headers: AxiosRequestHeaders): ResultOfChainAxiosRequestTransformer;
}
interface AxiosRequestConfig<D = any> {
transformRequest?: FirstAndOnlyAxiosRequestTransformer | [FirstOfChainAxiosRequestTransformer, ...NotLastOfChainAxiosRequestTransformer[], LastOfChainAxiosRequestTransformer]; |
Summary
AxiosRequestTransformergeneric over the request data typeAxiosRequestConfig<D>.transformRequestaxiosRoot cause
AxiosRequestConfig<D>carried the request data type, buttransformRequestalways receiveddata: any, so transformer implementations lost type information even when the surrounding request config was generic.The module typing fixtures also created temporary projects without a local
axiospackage entry, which let TypeScript resolve a higher-level installed copy instead of the current checkout. That made the new typing assertions unreliable until the fixture helpers were pointed at the local repo.Testing
npm run buildnpm run test:module:esmnpm run test:module:cjsFixes #10744
Summary by cubic
Fix TypeScript typings so
transformRequestreceives the correct data type and make response transformers generic. Improve ESM/CJS typing tests by linking the localaxiosand adding cases for generic request transformers.Description
Summary of changes
AxiosRequestTransformer<D>generic and threadedDthroughAxiosRequestConfig<D>.transformRequest.AxiosResponseTransformer<T>generic over response data.--noEmitand higher timeouts.repoRootand symlink the localaxios; tests passrepoRoot(ornull) and clean up temp fixtures.Reasoning
axios, fixing previous failing tests.Additional context
AxiosRequestTransformer#10744.Docs
Testing
repoRootis provided; tests use--noEmit, increased timeouts, and clean up temp dirs.Semantic version impact
Patch: Type-only improvements with no runtime changes.
Written for commit 3228da4. Summary will update on new commits.