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

Use custom fetch handler if provided #174

Merged
merged 11 commits into from
Mar 3, 2018
Merged

Use custom fetch handler if provided #174

merged 11 commits into from
Mar 3, 2018

Conversation

alanfriedman
Copy link
Contributor

@alanfriedman alanfriedman commented Mar 2, 2018

Just a first pass, opening PR to start a discussion. Let me know what you think. Thanks!

To do:

  • Docs
  • Validation tests

@coveralls
Copy link

coveralls commented Mar 2, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling b830a6f on alanfriedman:custom-fetch into af94473 on agraboso:master.

Copy link
Collaborator

@nason nason left a comment

Choose a reason for hiding this comment

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

This is great @alanfriedman! Thanks for putting this together. I left a few comments/questions below, but nothing major.

Can you update the docs in this PR to reflect this change? We should update the following sections at least:

body,
headers,
options = {},
fetch: customFetch
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice! For simplicity below (line 51), what if this line read: fetch: doFetch = global.fetch

This way, we can call doFetch internally and it will always be callAPI.fetch, with a default of global.fetch

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, good call!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually we can't always rely on global being defined right?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Great catch. Yeah, this should be just fetch. This middleware expects fetch to be available globally, whether that's on window in browsers or global in node.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to default to fetch

foo: 'bar'
}),
{
// Example of custom `res.ok`
Copy link
Collaborator

Choose a reason for hiding this comment

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

#170 is addressing the ability to customize "ok" behavior per-request, what's going on here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

res.ok is true when the status is 200-299, so this is just an example of setting that boolean with something aside from the actual response status code.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Gotcha. The issue I linked to is proposing a way to pass in your own 'ok' check instead of res.ok so I was just confused seeing this here. Carry on!

@nason nason added this to the 3.0.0 milestone Mar 3, 2018
@alanfriedman
Copy link
Contributor Author

@nason I've added docs for custom fetch.

From what I can tell, this PR might also address #39 and #171.

Copy link
Collaborator

@nason nason left a comment

Choose a reason for hiding this comment

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

Amazing! Thanks so much for your contribution.

I left one point of feedback on the docs below, but other than that, this is good to go! Let me know what you think there, and I'll work on releasing this change soon.

README.md Outdated

A custom [Fetch](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch) implementation, useful for intercepting the fetch request to customize the response status, modify the response payload or skip the request altogether and provide a cached response instead.

`fetch` must be a function that returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) which resolves with an instance of [Request](https://developer.mozilla.org/en-US/docs/Web/API/Response).
Copy link
Collaborator

Choose a reason for hiding this comment

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

  • This should resolve to Response, yeah?
  • Also this only has to conform to the Fetch/Request/Response API, not actually to resolve to a Response object. ie, you can resolve to an object that has json(), statusCode, etc as this middleware isn't doing any sort of type checking here.

Can I suggest phrasing this as:

A custom Fetch implementation, useful for intercepting the fetch request to customize the response status, modifying the response payload or skipping the request altogether and providing a cached response instead.

If provided, the fetch option must be a function that conforms to the Fetch API. Otherwise, the global fetch will be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, that was a typo. Also good point, it doesn't need to be a Response object.

@nason
Copy link
Collaborator

nason commented Mar 3, 2018

From what I can tell, this PR might also address #39 and #171.

You're not wrong, but custom fetch might be overkill for those who just want a 304 to return a success FSA etc. Having the ability to pass in a successCheck function that defaults to res.ok() gives a lot of flexibility. These two features in combination open a lot of exciting possibilities 🎉

@nason nason merged commit 72906ea into agraboso:master Mar 3, 2018
@nason
Copy link
Collaborator

nason commented Mar 3, 2018

Released in v2.3.0

@alanfriedman alanfriedman deleted the custom-fetch branch March 3, 2018 18:35
@iamandrewluca iamandrewluca mentioned this pull request Mar 7, 2018
7 tasks
@iamandrewluca
Copy link
Collaborator

iamandrewluca commented Mar 7, 2018

In #171 I added an option so user can add global fetch when creating the middleware
Also added status checker. I left JSON handler to be handled by this PR.

@nason nason removed this from the 3.0.0 milestone Nov 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants