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
"Headers is not defined" occur when using apollo-link-rest in weixin mini program #222
Comments
Hi @Janice1114, I’m unfamiliar with weixin, but maybe you can use a Headers Polyfill? Often, if Headers is unavailable, Fetch will also be unavailable. So a polyfill that might work for both is this one: https://github.com/bitinn/node-fetch |
Hi @fbartho , thanks for your quick reply. I've tried to use the polyfill to enable fetch headers, but still get the errors. And I found that under weixin, it uses wx.request for the fetch, which does not support window related variable, so I'm afraid the polyfill won't work. And may I know would there be any hack or implementations to help to skip the headers loop in source code of apollo-link-rest? Or could you please give some custom options to suit under different scenario? |
You can use the Something like the following (untested, written in my browser): function customFetch(url, options) {
const headers = options.headers.entries().reduce((accumulator, h) => { accumulator[h[0]] = h[1]; return accumulator;}, {})
return fetch(url, {…options, headers});
}
// Elsewhere: new RestLink({ customFetch, ... |
So much appreciate for your kindly response, I will try to use this polyfill in weixin development. And as I read the source code in RestLink.ts, it will try to access the Headers, which is not available in weixin. It could assign the variables in So I wonder if any custom or overrided settings, could be applied during RestLink constructor process? Best regards again for your so wonderful tips. |
Hi @fbartho ,to fix this issue in weixin and related special browser, I've create a PR as below: Could you please help and review whether the code PR is available to merge into master? |
I have the same problem in |
I'm also having this problem with next, version
But using the same package to polyfill with the rest link did not work:
I also tried
In both cases I got Love the idea here, would love if you could provide some direction for how to implement in server environments. Thanks! |
You can polyfill the Headers API while still using your own customFetch API! Let me know if you would like some sample code there, but I’m confident this is doable without too much hassle. I’m not at my computer just this minute. |
@fbartho Some sample code would be great when you have a chance! I took a stab at it, but wasn't successful. Thanks! |
import * as Polyfillheaders from 'fetch-headers'
import fetch from 'isomorphic-unfetch'
// hook in the Headers polyfill for your environment!
global.Headers = global.Headers || Polyfillheaders;
function customFetch(url, options) {
const headers = options.headers.entries().reduce((accumulator, h) => {
accumulator[h[0]] = h[1]
return accumulator
}, {})
return fetch(url, { ...options, headers })
}
const restLink = new RestLink({
uri: CONTENTFUL_CONTENT_DELIVERY_API,
customFetch,
}) Something like the above might do the trick @2WheelCoder |
Thanks! I'll report back here after I have a chance to give this a shot. |
@fbartho Hi! I'm facing this problem on ios 9.3.5.I tried import the Is there any other solutions? |
I'm going to close this, because I believe we can resolve this with a polyfill on the App-side, and we don't want to make the polyfill a dependency of this repository. |
I have not been able to solve this with a polyfill. I've tried isomorphic-fetch, isomorphic-unfetch, fetch-headers, etc and always end up with an issue about |
@lintuming yes but it doesn’t work on the server. |
@lintuming thanks for trying to help. With that one, I get:
seems to be a tangled web of problems 🤣 |
@VinSpee Sorry,I have no idea why this happen.But i think this could help: |
Here's what ended up working for me: import clientFetch from 'unfetch';
import serverFetch, { Headers as ServerHeaders } from 'node-fetch';
const client = typeof document !== 'undefined';
global.Headers = client ? global.Headers : ServerHeaders;
const customFetch = client ? clientFetch : serverFetch;
const acmeLink = new JsonApiLink({
…
credentials: 'same-origin',
customFetch,
}); |
@VinSpee i am facing same issue and i tried to fix this issue with your solution but i am not succeeded. |
Thanks @VinSpee —this was exactly what I needed |
I was able to fix this issue by using custom fetch
|
@VinSpee 's solution worked for me for a Gatsby JS application as well. |
I'm using crossfetch, , but it complained about the |
Hey @damiangreen — do you have a gist or a full snippet of the code you’re using for poly filling the headers? This is a situation where you need the runtime to have a Headers instance, TypeScript is just being grumpy about some Anyhow, your code should look something like: import { Headers as HeadersPolyfill } from “my-headers-polyfill-source”
global.Headers = (HeadersPolyfill as any); // ‘as any’ to bypass the ‘prototype’ warning
…
const restLink = new RestLink(…); |
I was able to use a custom polyfill modified to work for typescript, as described in the node-fetch readme // fetch-polyfill.ts
import fetch, {
Blob,
blobFrom,
blobFromSync,
File,
fileFrom,
fileFromSync,
FormData,
Headers as FetchHeaders,
Request as FetchRequest,
Response as FetchResponse,
} from "node-fetch";
declare global {
var Headers: typeof FetchHeaders;
var Request: typeof FetchRequest;
var Response: typeof FetchResponse;
}
if (!globalThis.Headers) {
globalThis.Headers = FetchHeaders;
globalThis.Request = FetchRequest;
globalThis.Response = FetchResponse;
} Then from somewhere, early in your code // index.ts
import 'fetch-polyfill.ts'; |
When we try to use apollo-link-rest in weixin mini program, we encounted a problem which has throw an error as below:
As I found the source code in RestLink.ts, it does try to find the Headers, but absolutely, there is no Headers under weixin environment, so can anyone help to provide some hack or solutions to solve this issue? Much appreciate if you could help, thanks.
The text was updated successfully, but these errors were encountered: