diff --git a/generators/typescript/sdk/CHANGELOG.md b/generators/typescript/sdk/CHANGELOG.md index dbf32082757..7964daf0979 100644 --- a/generators/typescript/sdk/CHANGELOG.md +++ b/generators/typescript/sdk/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.46.7] - 2025-01-09 + +- Fix: Simplify runtime detection to reduce the chance of using an unsupported API like `process.` + Detect Edge Runtime by Vercel. + ## [0.46.6] - 2025-01-09 - Fix: Update `@types/node` to `18+`, required for the generated `Node18UniversalStreamWrapper` test. diff --git a/generators/typescript/sdk/VERSION b/generators/typescript/sdk/VERSION index 6b9ee5cbf15..471890e8039 100644 --- a/generators/typescript/sdk/VERSION +++ b/generators/typescript/sdk/VERSION @@ -1 +1 @@ -0.46.6 +0.46.7 diff --git a/generators/typescript/utils/core-utilities/fetcher/src/fetcher/__test__/makeRequest.test.ts b/generators/typescript/utils/core-utilities/fetcher/src/fetcher/__test__/makeRequest.test.ts index d5a762b3c1e..f0978af47c5 100644 --- a/generators/typescript/utils/core-utilities/fetcher/src/fetcher/__test__/makeRequest.test.ts +++ b/generators/typescript/utils/core-utilities/fetcher/src/fetcher/__test__/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../runtime"; import { makeRequest } from "../makeRequest"; describe("Test makeRequest", () => { diff --git a/generators/typescript/utils/core-utilities/fetcher/src/fetcher/__test__/requestWithRetries.test.ts b/generators/typescript/utils/core-utilities/fetcher/src/fetcher/__test__/requestWithRetries.test.ts index a40d5bac977..0047a420014 100644 --- a/generators/typescript/utils/core-utilities/fetcher/src/fetcher/__test__/requestWithRetries.test.ts +++ b/generators/typescript/utils/core-utilities/fetcher/src/fetcher/__test__/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../runtime"; import { requestWithRetries } from "../requestWithRetries"; describe("requestWithRetries", () => { diff --git a/generators/typescript/utils/core-utilities/fetcher/src/runtime/runtime.ts b/generators/typescript/utils/core-utilities/fetcher/src/runtime/runtime.ts index 22fe45ba685..19f04b9e54b 100644 --- a/generators/typescript/utils/core-utilities/fetcher/src/runtime/runtime.ts +++ b/generators/typescript/utils/core-utilities/fetcher/src/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd" }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime" + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker" }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native" diff --git a/seed/ts-sdk/alias-extends/src/core/runtime/runtime.ts b/seed/ts-sdk/alias-extends/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/alias-extends/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/alias-extends/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/alias-extends/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/alias-extends/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/alias-extends/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/alias-extends/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/alias-extends/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/alias-extends/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/alias-extends/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/alias-extends/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/alias/src/core/runtime/runtime.ts b/seed/ts-sdk/alias/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/alias/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/alias/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/alias/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/alias/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/alias/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/alias/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/alias/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/alias/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/alias/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/alias/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/any-auth/src/core/runtime/runtime.ts b/seed/ts-sdk/any-auth/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/any-auth/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/any-auth/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/any-auth/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/any-auth/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/any-auth/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/any-auth/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/any-auth/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/any-auth/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/any-auth/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/any-auth/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/api-wide-base-path/src/core/runtime/runtime.ts b/seed/ts-sdk/api-wide-base-path/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/api-wide-base-path/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/api-wide-base-path/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/api-wide-base-path/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/api-wide-base-path/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/api-wide-base-path/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/api-wide-base-path/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/api-wide-base-path/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/api-wide-base-path/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/api-wide-base-path/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/api-wide-base-path/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/audiences/no-custom-config/src/core/runtime/runtime.ts b/seed/ts-sdk/audiences/no-custom-config/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/audiences/no-custom-config/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/audiences/no-custom-config/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/audiences/no-custom-config/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/audiences/no-custom-config/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/audiences/no-custom-config/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/audiences/no-custom-config/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/audiences/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/audiences/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/audiences/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/audiences/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/audiences/with-partner-audience/src/core/runtime/runtime.ts b/seed/ts-sdk/audiences/with-partner-audience/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/audiences/with-partner-audience/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/audiences/with-partner-audience/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/audiences/with-partner-audience/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/audiences/with-partner-audience/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/audiences/with-partner-audience/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/audiences/with-partner-audience/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/audiences/with-partner-audience/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/audiences/with-partner-audience/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/audiences/with-partner-audience/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/audiences/with-partner-audience/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/auth-environment-variables/src/core/runtime/runtime.ts b/seed/ts-sdk/auth-environment-variables/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/auth-environment-variables/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/auth-environment-variables/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/auth-environment-variables/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/auth-environment-variables/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/auth-environment-variables/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/auth-environment-variables/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/auth-environment-variables/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/auth-environment-variables/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/auth-environment-variables/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/auth-environment-variables/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/basic-auth-environment-variables/src/core/runtime/runtime.ts b/seed/ts-sdk/basic-auth-environment-variables/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/basic-auth-environment-variables/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/basic-auth-environment-variables/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/basic-auth-environment-variables/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/basic-auth-environment-variables/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/basic-auth-environment-variables/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/basic-auth-environment-variables/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/basic-auth-environment-variables/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/basic-auth-environment-variables/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/basic-auth-environment-variables/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/basic-auth-environment-variables/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/basic-auth/src/core/runtime/runtime.ts b/seed/ts-sdk/basic-auth/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/basic-auth/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/basic-auth/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/basic-auth/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/basic-auth/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/basic-auth/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/basic-auth/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/basic-auth/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/basic-auth/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/basic-auth/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/basic-auth/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/bearer-token-environment-variable/src/core/runtime/runtime.ts b/seed/ts-sdk/bearer-token-environment-variable/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/bearer-token-environment-variable/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/bearer-token-environment-variable/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/bearer-token-environment-variable/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/bearer-token-environment-variable/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/bearer-token-environment-variable/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/bearer-token-environment-variable/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/bearer-token-environment-variable/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/bearer-token-environment-variable/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/bearer-token-environment-variable/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/bearer-token-environment-variable/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/bytes/src/core/runtime/runtime.ts b/seed/ts-sdk/bytes/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/bytes/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/bytes/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/bytes/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/bytes/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/bytes/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/bytes/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/bytes/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/bytes/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/bytes/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/bytes/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/cross-package-type-names/src/core/runtime/runtime.ts b/seed/ts-sdk/cross-package-type-names/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/cross-package-type-names/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/cross-package-type-names/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/cross-package-type-names/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/cross-package-type-names/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/cross-package-type-names/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/cross-package-type-names/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/cross-package-type-names/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/cross-package-type-names/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/cross-package-type-names/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/cross-package-type-names/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/custom-auth/src/core/runtime/runtime.ts b/seed/ts-sdk/custom-auth/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/custom-auth/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/custom-auth/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/custom-auth/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/custom-auth/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/custom-auth/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/custom-auth/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/custom-auth/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/custom-auth/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/custom-auth/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/custom-auth/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/enum/src/core/runtime/runtime.ts b/seed/ts-sdk/enum/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/enum/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/enum/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/enum/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/enum/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/enum/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/enum/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/enum/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/enum/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/enum/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/enum/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/error-property/union-utils/src/core/runtime/runtime.ts b/seed/ts-sdk/error-property/union-utils/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/error-property/union-utils/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/error-property/union-utils/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/error-property/union-utils/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/error-property/union-utils/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/error-property/union-utils/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/error-property/union-utils/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/error-property/union-utils/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/error-property/union-utils/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/error-property/union-utils/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/error-property/union-utils/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/examples/examples-with-api-reference/src/core/runtime/runtime.ts b/seed/ts-sdk/examples/examples-with-api-reference/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/examples/examples-with-api-reference/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/examples/examples-with-api-reference/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/examples/examples-with-api-reference/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/examples/examples-with-api-reference/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/examples/examples-with-api-reference/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/examples/examples-with-api-reference/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/examples/examples-with-api-reference/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/examples/examples-with-api-reference/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/examples/examples-with-api-reference/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/examples/examples-with-api-reference/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/examples/retain-original-casing/src/core/runtime/runtime.ts b/seed/ts-sdk/examples/retain-original-casing/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/examples/retain-original-casing/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/examples/retain-original-casing/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/examples/retain-original-casing/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/examples/retain-original-casing/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/examples/retain-original-casing/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/examples/retain-original-casing/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/examples/retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/examples/retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/examples/retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/examples/retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/exhaustive/allow-extra-fields/src/core/runtime/runtime.ts b/seed/ts-sdk/exhaustive/allow-extra-fields/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/exhaustive/allow-extra-fields/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/exhaustive/allow-extra-fields/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/exhaustive/allow-extra-fields/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/exhaustive/allow-extra-fields/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/exhaustive/allow-extra-fields/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/exhaustive/allow-extra-fields/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/exhaustive/allow-extra-fields/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/exhaustive/allow-extra-fields/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/exhaustive/allow-extra-fields/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/exhaustive/allow-extra-fields/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/exhaustive/bigint/src/core/runtime/runtime.ts b/seed/ts-sdk/exhaustive/bigint/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/exhaustive/bigint/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/exhaustive/bigint/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/exhaustive/bigint/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/exhaustive/bigint/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/exhaustive/bigint/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/exhaustive/bigint/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/exhaustive/bigint/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/exhaustive/bigint/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/exhaustive/bigint/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/exhaustive/bigint/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/exhaustive/bundle/src/core/runtime/runtime.ts b/seed/ts-sdk/exhaustive/bundle/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/exhaustive/bundle/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/exhaustive/bundle/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/exhaustive/bundle/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/exhaustive/bundle/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/exhaustive/bundle/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/exhaustive/bundle/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/exhaustive/bundle/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/exhaustive/bundle/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/exhaustive/bundle/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/exhaustive/bundle/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/exhaustive/custom-package-json/src/core/runtime/runtime.ts b/seed/ts-sdk/exhaustive/custom-package-json/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/exhaustive/custom-package-json/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/exhaustive/custom-package-json/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/exhaustive/custom-package-json/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/exhaustive/custom-package-json/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/exhaustive/custom-package-json/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/exhaustive/custom-package-json/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/exhaustive/custom-package-json/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/exhaustive/custom-package-json/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/exhaustive/custom-package-json/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/exhaustive/custom-package-json/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/exhaustive/dev-dependencies/src/core/runtime/runtime.ts b/seed/ts-sdk/exhaustive/dev-dependencies/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/exhaustive/dev-dependencies/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/exhaustive/dev-dependencies/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/exhaustive/dev-dependencies/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/exhaustive/dev-dependencies/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/exhaustive/dev-dependencies/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/exhaustive/dev-dependencies/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/exhaustive/dev-dependencies/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/exhaustive/dev-dependencies/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/exhaustive/dev-dependencies/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/exhaustive/dev-dependencies/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/exhaustive/jsr/src/core/runtime/runtime.ts b/seed/ts-sdk/exhaustive/jsr/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/exhaustive/jsr/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/exhaustive/jsr/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/exhaustive/jsr/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/exhaustive/jsr/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/exhaustive/jsr/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/exhaustive/jsr/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/exhaustive/jsr/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/exhaustive/jsr/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/exhaustive/jsr/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/exhaustive/jsr/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/exhaustive/no-custom-config/src/core/runtime/runtime.ts b/seed/ts-sdk/exhaustive/no-custom-config/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/exhaustive/no-custom-config/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/exhaustive/no-custom-config/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/exhaustive/no-custom-config/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/exhaustive/no-custom-config/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/exhaustive/no-custom-config/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/exhaustive/no-custom-config/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/exhaustive/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/exhaustive/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/exhaustive/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/exhaustive/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/exhaustive/no-serde-layer/src/core/runtime/runtime.ts b/seed/ts-sdk/exhaustive/no-serde-layer/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/exhaustive/no-serde-layer/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/exhaustive/no-serde-layer/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/exhaustive/no-serde-layer/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/exhaustive/no-serde-layer/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/exhaustive/no-serde-layer/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/exhaustive/no-serde-layer/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/exhaustive/no-serde-layer/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/exhaustive/no-serde-layer/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/exhaustive/no-serde-layer/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/exhaustive/no-serde-layer/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/exhaustive/retain-original-casing/src/core/runtime/runtime.ts b/seed/ts-sdk/exhaustive/retain-original-casing/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/exhaustive/retain-original-casing/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/exhaustive/retain-original-casing/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/exhaustive/retain-original-casing/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/exhaustive/retain-original-casing/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/exhaustive/retain-original-casing/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/exhaustive/retain-original-casing/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/exhaustive/retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/exhaustive/retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/exhaustive/retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/exhaustive/retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/extends/src/core/runtime/runtime.ts b/seed/ts-sdk/extends/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/extends/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/extends/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/extends/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/extends/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/extends/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/extends/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/extends/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/extends/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/extends/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/extends/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/extra-properties/src/core/runtime/runtime.ts b/seed/ts-sdk/extra-properties/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/extra-properties/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/extra-properties/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/extra-properties/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/extra-properties/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/extra-properties/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/extra-properties/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/extra-properties/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/extra-properties/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/extra-properties/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/extra-properties/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/file-download/file-download-reponse-headers/src/core/runtime/runtime.ts b/seed/ts-sdk/file-download/file-download-reponse-headers/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/file-download/file-download-reponse-headers/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/file-download/file-download-reponse-headers/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/file-download/file-download-reponse-headers/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/file-download/file-download-reponse-headers/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/file-download/file-download-reponse-headers/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/file-download/file-download-reponse-headers/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/file-download/file-download-reponse-headers/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/file-download/file-download-reponse-headers/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/file-download/file-download-reponse-headers/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/file-download/file-download-reponse-headers/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/file-download/no-custom-config/src/core/runtime/runtime.ts b/seed/ts-sdk/file-download/no-custom-config/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/file-download/no-custom-config/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/file-download/no-custom-config/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/file-download/no-custom-config/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/file-download/no-custom-config/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/file-download/no-custom-config/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/file-download/no-custom-config/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/file-download/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/file-download/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/file-download/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/file-download/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/file-upload/no-custom-config/src/core/runtime/runtime.ts b/seed/ts-sdk/file-upload/no-custom-config/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/file-upload/no-custom-config/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/file-upload/no-custom-config/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/file-upload/no-custom-config/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/file-upload/no-custom-config/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/file-upload/no-custom-config/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/file-upload/no-custom-config/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/file-upload/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/file-upload/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/file-upload/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/file-upload/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/file-upload/wrap-file-properties/src/core/runtime/runtime.ts b/seed/ts-sdk/file-upload/wrap-file-properties/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/file-upload/wrap-file-properties/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/file-upload/wrap-file-properties/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/file-upload/wrap-file-properties/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/file-upload/wrap-file-properties/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/file-upload/wrap-file-properties/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/file-upload/wrap-file-properties/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/file-upload/wrap-file-properties/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/file-upload/wrap-file-properties/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/file-upload/wrap-file-properties/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/file-upload/wrap-file-properties/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/folders/src/core/runtime/runtime.ts b/seed/ts-sdk/folders/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/folders/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/folders/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/folders/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/folders/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/folders/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/folders/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/folders/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/folders/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/folders/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/folders/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/idempotency-headers/src/core/runtime/runtime.ts b/seed/ts-sdk/idempotency-headers/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/idempotency-headers/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/idempotency-headers/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/idempotency-headers/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/idempotency-headers/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/idempotency-headers/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/idempotency-headers/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/idempotency-headers/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/idempotency-headers/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/idempotency-headers/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/idempotency-headers/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/imdb/no-custom-config/src/core/runtime/runtime.ts b/seed/ts-sdk/imdb/no-custom-config/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/imdb/no-custom-config/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/imdb/no-custom-config/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/imdb/no-custom-config/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/imdb/no-custom-config/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/imdb/no-custom-config/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/imdb/no-custom-config/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/imdb/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/imdb/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/imdb/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/imdb/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/imdb/noScripts/src/core/runtime/runtime.ts b/seed/ts-sdk/imdb/noScripts/src/core/runtime/runtime.ts index 22fe45ba685..19f04b9e54b 100644 --- a/seed/ts-sdk/imdb/noScripts/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/imdb/noScripts/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd" }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime" + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker" }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native" diff --git a/seed/ts-sdk/imdb/noScripts/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/imdb/noScripts/tests/unit/fetcher/makeRequest.test.ts index 82aac80bb3f..b1d08eacc81 100644 --- a/seed/ts-sdk/imdb/noScripts/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/imdb/noScripts/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/imdb/noScripts/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/imdb/noScripts/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/imdb/noScripts/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/imdb/noScripts/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/imdb/omit-undefined/src/core/runtime/runtime.ts b/seed/ts-sdk/imdb/omit-undefined/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/imdb/omit-undefined/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/imdb/omit-undefined/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/imdb/omit-undefined/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/imdb/omit-undefined/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/imdb/omit-undefined/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/imdb/omit-undefined/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/imdb/omit-undefined/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/imdb/omit-undefined/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/imdb/omit-undefined/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/imdb/omit-undefined/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/license/src/core/runtime/runtime.ts b/seed/ts-sdk/license/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/license/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/license/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/license/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/license/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/license/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/license/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/license/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/license/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/license/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/license/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/literal/src/core/runtime/runtime.ts b/seed/ts-sdk/literal/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/literal/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/literal/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/literal/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/literal/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/literal/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/literal/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/literal/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/literal/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/literal/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/literal/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/mixed-case/no-custom-config/src/core/runtime/runtime.ts b/seed/ts-sdk/mixed-case/no-custom-config/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/mixed-case/no-custom-config/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/mixed-case/no-custom-config/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/mixed-case/no-custom-config/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/mixed-case/no-custom-config/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/mixed-case/no-custom-config/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/mixed-case/no-custom-config/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/mixed-case/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/mixed-case/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/mixed-case/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/mixed-case/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/mixed-case/retain-original-casing/src/core/runtime/runtime.ts b/seed/ts-sdk/mixed-case/retain-original-casing/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/mixed-case/retain-original-casing/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/mixed-case/retain-original-casing/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/mixed-case/retain-original-casing/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/mixed-case/retain-original-casing/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/mixed-case/retain-original-casing/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/mixed-case/retain-original-casing/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/mixed-case/retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/mixed-case/retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/mixed-case/retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/mixed-case/retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/mixed-file-directory/src/core/runtime/runtime.ts b/seed/ts-sdk/mixed-file-directory/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/mixed-file-directory/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/mixed-file-directory/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/mixed-file-directory/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/mixed-file-directory/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/mixed-file-directory/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/mixed-file-directory/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/mixed-file-directory/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/mixed-file-directory/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/mixed-file-directory/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/mixed-file-directory/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/multi-line-docs/src/core/runtime/runtime.ts b/seed/ts-sdk/multi-line-docs/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/multi-line-docs/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/multi-line-docs/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/multi-line-docs/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/multi-line-docs/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/multi-line-docs/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/multi-line-docs/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/multi-line-docs/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/multi-line-docs/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/multi-line-docs/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/multi-line-docs/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/multi-url-environment-no-default/src/core/runtime/runtime.ts b/seed/ts-sdk/multi-url-environment-no-default/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/multi-url-environment-no-default/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/multi-url-environment-no-default/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/multi-url-environment-no-default/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/multi-url-environment-no-default/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/multi-url-environment-no-default/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/multi-url-environment-no-default/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/multi-url-environment-no-default/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/multi-url-environment-no-default/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/multi-url-environment-no-default/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/multi-url-environment-no-default/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/multi-url-environment/src/core/runtime/runtime.ts b/seed/ts-sdk/multi-url-environment/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/multi-url-environment/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/multi-url-environment/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/multi-url-environment/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/multi-url-environment/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/multi-url-environment/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/multi-url-environment/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/multi-url-environment/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/multi-url-environment/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/multi-url-environment/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/multi-url-environment/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/no-environment/src/core/runtime/runtime.ts b/seed/ts-sdk/no-environment/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/no-environment/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/no-environment/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/no-environment/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/no-environment/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/no-environment/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/no-environment/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/no-environment/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/no-environment/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/no-environment/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/no-environment/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/oauth-client-credentials-default/src/core/runtime/runtime.ts b/seed/ts-sdk/oauth-client-credentials-default/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/oauth-client-credentials-default/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/oauth-client-credentials-default/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/oauth-client-credentials-default/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/oauth-client-credentials-default/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/oauth-client-credentials-default/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/oauth-client-credentials-default/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/oauth-client-credentials-default/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/oauth-client-credentials-default/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/oauth-client-credentials-default/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/oauth-client-credentials-default/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/oauth-client-credentials-environment-variables/src/core/runtime/runtime.ts b/seed/ts-sdk/oauth-client-credentials-environment-variables/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/oauth-client-credentials-environment-variables/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/oauth-client-credentials-environment-variables/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/oauth-client-credentials-environment-variables/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/oauth-client-credentials-environment-variables/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/oauth-client-credentials-environment-variables/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/oauth-client-credentials-environment-variables/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/oauth-client-credentials-environment-variables/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/oauth-client-credentials-environment-variables/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/oauth-client-credentials-environment-variables/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/oauth-client-credentials-environment-variables/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/oauth-client-credentials-nested-root/never-throw-errors/src/core/runtime/runtime.ts b/seed/ts-sdk/oauth-client-credentials-nested-root/never-throw-errors/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/oauth-client-credentials-nested-root/never-throw-errors/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/oauth-client-credentials-nested-root/never-throw-errors/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/oauth-client-credentials-nested-root/never-throw-errors/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/oauth-client-credentials-nested-root/never-throw-errors/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/oauth-client-credentials-nested-root/never-throw-errors/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/oauth-client-credentials-nested-root/never-throw-errors/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/oauth-client-credentials-nested-root/never-throw-errors/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/oauth-client-credentials-nested-root/never-throw-errors/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/oauth-client-credentials-nested-root/never-throw-errors/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/oauth-client-credentials-nested-root/never-throw-errors/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/oauth-client-credentials-nested-root/no-custom-config/src/core/runtime/runtime.ts b/seed/ts-sdk/oauth-client-credentials-nested-root/no-custom-config/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/oauth-client-credentials-nested-root/no-custom-config/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/oauth-client-credentials-nested-root/no-custom-config/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/oauth-client-credentials-nested-root/no-custom-config/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/oauth-client-credentials-nested-root/no-custom-config/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/oauth-client-credentials-nested-root/no-custom-config/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/oauth-client-credentials-nested-root/no-custom-config/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/oauth-client-credentials-nested-root/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/oauth-client-credentials-nested-root/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/oauth-client-credentials-nested-root/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/oauth-client-credentials-nested-root/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/oauth-client-credentials/src/core/runtime/runtime.ts b/seed/ts-sdk/oauth-client-credentials/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/oauth-client-credentials/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/oauth-client-credentials/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/oauth-client-credentials/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/oauth-client-credentials/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/oauth-client-credentials/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/oauth-client-credentials/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/oauth-client-credentials/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/oauth-client-credentials/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/oauth-client-credentials/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/oauth-client-credentials/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/optional/src/core/runtime/runtime.ts b/seed/ts-sdk/optional/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/optional/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/optional/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/optional/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/optional/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/optional/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/optional/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/optional/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/optional/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/optional/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/optional/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/package-yml/src/core/runtime/runtime.ts b/seed/ts-sdk/package-yml/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/package-yml/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/package-yml/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/package-yml/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/package-yml/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/package-yml/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/package-yml/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/package-yml/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/package-yml/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/package-yml/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/package-yml/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/pagination/src/core/runtime/runtime.ts b/seed/ts-sdk/pagination/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/pagination/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/pagination/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/pagination/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/pagination/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/pagination/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/pagination/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/pagination/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/pagination/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/pagination/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/pagination/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/path-parameters/default/src/core/runtime/runtime.ts b/seed/ts-sdk/path-parameters/default/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/path-parameters/default/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/path-parameters/default/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/path-parameters/default/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/path-parameters/default/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/path-parameters/default/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/path-parameters/default/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/path-parameters/default/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/path-parameters/default/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/path-parameters/default/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/path-parameters/default/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/path-parameters/inline-path-parameters-no-serde/src/core/runtime/runtime.ts b/seed/ts-sdk/path-parameters/inline-path-parameters-no-serde/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/path-parameters/inline-path-parameters-no-serde/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/path-parameters/inline-path-parameters-no-serde/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/path-parameters/inline-path-parameters-no-serde/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/path-parameters/inline-path-parameters-no-serde/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/path-parameters/inline-path-parameters-no-serde/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/path-parameters/inline-path-parameters-no-serde/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/path-parameters/inline-path-parameters-no-serde/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/path-parameters/inline-path-parameters-no-serde/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/path-parameters/inline-path-parameters-no-serde/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/path-parameters/inline-path-parameters-no-serde/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/path-parameters/inline-path-parameters-retain-original-casing/src/core/runtime/runtime.ts b/seed/ts-sdk/path-parameters/inline-path-parameters-retain-original-casing/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/path-parameters/inline-path-parameters-retain-original-casing/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/path-parameters/inline-path-parameters-retain-original-casing/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/path-parameters/inline-path-parameters-retain-original-casing/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/path-parameters/inline-path-parameters-retain-original-casing/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/path-parameters/inline-path-parameters-retain-original-casing/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/path-parameters/inline-path-parameters-retain-original-casing/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/path-parameters/inline-path-parameters-retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/path-parameters/inline-path-parameters-retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/path-parameters/inline-path-parameters-retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/path-parameters/inline-path-parameters-retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/path-parameters/inline-path-parameters/src/core/runtime/runtime.ts b/seed/ts-sdk/path-parameters/inline-path-parameters/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/path-parameters/inline-path-parameters/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/path-parameters/inline-path-parameters/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/path-parameters/inline-path-parameters/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/path-parameters/inline-path-parameters/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/path-parameters/inline-path-parameters/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/path-parameters/inline-path-parameters/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/path-parameters/inline-path-parameters/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/path-parameters/inline-path-parameters/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/path-parameters/inline-path-parameters/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/path-parameters/inline-path-parameters/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/path-parameters/retain-original-casing/src/core/runtime/runtime.ts b/seed/ts-sdk/path-parameters/retain-original-casing/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/path-parameters/retain-original-casing/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/path-parameters/retain-original-casing/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/path-parameters/retain-original-casing/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/path-parameters/retain-original-casing/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/path-parameters/retain-original-casing/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/path-parameters/retain-original-casing/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/path-parameters/retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/path-parameters/retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/path-parameters/retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/path-parameters/retain-original-casing/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/plain-text/src/core/runtime/runtime.ts b/seed/ts-sdk/plain-text/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/plain-text/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/plain-text/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/plain-text/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/plain-text/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/plain-text/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/plain-text/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/plain-text/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/plain-text/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/plain-text/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/plain-text/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/query-parameters/no-custom-config/src/core/runtime/runtime.ts b/seed/ts-sdk/query-parameters/no-custom-config/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/query-parameters/no-custom-config/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/query-parameters/no-custom-config/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/query-parameters/no-custom-config/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/query-parameters/no-custom-config/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/query-parameters/no-custom-config/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/query-parameters/no-custom-config/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/query-parameters/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/query-parameters/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/query-parameters/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/query-parameters/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/query-parameters/no-serde-layer-query/src/core/runtime/runtime.ts b/seed/ts-sdk/query-parameters/no-serde-layer-query/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/query-parameters/no-serde-layer-query/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/query-parameters/no-serde-layer-query/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/query-parameters/no-serde-layer-query/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/query-parameters/no-serde-layer-query/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/query-parameters/no-serde-layer-query/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/query-parameters/no-serde-layer-query/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/query-parameters/no-serde-layer-query/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/query-parameters/no-serde-layer-query/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/query-parameters/no-serde-layer-query/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/query-parameters/no-serde-layer-query/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/reserved-keywords/src/core/runtime/runtime.ts b/seed/ts-sdk/reserved-keywords/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/reserved-keywords/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/reserved-keywords/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/reserved-keywords/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/reserved-keywords/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/reserved-keywords/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/reserved-keywords/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/reserved-keywords/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/reserved-keywords/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/reserved-keywords/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/reserved-keywords/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/response-property/src/core/runtime/runtime.ts b/seed/ts-sdk/response-property/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/response-property/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/response-property/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/response-property/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/response-property/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/response-property/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/response-property/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/response-property/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/response-property/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/response-property/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/response-property/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/server-sent-event-examples/src/core/runtime/runtime.ts b/seed/ts-sdk/server-sent-event-examples/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/server-sent-event-examples/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/server-sent-event-examples/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/server-sent-event-examples/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/server-sent-event-examples/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/server-sent-event-examples/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/server-sent-event-examples/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/server-sent-event-examples/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/server-sent-event-examples/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/server-sent-event-examples/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/server-sent-event-examples/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/server-sent-events/src/core/runtime/runtime.ts b/seed/ts-sdk/server-sent-events/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/server-sent-events/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/server-sent-events/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/server-sent-events/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/server-sent-events/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/server-sent-events/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/server-sent-events/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/server-sent-events/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/server-sent-events/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/server-sent-events/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/server-sent-events/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/simple-fhir/src/core/runtime/runtime.ts b/seed/ts-sdk/simple-fhir/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/simple-fhir/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/simple-fhir/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/simple-fhir/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/simple-fhir/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/simple-fhir/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/simple-fhir/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/simple-fhir/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/simple-fhir/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/simple-fhir/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/simple-fhir/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/single-url-environment-default/src/core/runtime/runtime.ts b/seed/ts-sdk/single-url-environment-default/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/single-url-environment-default/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/single-url-environment-default/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/single-url-environment-default/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/single-url-environment-default/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/single-url-environment-default/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/single-url-environment-default/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/single-url-environment-default/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/single-url-environment-default/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/single-url-environment-default/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/single-url-environment-default/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/single-url-environment-no-default/src/core/runtime/runtime.ts b/seed/ts-sdk/single-url-environment-no-default/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/single-url-environment-no-default/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/single-url-environment-no-default/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/single-url-environment-no-default/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/single-url-environment-no-default/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/single-url-environment-no-default/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/single-url-environment-no-default/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/single-url-environment-no-default/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/single-url-environment-no-default/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/single-url-environment-no-default/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/single-url-environment-no-default/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/streaming/allow-custom-fetcher/src/core/runtime/runtime.ts b/seed/ts-sdk/streaming/allow-custom-fetcher/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/streaming/allow-custom-fetcher/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/streaming/allow-custom-fetcher/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/streaming/allow-custom-fetcher/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/streaming/allow-custom-fetcher/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/streaming/allow-custom-fetcher/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/streaming/allow-custom-fetcher/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/streaming/allow-custom-fetcher/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/streaming/allow-custom-fetcher/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/streaming/allow-custom-fetcher/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/streaming/allow-custom-fetcher/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/streaming/no-custom-config/src/core/runtime/runtime.ts b/seed/ts-sdk/streaming/no-custom-config/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/streaming/no-custom-config/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/streaming/no-custom-config/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/streaming/no-custom-config/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/streaming/no-custom-config/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/streaming/no-custom-config/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/streaming/no-custom-config/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/streaming/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/streaming/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/streaming/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/streaming/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/streaming/no-serde-layer/src/core/runtime/runtime.ts b/seed/ts-sdk/streaming/no-serde-layer/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/streaming/no-serde-layer/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/streaming/no-serde-layer/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/streaming/no-serde-layer/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/streaming/no-serde-layer/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/streaming/no-serde-layer/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/streaming/no-serde-layer/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/streaming/no-serde-layer/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/streaming/no-serde-layer/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/streaming/no-serde-layer/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/streaming/no-serde-layer/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/trace/exhaustive/src/core/runtime/runtime.ts b/seed/ts-sdk/trace/exhaustive/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/trace/exhaustive/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/trace/exhaustive/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/trace/exhaustive/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/trace/exhaustive/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/trace/exhaustive/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/trace/exhaustive/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/trace/exhaustive/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/trace/exhaustive/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/trace/exhaustive/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/trace/exhaustive/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/trace/no-custom-config/src/core/runtime/runtime.ts b/seed/ts-sdk/trace/no-custom-config/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/trace/no-custom-config/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/trace/no-custom-config/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/trace/no-custom-config/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/trace/no-custom-config/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/trace/no-custom-config/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/trace/no-custom-config/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/trace/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/trace/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/trace/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/trace/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/trace/no-zurg-no-throwing/src/core/runtime/runtime.ts b/seed/ts-sdk/trace/no-zurg-no-throwing/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/trace/no-zurg-no-throwing/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/trace/no-zurg-no-throwing/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/trace/no-zurg-no-throwing/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/trace/no-zurg-no-throwing/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/trace/no-zurg-no-throwing/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/trace/no-zurg-no-throwing/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/trace/no-zurg-no-throwing/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/trace/no-zurg-no-throwing/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/trace/no-zurg-no-throwing/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/trace/no-zurg-no-throwing/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/trace/no-zurg-trace/src/core/runtime/runtime.ts b/seed/ts-sdk/trace/no-zurg-trace/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/trace/no-zurg-trace/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/trace/no-zurg-trace/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/trace/no-zurg-trace/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/trace/no-zurg-trace/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/trace/no-zurg-trace/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/trace/no-zurg-trace/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/trace/no-zurg-trace/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/trace/no-zurg-trace/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/trace/no-zurg-trace/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/trace/no-zurg-trace/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/ts-express-casing/src/core/runtime/runtime.ts b/seed/ts-sdk/ts-express-casing/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/ts-express-casing/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/ts-express-casing/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/ts-express-casing/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/ts-express-casing/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/ts-express-casing/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/ts-express-casing/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/ts-express-casing/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/ts-express-casing/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/ts-express-casing/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/ts-express-casing/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/ts-inline-types/inline/src/core/runtime/runtime.ts b/seed/ts-sdk/ts-inline-types/inline/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/ts-inline-types/inline/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/ts-inline-types/inline/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/ts-inline-types/inline/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/ts-inline-types/inline/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/ts-inline-types/inline/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/ts-inline-types/inline/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/ts-inline-types/inline/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/ts-inline-types/inline/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/ts-inline-types/inline/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/ts-inline-types/inline/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/ts-inline-types/no-inline/src/core/runtime/runtime.ts b/seed/ts-sdk/ts-inline-types/no-inline/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/ts-inline-types/no-inline/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/ts-inline-types/no-inline/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/ts-inline-types/no-inline/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/ts-inline-types/no-inline/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/ts-inline-types/no-inline/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/ts-inline-types/no-inline/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/ts-inline-types/no-inline/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/ts-inline-types/no-inline/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/ts-inline-types/no-inline/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/ts-inline-types/no-inline/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/undiscriminated-unions/no-custom-config/src/core/runtime/runtime.ts b/seed/ts-sdk/undiscriminated-unions/no-custom-config/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/undiscriminated-unions/no-custom-config/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/undiscriminated-unions/no-custom-config/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/undiscriminated-unions/no-custom-config/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/undiscriminated-unions/no-custom-config/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/undiscriminated-unions/no-custom-config/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/undiscriminated-unions/no-custom-config/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/undiscriminated-unions/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/undiscriminated-unions/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/undiscriminated-unions/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/undiscriminated-unions/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/undiscriminated-unions/skip-response-validation/src/core/runtime/runtime.ts b/seed/ts-sdk/undiscriminated-unions/skip-response-validation/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/undiscriminated-unions/skip-response-validation/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/undiscriminated-unions/skip-response-validation/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/undiscriminated-unions/skip-response-validation/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/undiscriminated-unions/skip-response-validation/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/undiscriminated-unions/skip-response-validation/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/undiscriminated-unions/skip-response-validation/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/undiscriminated-unions/skip-response-validation/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/undiscriminated-unions/skip-response-validation/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/undiscriminated-unions/skip-response-validation/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/undiscriminated-unions/skip-response-validation/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/unions/src/core/runtime/runtime.ts b/seed/ts-sdk/unions/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/unions/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/unions/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/unions/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/unions/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/unions/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/unions/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/unions/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/unions/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/unions/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/unions/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/unknown/no-custom-config/src/core/runtime/runtime.ts b/seed/ts-sdk/unknown/no-custom-config/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/unknown/no-custom-config/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/unknown/no-custom-config/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/unknown/no-custom-config/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/unknown/no-custom-config/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/unknown/no-custom-config/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/unknown/no-custom-config/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/unknown/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/unknown/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/unknown/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/unknown/no-custom-config/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/unknown/unknown-as-any/src/core/runtime/runtime.ts b/seed/ts-sdk/unknown/unknown-as-any/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/unknown/unknown-as-any/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/unknown/unknown-as-any/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/unknown/unknown-as-any/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/unknown/unknown-as-any/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/unknown/unknown-as-any/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/unknown/unknown-as-any/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/unknown/unknown-as-any/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/unknown/unknown-as-any/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/unknown/unknown-as-any/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/unknown/unknown-as-any/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/validation/src/core/runtime/runtime.ts b/seed/ts-sdk/validation/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/validation/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/validation/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/validation/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/validation/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/validation/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/validation/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/validation/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/validation/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/validation/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/validation/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/variables/src/core/runtime/runtime.ts b/seed/ts-sdk/variables/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/variables/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/variables/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/variables/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/variables/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/variables/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/variables/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/variables/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/variables/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/variables/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/variables/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/version-no-default/src/core/runtime/runtime.ts b/seed/ts-sdk/version-no-default/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/version-no-default/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/version-no-default/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/version-no-default/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/version-no-default/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/version-no-default/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/version-no-default/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/version-no-default/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/version-no-default/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/version-no-default/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/version-no-default/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => { diff --git a/seed/ts-sdk/version/src/core/runtime/runtime.ts b/seed/ts-sdk/version/src/core/runtime/runtime.ts index 4d0687e8eb4..a975017514b 100644 --- a/seed/ts-sdk/version/src/core/runtime/runtime.ts +++ b/seed/ts-sdk/version/src/core/runtime/runtime.ts @@ -8,58 +8,9 @@ interface BunGlobal { version: string; } -declare const Deno: DenoGlobal; -declare const Bun: BunGlobal; - -/** - * A constant that indicates whether the environment the code is running is a Web Browser. - */ -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is a Web Worker. - */ -const isWebWorker = - typeof self === "object" && - // @ts-ignore - typeof self?.importScripts === "function" && - (self.constructor?.name === "DedicatedWorkerGlobalScope" || - self.constructor?.name === "ServiceWorkerGlobalScope" || - self.constructor?.name === "SharedWorkerGlobalScope"); - -/** - * A constant that indicates whether the environment the code is running is Deno. - */ -const isDeno = - typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Bun.sh. - */ -const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; - -/** - * A constant that indicates whether the environment the code is running is Node.JS. - */ -const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; - -/** - * A constant that indicates whether the environment the code is running is in React-Native. - * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js - */ -const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; - -/** - * A constant that indicates whether the environment the code is running is Cloudflare. - * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent - */ -const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; /** * A constant that indicates which environment and version the SDK is running in. @@ -67,12 +18,16 @@ const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator? export const RUNTIME: Runtime = evaluateRuntime(); export interface Runtime { - type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd"; + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; version?: string; parsedVersion?: number; } function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; if (isBrowser) { return { type: "browser", @@ -80,18 +35,50 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; if (isCloudflare) { return { type: "workerd", }; } + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + // @ts-ignore + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); if (isWebWorker) { return { type: "web-worker", }; } + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; if (isDeno) { return { type: "deno", @@ -99,6 +86,10 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; if (isBun) { return { type: "bun", @@ -106,6 +97,15 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is Node.JS. + */ + const isNode = + typeof process !== "undefined" && + "version" in process && + !!process.version && + "versions" in process && + !!process.versions?.node; if (isNode) { return { type: "node", @@ -114,6 +114,11 @@ function evaluateRuntime(): Runtime { }; } + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; if (isReactNative) { return { type: "react-native", diff --git a/seed/ts-sdk/version/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/version/tests/unit/fetcher/makeRequest.test.ts index 845cf082865..43ed9d11bc4 100644 --- a/seed/ts-sdk/version/tests/unit/fetcher/makeRequest.test.ts +++ b/seed/ts-sdk/version/tests/unit/fetcher/makeRequest.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { makeRequest } from "../../../src/core/fetcher/makeRequest"; describe("Test makeRequest", () => { diff --git a/seed/ts-sdk/version/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/version/tests/unit/fetcher/requestWithRetries.test.ts index 2e542231d41..73f311152d9 100644 --- a/seed/ts-sdk/version/tests/unit/fetcher/requestWithRetries.test.ts +++ b/seed/ts-sdk/version/tests/unit/fetcher/requestWithRetries.test.ts @@ -1,4 +1,3 @@ -import { RUNTIME } from "../../../src/core/runtime"; import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; describe("requestWithRetries", () => {