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

Fix several bugs on HttpNativeImpl #626

Merged
merged 3 commits into from
Jun 18, 2022
Merged

Conversation

dahlia
Copy link
Contributor

@dahlia dahlia commented Jun 17, 2022

Trim content type parameters from HTTP responses

Previously, request() method from HttpAxiosImpl & HttpNativeImpl sometimes threw an error like below:

Uncaught (in promise) Error: Unknown content type application/json; charset=utf-8, {...}

It happened when a response from server contains Content-Type header with some parameters, e.g., application/json; charset=utf-8.

It might be better to make deserialize() method from SerializerNodejsImpl & SerializerNativeImpl to trim parameters from their input, but I trim parameters from response content types instead. If you leave a comment on this I'm going to address that.

On HttpNativeImpl.request<T>()'s return type

Also there's one more fix on HttpNativeImpl.request<T>() method. It should return Response<T>, but actually returned T instead. I addressed this bug as well.

Let HttpNativeImpl set an appropriate boundary for multipart/form-data

The standard fetch() API automatically configures an arbitrary boundary for multipart/form-data when its body is FormData. However, the boundary can be omitted if Content-Type header is manually set. I made HttpNativeImpl to leave Content-Type header unset when it's multipart/form-data so that the underlying fetch() can determine an appropriate boundary for the form data.

@dahlia dahlia changed the title fix: Trim content type parameters from HTTP responses Trim content type parameters from HTTP responses Jun 18, 2022
@dahlia dahlia force-pushed the trim-mime-types-parameters branch from 8b61ea4 to dbe520a Compare June 18, 2022 17:00
@dahlia dahlia changed the title Trim content type parameters from HTTP responses Fix several bugs on HttpNativeImpl Jun 18, 2022
Comment on lines 38 to 40
headers['Content-Type'] ?? 'application/json',
(headers['Content-Type'] ?? 'application/json')
.get('Content-Type')
.replace(/\s*;.*$/, ""),
Copy link
Owner

@neet neet Jun 18, 2022

Choose a reason for hiding this comment

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

I'd like to avoid repeating this code in both axios-impl and native-impl because I'm going to keep maintaining both implementations until fetch is well supported on Node.js.

How about adding something like getContentType(headers): string to BaseHttp class so that they can call the shared inherited method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That sounds way better. I'm going to extract duplicated code into a common method.

@@ -37,11 +47,14 @@ export class HttpNativeImpl extends BaseHttp implements Http {
body: body as string,
});
const text = await response.text();
const contentType = response.headers
Copy link
Owner

Choose a reason for hiding this comment

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

This block-scoped variable contentType has the same name and role as the variable in line 30.

@dahlia dahlia force-pushed the trim-mime-types-parameters branch from dbe520a to f277388 Compare June 18, 2022 18:26
@dahlia
Copy link
Contributor Author

dahlia commented Jun 18, 2022

@neet I did my best to address everything you reviewed. Could you take a look again?

@neet neet self-requested a review June 18, 2022 18:35
Copy link
Owner

@neet neet left a comment

Choose a reason for hiding this comment

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

Looks great, kudos @dahlia!

@neet neet merged commit 8432b19 into neet:main Jun 18, 2022
@github-actions
Copy link

🎉 This PR is included in version 4.3.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants