UNPKG

@envkit/nextjs

Version:

Environment variable management for Next.js applications

99 lines 3.36 kB
"use strict"; 'use client'; Object.defineProperty(exports, "__esModule", { value: true }); exports.envKitApi = exports.EnvKitApi = void 0; /** * EnvKit API client for managing environment variables * This is safe to use in client components */ class EnvKitApi { constructor(options = {}) { /** * Check the status of environment variables * @returns Promise with the status of environment variables */ // Cache for checkStatus results to prevent multiple API calls this.statusCache = null; this.lastCheckTime = 0; this.CACHE_TTL = 5000; // 5 seconds cache TTL this.baseUrl = options.baseUrl || '/api/envkit'; this.headers = { 'Content-Type': 'application/json', ...options.headers }; } async checkStatus() { // Return cached result if it's still valid const now = Date.now(); if (this.statusCache && (now - this.lastCheckTime < this.CACHE_TTL)) { return this.statusCache; } try { const response = await fetch(`${this.baseUrl}`, { method: 'GET', headers: this.headers, cache: 'no-store' }); if (!response.ok) { const error = await response.text(); const result = { success: false, missingVars: [], error: `API request failed: ${error}` }; this.statusCache = result; this.lastCheckTime = now; return result; } const result = await response.json(); this.statusCache = result; this.lastCheckTime = now; return result; } catch (error) { console.error('Error checking environment variables:', error); const result = { success: false, missingVars: [], error: error instanceof Error ? error.message : String(error) }; this.statusCache = result; this.lastCheckTime = now; return result; } } /** * Update environment variables * @param variables The environment variables to update * @returns Promise with the result of the update */ async updateVariables(variables) { try { const response = await fetch(`${this.baseUrl}`, { method: 'POST', headers: this.headers, body: JSON.stringify(variables), cache: 'no-store' }); if (!response.ok) { const error = await response.text(); return { success: false, error: `API request failed: ${error}` }; } return await response.json(); } catch (error) { console.error('Error updating environment variables:', error); return { success: false, error: error instanceof Error ? error.message : String(error) }; } } } exports.EnvKitApi = EnvKitApi; // Create a default instance for easy import exports.envKitApi = new EnvKitApi(); //# sourceMappingURL=index.js.map