Airtable / airtable.js

Airtable javascript client
MIT License
1.95k stars 395 forks source link

Can't use in a Cloudflare Worker #242

Open jaymakes11 opened 3 years ago

jaymakes11 commented 3 years ago

When trying to use via a Cloudflare Worker (https://workers.cloudflare.com/), I get:

TypeError: c.default is not a constructor
    at Object.t [as default] (worker.js:1:95829)
    at t.runAction (worker.js:1:64890)
    at t._createRecords (worker.js:1:20673)
    at worker.js:1:9086
    at new Promise (<anonymous>)
    at t.create (worker.js:1:9020)
    at worker.js:1:96859
    at o (worker.js:1:96877)
    at async worker.js:1:97295
jaymakes11 commented 3 years ago

The runtime for Cloudflare Workers is not browser or node, which I suspect is the issue here. It uses the V8 JS engine directly (https://developers.cloudflare.com/workers/runtime-apis/web-standards). This means it doesn't have access to some runtime APIs that are available in Node.

Hoping this is not a blocker for being able to run Airtable in Cloudflare Workers.

jaymakes11 commented 3 years ago

Posted this in the Cloudflare forum. Findings (from here https://community.cloudflare.com/t/cant-use-airtable-from-worker/232590/2):

The problem looks to be that it’s trying to grab window.fetch, which isn’t possible within a service worker, and then defaulting to nodeFetch if that’s not available.

If that is, indeed, the case, this seems similar to the previous problems FaunaDB's JS driver was having in Cloudflare Workers. There's already a fetch method available to workers, so the solution they took was to allow passing in a custom fetch handler when initiating the client. See here: https://github.com/fauna/faunadb-js/issues/207

Perhaps the same approach would work here?

rcotrina94 commented 2 years ago

I ended up using airtable-dnd, an "Unofficial Airtable API client for Node.js ported from airtable-deno" as it internally uses fetch and assumes it exists. API is quite similar to @Airtable/airtable.js.