UNPKG

teddi-x

Version:

Teddi (teddi-x) is a Node package that extends security to vertical agents., applications, and tooling built on, for, or with AI.

90 lines (82 loc) 2.97 kB
/** * Retrieves the Teddi hash from environment variables across different environments. * Handles Vite, Next.js, Create React App, and standard Node.js environments. * * @returns {string|undefined} The Teddi hash or undefined if not found */ export const getHash = () => { // Check for browser environment const isBrowser = typeof window !== "undefined"; // For client-side environments, try to access environment variables injected into the browser if (isBrowser) { // Vite client-side if (typeof import.meta !== "undefined" && import.meta.env) { const viteHash = import.meta.env.VITE_TEDDI_HASH || import.meta.env.VITE_TEDDI_API_KEY; if (viteHash) return viteHash; } // Next.js and Create React App client-side if (typeof window.process !== "undefined" && window.process.env) { return ( window.process.env.NEXT_PUBLIC_TEDDI_HASH || window.process.env.NEXT_PUBLIC_TEDDI_API_KEY || window.process.env.REACT_APP_TEDDI_HASH || window.process.env.REACT_APP_TEDDI_API_KEY ); } // Fallback to window variables that might be injected return window.__TEDDI_HASH__ || window.TEDDI_HASH; } // For server-side environments if (typeof process !== "undefined" && process.env) { return ( process.env.TEDDI_HASH || process.env.TEDDI_API_KEY || process.env.NEXT_PUBLIC_TEDDI_HASH || process.env.NEXT_PUBLIC_TEDDI_API_KEY || process.env.VITE_TEDDI_HASH || process.env.VITE_TEDDI_API_KEY || process.env.REACT_APP_TEDDI_HASH || process.env.REACT_APP_TEDDI_API_KEY ); } // Handle environments where process might be defined but process.env is not try { // Try with globalThis if ( typeof globalThis !== "undefined" && globalThis.process && globalThis.process.env ) { return ( globalThis.process.env.TEDDI_HASH || globalThis.process.env.TEDDI_API_KEY ); } } catch (e) { // Silently fail if globalThis is not available } // Return undefined if no hash was found return undefined; }; /** * Validates if a hash is present, throwing an error if not found * * @param {string|undefined} hash - The hash to validate * @returns {string} The validated hash * @throws {Error} If hash is not found */ export const validateHash = (hash) => { if (!hash) { throw new Error( "Teddi API key not found in environment variables. " + "Please set TEDDI_HASH, NEXT_PUBLIC_TEDDI_HASH, " + "VITE_TEDDI_HASH, or REACT_APP_TEDDI_HASH in your environment." ); } return hash; }; export default getHash;