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
@wordpress/api-fetch remove window dependency to support node env #17551
Comments
@swissspidy I'm planning to make a PR for this feature, this is the idea isBatak@72e3505 for the API. But, now tests are failing and I have no idea how to fix them. Do you have any suggestion? |
Well which tests are failing...? If you'd create the PR already then you could point to the failing tests on Travis CI and more easily get technical feedback from maintainers. |
I'll make a PR now, and see what will happen :D |
The PR #17574 seems to have plenty of technical feedback, so I'm removing that label. Also removing In Progress, since the PR hasn't had recent updates. |
FYI, added this to the "Mobile Apps" project board to cover the apps side of the issue. |
Besides this solutions
another solution would be to export a factory function import { createApiFetch } from '@wordpress/api-fetch'
// Browser
const apiFetch = createApiFetch({ fetch: window.fetch })
// Node
const apiFetch = createApiFetch({ fetch: require('node-fetch') }) My workaround import fetch from 'node-fetch'
global.window = { fetch }
import apiFetch from '@wordpress/api-fetch'
apiFetch({ path: '/wp-json/wp/v2/users' }).then(console.log) |
I'm trying to use
@wordpress/api-fetch
on node (decoupled WordPress and SSR), but it is not easy to polyfill fetch because ofwindow.fetch
usage https://github.com/WordPress/gutenberg/blob/master/packages/api-fetch/src/index.js#L161 and I don't want to reimplement wholedefaultFetchHandler
logic withsetFetchHandler
solution. I just want to replacefetch
with something else likeisomorphic-fetch
.Solution:
fetch
The text was updated successfully, but these errors were encountered: