Typeform / js-api-client

Typeform API js client
MIT License
78 stars 29 forks source link

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

Open magnuslundin opened 1 year ago

magnuslundin commented 1 year ago

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/@typeform+api-client@1.8.0/node_modules/@typeform/api-client/dist/typeform-api.js:1:209)
    at (../../../../node_modules/.pnpm/@typeform+api-client@1.8.0/node_modules/@typeform/api-client/dist/typeform-api.js:1:68)
    at (../../../../node_modules/.pnpm/@typeform+api-client@1.8.0/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.

mathio commented 11 months ago

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.

magnuslundin commented 11 months ago

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

mathio commented 11 months ago

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

mathio commented 11 months ago

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.

magnuslundin commented 11 months ago

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.

mathio commented 10 months ago

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

teamclouday commented 1 month ago

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

magnuslundin commented 2 weeks ago

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/

teamclouday commented 2 weeks ago

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.