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: proper fetch-spec related input handling #86

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

AuHau
Copy link

@AuHau AuHau commented Sep 7, 2021

Closes #83

This PR improves spec compliance of this library with accepting Request instance as input and several other small improvements (see comments).

Also, I am not sure how form-data package works with axios as axios should not accept anything else than DOM instance of form-data as you can see here, but it indeed works which leaves me bit buffled. But I would advise against using `form-data as it is non-compliant with the spec.

Btw. also I would recommend separating the compiled JS files into build directory as for example if you build the project and then run tests it uses the builder sources and not the TypeScript one.

@@ -6,7 +6,7 @@
"types": "src/index.ts",
"scripts": {
"test": "nyc ava",
"lint": "eslint . --ext .js,.ts -f codeframe && tsc --noEmit",
Copy link
Author

Choose a reason for hiding this comment

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

I have removed JS files as if you have compiled files around it is not passing linting

@AuHau
Copy link
Author

AuHau commented Sep 19, 2021

@jagoda @mdlavin could you please have a look on the PR? Thanks!

Copy link
Member

@mdlavin mdlavin left a comment

Choose a reason for hiding this comment

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

Thank you for the fix! I think this improvement would be very nice to add. I'd love to help get it merged. There are a couple of changes that I'm curiuos about.

If you don't mind, can you @ message me so I get a direct notification again? It's too easy for things to get lost

test/index.test.ts Outdated Show resolved Hide resolved
@@ -129,24 +129,8 @@ test('handles text body with content-type init options', async (test) => {
test.deepEqual(axiosBody.headers['content-type'], expectedBody.headers['content-type']);
});

test('handles json body init options', async (test) => {
Copy link
Member

Choose a reason for hiding this comment

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

Can this test be added back, or did it stop working?

Copy link
Author

Choose a reason for hiding this comment

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

Well, thanks to proper Fetch function types definition on line 7, the types follow the Fetch spec, which does not allow JSON in body. The test is working, because underneath axios that support JSON body converts it and send it, but this is not use case supported by fetch.

I can make it working using @ts-ignore as otherwise there are types errors. Let me know what you want.

Copy link
Member

Choose a reason for hiding this comment

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

Dropping support for JSON in body is scary to me. Since the tests run both node-fetch and also axios-fetch based requests. Does that mean that node-fetch already supported the non-standard "JSON in body" approach? If so, I'd prefer to avoid breaking that feature, I'm almost positive that some code already depends on it working.

Copy link
Author

Choose a reason for hiding this comment

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

Sooo 😅 I wanted to implement it based on your request, but...

I have found out that actually, that use-case does not work even with the current master version 😉 The reason why it seems it works is that the test is written wrongly. I have printed out the body that nock receives and as per my suspicion I got:

Body:  [object Object]
Body:  [object Object]

And that is most probably because you use the String(init.body) and the tests is passing because you don't actually assert that the received body is the same as the sent one, but that the received bodies from both fetch and axios-fetch are the same which they are... [object Object].

So I think it is safe to "break" this feature because nobody could have used it 😉

Copy link
Member

Choose a reason for hiding this comment

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

Oh wow. That's a great insight! Sorry I missed this last comment. I just did a pass through my GitHub notifications and saw it. Let me check how close this is to mergable.

src/index.ts Show resolved Hide resolved
src/index.ts Show resolved Hide resolved
Copy link
Author

@AuHau AuHau left a comment

Choose a reason for hiding this comment

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

@mdlavin I have commented on your feedback with an explanation of my reasoning. Please let me know what you prefer and I will implement your requests.

src/index.ts Show resolved Hide resolved
src/index.ts Show resolved Hide resolved
@@ -129,24 +129,8 @@ test('handles text body with content-type init options', async (test) => {
test.deepEqual(axiosBody.headers['content-type'], expectedBody.headers['content-type']);
});

test('handles json body init options', async (test) => {
Copy link
Author

Choose a reason for hiding this comment

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

Well, thanks to proper Fetch function types definition on line 7, the types follow the Fetch spec, which does not allow JSON in body. The test is working, because underneath axios that support JSON body converts it and send it, but this is not use case supported by fetch.

I can make it working using @ts-ignore as otherwise there are types errors. Let me know what you want.

test/index.test.ts Outdated Show resolved Hide resolved
@AuHau AuHau requested a review from mdlavin September 22, 2021 15:27
@AuHau
Copy link
Author

AuHau commented Sep 23, 2021

@mdlavin I have fixed the extra option as you requested, but the JSON one is actually not working...

@@ -2,9 +2,11 @@ import { Response, Request, Headers as FetchHeaders, RequestInfo, RequestInit }
import { AxiosInstance, AxiosRequestConfig } from './types';
import FormData from 'form-data';

export type AxiosTransformer = (config: AxiosRequestConfig, input: RequestInfo, init?: RequestInit) => AxiosRequestConfig;
export type FetchInit = RequestInit & { extra?: any }
Copy link
Member

Choose a reason for hiding this comment

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

The idea was that any extra options (a superset of RequestInit) would be passed to the transformer, not just extra

Copy link
Member

Choose a reason for hiding this comment

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

It's be fine if you did some hacky typing in the test code to make it work

"pretest": "yarn lint",
"coverage": "nyc report --reporter=text-lcov > ./.nyc_output/lcov.info",
"prepublishOnly": "yarn tsc"
Copy link
Contributor

Choose a reason for hiding this comment

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

Why rename this step?

Copy link
Author

@AuHau AuHau Oct 7, 2021

Choose a reason for hiding this comment

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

This rename allows installation of this branch (and generally the package) from GitHub. If you don't want it, I can revert that before merge, but I needed it for our project to be used as a workaround until this PR is merged and released.

See: https://docs.npmjs.com/cli/v7/using-npm/scripts#life-cycle-scripts

Copy link
Contributor

Choose a reason for hiding this comment

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

When the tests all pass then there is a pre-release version that is published to NPM.

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.

fetch should also accept Request object
3 participants