Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration to not import "whatwg-fetch" nor use window at all on typescript-fetch-client-generator #49

Open
tolstenko opened this issue Sep 18, 2024 · 2 comments · May be fixed by #50

Comments

@tolstenko
Copy link

tolstenko commented Sep 18, 2024

Hi!

On Nextjs, SSR only allows fetch API and doesn't allow window.

The generated file runtime.ts contains:

import "whatwg-fetch";
export const defaultFetch = window.fetch;

but I constantly edit to be like this:

export const defaultFetch = fetch;

Today, I just created a script to do that for me, and this change would help react-ssr people like me. :-)

So please create a new generator or add a configuration to do that for us :)

BTW, Thank you for your excellent work!

@karlvr karlvr transferred this issue from karlvr/openapi-generator-plus Sep 18, 2024
@karlvr
Copy link
Owner

karlvr commented Sep 18, 2024

@tolstenko thanks for this comment. Yes, the setup for fetch is pretty rough. It does work in the browser but it feels a bit weird... and I'm not sure if it's the generator's responsibility to provide a polyfill.

I have currently setup the template with a template hook so you can customise it... it's not super nice but possible.

In your config file you need to add:

custom: ./custom

And then in that custom folder, create a hooks folder and put a file called runtimeImports.hbs and put a single line in that:

export const defaultFetch = fetch;

This is using the custom templates capability.

I'm not sure whether this is an answer to this issue... I have setup the generator so you can customise this...

Maybe it would be better to put in some auto detection of the environment by default? In JavaScript we could check to see if there's a fetch global and use that and then check if there's a window.fetch and use that, or not have a default and then the user has to provide fetch when they create the API.

What do you think about that?

@karlvr
Copy link
Owner

karlvr commented Sep 18, 2024

Something like this instead:

export const defaultFetch = (typeof fetch !== "undefined" ? fetch : typeof window !== "undefined" && typeof window.fetch !== "undefined" ? window.fetch : undefined);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants