UNPKG

js-tts-wrapper

Version:

A JavaScript/TypeScript library that provides a unified API for working with multiple cloud-based Text-to-Speech (TTS) services

50 lines (49 loc) 1.55 kB
"use strict"; /** * Utility functions for handling fetch in different environments */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getFetch = getFetch; exports.isFetchAvailable = isFetchAvailable; /** * Get a fetch implementation that works in both Node.js and browser environments * * This function tries to use: * 1. The global fetch if available (browsers and Node.js >= 18) * 2. node-fetch if installed (for older Node.js versions) * 3. Falls back to a mock implementation that throws an error * * @returns A fetch function that works in the current environment */ function getFetch() { // Check if global fetch is available (browsers and Node.js >= 18) if (typeof globalThis.fetch === "function") { return globalThis.fetch; } // Try to use node-fetch if available try { // eslint-disable-next-line @typescript-eslint/no-var-requires const nodeFetch = require("node-fetch"); return nodeFetch; } catch (_error) { // Return a mock implementation that throws an error return async () => { throw new Error("Fetch API is not available. Please install node-fetch package or use a newer version of Node.js."); }; } } /** * Check if fetch is available in the current environment * * @returns True if fetch is available, false otherwise */ function isFetchAvailable() { try { const fetch = getFetch(); return typeof fetch === "function"; } catch (_error) { return false; } }