contentful.js
contentful.js copied to clipboard
Unusable with CloudFlare workers due to Axios bug
Expected Behavior
I would expect to be able to use Contentful JS from a CloudFlare worker.
Actual Behavior
I cannot use Contentful's Node/JS SDK from a CloudFlare worker because of axios/axios#2507.
Possible Solution(s)
- Stop using Axios and just use native APIs.
- Switch to a commit of Axios with the proposed fix (axios/axios#2508).
- Write an adapter for Axios to use WebWorker-available tools like
fetch
.
Steps to Reproduce
- Create a CloudFlare Worker.
- Install Contentful's JS SDK.
- Follow Contentful's directions to initialize the JS SDK.
- Observe that, even with minimal code (any code), the following error occurs:
var client = contentful.createClient({
accessToken: config.accessToken,
space: config.space
});
// ... later, in a function ...
const response = Contentful.client.getEntries({
'content_type': 'SomeObject'
});
response.then((data) => {
console.log('we got some simple data', data);
}, (err) => {
console.error('we could not fetch some simple data', err);
});
![Screen Shot 2020-04-11 at 2 08 26 AM](https://user-images.githubusercontent.com/171897/79039895-56852300-7b99-11ea-9172-d96efd21004f.png)
Context
This issue is blocking us from proceeding with Contentful.
Environment
- Language Version: CloudFlare Worker
-
Package Version:
7.14.2
- Which API are you using?: Delivery (but this issue will affect any network traffic from CFW)
hi contentful devs
people are pissed off about this
you've refused to fix it
we've stopped using your platform because of it
we even pushed code for you to try and fix this, but we just can't maintain it ourselves
there is no reason to use axios anymore when fetch is the norm and XHR is deprecated.
thanks, a pissed off dev
I have a quick code to make axios works on wrangler dev
. This is very the first iteration, and a lot of features are missing, but this can can fire a request to Contentful API.
return createClient({
space: CONTENTFUL_SPACE_ID as string,
accessToken: CONTENTFUL_DELIVERY_API as string,
environment: CONTENTFUL_ENV || "master",
adapter: async (config: AxiosRequestConfig) => {
const url = new URL(`${config.baseURL}/${config.url}`);
if (config.params) {
for(const key of Object.keys(config.params)) {
url.searchParams.append(key, config.params[key]);
}
}
const request = new Request(url.href, {
method: config.method ? config.method.toUpperCase() : "GET",
body: config.data,
redirect: 'manual',
headers: config.headers ? config.headers : {}
// credentials: config.withCredentials ? 'include' : 'omit', // not implemented on CF
//mode: 'cors' // not implemented on CF,
});
const response = await fetch(request);
return {
data: await response.json(),
status: response.status,
statusText: response.statusText,
headers: response.headers,
config: config,
request: request
};
}
});
closing the issue, if you still need help with this please reach out to Contentful support