Files
ipal-kit/dist/modules/env/assertEnv.d.ts
T

50 lines
2.2 KiB
TypeScript

/**
* Zero-dependency environment validation. The plugin deliberately does NOT pull
* in zod — forcing a validation library on every project would be a dependency
* the project didn't ask for (against the A0 "no imposed deps" rule). This is a
* tiny validator that does the one thing we need: check the vars a deploy needs
* and FAIL FAST with a readable, all-at-once message instead of a cryptic
* runtime crash (e.g. `getaddrinfo ENOTFOUND` halfway through a Docker build).
*/
export type EnvKind = 'boolean' | 'email' | 'number' | 'port' | 'string' | 'url';
export type EnvRule = {
/** Shown in the error to tell the deployer what the var is for. */
description?: string;
/** Shape check applied when a value is present. Default 'string' (non-empty). */
kind?: EnvKind;
/** Variable name, e.g. 'DATABASE_URI'. */
name: string;
/** Allowed values (e.g. NODE_ENV). Checked when a value is present. */
oneOf?: readonly string[];
/** Default true. When false, the var is only validated if present. */
required?: boolean;
/** Custom check: return true if OK, or an error message string. */
validate?: (value: string) => string | true;
};
export type AssertEnvOptions = {
/** Source of values. Default process.env. */
env?: Record<string, string | undefined>;
/** Error prefix. Default '[ipal]'. */
label?: string;
};
/**
* Validates the given rules against the environment and THROWS a single,
* aggregated error listing every problem at once (not just the first). Call it
* once at boot — in payload.config.ts, or Next's instrumentation.ts register().
*
* import { assertEnv, ipalEnvRules } from '@intecion/ipal-kit'
* assertEnv(ipalEnvRules({ storage: true, turnstile: true }))
*
* or with hand-written rules:
*
* assertEnv([
* { name: 'DATABASE_URI', description: 'MongoDB connection string' },
* { name: 'NEXT_PUBLIC_SERVER_URL', kind: 'url' },
* { name: 'NODE_ENV', oneOf: ['development', 'production', 'test'] },
* ])
*
* Runs server-side only (reads process.env) — never import it into a client
* component.
*/
export declare function assertEnv(rules: EnvRule[], options?: AssertEnvOptions): void;