Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Support Axios v1 #46

Merged
merged 8 commits into from
Oct 11, 2022
Merged

feat!: Support Axios v1 #46

merged 8 commits into from
Oct 11, 2022

Conversation

siketyan
Copy link
Collaborator

Closes #45

Warning BREAKING CHANGE: CreateAxiosRequestTransformer now must return AxiosRequestTransformer instead of AxiosResponseTransformer.

axios [1.0.0, 2.0.0) are now supported.
< 1.0.0 versions are also supported, but it will be deleted in the next major release.

To run jest testing on axios v1, I also added babel-jest to transform ES modules that axios exports because jest only supports CommonJS without enabling experimental ESM support implicitly.

BREAKING CHANGE: CreateAxiosRequestTransformer now must return AxiosRequestTransformer instead of AxiosResponseTransformer.
axios [1.0.0, 2.0.0) are now supported.
< 1.0.0 versions are also supported, but it will be deleted in the next
major release.

To run jest testing on axios v1, I also added babel-jest to transform
ES modules that axios exports because jest only supports CommonJS
without enabling experimental ESM support implicitly.
Comment on lines +4 to +7
// HACK: Axios exports their module in both CommonJS style and ESM style.
// However, jest does prefer the ESM one that occurs a runtime error.
// So in this resolver the main module of axios package is replaced to CommonJS one.
// This only applies to tests.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice solution 👍

@@ -78,7 +78,7 @@ export interface CreateAxiosInterceptor {
(options?: AxiosCaseMiddlewareOptions): AxiosInterceptor;
}
export interface CreateAxiosRequestTransformer {
(options?: AxiosCaseMiddlewareOptions): AxiosResponseTransformer;
(options?: AxiosCaseMiddlewareOptions): AxiosRequestTransformer;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch ✨

@@ -94,7 +94,7 @@ test('it should be converted on success', (done) => {
})
.then((response) => {
expect(JSON.stringify(response.data)).toBe(JSON.stringify(camelData));
expect(response.headers.contentType).toBe('application/json');
expect(response.headers.contentType || response.headers['Content-Type']).toBe('application/json');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of these changes?

Copy link
Collaborator Author

@siketyan siketyan Oct 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mpyw response.headers.contentType is no longer available in axios >= 1.0, so I added response.headers['Content-Type'] which actually exists. To keep backward compatibility, I left the previous one with logical OR. This means the tests can be run even with axios < 1.0.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siketyan

Was headers.contentType really generated by axios, not by axios-case-converter? I suspect that the camelCase conversion no longer works for latest axios.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siketyan
Copy link
Collaborator Author

@mpyw Could you please approve to run CI workflow for confirming the tests pass?

@mpyw mpyw merged commit 54c04da into mpyw:master Oct 11, 2022
@siketyan siketyan deleted the feat/support-axios-1 branch October 11, 2022 10:54
mpyw added a commit that referenced this pull request Oct 11, 2022
mpyw added a commit that referenced this pull request Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Axios v1.0.0 compatibility
2 participants