Square SDK is unusable in Cloudflare Workers
cuibonobo opened this issue · comments
Describe the bug
Attempting to use the Square SDK in a Cloudflare Workers environment will throw the following error:
TypeError: adapter is not a function
at module.exports (C:\Users\Jen\Dev\hive23.com\node_modules\axios\lib\core\dispatchRequest.js:53:10)
at Axios.request (C:\Users\Jen\Dev\hive23.com\node_modules\axios\lib\core\Axios.js:108:15)
at HttpClient2._axiosInstance (C:\Users\Jen\Dev\hive23.com\node_modules\axios\lib\helpers\bind.js:9:15)
at HttpClient2.<anonymous> (C:\Users\Jen\Dev\hive23.com\node_modules\@apimatic\core\es\http\httpClient.js:189:20)
at step (C:\Users\Jen\Dev\hive23.com\node_modules\@apimatic\core\node_modules\tslib\tslib.js:144:27)
at Object.next (C:\Users\Jen\Dev\hive23.com\node_modules\@apimatic\core\node_modules\tslib\tslib.js:125:57)
at C:\Users\Jen\Dev\hive23.com\node_modules\@apimatic\core\node_modules\tslib\tslib.js:118:75
at new Promise (<anonymous>)
at __awaiter (C:\Users\Jen\Dev\hive23.com\node_modules\@apimatic\core\node_modules\tslib\tslib.js:114:16)
at HttpClient2.executeRequest (C:\Users\Jen\Dev\hive23.com\node_modules\@apimatic\core\es\http\httpClient.js:159:12)
The root cause of this error is that the Axios library does not use fetch
for network requests (which is required for Cloudflare Workers and other ServiceWorker
-compatible environments). There is a feature request in Axios that has been open for a few years related to this: axios/axios#1219
The error says adapter is not a function
which is because the XMLHTTPRequest
adapter is not available in a Cloudflare Workers environment. It might be possible to fix this bug by allowing users to pass their own adapter to Axios. Example of a fetch
adapter for Axios: https://github.com/vespaiach/axios-fetch-adapter
Expected behavior
Square SDK is able to access payment APIs in a Cloudflare Workers environment.
To Reproduce
Steps to reproduce the bug:
- Create a Cloudflare Workers function that initializes a Square SDK client
- Call
createPayment
(or some other Square API) when the function is called - Error is thrown
Square SDK version
For example: 24.0.0
Additional context
- Here's a Contentful.js bug caused by the same Axios issue: contentful/contentful.js#395
- Users that want to create Progressive Web Apps will also be affected by this bug, since PWAs use
ServiceWorker
s to fetch content.
From reviewing the SDK code I've realized that the unstable_httpClientOptions
parameter for the Square Client
is passed directly to the axios.create()
function. This allows me to pass a fetch
adapter like this:
const client = new Client({
accessToken,
environment,
unstable_httpClientOptions: { adapter: fetchAdapter }
});
Unfortunately the fetch
adapter provided by vespaiach/axios-fetch-adapter imports utilities directly from axios
, which causes errors in a Cloudflare Worker. To fix this, I've made an alternate version without the axios
dependency: @haverstack/axios-fetch-adapter
.
Since it is possible to supply an adapter to the Square SDK, this issue can be closed.