UNPKG

4.14 kBTypeScriptView Raw
1export interface Spec<T> {
2 /**
3 * An Array that lists the admissable parsed values for the env var.
4 */
5 choices?: ReadonlyArray<T>;
6 /**
7 * A string that describes the env var.
8 */
9 desc?: string;
10 /**
11 * An example value for the env var.
12 */
13 example?: string;
14 /**
15 * A url that leads to more detailed documentation about the env var.
16 */
17 docs?: string;
18 /**
19 * A fallback value, which will be used if the env var wasn't specified. Providing a default effectively makes the env var optional.
20 */
21 default?: NonNullable<T> | undefined;
22 /**
23 * A fallback value to use only when NODE_ENV is not 'production'.
24 * This is handy for env vars that are required for production environments, but optional for development and testing.
25 */
26 devDefault?: NonNullable<T> | undefined;
27}
28type OptionalAttrs<T> = {
29 default: undefined;
30} | {
31 devDefault: undefined;
32} | {
33 default: undefined;
34 devDefault: undefined;
35} | {
36 default: NonNullable<T>;
37 devDefault: undefined;
38} | {
39 default: undefined;
40 devDefault: NonNullable<T>;
41};
42type RequiredAttrs<T> = {
43 default: NonNullable<T>;
44} | {
45 devDefault: NonNullable<T>;
46} | {
47 devDefault: NonNullable<T>;
48 default: NonNullable<T>;
49} | {};
50type DefaultKeys = 'default' | 'devDefault';
51type OptionalSpec<T> = Spec<T> & OptionalAttrs<T>;
52type OptionalTypelessSpec = Omit<OptionalSpec<unknown>, 'choices'>;
53type RequiredSpec<T> = Spec<T> & RequiredAttrs<T>;
54type RequiredTypelessSpec = Omit<RequiredSpec<unknown>, 'choices' | DefaultKeys>;
55type ChoicelessOptionalSpec<T> = Omit<Spec<T>, 'choices' | DefaultKeys> & OptionalAttrs<T>;
56type ChoicelessRequiredSpec<T> = Omit<Spec<T>, 'choices' | DefaultKeys> & RequiredAttrs<T>;
57type WithParser<T> = {
58 _parse: (input: string) => T;
59};
60export type RequiredValidatorSpec<T> = RequiredSpec<T> & WithParser<T>;
61export type OptionalValidatorSpec<T> = OptionalSpec<T> & WithParser<T>;
62export type ValidatorSpec<T> = RequiredValidatorSpec<T> | OptionalValidatorSpec<T>;
63export interface ExactValidator<T> {
64 (spec: OptionalSpec<T>): OptionalValidatorSpec<T>;
65 (spec?: RequiredSpec<T>): RequiredValidatorSpec<T>;
66}
67export interface BaseValidator<BaseT> {
68 <T extends BaseT>(spec: OptionalSpec<T>): OptionalValidatorSpec<T>;
69 (spec: ChoicelessRequiredSpec<BaseT>): RequiredValidatorSpec<BaseT>;
70 <T extends BaseT>(spec?: RequiredSpec<T>): RequiredValidatorSpec<T>;
71}
72export interface StructuredValidator {
73 (): RequiredValidatorSpec<any>;
74 <T>(): RequiredValidatorSpec<T>;
75 (spec: RequiredTypelessSpec): RequiredValidatorSpec<any>;
76 (spec: OptionalTypelessSpec): OptionalValidatorSpec<any>;
77 <T>(spec: ChoicelessOptionalSpec<T>): OptionalValidatorSpec<T>;
78 <T>(spec: ChoicelessRequiredSpec<T>): RequiredValidatorSpec<T>;
79}
80export type SpecsOutput<S> = {
81 [K in keyof S]: unknown;
82};
83export type CleanedEnv<S> = S extends Record<string, ValidatorSpec<unknown>> ? Readonly<{
84 [K in keyof S]: S[K] extends OptionalValidatorSpec<infer U> ? U | undefined : S[K] extends RequiredValidatorSpec<infer U> ? U : never;
85} & CleanedEnvAccessors> : never;
86export interface CleanedEnvAccessors {
87 /** true if NODE_ENV === 'development' */
88 readonly isDevelopment: boolean;
89 readonly isDev: boolean;
90 /** true if NODE_ENV === 'test' */
91 readonly isTest: boolean;
92 /** true if NODE_ENV === 'production' */
93 readonly isProduction: boolean;
94 readonly isProd: boolean;
95}
96export interface ReporterOptions<T> {
97 errors: Partial<Record<keyof T, Error>>;
98 env: unknown;
99}
100export interface CleanOptions<T> {
101 /**
102 * Pass in a function to override the default error handling and console output.
103 * See ./reporter.ts for the default implementation.
104 */
105 reporter?: ((opts: ReporterOptions<T>) => void) | null;
106}
107export interface StrictProxyMiddlewareOptions {
108 /**
109 * A list of extra inspectable properties to add to the middleware.
110 *
111 * This is useful if you want to add support for framework-specific values.
112 */
113 extraInspectables?: string[];
114}
115export {};
116
\No newline at end of file