js-api-client icon indicating copy to clipboard operation
js-api-client copied to clipboard

Typeform API client not compatible with Cloudflare workers / Vercel edge runtime

Open magnuslundin opened this issue 2 years ago • 9 comments

The current version of the Typeform API client does not run well on a Cloudflare worker / Vercel edge runtime. Creating a client result in the following error:

ReferenceError: window is not defined
    at (../../../../node_modules/.pnpm/@[email protected]/node_modules/@typeform/api-client/dist/typeform-api.js:1:209)
    at (../../../../node_modules/.pnpm/@[email protected]/node_modules/@typeform/api-client/dist/typeform-api.js:1:68)
    at (../../../../node_modules/.pnpm/@[email protected]/node_modules/@typeform/api-client/dist/typeform-api.js:1:170)
...

This is most probably caused by the (3 year old..) dependency to Axios. A switch to fetch should not only resolve the compatibility issue, but render the project free from build dependencies.

magnuslundin avatar Jul 06 '23 08:07 magnuslundin

Hello @magnuslundin

can you please show us how you are using the library? How do you import it? The dist/typeform-api.js file is intended to be used in a browser, not server-side which might be causing your issue.

Also, the axios dependency was updated recently. Please try with the latest version of the lib.

mathio avatar Oct 09 '23 21:10 mathio

Hello @magnuslundin

can you please show us how you are using the library? How do you import it? The dist/typeform-api.js file is intended to be used in a browser, not server-side which might be causing your issue.

Also, the axios dependency was updated recently. Please try with the latest version of the lib.

I'm using the library in a SvelteKit project deployed on Vercel. The typeform api client works well with the regular serverless functions (node). When I switched to using their Edge functions, which are utilizing CloudFlare workers the library generated the errors described above.

I'm currently circumventing this by forcing the specific part of the app that uses your library to use the serverless runtime.

Nice that the dependency is updated. I'll give it a go.

Edit: updated problem explanation

magnuslundin avatar Oct 10 '23 07:10 magnuslundin

Please let us know if the problem still remains with the latest version.

mathio avatar Oct 10 '23 11:10 mathio

I just noticed you mentioned a different Typeform library - Embed SDK. This library is not intended to be used server side. However when used with eg. NextJS it should render an empty div server-side and populate it with the embed (iframe with your typeform) when rendered client side - see React NextJS demos.

If the issue persists please raise it in embed SDK with details.

mathio avatar Oct 12 '23 12:10 mathio

Hi @mathio, Sorry for the confusion. It was a long time ago I posted the issue and I mixed up my typeform-challenges when this popped up :)

I've updated my comment above with the actual issue I was facing.

Version 1.15.0 of the client has now been tested with the same result. Axios does not play well on cloudflare workers.

magnuslundin avatar Oct 12 '23 15:10 magnuslundin

Thank you. I dont have much experience with Cloudflare workers, but I can test with Vercel.

mathio avatar Oct 23 '23 09:10 mathio

I'm getting the same error on nextjs server side. Is there an API client for server side?

teamclouday avatar Aug 07 '24 02:08 teamclouday

I'm getting the same error on nextjs server side. Is there an API client for server side?

No, there isn't. We have to make due creating our own implementations from this: https://www.typeform.com/developers/responses/

magnuslundin avatar Aug 29 '24 11:08 magnuslundin

I'm getting the same error on nextjs server side. Is there an API client for server side?

No, there isn't. We have to make due creating our own implementations from this: https://www.typeform.com/developers/responses/

Thank you! I was able to make it work by using fetch calls to the API directly, and cast the response to the types defined in this library.

teamclouday avatar Aug 30 '24 22:08 teamclouday