[typescript-fetch] check if window is undefined#3326
Conversation
|
👍 Thanks for opening this issue! The team will review the labels and make any necessary changes. |
|
|
||
| get fetchApi(): FetchAPI { | ||
| return this.configuration.fetchApi || window.fetch.bind(window); | ||
| return this.configuration.fetchApi || (typeof window !== 'undefined' ? window.fetch.bind(window) : fetch ); |
There was a problem hiding this comment.
the symbol fetch isn't declared or defined anywhere in this file, so this should be a compilation error, right?
There was a problem hiding this comment.
No, fetch is available on global scope, if you have a shim in nodejs:
global.fetch = global.fetch || require('node-fetch')
Similarly to FormData:
global.FormData = global.FormData || require('form-data')
There was a problem hiding this comment.
but what if fetch is not defined in the global scope? or is it always available in the global scope? if so, why use window.fetch at all?
There was a problem hiding this comment.
according to https://developer.mozilla.org/de/docs/Web/API/WindowOrWorkerGlobalScope/fetch fetch should be available in any context
There was a problem hiding this comment.
If it is really available in the global scope in every browser (checked in Firefox & Chromium) & in node, I'd be in favor of only checking fetch if `configuration.fetchApi' isn't defined, i.e.
return this.configuration.fetchApi || fetch;
There was a problem hiding this comment.
@hello-brsd can you implement the change suggested by @TiFu?
|
Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors. Let me know if you need help fixing it. |
|
@hello-brsd any update here? |
1 similar comment
|
@hello-brsd any update here? |
PR checklist
./bin/to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh,./bin/openapi3/{LANG}-petstore.shif updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\. If contributing template-only or documentation-only changes which will change sample output, be sure to build the project first.master,4.1.x,5.0.x. Default:master.Description of the PR
The
windowreference should be checked, because if the client run on server side (universal js),windowis maybe undefined andfetchcan be in the global scope (for examplenode-fetch).@TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @nicokoenig (2018/09) @topce (2018/10) @akehir (2019/07)