From d1995243deb48d8d4bc1a5940fadffc1713d25f0 Mon Sep 17 00:00:00 2001 From: Karl von Randow Date: Thu, 19 Sep 2024 11:31:46 +1200 Subject: [PATCH] typescript-fetch-client: Try to autodetect default `fetch` rather than assuming a browser environment --- .changeset/fast-cheetahs-repair.md | 6 ++++++ .../templates/hooks/runtimeImports.hbs | 2 +- .../templates/hooks/runtimeImports.hbs | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 .changeset/fast-cheetahs-repair.md diff --git a/.changeset/fast-cheetahs-repair.md b/.changeset/fast-cheetahs-repair.md new file mode 100644 index 00000000..fa9e0e45 --- /dev/null +++ b/.changeset/fast-cheetahs-repair.md @@ -0,0 +1,6 @@ +--- +"@openapi-generator-plus/typescript-fetch-client-generator2": minor +"@openapi-generator-plus/typescript-fetch-client-generator": minor +--- + +Try to autodetect default `fetch` rather than assuming a browser environment diff --git a/packages/typescript-fetch-client/templates/hooks/runtimeImports.hbs b/packages/typescript-fetch-client/templates/hooks/runtimeImports.hbs index 14850ac4..5e983212 100644 --- a/packages/typescript-fetch-client/templates/hooks/runtimeImports.hbs +++ b/packages/typescript-fetch-client/templates/hooks/runtimeImports.hbs @@ -1,4 +1,4 @@ {{#if @root.includePolyfills}} import "whatwg-fetch"; {{/if}} -export const defaultFetch = window.fetch; +export const defaultFetch = (typeof fetch !== "undefined" ? fetch : typeof window !== "undefined" && typeof window.fetch !== "undefined" ? window.fetch : undefined); diff --git a/packages/typescript-fetch-client2/templates/hooks/runtimeImports.hbs b/packages/typescript-fetch-client2/templates/hooks/runtimeImports.hbs index 14850ac4..5e983212 100644 --- a/packages/typescript-fetch-client2/templates/hooks/runtimeImports.hbs +++ b/packages/typescript-fetch-client2/templates/hooks/runtimeImports.hbs @@ -1,4 +1,4 @@ {{#if @root.includePolyfills}} import "whatwg-fetch"; {{/if}} -export const defaultFetch = window.fetch; +export const defaultFetch = (typeof fetch !== "undefined" ? fetch : typeof window !== "undefined" && typeof window.fetch !== "undefined" ? window.fetch : undefined);