@envkit/nextjs
Version:
Environment variable management for Next.js applications
121 lines • 6 kB
JavaScript
;
'use client';
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnvKitProvider = EnvKitProvider;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importStar(require("react"));
const navigation_1 = require("next/navigation");
const api_1 = require("../api");
const DefaultFallbackUI_1 = require("./DefaultFallbackUI");
/**
* EnvKitProvider component that wraps the application and handles missing environment variables
* Compatible with Next.js and App Router
*/
function EnvKitProvider({ children, requiredVars = [], fallbackPath = '/env-setup', isProduction = process.env.NODE_ENV === 'production', customFallbackUI: CustomFallbackUI, logoUrl, title, description, onMissingVars, maskAllEnvs = false, disableAddNew = false, }) {
const router = (0, navigation_1.useRouter)();
const pathname = (0, navigation_1.usePathname)();
const [isLoading, setIsLoading] = (0, react_1.useState)(true);
const [missingVars, setMissingVars] = (0, react_1.useState)([]);
const [isReady, setIsReady] = (0, react_1.useState)(false);
// Use the fallback UI if provided, otherwise use the default
const FallbackUI = CustomFallbackUI || DefaultFallbackUI_1.DefaultFallbackUI;
// Use a ref to track if we've already checked for missing variables
// This prevents multiple API calls in development mode due to React's StrictMode
const isFirstRender = react_1.default.useRef(true);
(0, react_1.useEffect)(() => {
const checkEnvironmentVariables = async () => {
try {
// Check environment variables on client side with cache control
const result = await api_1.envKitApi.checkStatus();
if (!result.success) {
// If there are missing vars, update state
const missingEnvVars = result.missingVars.map(key => ({
key,
value: '',
description: `Required environment variable: ${key}`
}));
setMissingVars(missingEnvVars);
// Call onMissingVars callback if provided
if (onMissingVars) {
onMissingVars(result.missingVars);
}
// If we're not already on the fallback path and we're not in production
// Redirect to the fallback path
if (pathname !== fallbackPath && !isProduction) {
router.push(fallbackPath);
}
}
else {
// If all variables are set, mark as ready
setIsReady(true);
}
}
catch (error) {
console.error('Error checking environment variables:', error);
// If there's an error, we still want to mark as ready to avoid blocking the app
setIsReady(true);
}
finally {
setIsLoading(false);
}
};
// Only run on first render or when pathname changes
if (isFirstRender.current || pathname === fallbackPath) {
isFirstRender.current = false;
checkEnvironmentVariables();
}
}, [pathname, fallbackPath, isProduction, router, onMissingVars]);
// Handle completion of environment variable setup
const handleComplete = () => {
// Mark as ready and redirect to the home page
setIsReady(true);
if (pathname === fallbackPath) {
router.push('/');
}
};
// If we're loading or on the fallback path with missing vars, show the fallback UI
if (!isReady && (isLoading || (pathname === fallbackPath && missingVars.length > 0))) {
if (CustomFallbackUI) {
return ((0, jsx_runtime_1.jsx)(CustomFallbackUI, { missingVars: missingVars, isLoading: isLoading, onSubmit: handleComplete, logoUrl: logoUrl, title: title, description: description, maskAllEnvs: maskAllEnvs, disableAddNew: disableAddNew }));
}
// Otherwise use the default UI
return ((0, jsx_runtime_1.jsx)(DefaultFallbackUI_1.DefaultFallbackUI, { missingVars: missingVars, onSubmit: handleComplete, logoUrl: logoUrl, title: title, description: description, isLoading: isLoading, maskAllEnvs: maskAllEnvs, disableAddNew: disableAddNew }));
}
// Otherwise, render the children
return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children });
}
exports.default = EnvKitProvider;
//# sourceMappingURL=EnvKitProvider.js.map