contentful.js icon indicating copy to clipboard operation
contentful.js copied to clipboard

Unusable with CloudFlare workers due to Axios bug

Open sgammon opened this issue 4 years ago • 2 comments

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

  1. Create a CloudFlare Worker.
  2. Install Contentful's JS SDK.
  3. Follow Contentful's directions to initialize the JS SDK.
  4. 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

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)

sgammon avatar Apr 11 '20 09:04 sgammon

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

sgammon avatar Jul 24 '20 16:07 sgammon

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
      };
    }
  });

rande avatar Aug 27 '20 23:08 rande

closing the issue, if you still need help with this please reach out to Contentful support

mayakarabula avatar Jun 13 '23 14:06 mayakarabula