fabien0102 / openapi-codegen

A tool for generating code base on an OpenAPI schema.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Change window.fetch to fetch

SeverinBucher opened this issue · comments

Generated code uses window.fetch to fetch from the backend api. The window object is only available in the browser.

const response = await window.fetch(\`\${baseUrl}\${resolveUrl(url, queryParams, pathParams)}\`,

I would like to do server side rendering. This means I have to be able to fetch data with nodeJS.

  • Currently all browser seams to alias window.fetch to fetch.
  • nodeJS 18+ supports fetch natively.

I'm willing to work on this issue. It looks like an easy fix.

Motivation:
I'm using openapi-codegen to generate code to use in a frontend framework using nodeJS. Server-side-rendering is a basic feature supported by all well known frameworks. So far the generated code cannot be used directly because on the server there is no window object. After this fix every framework using nodeJS v18+ can use the generated code directly with no impact on the client side code.

Hey!
This is not an issue since the {namespace}fetcher file is only generated once and is meant to be tweaked depending on your need 😉
You can also use another fetch library or whatever you want as soon as the signature is fulfilled.

Thanks for looking at it. Just a follow up question. Is there a benefit of using window in any case?

No real benefit, it's more about habit, I do prefer explicit things so you know that we are using the global fetch and not an external library. I think I start using window.fetch instead of fetch after some weird TypeScript issues (probably fixed now).

No real benefit, it's more about habit, I do prefer explicit things so you know that we are using the global fetch and not an external library. I think I start using window.fetch instead of fetch after some weird TypeScript issues (probably fixed now).

To bring this back up but, that does create issues with things that do not have a window such as, nodejs anything with ssr and probably more that I do not know, if not to remove window directly at least make it an opt out