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

Add a fetch option to allow wrappers #229

Closed
wants to merge 1 commit into from
Closed

Add a fetch option to allow wrappers #229

wants to merge 1 commit into from

Conversation

valeriangalliat
Copy link

@valeriangalliat valeriangalliat commented Feb 5, 2017

This way a wrapper library like fetch-cookie could transparently leverage node-fetch way of handling redirects without having to force { redirect: 'manual' } and re-implement the logic.

Instead, a wrapper could just pass the fetch option and point it to the wrapped function. And this would also allow wrappers to be combined as long as they also support being given the fetch option, e.g.:

function fetchCookie(url, opts) {
    return fetch(url, Object.assign({ fetch: fetchCookie }, opts));
}

@valeriangalliat valeriangalliat changed the title Add a self option to allow wrappers Add a fetch option to allow wrappers Feb 5, 2017
@bitinn
Copy link
Collaborator

bitinn commented Feb 8, 2017

From what I see this saves you from duplicating code, basically allow you to hook into the redirection process which is where automate cookie handling is desired, true?

@TimothyGu I think this makes sense, just wondering if fetch option naming is the way to go. I think it solves #146 and #94, or at least make implementing them trivial to users.

@valeriangalliat
Copy link
Author

From what I see this saves you from duplicating code, basically allow you to hook into the redirection process which is where automate cookie handling is desired, true?

Exactly. 👍

@ahmadnassri
Copy link
Contributor

resolver instead of fetch would be my choice for naming.

@valeriangalliat
Copy link
Author

I can rename it to resolver. Would that be fine @bitinn?

@bitinn
Copy link
Collaborator

bitinn commented Feb 28, 2017

@valeriangalliat I think resolver should do just fine, would you kindly add some comment, test and document for the change?

@TimothyGu what do you think, I do want this to happen, because it's one of the most discussed pain point of node-fetch. Given we are unlikely ever going to implement server-side cookies ourselves, we should make it easier for others to add this feature.

This way a wrapper library like fetch-cookie [1] could transparently
leverage node-fetch way of handling redirects without having to
force `{ redirect: 'manual' }` and re-implement the logic.

[1]: https://github.com/valeriangalliat/fetch-cookie

Instead, a wrapper could just pass the `resolver` option and point it to
the wrapped function. And this would also allow wrappers to be combined
as long as they also support being given the `resolver` option, e.g.:

    function fetchCookie(url, opts) {
        return fetch(url, Object.assign({ resolver: fetchCookie }, opts));
    }
@valeriangalliat
Copy link
Author

@bitinn I added comment, test and documentation for the resolver option. Does it looks good to you?

@TimothyGu
Copy link
Collaborator

TimothyGu commented Feb 28, 2017

For now this PR looks fine to me. On the long term, however, I envision a mechanism to have a "store" of Web interfaces including fetch, Promise, FormData, URL, Blob, URLSearchParams, CookieJar etc., that the users can pass in, and for node-fetch to use.

The approach I have on my mind is to exports an additional createFetch() function that allows users to bind specific available features. The traditional { fetch, Headers, Request, Response } API is created with createFetch() with a default set of available utilities (native Promise, etc.).

So to implement a fuller-featured isomorphic-fetch, something like this would work:

// isomorphic-fetch-node.js
import { URL, URLSearchParams } from 'url';

import { Blob } from 'file-api';
import { FormData } from 'form-data';
import { createFetch } from 'node-fetch';
import { CookieJar } from 'tough-cookie';

export createFetch({
  Blob,
  CookieJar,
  FormData,
  URL,
  URLSearchParams
});
// isomorphic-fetch-browser.js
import 'whatwg-fetch';
export { fetch, Headers, Request, Response };

And for traditional uses of the API can be extended:

import { Blob, readBlob } from 'file-api';
import createFetchCookie from 'fetch-cookie';
import originalFetch from 'node-fetch';

const fetch = createFetchCookie(originalFetch);

(async () => {
  const res = await fetch(url, {
    method: 'POST',
    body: 'abc',
    interfaces: {
      Blob,
      fetch // this line might not even be necessary
    }
  });
  const blob = await res.blob();
  const buf = await readBlob(blob);
  // Buffer.isBuffer(buf);
})();

@codecov-io
Copy link

Codecov Report

Merging #229 into master will decrease coverage by -1.03%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #229      +/-   ##
==========================================
- Coverage     100%   98.97%   -1.03%     
==========================================
  Files           6        6              
  Lines         383      392       +9     
  Branches      117      118       +1     
==========================================
+ Hits          383      388       +5     
- Misses          0        2       +2     
- Partials        0        2       +2
Impacted Files Coverage Δ
src/index.js 97.33% <100%> (-2.67%)
src/body.js 98.56% <0%> (-1.44%)
src/response.js 100% <0%> (ø)
src/headers.js 100% <0%> (ø)
src/request.js 100% <0%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a1e76b9...ad2f402. Read the comment docs.

@bitinn
Copy link
Collaborator

bitinn commented Mar 20, 2017

The approach I have on my mind is to exports an additional createFetch() function that allows users to bind specific available features.

I agree this would be the superior approach, but significant work needs to be done to support this modularize approach. Anyone up to the task?

(Also color me a bit wary of adding features most don't use)

@TimothyGu
Copy link
Collaborator

Closing, as we need a bit more time to figure out the best way to tackle this issue.

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.

5 participants