@geekmidas/envkit 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{EnvironmentParser-DlWHnhDY.cjs → EnvironmentParser-DJdW7vOL.cjs} +2 -2
- package/dist/{EnvironmentParser-DlWHnhDY.cjs.map → EnvironmentParser-DJdW7vOL.cjs.map} +1 -1
- package/dist/{EnvironmentParser-CBLsPUyQ.mjs → EnvironmentParser-zMblItla.mjs} +2 -2
- package/dist/{EnvironmentParser-CBLsPUyQ.mjs.map → EnvironmentParser-zMblItla.mjs.map} +1 -1
- package/dist/EnvironmentParser.cjs +2 -2
- package/dist/EnvironmentParser.mjs +2 -2
- package/dist/SnifferEnvironmentParser.cjs +58 -2
- package/dist/SnifferEnvironmentParser.cjs.map +1 -1
- package/dist/SnifferEnvironmentParser.d.cts +54 -1
- package/dist/SnifferEnvironmentParser.d.cts.map +1 -1
- package/dist/SnifferEnvironmentParser.d.mts +54 -1
- package/dist/SnifferEnvironmentParser.d.mts.map +1 -1
- package/dist/SnifferEnvironmentParser.mjs +58 -3
- package/dist/SnifferEnvironmentParser.mjs.map +1 -1
- package/dist/{formatter-fz8V7x6i.mjs → formatter-BRRrxQi3.mjs} +2 -4
- package/dist/formatter-BRRrxQi3.mjs.map +1 -0
- package/dist/formatter-Cox0NGxT.d.mts.map +1 -1
- package/dist/formatter-D85aIkpd.d.cts.map +1 -1
- package/dist/{formatter-w8Tsccw4.cjs → formatter-HxePpSy2.cjs} +2 -4
- package/dist/formatter-HxePpSy2.cjs.map +1 -0
- package/dist/formatter.cjs +1 -1
- package/dist/formatter.mjs +1 -1
- package/dist/index.cjs +2 -2
- package/dist/index.mjs +2 -2
- package/package.json +1 -1
- package/src/SnifferEnvironmentParser.ts +97 -0
- package/src/__tests__/SnifferEnvironmentParser.spec.ts +105 -1
- package/src/__tests__/formatter.spec.ts +17 -0
- package/src/formatter.ts +4 -5
- package/dist/formatter-fz8V7x6i.mjs.map +0 -1
- package/dist/formatter-w8Tsccw4.cjs.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
-
const require_formatter = require('./formatter-
|
|
2
|
+
const require_formatter = require('./formatter-HxePpSy2.cjs');
|
|
3
3
|
const lodash_get = require_chunk.__toESM(require("lodash.get"));
|
|
4
4
|
const lodash_set = require_chunk.__toESM(require("lodash.set"));
|
|
5
5
|
const zod_v4 = require_chunk.__toESM(require("zod/v4"));
|
|
@@ -223,4 +223,4 @@ Object.defineProperty(exports, 'EnvironmentParser', {
|
|
|
223
223
|
return EnvironmentParser;
|
|
224
224
|
}
|
|
225
225
|
});
|
|
226
|
-
//# sourceMappingURL=EnvironmentParser-
|
|
226
|
+
//# sourceMappingURL=EnvironmentParser-DJdW7vOL.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EnvironmentParser-DlWHnhDY.cjs","names":["config: TResponse","envVars: Set<string>","errors: z.core.$ZodIssue[]","config: T","path: string[]","result: EmptyObject","z","schema: z.ZodType","name: string","issue: z.core.$ZodIssue","builder: (get: EnvFetcher) => TReturn"],"sources":["../src/EnvironmentParser.ts"],"sourcesContent":["import get from 'lodash.get';\nimport set from 'lodash.set';\nimport { z } from 'zod/v4';\nimport { formatParseError, isDevelopment } from './formatter.js';\n\n/**\n * Parses and validates configuration objects against Zod schemas.\n * Handles nested configurations and aggregates validation errors.\n *\n * @template TResponse - The shape of the configuration object\n */\nexport class ConfigParser<TResponse extends EmptyObject> {\n\t/**\n\t * Creates a new ConfigParser instance.\n\t *\n\t * @param config - The configuration object to parse\n\t * @param envVars - Set of environment variable names that were accessed\n\t */\n\tconstructor(\n\t\tprivate readonly config: TResponse,\n\t\tprivate readonly envVars: Set<string> = new Set(),\n\t) {}\n\t/**\n\t * Parses the config object and validates it against the Zod schemas\n\t * @returns The parsed config object\n\t */\n\tparse(): InferConfig<TResponse> {\n\t\tconst errors: z.core.$ZodIssue[] = [];\n\n\t\tconst parseDeep = <T>(config: T, path: string[] = []) => {\n\t\t\tconst result: EmptyObject = {};\n\n\t\t\tif (config && typeof config !== 'object') {\n\t\t\t\treturn config;\n\t\t\t}\n\n\t\t\tfor (const key in config) {\n\t\t\t\tconst schema = config[key];\n\t\t\t\tconst currentPath = [...path, key];\n\n\t\t\t\tif (schema instanceof z.ZodType) {\n\t\t\t\t\tconst parsed = schema.safeParse(undefined);\n\t\t\t\t\tif (parsed.success) {\n\t\t\t\t\t\tset(result, key, parsed.data);\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// If the schema is invalid, assign the error\n\t\t\t\t\t\terrors.push(\n\t\t\t\t\t\t\t...parsed.error.issues.map((issue) => ({\n\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\tpath: [...currentPath, ...(issue.path as string[])],\n\t\t\t\t\t\t\t})),\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t} else if (schema) {\n\t\t\t\t\tset(result, key, parseDeep(schema as EmptyObject, currentPath));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn result;\n\t\t};\n\n\t\tconst parsedConfig = parseDeep(\n\t\t\tthis.config,\n\t\t) as unknown as InferConfig<TResponse>;\n\n\t\tif (errors.length > 0) {\n\t\t\tconst zodError = new z.ZodError(errors);\n\t\t\t// In development, log a formatted error message before throwing\n\t\t\tif (isDevelopment()) {\n\t\t\t\tconsole.error(formatParseError(zodError));\n\t\t\t}\n\t\t\tthrow zodError;\n\t\t}\n\n\t\treturn parsedConfig;\n\t}\n\n\t/**\n\t * Returns an array of environment variable names that were accessed during config creation.\n\t * This is useful for deployment and configuration management to know which env vars are required.\n\t *\n\t * @returns Array of environment variable names, sorted alphabetically\n\t *\n\t * @example\n\t * ```typescript\n\t * const config = envParser.create((get) => ({\n\t * dbUrl: get('DATABASE_URL').string(),\n\t * port: get('PORT').number()\n\t * }));\n\t *\n\t * config.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']\n\t * ```\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.envVars).sort();\n\t}\n}\n\n/**\n * Parses environment variables with type-safe validation using Zod schemas.\n * Provides a fluent API for defining environment variable schemas with automatic\n * error context enrichment.\n *\n * @template T - The type of the configuration object (typically process.env)\n *\n * @example\n * ```typescript\n * const config = new EnvironmentParser(process.env)\n * .create((get) => ({\n * port: get('PORT').string().transform(Number).default(3000),\n * database: {\n * url: get('DATABASE_URL').string().url()\n * }\n * }))\n * .parse();\n * ```\n */\nexport class EnvironmentParser<T extends EmptyObject> {\n\t/**\n\t * Set to track which environment variable names have been accessed\n\t */\n\tprivate readonly accessedVars: Set<string> = new Set();\n\n\t/**\n\t * Creates a new EnvironmentParser instance.\n\t *\n\t * @param config - The configuration object to parse (typically process.env)\n\t */\n\tconstructor(private readonly config: T) {}\n\n\t/**\n\t * Wraps a Zod schema to intercept parse/safeParse calls and enrich error messages\n\t * with environment variable context.\n\t *\n\t * @param schema - The Zod schema to wrap\n\t * @param name - The environment variable name for error context\n\t * @returns A wrapped Zod schema with enhanced error reporting\n\t */\n\tprivate wrapSchema = (schema: z.ZodType, name: string): z.ZodType => {\n\t\t// Create a proxy that intercepts all method calls on the schema\n\t\treturn new Proxy(schema, {\n\t\t\tget: (target, prop) => {\n\t\t\t\tif (prop === 'parse') {\n\t\t\t\t\treturn () => {\n\t\t\t\t\t\tconst value = get(this.config, name);\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\treturn target.parse(value);\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\tif (error instanceof z.ZodError) {\n\t\t\t\t\t\t\t\t// Modify the error to include the environment variable name\n\t\t\t\t\t\t\t\tconst modifiedIssues = error.issues.map((issue) => ({\n\t\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\t\tmessage: `Environment variable \"${name}\": ${issue.message}`,\n\t\t\t\t\t\t\t\t\tpath: [name, ...issue.path],\n\t\t\t\t\t\t\t\t}));\n\t\t\t\t\t\t\t\tthrow new z.ZodError(modifiedIssues);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tthrow error;\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (prop === 'safeParse') {\n\t\t\t\t\treturn () => {\n\t\t\t\t\t\tconst value = get(this.config, name);\n\t\t\t\t\t\tconst result = target.safeParse(value);\n\n\t\t\t\t\t\tif (!result.success) {\n\t\t\t\t\t\t\t// Modify the error to include the environment variable name\n\t\t\t\t\t\t\tconst modifiedIssues = result.error.issues.map(\n\t\t\t\t\t\t\t\t(issue: z.core.$ZodIssue) => ({\n\t\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\t\tmessage: `Environment variable \"${name}\": ${issue.message}`,\n\t\t\t\t\t\t\t\t\tpath: [name, ...issue.path],\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tsuccess: false as const,\n\t\t\t\t\t\t\t\terror: new z.ZodError(modifiedIssues),\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\t// For any method that returns a new schema (like transform, optional, etc.),\n\t\t\t\t// wrap the result as well\n\t\t\t\tconst originalProp = target[prop as keyof typeof target];\n\t\t\t\tif (typeof originalProp === 'function') {\n\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\tconst result = originalProp.apply(target, args);\n\t\t\t\t\t\t// If the result is a ZodType, wrap it too\n\t\t\t\t\t\tif (result && typeof result === 'object' && 'parse' in result) {\n\t\t\t\t\t\t\treturn this.wrapSchema(result, name);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\treturn originalProp;\n\t\t\t},\n\t\t});\n\t};\n\n\t/**\n\t * Creates a proxied version of the Zod object that wraps all schema creators\n\t * to provide enhanced error messages with environment variable context.\n\t *\n\t * @param name - The environment variable name\n\t * @returns A proxied Zod object with wrapped schema creators\n\t */\n\tprivate getZodGetter = (name: string) => {\n\t\t// Track that this environment variable was accessed\n\t\tthis.accessedVars.add(name);\n\n\t\t// Return an object that has all Zod schemas but with our wrapper\n\t\treturn new Proxy(\n\t\t\t{ ...z },\n\t\t\t{\n\t\t\t\tget: (target, prop) => {\n\t\t\t\t\t// deno-lint-ignore ban-ts-comment\n\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\tconst value = target[prop];\n\n\t\t\t\t\tif (typeof value === 'function') {\n\t\t\t\t\t\t// Return a wrapper around each Zod schema creator\n\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\tconst schema = value(...args);\n\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\t// Handle objects like z.coerce\n\t\t\t\t\tif (value && typeof value === 'object') {\n\t\t\t\t\t\treturn new Proxy(value, {\n\t\t\t\t\t\t\tget: (nestedTarget, nestedProp) => {\n\t\t\t\t\t\t\t\tconst nestedValue =\n\t\t\t\t\t\t\t\t\tnestedTarget[nestedProp as keyof typeof nestedTarget];\n\t\t\t\t\t\t\t\tif (typeof nestedValue === 'function') {\n\t\t\t\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\t\t\t\tconst schema = nestedValue(...args);\n\t\t\t\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn nestedValue;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\treturn value;\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t};\n\n\t/**\n\t * Creates a new ConfigParser object that can be used to parse the config object\n\t *\n\t * @param builder - A function that takes a getter function and returns a config object\n\t * @returns A ConfigParser object that can be used to parse the config object\n\t */\n\tcreate<TReturn extends EmptyObject>(\n\t\tbuilder: (get: EnvFetcher) => TReturn,\n\t): ConfigParser<TReturn> {\n\t\tconst config = builder(this.getZodGetter);\n\t\treturn new ConfigParser(config, this.accessedVars);\n\t}\n\n\t/**\n\t * Returns an array of environment variable names that were accessed via the getter.\n\t * This is useful for build-time analysis to determine which env vars a service needs.\n\t *\n\t * @returns Array of environment variable names, sorted alphabetically\n\t *\n\t * @example\n\t * ```typescript\n\t * const sniffer = new EnvironmentParser({});\n\t * service.register(sniffer);\n\t * const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']\n\t * ```\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.accessedVars).sort();\n\t}\n}\n\n/**\n * Infers the TypeScript type of a configuration object based on its Zod schemas.\n * Recursively processes nested objects and extracts types from Zod schemas.\n *\n * @template T - The configuration object type\n */\nexport type InferConfig<T extends EmptyObject> = {\n\t[K in keyof T]: T[K] extends z.ZodSchema\n\t\t? z.infer<T[K]>\n\t\t: T[K] extends Record<string, unknown>\n\t\t\t? InferConfig<T[K]>\n\t\t\t: T[K];\n};\n\n/**\n * Function type for fetching environment variables with Zod validation.\n * Returns a Zod object scoped to a specific environment variable.\n *\n * @template TPath - The environment variable path type\n * @param name - The environment variable name\n * @returns A Zod object for defining the schema\n */\nexport type EnvFetcher<TPath extends string = string> = (\n\tname: TPath,\n) => typeof z;\n\n/**\n * Function type for building environment configuration objects.\n * Takes an EnvFetcher and returns a configuration object with Zod schemas.\n *\n * @template TResponse - The response configuration object type\n * @param get - The environment variable fetcher function\n * @returns The configuration object with Zod schemas\n */\nexport type EnvironmentBuilder<TResponse extends EmptyObject> = (\n\tget: EnvFetcher,\n) => TResponse;\n\n/**\n * Type alias for a generic object with unknown values.\n * Used as a constraint for configuration objects.\n */\nexport type EmptyObject = Record<string | number | symbol, unknown>;\n"],"mappings":";;;;;;;;;;;;;AAWA,IA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,GAAG,SAAgB;IAC1B,MAAM,SAAS,MAAM,GAAG,KAAK;AAC7B,WAAO,KAAK,WAAW,QAAQ,KAAK;GACpC;AAIF,OAAI,gBAAgB,UAAU,SAC7B,QAAO,IAAI,MAAM,OAAO,EACvB,KAAK,CAAC,cAAc,eAAe;IAClC,MAAM,cACL,aAAa;AACd,eAAW,gBAAgB,WAC1B,QAAO,CAAC,GAAG,SAAgB;KAC1B,MAAM,SAAS,YAAY,GAAG,KAAK;AACnC,YAAO,KAAK,WAAW,QAAQ,KAAK;IACpC;AAEF,WAAO;GACP,EACD;AAGF,UAAO;EACP,EACD;CAEF;;;;;;;CAQD,OACCI,SACwB;EACxB,MAAM,SAAS,QAAQ,KAAK,aAAa;AACzC,SAAO,IAAI,aAAa,QAAQ,KAAK;CACrC;;;;;;;;;;;;;;CAeD,0BAAoC;AACnC,SAAO,MAAM,KAAK,KAAK,aAAa,CAAC,MAAM;CAC3C;AACD"}
|
|
1
|
+
{"version":3,"file":"EnvironmentParser-DJdW7vOL.cjs","names":["config: TResponse","envVars: Set<string>","errors: z.core.$ZodIssue[]","config: T","path: string[]","result: EmptyObject","z","schema: z.ZodType","name: string","issue: z.core.$ZodIssue","builder: (get: EnvFetcher) => TReturn"],"sources":["../src/EnvironmentParser.ts"],"sourcesContent":["import get from 'lodash.get';\nimport set from 'lodash.set';\nimport { z } from 'zod/v4';\nimport { formatParseError, isDevelopment } from './formatter.js';\n\n/**\n * Parses and validates configuration objects against Zod schemas.\n * Handles nested configurations and aggregates validation errors.\n *\n * @template TResponse - The shape of the configuration object\n */\nexport class ConfigParser<TResponse extends EmptyObject> {\n\t/**\n\t * Creates a new ConfigParser instance.\n\t *\n\t * @param config - The configuration object to parse\n\t * @param envVars - Set of environment variable names that were accessed\n\t */\n\tconstructor(\n\t\tprivate readonly config: TResponse,\n\t\tprivate readonly envVars: Set<string> = new Set(),\n\t) {}\n\t/**\n\t * Parses the config object and validates it against the Zod schemas\n\t * @returns The parsed config object\n\t */\n\tparse(): InferConfig<TResponse> {\n\t\tconst errors: z.core.$ZodIssue[] = [];\n\n\t\tconst parseDeep = <T>(config: T, path: string[] = []) => {\n\t\t\tconst result: EmptyObject = {};\n\n\t\t\tif (config && typeof config !== 'object') {\n\t\t\t\treturn config;\n\t\t\t}\n\n\t\t\tfor (const key in config) {\n\t\t\t\tconst schema = config[key];\n\t\t\t\tconst currentPath = [...path, key];\n\n\t\t\t\tif (schema instanceof z.ZodType) {\n\t\t\t\t\tconst parsed = schema.safeParse(undefined);\n\t\t\t\t\tif (parsed.success) {\n\t\t\t\t\t\tset(result, key, parsed.data);\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// If the schema is invalid, assign the error\n\t\t\t\t\t\terrors.push(\n\t\t\t\t\t\t\t...parsed.error.issues.map((issue) => ({\n\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\tpath: [...currentPath, ...(issue.path as string[])],\n\t\t\t\t\t\t\t})),\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t} else if (schema) {\n\t\t\t\t\tset(result, key, parseDeep(schema as EmptyObject, currentPath));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn result;\n\t\t};\n\n\t\tconst parsedConfig = parseDeep(\n\t\t\tthis.config,\n\t\t) as unknown as InferConfig<TResponse>;\n\n\t\tif (errors.length > 0) {\n\t\t\tconst zodError = new z.ZodError(errors);\n\t\t\t// In development, log a formatted error message before throwing\n\t\t\tif (isDevelopment()) {\n\t\t\t\tconsole.error(formatParseError(zodError));\n\t\t\t}\n\t\t\tthrow zodError;\n\t\t}\n\n\t\treturn parsedConfig;\n\t}\n\n\t/**\n\t * Returns an array of environment variable names that were accessed during config creation.\n\t * This is useful for deployment and configuration management to know which env vars are required.\n\t *\n\t * @returns Array of environment variable names, sorted alphabetically\n\t *\n\t * @example\n\t * ```typescript\n\t * const config = envParser.create((get) => ({\n\t * dbUrl: get('DATABASE_URL').string(),\n\t * port: get('PORT').number()\n\t * }));\n\t *\n\t * config.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']\n\t * ```\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.envVars).sort();\n\t}\n}\n\n/**\n * Parses environment variables with type-safe validation using Zod schemas.\n * Provides a fluent API for defining environment variable schemas with automatic\n * error context enrichment.\n *\n * @template T - The type of the configuration object (typically process.env)\n *\n * @example\n * ```typescript\n * const config = new EnvironmentParser(process.env)\n * .create((get) => ({\n * port: get('PORT').string().transform(Number).default(3000),\n * database: {\n * url: get('DATABASE_URL').string().url()\n * }\n * }))\n * .parse();\n * ```\n */\nexport class EnvironmentParser<T extends EmptyObject> {\n\t/**\n\t * Set to track which environment variable names have been accessed\n\t */\n\tprivate readonly accessedVars: Set<string> = new Set();\n\n\t/**\n\t * Creates a new EnvironmentParser instance.\n\t *\n\t * @param config - The configuration object to parse (typically process.env)\n\t */\n\tconstructor(private readonly config: T) {}\n\n\t/**\n\t * Wraps a Zod schema to intercept parse/safeParse calls and enrich error messages\n\t * with environment variable context.\n\t *\n\t * @param schema - The Zod schema to wrap\n\t * @param name - The environment variable name for error context\n\t * @returns A wrapped Zod schema with enhanced error reporting\n\t */\n\tprivate wrapSchema = (schema: z.ZodType, name: string): z.ZodType => {\n\t\t// Create a proxy that intercepts all method calls on the schema\n\t\treturn new Proxy(schema, {\n\t\t\tget: (target, prop) => {\n\t\t\t\tif (prop === 'parse') {\n\t\t\t\t\treturn () => {\n\t\t\t\t\t\tconst value = get(this.config, name);\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\treturn target.parse(value);\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\tif (error instanceof z.ZodError) {\n\t\t\t\t\t\t\t\t// Modify the error to include the environment variable name\n\t\t\t\t\t\t\t\tconst modifiedIssues = error.issues.map((issue) => ({\n\t\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\t\tmessage: `Environment variable \"${name}\": ${issue.message}`,\n\t\t\t\t\t\t\t\t\tpath: [name, ...issue.path],\n\t\t\t\t\t\t\t\t}));\n\t\t\t\t\t\t\t\tthrow new z.ZodError(modifiedIssues);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tthrow error;\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (prop === 'safeParse') {\n\t\t\t\t\treturn () => {\n\t\t\t\t\t\tconst value = get(this.config, name);\n\t\t\t\t\t\tconst result = target.safeParse(value);\n\n\t\t\t\t\t\tif (!result.success) {\n\t\t\t\t\t\t\t// Modify the error to include the environment variable name\n\t\t\t\t\t\t\tconst modifiedIssues = result.error.issues.map(\n\t\t\t\t\t\t\t\t(issue: z.core.$ZodIssue) => ({\n\t\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\t\tmessage: `Environment variable \"${name}\": ${issue.message}`,\n\t\t\t\t\t\t\t\t\tpath: [name, ...issue.path],\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tsuccess: false as const,\n\t\t\t\t\t\t\t\terror: new z.ZodError(modifiedIssues),\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\t// For any method that returns a new schema (like transform, optional, etc.),\n\t\t\t\t// wrap the result as well\n\t\t\t\tconst originalProp = target[prop as keyof typeof target];\n\t\t\t\tif (typeof originalProp === 'function') {\n\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\tconst result = originalProp.apply(target, args);\n\t\t\t\t\t\t// If the result is a ZodType, wrap it too\n\t\t\t\t\t\tif (result && typeof result === 'object' && 'parse' in result) {\n\t\t\t\t\t\t\treturn this.wrapSchema(result, name);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\treturn originalProp;\n\t\t\t},\n\t\t});\n\t};\n\n\t/**\n\t * Creates a proxied version of the Zod object that wraps all schema creators\n\t * to provide enhanced error messages with environment variable context.\n\t *\n\t * @param name - The environment variable name\n\t * @returns A proxied Zod object with wrapped schema creators\n\t */\n\tprivate getZodGetter = (name: string) => {\n\t\t// Track that this environment variable was accessed\n\t\tthis.accessedVars.add(name);\n\n\t\t// Return an object that has all Zod schemas but with our wrapper\n\t\treturn new Proxy(\n\t\t\t{ ...z },\n\t\t\t{\n\t\t\t\tget: (target, prop) => {\n\t\t\t\t\t// deno-lint-ignore ban-ts-comment\n\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\tconst value = target[prop];\n\n\t\t\t\t\tif (typeof value === 'function') {\n\t\t\t\t\t\t// Return a wrapper around each Zod schema creator\n\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\tconst schema = value(...args);\n\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\t// Handle objects like z.coerce\n\t\t\t\t\tif (value && typeof value === 'object') {\n\t\t\t\t\t\treturn new Proxy(value, {\n\t\t\t\t\t\t\tget: (nestedTarget, nestedProp) => {\n\t\t\t\t\t\t\t\tconst nestedValue =\n\t\t\t\t\t\t\t\t\tnestedTarget[nestedProp as keyof typeof nestedTarget];\n\t\t\t\t\t\t\t\tif (typeof nestedValue === 'function') {\n\t\t\t\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\t\t\t\tconst schema = nestedValue(...args);\n\t\t\t\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn nestedValue;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\treturn value;\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t};\n\n\t/**\n\t * Creates a new ConfigParser object that can be used to parse the config object\n\t *\n\t * @param builder - A function that takes a getter function and returns a config object\n\t * @returns A ConfigParser object that can be used to parse the config object\n\t */\n\tcreate<TReturn extends EmptyObject>(\n\t\tbuilder: (get: EnvFetcher) => TReturn,\n\t): ConfigParser<TReturn> {\n\t\tconst config = builder(this.getZodGetter);\n\t\treturn new ConfigParser(config, this.accessedVars);\n\t}\n\n\t/**\n\t * Returns an array of environment variable names that were accessed via the getter.\n\t * This is useful for build-time analysis to determine which env vars a service needs.\n\t *\n\t * @returns Array of environment variable names, sorted alphabetically\n\t *\n\t * @example\n\t * ```typescript\n\t * const sniffer = new EnvironmentParser({});\n\t * service.register(sniffer);\n\t * const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']\n\t * ```\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.accessedVars).sort();\n\t}\n}\n\n/**\n * Infers the TypeScript type of a configuration object based on its Zod schemas.\n * Recursively processes nested objects and extracts types from Zod schemas.\n *\n * @template T - The configuration object type\n */\nexport type InferConfig<T extends EmptyObject> = {\n\t[K in keyof T]: T[K] extends z.ZodSchema\n\t\t? z.infer<T[K]>\n\t\t: T[K] extends Record<string, unknown>\n\t\t\t? InferConfig<T[K]>\n\t\t\t: T[K];\n};\n\n/**\n * Function type for fetching environment variables with Zod validation.\n * Returns a Zod object scoped to a specific environment variable.\n *\n * @template TPath - The environment variable path type\n * @param name - The environment variable name\n * @returns A Zod object for defining the schema\n */\nexport type EnvFetcher<TPath extends string = string> = (\n\tname: TPath,\n) => typeof z;\n\n/**\n * Function type for building environment configuration objects.\n * Takes an EnvFetcher and returns a configuration object with Zod schemas.\n *\n * @template TResponse - The response configuration object type\n * @param get - The environment variable fetcher function\n * @returns The configuration object with Zod schemas\n */\nexport type EnvironmentBuilder<TResponse extends EmptyObject> = (\n\tget: EnvFetcher,\n) => TResponse;\n\n/**\n * Type alias for a generic object with unknown values.\n * Used as a constraint for configuration objects.\n */\nexport type EmptyObject = Record<string | number | symbol, unknown>;\n"],"m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AW,UAAU,WAEpB,QAAO,CAAC,GAAG,SAAgB;IAC1B,MAAM,SAAS,MAAM,GAAG,KAAK;AAC7B,WAAO,KAAK,WAAW,QAAQ,KAAK;GACpC;AAIF,OAAI,gBAAgB,UAAU,SAC7B,QAAO,IAAI,MAAM,OAAO,EACvB,KAAK,CAAC,cAAc,eAAe;IAClC,MAAM,cACL,aAAa;AACd,eAAW,gBAAgB,WAC1B,QAAO,CAAC,GAAG,SAAgB;KAC1B,MAAM,SAAS,YAAY,GAAG,KAAK;AACnC,YAAO,KAAK,WAAW,QAAQ,KAAK;IACpC;AAEF,WAAO;GACP,EACD;AAGF,UAAO;EACP,EACD;CAEF;;;;;;;CAQD,OACCI,SACwB;EACxB,MAAM,SAAS,QAAQ,KAAK,aAAa;AACzC,SAAO,IAAI,aAAa,QAAQ,KAAK;CACrC;;;;;;;;;;;;;;CAeD,0BAAoC;AACnC,SAAO,MAAM,KAAK,KAAK,aAAa,CAAC,MAAM;CAC3C;AACD"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { formatParseError, isDevelopment } from "./formatter-
|
|
1
|
+
import { formatParseError, isDevelopment } from "./formatter-BRRrxQi3.mjs";
|
|
2
2
|
import get from "lodash.get";
|
|
3
3
|
import set from "lodash.set";
|
|
4
4
|
import { z } from "zod/v4";
|
|
@@ -211,4 +211,4 @@ var EnvironmentParser = class {
|
|
|
211
211
|
|
|
212
212
|
//#endregion
|
|
213
213
|
export { ConfigParser, EnvironmentParser };
|
|
214
|
-
//# sourceMappingURL=EnvironmentParser-
|
|
214
|
+
//# sourceMappingURL=EnvironmentParser-zMblItla.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EnvironmentParser-CBLsPUyQ.mjs","names":["config: TResponse","envVars: Set<string>","errors: z.core.$ZodIssue[]","config: T","path: string[]","result: EmptyObject","schema: z.ZodType","name: string","issue: z.core.$ZodIssue","builder: (get: EnvFetcher) => TReturn"],"sources":["../src/EnvironmentParser.ts"],"sourcesContent":["import get from 'lodash.get';\nimport set from 'lodash.set';\nimport { z } from 'zod/v4';\nimport { formatParseError, isDevelopment } from './formatter.js';\n\n/**\n * Parses and validates configuration objects against Zod schemas.\n * Handles nested configurations and aggregates validation errors.\n *\n * @template TResponse - The shape of the configuration object\n */\nexport class ConfigParser<TResponse extends EmptyObject> {\n\t/**\n\t * Creates a new ConfigParser instance.\n\t *\n\t * @param config - The configuration object to parse\n\t * @param envVars - Set of environment variable names that were accessed\n\t */\n\tconstructor(\n\t\tprivate readonly config: TResponse,\n\t\tprivate readonly envVars: Set<string> = new Set(),\n\t) {}\n\t/**\n\t * Parses the config object and validates it against the Zod schemas\n\t * @returns The parsed config object\n\t */\n\tparse(): InferConfig<TResponse> {\n\t\tconst errors: z.core.$ZodIssue[] = [];\n\n\t\tconst parseDeep = <T>(config: T, path: string[] = []) => {\n\t\t\tconst result: EmptyObject = {};\n\n\t\t\tif (config && typeof config !== 'object') {\n\t\t\t\treturn config;\n\t\t\t}\n\n\t\t\tfor (const key in config) {\n\t\t\t\tconst schema = config[key];\n\t\t\t\tconst currentPath = [...path, key];\n\n\t\t\t\tif (schema instanceof z.ZodType) {\n\t\t\t\t\tconst parsed = schema.safeParse(undefined);\n\t\t\t\t\tif (parsed.success) {\n\t\t\t\t\t\tset(result, key, parsed.data);\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// If the schema is invalid, assign the error\n\t\t\t\t\t\terrors.push(\n\t\t\t\t\t\t\t...parsed.error.issues.map((issue) => ({\n\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\tpath: [...currentPath, ...(issue.path as string[])],\n\t\t\t\t\t\t\t})),\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t} else if (schema) {\n\t\t\t\t\tset(result, key, parseDeep(schema as EmptyObject, currentPath));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn result;\n\t\t};\n\n\t\tconst parsedConfig = parseDeep(\n\t\t\tthis.config,\n\t\t) as unknown as InferConfig<TResponse>;\n\n\t\tif (errors.length > 0) {\n\t\t\tconst zodError = new z.ZodError(errors);\n\t\t\t// In development, log a formatted error message before throwing\n\t\t\tif (isDevelopment()) {\n\t\t\t\tconsole.error(formatParseError(zodError));\n\t\t\t}\n\t\t\tthrow zodError;\n\t\t}\n\n\t\treturn parsedConfig;\n\t}\n\n\t/**\n\t * Returns an array of environment variable names that were accessed during config creation.\n\t * This is useful for deployment and configuration management to know which env vars are required.\n\t *\n\t * @returns Array of environment variable names, sorted alphabetically\n\t *\n\t * @example\n\t * ```typescript\n\t * const config = envParser.create((get) => ({\n\t * dbUrl: get('DATABASE_URL').string(),\n\t * port: get('PORT').number()\n\t * }));\n\t *\n\t * config.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']\n\t * ```\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.envVars).sort();\n\t}\n}\n\n/**\n * Parses environment variables with type-safe validation using Zod schemas.\n * Provides a fluent API for defining environment variable schemas with automatic\n * error context enrichment.\n *\n * @template T - The type of the configuration object (typically process.env)\n *\n * @example\n * ```typescript\n * const config = new EnvironmentParser(process.env)\n * .create((get) => ({\n * port: get('PORT').string().transform(Number).default(3000),\n * database: {\n * url: get('DATABASE_URL').string().url()\n * }\n * }))\n * .parse();\n * ```\n */\nexport class EnvironmentParser<T extends EmptyObject> {\n\t/**\n\t * Set to track which environment variable names have been accessed\n\t */\n\tprivate readonly accessedVars: Set<string> = new Set();\n\n\t/**\n\t * Creates a new EnvironmentParser instance.\n\t *\n\t * @param config - The configuration object to parse (typically process.env)\n\t */\n\tconstructor(private readonly config: T) {}\n\n\t/**\n\t * Wraps a Zod schema to intercept parse/safeParse calls and enrich error messages\n\t * with environment variable context.\n\t *\n\t * @param schema - The Zod schema to wrap\n\t * @param name - The environment variable name for error context\n\t * @returns A wrapped Zod schema with enhanced error reporting\n\t */\n\tprivate wrapSchema = (schema: z.ZodType, name: string): z.ZodType => {\n\t\t// Create a proxy that intercepts all method calls on the schema\n\t\treturn new Proxy(schema, {\n\t\t\tget: (target, prop) => {\n\t\t\t\tif (prop === 'parse') {\n\t\t\t\t\treturn () => {\n\t\t\t\t\t\tconst value = get(this.config, name);\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\treturn target.parse(value);\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\tif (error instanceof z.ZodError) {\n\t\t\t\t\t\t\t\t// Modify the error to include the environment variable name\n\t\t\t\t\t\t\t\tconst modifiedIssues = error.issues.map((issue) => ({\n\t\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\t\tmessage: `Environment variable \"${name}\": ${issue.message}`,\n\t\t\t\t\t\t\t\t\tpath: [name, ...issue.path],\n\t\t\t\t\t\t\t\t}));\n\t\t\t\t\t\t\t\tthrow new z.ZodError(modifiedIssues);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tthrow error;\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (prop === 'safeParse') {\n\t\t\t\t\treturn () => {\n\t\t\t\t\t\tconst value = get(this.config, name);\n\t\t\t\t\t\tconst result = target.safeParse(value);\n\n\t\t\t\t\t\tif (!result.success) {\n\t\t\t\t\t\t\t// Modify the error to include the environment variable name\n\t\t\t\t\t\t\tconst modifiedIssues = result.error.issues.map(\n\t\t\t\t\t\t\t\t(issue: z.core.$ZodIssue) => ({\n\t\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\t\tmessage: `Environment variable \"${name}\": ${issue.message}`,\n\t\t\t\t\t\t\t\t\tpath: [name, ...issue.path],\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tsuccess: false as const,\n\t\t\t\t\t\t\t\terror: new z.ZodError(modifiedIssues),\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\t// For any method that returns a new schema (like transform, optional, etc.),\n\t\t\t\t// wrap the result as well\n\t\t\t\tconst originalProp = target[prop as keyof typeof target];\n\t\t\t\tif (typeof originalProp === 'function') {\n\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\tconst result = originalProp.apply(target, args);\n\t\t\t\t\t\t// If the result is a ZodType, wrap it too\n\t\t\t\t\t\tif (result && typeof result === 'object' && 'parse' in result) {\n\t\t\t\t\t\t\treturn this.wrapSchema(result, name);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\treturn originalProp;\n\t\t\t},\n\t\t});\n\t};\n\n\t/**\n\t * Creates a proxied version of the Zod object that wraps all schema creators\n\t * to provide enhanced error messages with environment variable context.\n\t *\n\t * @param name - The environment variable name\n\t * @returns A proxied Zod object with wrapped schema creators\n\t */\n\tprivate getZodGetter = (name: string) => {\n\t\t// Track that this environment variable was accessed\n\t\tthis.accessedVars.add(name);\n\n\t\t// Return an object that has all Zod schemas but with our wrapper\n\t\treturn new Proxy(\n\t\t\t{ ...z },\n\t\t\t{\n\t\t\t\tget: (target, prop) => {\n\t\t\t\t\t// deno-lint-ignore ban-ts-comment\n\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\tconst value = target[prop];\n\n\t\t\t\t\tif (typeof value === 'function') {\n\t\t\t\t\t\t// Return a wrapper around each Zod schema creator\n\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\tconst schema = value(...args);\n\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\t// Handle objects like z.coerce\n\t\t\t\t\tif (value && typeof value === 'object') {\n\t\t\t\t\t\treturn new Proxy(value, {\n\t\t\t\t\t\t\tget: (nestedTarget, nestedProp) => {\n\t\t\t\t\t\t\t\tconst nestedValue =\n\t\t\t\t\t\t\t\t\tnestedTarget[nestedProp as keyof typeof nestedTarget];\n\t\t\t\t\t\t\t\tif (typeof nestedValue === 'function') {\n\t\t\t\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\t\t\t\tconst schema = nestedValue(...args);\n\t\t\t\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn nestedValue;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\treturn value;\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t};\n\n\t/**\n\t * Creates a new ConfigParser object that can be used to parse the config object\n\t *\n\t * @param builder - A function that takes a getter function and returns a config object\n\t * @returns A ConfigParser object that can be used to parse the config object\n\t */\n\tcreate<TReturn extends EmptyObject>(\n\t\tbuilder: (get: EnvFetcher) => TReturn,\n\t): ConfigParser<TReturn> {\n\t\tconst config = builder(this.getZodGetter);\n\t\treturn new ConfigParser(config, this.accessedVars);\n\t}\n\n\t/**\n\t * Returns an array of environment variable names that were accessed via the getter.\n\t * This is useful for build-time analysis to determine which env vars a service needs.\n\t *\n\t * @returns Array of environment variable names, sorted alphabetically\n\t *\n\t * @example\n\t * ```typescript\n\t * const sniffer = new EnvironmentParser({});\n\t * service.register(sniffer);\n\t * const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']\n\t * ```\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.accessedVars).sort();\n\t}\n}\n\n/**\n * Infers the TypeScript type of a configuration object based on its Zod schemas.\n * Recursively processes nested objects and extracts types from Zod schemas.\n *\n * @template T - The configuration object type\n */\nexport type InferConfig<T extends EmptyObject> = {\n\t[K in keyof T]: T[K] extends z.ZodSchema\n\t\t? z.infer<T[K]>\n\t\t: T[K] extends Record<string, unknown>\n\t\t\t? InferConfig<T[K]>\n\t\t\t: T[K];\n};\n\n/**\n * Function type for fetching environment variables with Zod validation.\n * Returns a Zod object scoped to a specific environment variable.\n *\n * @template TPath - The environment variable path type\n * @param name - The environment variable name\n * @returns A Zod object for defining the schema\n */\nexport type EnvFetcher<TPath extends string = string> = (\n\tname: TPath,\n) => typeof z;\n\n/**\n * Function type for building environment configuration objects.\n * Takes an EnvFetcher and returns a configuration object with Zod schemas.\n *\n * @template TResponse - The response configuration object type\n * @param get - The environment variable fetcher function\n * @returns The configuration object with Zod schemas\n */\nexport type EnvironmentBuilder<TResponse extends EmptyObject> = (\n\tget: EnvFetcher,\n) => TResponse;\n\n/**\n * Type alias for a generic object with unknown values.\n * Used as a constraint for configuration objects.\n */\nexport type EmptyObject = Record<string | number | symbol, unknown>;\n"],"mappings":";;;;;;;;;;;;AAWA,IAAa,eAAb,MAAyD;;;;;;;CAOxD,YACkBA,QACAC,0BAAuB,IAAI,OAC3C;EAFgB;EACA;CACd;;;;;CAKJ,QAAgC;EAC/B,MAAMC,SAA6B,CAAE;EAErC,MAAM,YAAY,CAAIC,QAAWC,OAAiB,CAAE,MAAK;GACxD,MAAMC,SAAsB,CAAE;AAE9B,OAAI,iBAAiB,WAAW,SAC/B,QAAO;AAGR,QAAK,MAAM,OAAO,QAAQ;IACzB,MAAM,SAAS,OAAO;IACtB,MAAM,cAAc,CAAC,GAAG,MAAM,GAAI;AAElC,QAAI,kBAAkB,EAAE,SAAS;KAChC,MAAM,SAAS,OAAO,iBAAoB;AAC1C,SAAI,OAAO,QACV,KAAI,QAAQ,KAAK,OAAO,KAAK;SAG7B,QAAO,KACN,GAAG,OAAO,MAAM,OAAO,IAAI,CAAC,WAAW;MACtC,GAAG;MACH,MAAM,CAAC,GAAG,aAAa,GAAI,MAAM,IAAkB;KACnD,GAAE,CACH;IAEF,WAAU,OACV,KAAI,QAAQ,KAAK,UAAU,QAAuB,YAAY,CAAC;GAEhE;AAED,UAAO;EACP;EAED,MAAM,eAAe,UACpB,KAAK,OACL;AAED,MAAI,OAAO,SAAS,GAAG;GACtB,MAAM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GACP;GAKF,MAAM,eAAe,OAAO;AAC5B,cAAW,iBAAiB,WAC3B,QAAO,CAAC,GAAG,SAAgB;IAC1B,MAAM,SAAS,aAAa,MAAM,QAAQ,KAAK;AAE/C,QAAI,iBAAiB,WAAW,YAAY,WAAW,OACtD,QAAO,KAAK,WAAW,QAAQ,KAAK;AAErC,WAAO;GACP;AAGF,UAAO;EACP,EACD;CACD;;;;;;;;CASD,AAAQ,eAAe,CAACD,SAAiB;AAExC,OAAK,aAAa,IAAI,KAAK;AAG3B,SAAO,IAAI,MACV,EAAE,GAAG,EAAG,GACR,EACC,KAAK,CAAC,QAAQ,SAAS;GAGtB,MAAM,QAAQ,OAAO;AAErB,cAAW,UAAU,WAEpB,QAAO,CAAC,GAAG,SAAgB;IAC1B,MAAM,SAAS,MAAM,GAAG,KAAK;AAC7B,WAAO,KAAK,WAAW,QAAQ,KAAK;GACpC;AAIF,OAAI,gBAAgB,UAAU,SAC7B,QAAO,IAAI,MAAM,OAAO,EACvB,KAAK,CAAC,cAAc,eAAe;IAClC,MAAM,cACL,aAAa;AACd,eAAW,gBAAgB,WAC1B,QAAO,CAAC,GAAG,SAAgB;KAC1B,MAAM,SAAS,YAAY,GAAG,KAAK;AACnC,YAAO,KAAK,WAAW,QAAQ,KAAK;IACpC;AAEF,WAAO;GACP,EACD;AAGF,UAAO;EACP,EACD;CAEF;;;;;;;CAQD,OACCE,SACwB;EACxB,MAAM,SAAS,QAAQ,KAAK,aAAa;AACzC,SAAO,IAAI,aAAa,QAAQ,KAAK;CACrC;;;;;;;;;;;;;;CAeD,0BAAoC;AACnC,SAAO,MAAM,KAAK,KAAK,aAAa,CAAC,MAAM;CAC3C;AACD"}
|
|
1
|
+
{"version":3,"file":"EnvironmentParser-zMblItla.mjs","names":["config: TResponse","envVars: Set<string>","errors: z.core.$ZodIssue[]","config: T","path: string[]","result: EmptyObject","schema: z.ZodType","name: string","issue: z.core.$ZodIssue","builder: (get: EnvFetcher) => TReturn"],"sources":["../src/EnvironmentParser.ts"],"sourcesContent":["import get from 'lodash.get';\nimport set from 'lodash.set';\nimport { z } from 'zod/v4';\nimport { formatParseError, isDevelopment } from './formatter.js';\n\n/**\n * Parses and validates configuration objects against Zod schemas.\n * Handles nested configurations and aggregates validation errors.\n *\n * @template TResponse - The shape of the configuration object\n */\nexport class ConfigParser<TResponse extends EmptyObject> {\n\t/**\n\t * Creates a new ConfigParser instance.\n\t *\n\t * @param config - The configuration object to parse\n\t * @param envVars - Set of environment variable names that were accessed\n\t */\n\tconstructor(\n\t\tprivate readonly config: TResponse,\n\t\tprivate readonly envVars: Set<string> = new Set(),\n\t) {}\n\t/**\n\t * Parses the config object and validates it against the Zod schemas\n\t * @returns The parsed config object\n\t */\n\tparse(): InferConfig<TResponse> {\n\t\tconst errors: z.core.$ZodIssue[] = [];\n\n\t\tconst parseDeep = <T>(config: T, path: string[] = []) => {\n\t\t\tconst result: EmptyObject = {};\n\n\t\t\tif (config && typeof config !== 'object') {\n\t\t\t\treturn config;\n\t\t\t}\n\n\t\t\tfor (const key in config) {\n\t\t\t\tconst schema = config[key];\n\t\t\t\tconst currentPath = [...path, key];\n\n\t\t\t\tif (schema instanceof z.ZodType) {\n\t\t\t\t\tconst parsed = schema.safeParse(undefined);\n\t\t\t\t\tif (parsed.success) {\n\t\t\t\t\t\tset(result, key, parsed.data);\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// If the schema is invalid, assign the error\n\t\t\t\t\t\terrors.push(\n\t\t\t\t\t\t\t...parsed.error.issues.map((issue) => ({\n\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\tpath: [...currentPath, ...(issue.path as string[])],\n\t\t\t\t\t\t\t})),\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t} else if (schema) {\n\t\t\t\t\tset(result, key, parseDeep(schema as EmptyObject, currentPath));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn result;\n\t\t};\n\n\t\tconst parsedConfig = parseDeep(\n\t\t\tthis.config,\n\t\t) as unknown as InferConfig<TResponse>;\n\n\t\tif (errors.length > 0) {\n\t\t\tconst zodError = new z.ZodError(errors);\n\t\t\t// In development, log a formatted error message before throwing\n\t\t\tif (isDevelopment()) {\n\t\t\t\tconsole.error(formatParseError(zodError));\n\t\t\t}\n\t\t\tthrow zodError;\n\t\t}\n\n\t\treturn parsedConfig;\n\t}\n\n\t/**\n\t * Returns an array of environment variable names that were accessed during config creation.\n\t * This is useful for deployment and configuration management to know which env vars are required.\n\t *\n\t * @returns Array of environment variable names, sorted alphabetically\n\t *\n\t * @example\n\t * ```typescript\n\t * const config = envParser.create((get) => ({\n\t * dbUrl: get('DATABASE_URL').string(),\n\t * port: get('PORT').number()\n\t * }));\n\t *\n\t * config.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']\n\t * ```\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.envVars).sort();\n\t}\n}\n\n/**\n * Parses environment variables with type-safe validation using Zod schemas.\n * Provides a fluent API for defining environment variable schemas with automatic\n * error context enrichment.\n *\n * @template T - The type of the configuration object (typically process.env)\n *\n * @example\n * ```typescript\n * const config = new EnvironmentParser(process.env)\n * .create((get) => ({\n * port: get('PORT').string().transform(Number).default(3000),\n * database: {\n * url: get('DATABASE_URL').string().url()\n * }\n * }))\n * .parse();\n * ```\n */\nexport class EnvironmentParser<T extends EmptyObject> {\n\t/**\n\t * Set to track which environment variable names have been accessed\n\t */\n\tprivate readonly accessedVars: Set<string> = new Set();\n\n\t/**\n\t * Creates a new EnvironmentParser instance.\n\t *\n\t * @param config - The configuration object to parse (typically process.env)\n\t */\n\tconstructor(private readonly config: T) {}\n\n\t/**\n\t * Wraps a Zod schema to intercept parse/safeParse calls and enrich error messages\n\t * with environment variable context.\n\t *\n\t * @param schema - The Zod schema to wrap\n\t * @param name - The environment variable name for error context\n\t * @returns A wrapped Zod schema with enhanced error reporting\n\t */\n\tprivate wrapSchema = (schema: z.ZodType, name: string): z.ZodType => {\n\t\t// Create a proxy that intercepts all method calls on the schema\n\t\treturn new Proxy(schema, {\n\t\t\tget: (target, prop) => {\n\t\t\t\tif (prop === 'parse') {\n\t\t\t\t\treturn () => {\n\t\t\t\t\t\tconst value = get(this.config, name);\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\treturn target.parse(value);\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\tif (error instanceof z.ZodError) {\n\t\t\t\t\t\t\t\t// Modify the error to include the environment variable name\n\t\t\t\t\t\t\t\tconst modifiedIssues = error.issues.map((issue) => ({\n\t\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\t\tmessage: `Environment variable \"${name}\": ${issue.message}`,\n\t\t\t\t\t\t\t\t\tpath: [name, ...issue.path],\n\t\t\t\t\t\t\t\t}));\n\t\t\t\t\t\t\t\tthrow new z.ZodError(modifiedIssues);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tthrow error;\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (prop === 'safeParse') {\n\t\t\t\t\treturn () => {\n\t\t\t\t\t\tconst value = get(this.config, name);\n\t\t\t\t\t\tconst result = target.safeParse(value);\n\n\t\t\t\t\t\tif (!result.success) {\n\t\t\t\t\t\t\t// Modify the error to include the environment variable name\n\t\t\t\t\t\t\tconst modifiedIssues = result.error.issues.map(\n\t\t\t\t\t\t\t\t(issue: z.core.$ZodIssue) => ({\n\t\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\t\tmessage: `Environment variable \"${name}\": ${issue.message}`,\n\t\t\t\t\t\t\t\t\tpath: [name, ...issue.path],\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tsuccess: false as const,\n\t\t\t\t\t\t\t\terror: new z.ZodError(modifiedIssues),\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\t// For any method that returns a new schema (like transform, optional, etc.),\n\t\t\t\t// wrap the result as well\n\t\t\t\tconst originalProp = target[prop as keyof typeof target];\n\t\t\t\tif (typeof originalProp === 'function') {\n\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\tconst result = originalProp.apply(target, args);\n\t\t\t\t\t\t// If the result is a ZodType, wrap it too\n\t\t\t\t\t\tif (result && typeof result === 'object' && 'parse' in result) {\n\t\t\t\t\t\t\treturn this.wrapSchema(result, name);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\treturn originalProp;\n\t\t\t},\n\t\t});\n\t};\n\n\t/**\n\t * Creates a proxied version of the Zod object that wraps all schema creators\n\t * to provide enhanced error messages with environment variable context.\n\t *\n\t * @param name - The environment variable name\n\t * @returns A proxied Zod object with wrapped schema creators\n\t */\n\tprivate getZodGetter = (name: string) => {\n\t\t// Track that this environment variable was accessed\n\t\tthis.accessedVars.add(name);\n\n\t\t// Return an object that has all Zod schemas but with our wrapper\n\t\treturn new Proxy(\n\t\t\t{ ...z },\n\t\t\t{\n\t\t\t\tget: (target, prop) => {\n\t\t\t\t\t// deno-lint-ignore ban-ts-comment\n\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\tconst value = target[prop];\n\n\t\t\t\t\tif (typeof value === 'function') {\n\t\t\t\t\t\t// Return a wrapper around each Zod schema creator\n\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\tconst schema = value(...args);\n\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\t// Handle objects like z.coerce\n\t\t\t\t\tif (value && typeof value === 'object') {\n\t\t\t\t\t\treturn new Proxy(value, {\n\t\t\t\t\t\t\tget: (nestedTarget, nestedProp) => {\n\t\t\t\t\t\t\t\tconst nestedValue =\n\t\t\t\t\t\t\t\t\tnestedTarget[nestedProp as keyof typeof nestedTarget];\n\t\t\t\t\t\t\t\tif (typeof nestedValue === 'function') {\n\t\t\t\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\t\t\t\tconst schema = nestedValue(...args);\n\t\t\t\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn nestedValue;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\treturn value;\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t};\n\n\t/**\n\t * Creates a new ConfigParser object that can be used to parse the config object\n\t *\n\t * @param builder - A function that takes a getter function and returns a config object\n\t * @returns A ConfigParser object that can be used to parse the config object\n\t */\n\tcreate<TReturn extends EmptyObject>(\n\t\tbuilder: (get: EnvFetcher) => TReturn,\n\t): ConfigParser<TReturn> {\n\t\tconst config = builder(this.getZodGetter);\n\t\treturn new ConfigParser(config, this.accessedVars);\n\t}\n\n\t/**\n\t * Returns an array of environment variable names that were accessed via the getter.\n\t * This is useful for build-time analysis to determine which env vars a service needs.\n\t *\n\t * @returns Array of environment variable names, sorted alphabetically\n\t *\n\t * @example\n\t * ```typescript\n\t * const sniffer = new EnvironmentParser({});\n\t * service.register(sniffer);\n\t * const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']\n\t * ```\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.accessedVars).sort();\n\t}\n}\n\n/**\n * Infers the TypeScript type of a configuration object based on its Zod schemas.\n * Recursively processes nested objects and extracts types from Zod schemas.\n *\n * @template T - The configuration object type\n */\nexport type InferConfig<T extends EmptyObject> = {\n\t[K in keyof T]: T[K] extends z.ZodSchema\n\t\t? z.infer<T[K]>\n\t\t: T[K] extends Record<string, unknown>\n\t\t\t? InferConfig<T[K]>\n\t\t\t: T[K];\n};\n\n/**\n * Function type for fetching environment variables with Zod validation.\n * Returns a Zod object scoped to a specific environment variable.\n *\n * @template TPath - The environment variable path type\n * @param name - The environment variable name\n * @returns A Zod object for defining the schema\n */\nexport type EnvFetcher<TPath extends string = string> = (\n\tname: TPath,\n) => typeof z;\n\n/**\n * Function type for building environment configuration objects.\n * Takes an EnvFetcher and returns a configuration object with Zod schemas.\n *\n * @template TResponse - The response configuration object type\n * @param get - The environment variable fetcher function\n * @returns The configuration object with Zod schemas\n */\nexport type EnvironmentBuilder<TResponse extends EmptyObject> = (\n\tget: EnvFetcher,\n) => TResponse;\n\n/**\n * Type alias for a generic object with unknown values.\n * Used as a constraint for configuration objects.\n */\nexport type EmptyObject = Record<string | number | symbol, unknown>;\n"],"mappings":";;;;;;;;;;;;AAWA,IAAa,eAAb,MAAyD;;;;;;;CAOxD,YACkBA,QACAC,0BAAuB,IAAI,OAC3C;EAFgB;EACA;CACd;;;;;CAKJ,QAAgC;EAC/B,MAAMC,SAA6B,CAAE;EAErC,MAAM,YAAY,CAAIC,QAAWC,OAAiB,CAAE,MAAK;GACxD,MAAMC,SAAsB,CAAE;AAE9B,OAAI,iBAAiB,WAAW,SAC/B,QAAO;AAGR,QAAK,MAAM,OAAO,QAAQ;IACzB,MAAM,SAAS,OAAO;IACtB,MAAM,cAAc,CAAC,GAAG,MAAM,GAAI;AAElC,QAAI,kBAAkB,EAAE,SAAS;KAChC,MAAM,SAAS,OAAO,iBAAoB;AAC1C,SAAI,OAAO,QACV,KAAI,QAAQ,KAAK,OAAO,KAAK;SAG7B,QAAO,KACN,GAAG,OAAO,MAAM,OAAO,IAAI,CAAC,WAAW;MACtC,GAAG;MACH,MAAM,CAAC,GAAG,aAAa,GAAI,MAAM,IAAkB;KACnD,GAAE,CACH;IAEF,WAAU,OACV,KAAI,QAAQ,KAAK,UAAU,QAAuB,YAAY,CAAC;GAEhE;AAED,UAAO;EACP;EAED,MAAM,eAAe,UACpB,KAAK,OACL;AAED,MAAI,OAAO,SAAS,GAAG;GACt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,WAAO;GACP;GAKF,MAAM,eAAe,OAAO;AAC5B,cAAW,iBAAiB,WAC3B,QAAO,CAAC,GAAG,SAAgB;IAC1B,MAAM,SAAS,aAAa,MAAM,QAAQ,KAAK;AAE/C,QAAI,iBAAiB,WAAW,YAAY,WAAW,OACtD,QAAO,KAAK,WAAW,QAAQ,KAAK;AAErC,WAAO;GACP;AAGF,UAAO;EACP,EACD;CACD;;;;;;;;CASD,AAAQ,eAAe,CAACD,SAAiB;AAExC,OAAK,aAAa,IAAI,KAAK;AAG3B,SAAO,IAAI,MACV,EAAE,GAAG,EAAG,GACR,EACC,KAAK,CAAC,QAAQ,SAAS;GAGtB,MAAM,QAAQ,OAAO;AAErB,cAAW,UAAU,WAEpB,QAAO,CAAC,GAAG,SAAgB;IAC1B,MAAM,SAAS,MAAM,GAAG,KAAK;AAC7B,WAAO,KAAK,WAAW,QAAQ,KAAK;GACpC;AAIF,OAAI,gBAAgB,UAAU,SAC7B,QAAO,IAAI,MAAM,OAAO,EACvB,KAAK,CAAC,cAAc,eAAe;IAClC,MAAM,cACL,aAAa;AACd,eAAW,gBAAgB,WAC1B,QAAO,CAAC,GAAG,SAAgB;KAC1B,MAAM,SAAS,YAAY,GAAG,KAAK;AACnC,YAAO,KAAK,WAAW,QAAQ,KAAK;IACpC;AAEF,WAAO;GACP,EACD;AAGF,UAAO;EACP,EACD;CAEF;;;;;;;CAQD,OACCE,SACwB;EACxB,MAAM,SAAS,QAAQ,KAAK,aAAa;AACzC,SAAO,IAAI,aAAa,QAAQ,KAAK;CACrC;;;;;;;;;;;;;;CAeD,0BAAoC;AACnC,SAAO,MAAM,KAAK,KAAK,aAAa,CAAC,MAAM;CAC3C;AACD"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
require('./formatter-
|
|
2
|
-
const require_EnvironmentParser = require('./EnvironmentParser-
|
|
1
|
+
require('./formatter-HxePpSy2.cjs');
|
|
2
|
+
const require_EnvironmentParser = require('./EnvironmentParser-DJdW7vOL.cjs');
|
|
3
3
|
|
|
4
4
|
exports.ConfigParser = require_EnvironmentParser.ConfigParser;
|
|
5
5
|
exports.EnvironmentParser = require_EnvironmentParser.EnvironmentParser;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import "./formatter-
|
|
2
|
-
import { ConfigParser, EnvironmentParser } from "./EnvironmentParser-
|
|
1
|
+
import "./formatter-BRRrxQi3.mjs";
|
|
2
|
+
import { ConfigParser, EnvironmentParser } from "./EnvironmentParser-zMblItla.mjs";
|
|
3
3
|
|
|
4
4
|
export { ConfigParser, EnvironmentParser };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
-
require('./formatter-
|
|
3
|
-
const require_EnvironmentParser = require('./EnvironmentParser-
|
|
2
|
+
require('./formatter-HxePpSy2.cjs');
|
|
3
|
+
const require_EnvironmentParser = require('./EnvironmentParser-DJdW7vOL.cjs');
|
|
4
4
|
const zod_v4 = require_chunk.__toESM(require("zod/v4"));
|
|
5
5
|
|
|
6
6
|
//#region src/SnifferEnvironmentParser.ts
|
|
@@ -135,7 +135,63 @@ var SnifferConfigParser = class extends require_EnvironmentParser.ConfigParser {
|
|
|
135
135
|
return "";
|
|
136
136
|
}
|
|
137
137
|
};
|
|
138
|
+
/**
|
|
139
|
+
* Executes a sniffing operation with fire-and-forget handling.
|
|
140
|
+
*
|
|
141
|
+
* This function:
|
|
142
|
+
* 1. Captures unhandled promise rejections during the operation
|
|
143
|
+
* 2. Waits for async operations to settle before returning
|
|
144
|
+
* 3. Gracefully handles errors without throwing
|
|
145
|
+
*
|
|
146
|
+
* Use this when sniffing environment variables from code that may:
|
|
147
|
+
* - Throw synchronous errors
|
|
148
|
+
* - Create fire-and-forget promises that reject
|
|
149
|
+
* - Have async initialization that may fail
|
|
150
|
+
*
|
|
151
|
+
* @param sniffer - The SnifferEnvironmentParser instance to use
|
|
152
|
+
* @param operation - The async operation to execute (e.g., service.register)
|
|
153
|
+
* @param options - Optional configuration
|
|
154
|
+
* @returns SniffResult with env vars and any errors encountered
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* const sniffer = new SnifferEnvironmentParser();
|
|
159
|
+
* const result = await sniffWithFireAndForget(sniffer, async () => {
|
|
160
|
+
* await service.register({ envParser: sniffer });
|
|
161
|
+
* });
|
|
162
|
+
* console.log('Env vars:', result.envVars);
|
|
163
|
+
* console.log('Error:', result.error);
|
|
164
|
+
* console.log('Unhandled rejections:', result.unhandledRejections);
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
async function sniffWithFireAndForget(sniffer, operation, options = {}) {
|
|
168
|
+
const { settleTimeMs = 100 } = options;
|
|
169
|
+
const unhandledRejections = [];
|
|
170
|
+
const captureRejection = (reason) => {
|
|
171
|
+
const err = reason instanceof Error ? reason : new Error(String(reason));
|
|
172
|
+
unhandledRejections.push(err);
|
|
173
|
+
};
|
|
174
|
+
process.on("unhandledRejection", captureRejection);
|
|
175
|
+
let error;
|
|
176
|
+
try {
|
|
177
|
+
const result = operation();
|
|
178
|
+
if (result && typeof result === "object" && "then" in result) await Promise.resolve(result).catch((e) => {
|
|
179
|
+
error = e instanceof Error ? e : new Error(String(e));
|
|
180
|
+
});
|
|
181
|
+
} catch (e) {
|
|
182
|
+
error = e instanceof Error ? e : new Error(String(e));
|
|
183
|
+
} finally {
|
|
184
|
+
await new Promise((resolve) => setTimeout(resolve, settleTimeMs));
|
|
185
|
+
process.off("unhandledRejection", captureRejection);
|
|
186
|
+
}
|
|
187
|
+
return {
|
|
188
|
+
envVars: sniffer.getEnvironmentVariables(),
|
|
189
|
+
error,
|
|
190
|
+
unhandledRejections
|
|
191
|
+
};
|
|
192
|
+
}
|
|
138
193
|
|
|
139
194
|
//#endregion
|
|
140
195
|
exports.SnifferEnvironmentParser = SnifferEnvironmentParser;
|
|
196
|
+
exports.sniffWithFireAndForget = sniffWithFireAndForget;
|
|
141
197
|
//# sourceMappingURL=SnifferEnvironmentParser.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnifferEnvironmentParser.cjs","names":["schema: z.ZodType","name: string","z","result: Record<string, unknown>","builder: (get: EnvFetcher) => TReturn","ConfigParser","config: T","result: EmptyObject"],"sources":["../src/SnifferEnvironmentParser.ts"],"sourcesContent":["import { z } from 'zod/v4';\nimport {\n\tConfigParser,\n\ttype EmptyObject,\n\ttype EnvFetcher,\n} from './EnvironmentParser';\n\n/**\n * A specialized EnvironmentParser for build-time analysis that tracks\n * which environment variables are accessed without requiring actual values.\n *\n * Unlike the regular EnvironmentParser, the sniffer:\n * - Always returns mock values from .parse() and .safeParse()\n * - Never throws validation errors\n * - Tracks all accessed environment variable names\n *\n * This allows service registration to succeed during build-time analysis\n * even when environment variables are not set.\n *\n * @example\n * ```typescript\n * const sniffer = new SnifferEnvironmentParser();\n * await service.register(sniffer); // Always succeeds\n * const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'API_KEY']\n * ```\n */\nexport class SnifferEnvironmentParser<_T extends EmptyObject = EmptyObject> {\n\tprivate readonly accessedVars: Set<string> = new Set();\n\n\t/**\n\t * Wraps a Zod schema to always return mock values.\n\t * This ensures .parse() and .safeParse() never fail.\n\t */\n\tprivate wrapSchema = (schema: z.ZodType, name: string): z.ZodType => {\n\t\treturn new Proxy(schema, {\n\t\t\tget: (target, prop) => {\n\t\t\t\tif (prop === 'parse') {\n\t\t\t\t\treturn () => this.getMockValue(target);\n\t\t\t\t}\n\n\t\t\t\tif (prop === 'safeParse') {\n\t\t\t\t\treturn () => ({\n\t\t\t\t\t\tsuccess: true as const,\n\t\t\t\t\t\tdata: this.getMockValue(target),\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst originalProp = target[prop as keyof typeof target];\n\t\t\t\tif (typeof originalProp === 'function') {\n\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\tconst result = originalProp.apply(target, args);\n\t\t\t\t\t\tif (result && typeof result === 'object' && 'parse' in result) {\n\t\t\t\t\t\t\treturn this.wrapSchema(result, name);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\treturn originalProp;\n\t\t\t},\n\t\t});\n\t};\n\n\t/**\n\t * Returns a mock value based on the Zod schema type.\n\t */\n\tprivate getMockValue(schema: z.ZodType): unknown {\n\t\t// Return type-appropriate mock values\n\t\tif (schema instanceof z.ZodString) return '';\n\t\tif (schema instanceof z.ZodNumber) return 0;\n\t\tif (schema instanceof z.ZodBoolean) return false;\n\t\tif (schema instanceof z.ZodArray) return [];\n\t\tif (schema instanceof z.ZodOptional) return undefined;\n\t\tif (schema instanceof z.ZodNullable) return null;\n\n\t\t// For object schemas, build mock object from shape\n\t\tif (schema instanceof z.ZodObject && schema.shape) {\n\t\t\tconst result: Record<string, unknown> = {};\n\t\t\tfor (const [key, value] of Object.entries(schema.shape)) {\n\t\t\t\tif (value instanceof z.ZodType) {\n\t\t\t\t\tresult[key] = this.getMockValue(value);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\n\t\treturn '';\n\t}\n\n\t/**\n\t * Creates a proxied Zod getter that tracks environment variable access.\n\t */\n\tprivate getZodGetter = (name: string) => {\n\t\tthis.accessedVars.add(name);\n\n\t\treturn new Proxy(\n\t\t\t{ ...z },\n\t\t\t{\n\t\t\t\tget: (target, prop) => {\n\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\tconst value = target[prop];\n\n\t\t\t\t\tif (typeof value === 'function') {\n\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\tconst schema = value(...args);\n\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\tif (value && typeof value === 'object') {\n\t\t\t\t\t\treturn new Proxy(value, {\n\t\t\t\t\t\t\tget: (nestedTarget, nestedProp) => {\n\t\t\t\t\t\t\t\tconst nestedValue =\n\t\t\t\t\t\t\t\t\tnestedTarget[nestedProp as keyof typeof nestedTarget];\n\t\t\t\t\t\t\t\tif (typeof nestedValue === 'function') {\n\t\t\t\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\t\t\t\tconst schema = nestedValue(...args);\n\t\t\t\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn nestedValue;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\treturn value;\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t};\n\n\t/**\n\t * Creates a ConfigParser that will return mock values when parsed.\n\t */\n\tcreate<TReturn extends EmptyObject>(\n\t\tbuilder: (get: EnvFetcher) => TReturn,\n\t): ConfigParser<TReturn> {\n\t\tconst config = builder(this.getZodGetter);\n\t\treturn new SnifferConfigParser(config, this.accessedVars);\n\t}\n\n\t/**\n\t * Returns all environment variable names that were accessed.\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.accessedVars).sort();\n\t}\n}\n\n/**\n * A ConfigParser that always succeeds with mock values.\n */\nclass SnifferConfigParser<\n\tTResponse extends EmptyObject,\n> extends ConfigParser<TResponse> {\n\toverride parse(): any {\n\t\treturn this.parseWithMocks(this.getConfig());\n\t}\n\n\tprivate getConfig(): TResponse {\n\t\t// Access the private config via any cast\n\t\treturn (this as any).config;\n\t}\n\n\tprivate parseWithMocks<T>(config: T): any {\n\t\tconst result: EmptyObject = {};\n\n\t\tif (config && typeof config !== 'object') {\n\t\t\treturn config;\n\t\t}\n\n\t\tfor (const key in config) {\n\t\t\tconst schema = config[key];\n\n\t\t\tif (schema instanceof z.ZodType) {\n\t\t\t\t// Use safeParse which will return mock values from our wrapped schema\n\t\t\t\tconst parsed = schema.safeParse(undefined);\n\t\t\t\tresult[key] = parsed.success\n\t\t\t\t\t? parsed.data\n\t\t\t\t\t: this.getDefaultForSchema(schema);\n\t\t\t} else if (schema && typeof schema === 'object') {\n\t\t\t\tresult[key] = this.parseWithMocks(schema as EmptyObject);\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\tprivate getDefaultForSchema(schema: z.ZodType): unknown {\n\t\tif (schema instanceof z.ZodString) return '';\n\t\tif (schema instanceof z.ZodNumber) return 0;\n\t\tif (schema instanceof z.ZodBoolean) return false;\n\t\tif (schema instanceof z.ZodArray) return [];\n\t\tif (schema instanceof z.ZodOptional) return undefined;\n\t\tif (schema instanceof z.ZodNullable) return null;\n\n\t\tif (schema instanceof z.ZodObject && schema.shape) {\n\t\t\tconst result: Record<string, unknown> = {};\n\t\t\tfor (const [key, value] of Object.entries(schema.shape)) {\n\t\t\t\tif (value instanceof z.ZodType) {\n\t\t\t\t\tresult[key] = this.getDefaultForSchema(value);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\n\t\treturn '';\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,IAAa,2BAAb,MAA4E;CAC3E,AAAiB,+BAA4B,IAAI;;;;;CAMjD,AAAQ,aAAa,CAACA,QAAmBC,SAA4B;AACpE,SAAO,IAAI,MAAM,QAAQ,EACxB,KAAK,CAAC,QAAQ,SAAS;AACtB,OAAI,SAAS,QACZ,QAAO,MAAM,KAAK,aAAa,OAAO;AAGvC,OAAI,SAAS,YACZ,QAAO,OAAO;IACb,SAAS;IACT,MAAM,KAAK,aAAa,OAAO;GAC/B;GAGF,MAAM,eAAe,OAAO;AAC5B,cAAW,iBAAiB,WAC3B,QAAO,CAAC,GAAG,SAAgB;IAC1B,MAAM,SAAS,aAAa,MAAM,QAAQ,KAAK;AAC/C,QAAI,iBAAiB,WAAW,YAAY,WAAW,OACtD,QAAO,KAAK,WAAW,QAAQ,KAAK;AAErC,WAAO;GACP;AAGF,UAAO;EACP,EACD;CACD;;;;CAKD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AAO,MAAM,KAAK,KAAK,aAAa,CAAC,MAAM;CAC3C;AACD;;;;AAKD,IAAM,sBAAN,cAEUC,uCAAwB;CACjC,AAAS,QAAa;AACrB,SAAO,KAAK,eAAe,KAAK,WAAW,CAAC;CAC5C;CAED,AAAQ,YAAuB;AAE9B,SAAQ,KAAa;CACrB;CAED,AAAQ,eAAkBC,QAAgB;EACzC,MAAMC,SAAsB,CAAE;AAE9B,MAAI,iBAAiB,WAAW,SAC/B,QAAO;AAGR,OAAK,MAAM,OAAO,QAAQ;GACzB,MAAM,SAAS,OAAO;AAEtB,OAAI,kBAAkBL,SAAE,SAAS;IAEhC,MAAM,SAAS,OAAO,iBAAoB;AAC1C,WAAO,OAAO,OAAO,UAClB,OAAO,OACP,KAAK,oBAAoB,OAAO;GACnC,WAAU,iBAAiB,WAAW,SACtC,QAAO,OAAO,KAAK,eAAe,OAAsB;EAEzD;AAED,SAAO;CACP;CAED,AAAQ,oBAAoBF,QAA4B;AACvD,MAAI,kBAAkBE,SAAE,UAAW,QAAO;AAC1C,MAAI,kBAAkBA,SAAE,UAAW,QAAO;AAC1C,MAAI,kBAAkBA,SAAE,WAAY,QAAO;AAC3C,MAAI,kBAAkBA,SAAE,SAAU,QAAO,CAAE;AAC3C,MAAI,kBAAkBA,SAAE,YAAa;AACrC,MAAI,kBAAkBA,SAAE,YAAa,QAAO;AAE5C,MAAI,kBAAkBA,SAAE,aAAa,OAAO,OAAO;GAClD,MAAMC,SAAkC,CAAE;AAC1C,QAAK,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,QAAQ,OAAO,MAAM,CACtD,KAAI,iBAAiBD,SAAE,QACtB,QAAO,OAAO,KAAK,oBAAoB,MAAM;AAG/C,UAAO;EACP;AAED,SAAO;CACP;AACD"}
|
|
1
|
+
{"version":3,"file":"SnifferEnvironmentParser.cjs","names":["schema: z.ZodType","name: string","z","result: Record<string, unknown>","builder: (get: EnvFetcher) => TReturn","ConfigParser","config: T","result: EmptyObject","sniffer: SnifferEnvironmentParser","operation: () => unknown | Promise<unknown>","options: SniffOptions","unhandledRejections: Error[]","reason: unknown","error: Error | undefined"],"sources":["../src/SnifferEnvironmentParser.ts"],"sourcesContent":["import { z } from 'zod/v4';\nimport {\n\tConfigParser,\n\ttype EmptyObject,\n\ttype EnvFetcher,\n} from './EnvironmentParser';\n\n/**\n * A specialized EnvironmentParser for build-time analysis that tracks\n * which environment variables are accessed without requiring actual values.\n *\n * Unlike the regular EnvironmentParser, the sniffer:\n * - Always returns mock values from .parse() and .safeParse()\n * - Never throws validation errors\n * - Tracks all accessed environment variable names\n *\n * This allows service registration to succeed during build-time analysis\n * even when environment variables are not set.\n *\n * @example\n * ```typescript\n * const sniffer = new SnifferEnvironmentParser();\n * await service.register(sniffer); // Always succeeds\n * const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'API_KEY']\n * ```\n */\nexport class SnifferEnvironmentParser<_T extends EmptyObject = EmptyObject> {\n\tprivate readonly accessedVars: Set<string> = new Set();\n\n\t/**\n\t * Wraps a Zod schema to always return mock values.\n\t * This ensures .parse() and .safeParse() never fail.\n\t */\n\tprivate wrapSchema = (schema: z.ZodType, name: string): z.ZodType => {\n\t\treturn new Proxy(schema, {\n\t\t\tget: (target, prop) => {\n\t\t\t\tif (prop === 'parse') {\n\t\t\t\t\treturn () => this.getMockValue(target);\n\t\t\t\t}\n\n\t\t\t\tif (prop === 'safeParse') {\n\t\t\t\t\treturn () => ({\n\t\t\t\t\t\tsuccess: true as const,\n\t\t\t\t\t\tdata: this.getMockValue(target),\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst originalProp = target[prop as keyof typeof target];\n\t\t\t\tif (typeof originalProp === 'function') {\n\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\tconst result = originalProp.apply(target, args);\n\t\t\t\t\t\tif (result && typeof result === 'object' && 'parse' in result) {\n\t\t\t\t\t\t\treturn this.wrapSchema(result, name);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\treturn originalProp;\n\t\t\t},\n\t\t});\n\t};\n\n\t/**\n\t * Returns a mock value based on the Zod schema type.\n\t */\n\tprivate getMockValue(schema: z.ZodType): unknown {\n\t\t// Return type-appropriate mock values\n\t\tif (schema instanceof z.ZodString) return '';\n\t\tif (schema instanceof z.ZodNumber) return 0;\n\t\tif (schema instanceof z.ZodBoolean) return false;\n\t\tif (schema instanceof z.ZodArray) return [];\n\t\tif (schema instanceof z.ZodOptional) return undefined;\n\t\tif (schema instanceof z.ZodNullable) return null;\n\n\t\t// For object schemas, build mock object from shape\n\t\tif (schema instanceof z.ZodObject && schema.shape) {\n\t\t\tconst result: Record<string, unknown> = {};\n\t\t\tfor (const [key, value] of Object.entries(schema.shape)) {\n\t\t\t\tif (value instanceof z.ZodType) {\n\t\t\t\t\tresult[key] = this.getMockValue(value);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\n\t\treturn '';\n\t}\n\n\t/**\n\t * Creates a proxied Zod getter that tracks environment variable access.\n\t */\n\tprivate getZodGetter = (name: string) => {\n\t\tthis.accessedVars.add(name);\n\n\t\treturn new Proxy(\n\t\t\t{ ...z },\n\t\t\t{\n\t\t\t\tget: (target, prop) => {\n\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\tconst value = target[prop];\n\n\t\t\t\t\tif (typeof value === 'function') {\n\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\tconst schema = value(...args);\n\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\tif (value && typeof value === 'object') {\n\t\t\t\t\t\treturn new Proxy(value, {\n\t\t\t\t\t\t\tget: (nestedTarget, nestedProp) => {\n\t\t\t\t\t\t\t\tconst nestedValue =\n\t\t\t\t\t\t\t\t\tnestedTarget[nestedProp as keyof typeof nestedTarget];\n\t\t\t\t\t\t\t\tif (typeof nestedValue === 'function') {\n\t\t\t\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\t\t\t\tconst schema = nestedValue(...args);\n\t\t\t\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn nestedValue;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\treturn value;\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t};\n\n\t/**\n\t * Creates a ConfigParser that will return mock values when parsed.\n\t */\n\tcreate<TReturn extends EmptyObject>(\n\t\tbuilder: (get: EnvFetcher) => TReturn,\n\t): ConfigParser<TReturn> {\n\t\tconst config = builder(this.getZodGetter);\n\t\treturn new SnifferConfigParser(config, this.accessedVars);\n\t}\n\n\t/**\n\t * Returns all environment variable names that were accessed.\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.accessedVars).sort();\n\t}\n}\n\n/**\n * A ConfigParser that always succeeds with mock values.\n */\nclass SnifferConfigParser<\n\tTResponse extends EmptyObject,\n> extends ConfigParser<TResponse> {\n\toverride parse(): any {\n\t\treturn this.parseWithMocks(this.getConfig());\n\t}\n\n\tprivate getConfig(): TResponse {\n\t\t// Access the private config via any cast\n\t\treturn (this as any).config;\n\t}\n\n\tprivate parseWithMocks<T>(config: T): any {\n\t\tconst result: EmptyObject = {};\n\n\t\tif (config && typeof config !== 'object') {\n\t\t\treturn config;\n\t\t}\n\n\t\tfor (const key in config) {\n\t\t\tconst schema = config[key];\n\n\t\t\tif (schema instanceof z.ZodType) {\n\t\t\t\t// Use safeParse which will return mock values from our wrapped schema\n\t\t\t\tconst parsed = schema.safeParse(undefined);\n\t\t\t\tresult[key] = parsed.success\n\t\t\t\t\t? parsed.data\n\t\t\t\t\t: this.getDefaultForSchema(schema);\n\t\t\t} else if (schema && typeof schema === 'object') {\n\t\t\t\tresult[key] = this.parseWithMocks(schema as EmptyObject);\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\tprivate getDefaultForSchema(schema: z.ZodType): unknown {\n\t\tif (schema instanceof z.ZodString) return '';\n\t\tif (schema instanceof z.ZodNumber) return 0;\n\t\tif (schema instanceof z.ZodBoolean) return false;\n\t\tif (schema instanceof z.ZodArray) return [];\n\t\tif (schema instanceof z.ZodOptional) return undefined;\n\t\tif (schema instanceof z.ZodNullable) return null;\n\n\t\tif (schema instanceof z.ZodObject && schema.shape) {\n\t\t\tconst result: Record<string, unknown> = {};\n\t\t\tfor (const [key, value] of Object.entries(schema.shape)) {\n\t\t\t\tif (value instanceof z.ZodType) {\n\t\t\t\t\tresult[key] = this.getDefaultForSchema(value);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\n\t\treturn '';\n\t}\n}\n\n/**\n * Result of sniffing with fire-and-forget handling.\n */\nexport interface SniffResult {\n\t/** Environment variables that were accessed during sniffing */\n\tenvVars: string[];\n\t/** Error thrown during sniffing (env vars may still be captured) */\n\terror?: Error;\n\t/** Unhandled promise rejections captured during sniffing */\n\tunhandledRejections: Error[];\n}\n\n/**\n * Options for sniffing with fire-and-forget handling.\n */\nexport interface SniffOptions {\n\t/**\n\t * Time in milliseconds to wait for fire-and-forget promises to settle.\n\t * Some libraries like better-auth create async operations that may reject\n\t * after the initial event loop tick.\n\t * @default 100\n\t */\n\tsettleTimeMs?: number;\n}\n\n/**\n * Executes a sniffing operation with fire-and-forget handling.\n *\n * This function:\n * 1. Captures unhandled promise rejections during the operation\n * 2. Waits for async operations to settle before returning\n * 3. Gracefully handles errors without throwing\n *\n * Use this when sniffing environment variables from code that may:\n * - Throw synchronous errors\n * - Create fire-and-forget promises that reject\n * - Have async initialization that may fail\n *\n * @param sniffer - The SnifferEnvironmentParser instance to use\n * @param operation - The async operation to execute (e.g., service.register)\n * @param options - Optional configuration\n * @returns SniffResult with env vars and any errors encountered\n *\n * @example\n * ```typescript\n * const sniffer = new SnifferEnvironmentParser();\n * const result = await sniffWithFireAndForget(sniffer, async () => {\n * await service.register({ envParser: sniffer });\n * });\n * console.log('Env vars:', result.envVars);\n * console.log('Error:', result.error);\n * console.log('Unhandled rejections:', result.unhandledRejections);\n * ```\n */\nexport async function sniffWithFireAndForget(\n\tsniffer: SnifferEnvironmentParser,\n\toperation: () => unknown | Promise<unknown>,\n\toptions: SniffOptions = {},\n): Promise<SniffResult> {\n\tconst { settleTimeMs = 100 } = options;\n\tconst unhandledRejections: Error[] = [];\n\n\t// Capture unhandled rejections during sniffing (fire-and-forget promises)\n\t// Libraries like better-auth create async operations that may reject after\n\t// the initial event loop tick\n\tconst captureRejection = (reason: unknown) => {\n\t\tconst err = reason instanceof Error ? reason : new Error(String(reason));\n\t\tunhandledRejections.push(err);\n\t};\n\tprocess.on('unhandledRejection', captureRejection);\n\n\tlet error: Error | undefined;\n\n\ttry {\n\t\tconst result = operation();\n\n\t\t// Handle async result\n\t\tif (result && typeof result === 'object' && 'then' in result) {\n\t\t\tawait Promise.resolve(result).catch((e) => {\n\t\t\t\terror = e instanceof Error ? e : new Error(String(e));\n\t\t\t});\n\t\t}\n\t} catch (e) {\n\t\terror = e instanceof Error ? e : new Error(String(e));\n\t} finally {\n\t\t// Wait for fire-and-forget promises to settle\n\t\tawait new Promise((resolve) => setTimeout(resolve, settleTimeMs));\n\t\tprocess.off('unhandledRejection', captureRejection);\n\t}\n\n\treturn {\n\t\tenvVars: sniffer.getEnvironmentVariables(),\n\t\terror,\n\t\tunhandledRejections,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,IAAa,2BAAb,MAA4E;CAC3E,AAAiB,+BAA4B,IAAI;;;;;CAMjD,AAAQ,aAAa,CAACA,QAAmBC,SAA4B;AACpE,SAAO,IAAI,MAAM,QAAQ,EACxB,KAAK,CAAC,QAAQ,SAAS;AACtB,OAAI,SAAS,QACZ,QAAO,MAAM,KAAK,aAAa,OAAO;AAGv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MC,SAAkC,CAAE;AAC1C,QAAK,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,QAAQ,OAAO,MAAM,CACtD,KAAI,iBAAiBD,SAAE,QACtB,QAAO,OAAO,KAAK,oBAAoB,MAAM;AAG/C,UAAO;EACP;AAED,SAAO;CACP;AACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwDD,eAAsB,uBACrBM,SACAC,WACAC,UAAwB,CAAE,GACH;CACvB,MAAM,EAAE,eAAe,KAAK,GAAG;CAC/B,MAAMC,sBAA+B,CAAE;CAKvC,MAAM,mBAAmB,CAACC,WAAoB;EAC7C,MAAM,MAAM,kBAAkB,QAAQ,SAAS,IAAI,MAAM,OAAO,OAAO;AACvE,sBAAoB,KAAK,IAAI;CAC7B;AACD,SAAQ,GAAG,sBAAsB,iBAAiB;CAElD,IAAIC;AAEJ,KAAI;EACH,MAAM,SAAS,WAAW;AAG1B,MAAI,iBAAiB,WAAW,YAAY,UAAU,OACrD,OAAM,QAAQ,QAAQ,OAAO,CAAC,MAAM,CAAC,MAAM;AAC1C,WAAQ,aAAa,QAAQ,IAAI,IAAI,MAAM,OAAO,EAAE;EACpD,EAAC;CAEH,SAAQ,GAAG;AACX,UAAQ,aAAa,QAAQ,IAAI,IAAI,MAAM,OAAO,EAAE;CACpD,UAAS;AAET,QAAM,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,aAAa;AAChE,UAAQ,IAAI,sBAAsB,iBAAiB;CACnD;AAED,QAAO;EACN,SAAS,QAAQ,yBAAyB;EAC1C;EACA;CACA;AACD"}
|
|
@@ -45,7 +45,60 @@ declare class SnifferEnvironmentParser<_T extends EmptyObject = EmptyObject> {
|
|
|
45
45
|
*/
|
|
46
46
|
getEnvironmentVariables(): string[];
|
|
47
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Result of sniffing with fire-and-forget handling.
|
|
50
|
+
*/
|
|
51
|
+
interface SniffResult {
|
|
52
|
+
/** Environment variables that were accessed during sniffing */
|
|
53
|
+
envVars: string[];
|
|
54
|
+
/** Error thrown during sniffing (env vars may still be captured) */
|
|
55
|
+
error?: Error;
|
|
56
|
+
/** Unhandled promise rejections captured during sniffing */
|
|
57
|
+
unhandledRejections: Error[];
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Options for sniffing with fire-and-forget handling.
|
|
61
|
+
*/
|
|
62
|
+
interface SniffOptions {
|
|
63
|
+
/**
|
|
64
|
+
* Time in milliseconds to wait for fire-and-forget promises to settle.
|
|
65
|
+
* Some libraries like better-auth create async operations that may reject
|
|
66
|
+
* after the initial event loop tick.
|
|
67
|
+
* @default 100
|
|
68
|
+
*/
|
|
69
|
+
settleTimeMs?: number;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Executes a sniffing operation with fire-and-forget handling.
|
|
73
|
+
*
|
|
74
|
+
* This function:
|
|
75
|
+
* 1. Captures unhandled promise rejections during the operation
|
|
76
|
+
* 2. Waits for async operations to settle before returning
|
|
77
|
+
* 3. Gracefully handles errors without throwing
|
|
78
|
+
*
|
|
79
|
+
* Use this when sniffing environment variables from code that may:
|
|
80
|
+
* - Throw synchronous errors
|
|
81
|
+
* - Create fire-and-forget promises that reject
|
|
82
|
+
* - Have async initialization that may fail
|
|
83
|
+
*
|
|
84
|
+
* @param sniffer - The SnifferEnvironmentParser instance to use
|
|
85
|
+
* @param operation - The async operation to execute (e.g., service.register)
|
|
86
|
+
* @param options - Optional configuration
|
|
87
|
+
* @returns SniffResult with env vars and any errors encountered
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const sniffer = new SnifferEnvironmentParser();
|
|
92
|
+
* const result = await sniffWithFireAndForget(sniffer, async () => {
|
|
93
|
+
* await service.register({ envParser: sniffer });
|
|
94
|
+
* });
|
|
95
|
+
* console.log('Env vars:', result.envVars);
|
|
96
|
+
* console.log('Error:', result.error);
|
|
97
|
+
* console.log('Unhandled rejections:', result.unhandledRejections);
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
declare function sniffWithFireAndForget(sniffer: SnifferEnvironmentParser, operation: () => unknown | Promise<unknown>, options?: SniffOptions): Promise<SniffResult>;
|
|
48
101
|
//# sourceMappingURL=SnifferEnvironmentParser.d.ts.map
|
|
49
102
|
//#endregion
|
|
50
|
-
export { SnifferEnvironmentParser };
|
|
103
|
+
export { SniffOptions, SniffResult, SnifferEnvironmentParser, sniffWithFireAndForget };
|
|
51
104
|
//# sourceMappingURL=SnifferEnvironmentParser.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnifferEnvironmentParser.d.cts","names":[],"sources":["../src/SnifferEnvironmentParser.ts"],"sourcesContent":[],"mappings":";;;;;;AA0BA;;;;;;;;;AA8GgB
|
|
1
|
+
{"version":3,"file":"SnifferEnvironmentParser.d.cts","names":[],"sources":["../src/SnifferEnvironmentParser.ts"],"sourcesContent":[],"mappings":";;;;;;AA0BA;;;;;;;;;AA8GgB;AA6EhB;;;;AAM2B;AAM3B;AAuCA;AAA4C,cA9O/B,wBA8O+B,CAAA,WA9OK,WA8OL,GA9OmB,WA8OnB,CAAA,CAAA;EAAA,iBAClC,YAAA;EAAwB;;;;EAGxB,QAAA,UAAA;;;;;;;;;;;;yBAtIc,4BACP,eAAe,UAC5B,aAAa;;;;;;;;;UA6EA,WAAA;;;;UAIR;;uBAEa;;;;;UAML,YAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuCK,sBAAA,UACZ,qDACkB,4BAClB,eACP,QAAQ"}
|
|
@@ -45,7 +45,60 @@ declare class SnifferEnvironmentParser<_T extends EmptyObject = EmptyObject> {
|
|
|
45
45
|
*/
|
|
46
46
|
getEnvironmentVariables(): string[];
|
|
47
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Result of sniffing with fire-and-forget handling.
|
|
50
|
+
*/
|
|
51
|
+
interface SniffResult {
|
|
52
|
+
/** Environment variables that were accessed during sniffing */
|
|
53
|
+
envVars: string[];
|
|
54
|
+
/** Error thrown during sniffing (env vars may still be captured) */
|
|
55
|
+
error?: Error;
|
|
56
|
+
/** Unhandled promise rejections captured during sniffing */
|
|
57
|
+
unhandledRejections: Error[];
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Options for sniffing with fire-and-forget handling.
|
|
61
|
+
*/
|
|
62
|
+
interface SniffOptions {
|
|
63
|
+
/**
|
|
64
|
+
* Time in milliseconds to wait for fire-and-forget promises to settle.
|
|
65
|
+
* Some libraries like better-auth create async operations that may reject
|
|
66
|
+
* after the initial event loop tick.
|
|
67
|
+
* @default 100
|
|
68
|
+
*/
|
|
69
|
+
settleTimeMs?: number;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Executes a sniffing operation with fire-and-forget handling.
|
|
73
|
+
*
|
|
74
|
+
* This function:
|
|
75
|
+
* 1. Captures unhandled promise rejections during the operation
|
|
76
|
+
* 2. Waits for async operations to settle before returning
|
|
77
|
+
* 3. Gracefully handles errors without throwing
|
|
78
|
+
*
|
|
79
|
+
* Use this when sniffing environment variables from code that may:
|
|
80
|
+
* - Throw synchronous errors
|
|
81
|
+
* - Create fire-and-forget promises that reject
|
|
82
|
+
* - Have async initialization that may fail
|
|
83
|
+
*
|
|
84
|
+
* @param sniffer - The SnifferEnvironmentParser instance to use
|
|
85
|
+
* @param operation - The async operation to execute (e.g., service.register)
|
|
86
|
+
* @param options - Optional configuration
|
|
87
|
+
* @returns SniffResult with env vars and any errors encountered
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const sniffer = new SnifferEnvironmentParser();
|
|
92
|
+
* const result = await sniffWithFireAndForget(sniffer, async () => {
|
|
93
|
+
* await service.register({ envParser: sniffer });
|
|
94
|
+
* });
|
|
95
|
+
* console.log('Env vars:', result.envVars);
|
|
96
|
+
* console.log('Error:', result.error);
|
|
97
|
+
* console.log('Unhandled rejections:', result.unhandledRejections);
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
declare function sniffWithFireAndForget(sniffer: SnifferEnvironmentParser, operation: () => unknown | Promise<unknown>, options?: SniffOptions): Promise<SniffResult>;
|
|
48
101
|
//# sourceMappingURL=SnifferEnvironmentParser.d.ts.map
|
|
49
102
|
//#endregion
|
|
50
|
-
export { SnifferEnvironmentParser };
|
|
103
|
+
export { SniffOptions, SniffResult, SnifferEnvironmentParser, sniffWithFireAndForget };
|
|
51
104
|
//# sourceMappingURL=SnifferEnvironmentParser.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnifferEnvironmentParser.d.mts","names":[],"sources":["../src/SnifferEnvironmentParser.ts"],"sourcesContent":[],"mappings":";;;;;;AA0BA;;;;;;;;;AA8GgB
|
|
1
|
+
{"version":3,"file":"SnifferEnvironmentParser.d.mts","names":[],"sources":["../src/SnifferEnvironmentParser.ts"],"sourcesContent":[],"mappings":";;;;;;AA0BA;;;;;;;;;AA8GgB;AA6EhB;;;;AAM2B;AAM3B;AAuCA;AAA4C,cA9O/B,wBA8O+B,CAAA,WA9OK,WA8OL,GA9OmB,WA8OnB,CAAA,CAAA;EAAA,iBAClC,YAAA;EAAwB;;;;EAGxB,QAAA,UAAA;;;;;;;;;;;;yBAtIc,4BACP,eAAe,UAC5B,aAAa;;;;;;;;;UA6EA,WAAA;;;;UAIR;;uBAEa;;;;;UAML,YAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuCK,sBAAA,UACZ,qDACkB,4BAClB,eACP,QAAQ"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import "./formatter-
|
|
2
|
-
import { ConfigParser } from "./EnvironmentParser-
|
|
1
|
+
import "./formatter-BRRrxQi3.mjs";
|
|
2
|
+
import { ConfigParser } from "./EnvironmentParser-zMblItla.mjs";
|
|
3
3
|
import { z } from "zod/v4";
|
|
4
4
|
|
|
5
5
|
//#region src/SnifferEnvironmentParser.ts
|
|
@@ -134,7 +134,62 @@ var SnifferConfigParser = class extends ConfigParser {
|
|
|
134
134
|
return "";
|
|
135
135
|
}
|
|
136
136
|
};
|
|
137
|
+
/**
|
|
138
|
+
* Executes a sniffing operation with fire-and-forget handling.
|
|
139
|
+
*
|
|
140
|
+
* This function:
|
|
141
|
+
* 1. Captures unhandled promise rejections during the operation
|
|
142
|
+
* 2. Waits for async operations to settle before returning
|
|
143
|
+
* 3. Gracefully handles errors without throwing
|
|
144
|
+
*
|
|
145
|
+
* Use this when sniffing environment variables from code that may:
|
|
146
|
+
* - Throw synchronous errors
|
|
147
|
+
* - Create fire-and-forget promises that reject
|
|
148
|
+
* - Have async initialization that may fail
|
|
149
|
+
*
|
|
150
|
+
* @param sniffer - The SnifferEnvironmentParser instance to use
|
|
151
|
+
* @param operation - The async operation to execute (e.g., service.register)
|
|
152
|
+
* @param options - Optional configuration
|
|
153
|
+
* @returns SniffResult with env vars and any errors encountered
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* const sniffer = new SnifferEnvironmentParser();
|
|
158
|
+
* const result = await sniffWithFireAndForget(sniffer, async () => {
|
|
159
|
+
* await service.register({ envParser: sniffer });
|
|
160
|
+
* });
|
|
161
|
+
* console.log('Env vars:', result.envVars);
|
|
162
|
+
* console.log('Error:', result.error);
|
|
163
|
+
* console.log('Unhandled rejections:', result.unhandledRejections);
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
async function sniffWithFireAndForget(sniffer, operation, options = {}) {
|
|
167
|
+
const { settleTimeMs = 100 } = options;
|
|
168
|
+
const unhandledRejections = [];
|
|
169
|
+
const captureRejection = (reason) => {
|
|
170
|
+
const err = reason instanceof Error ? reason : new Error(String(reason));
|
|
171
|
+
unhandledRejections.push(err);
|
|
172
|
+
};
|
|
173
|
+
process.on("unhandledRejection", captureRejection);
|
|
174
|
+
let error;
|
|
175
|
+
try {
|
|
176
|
+
const result = operation();
|
|
177
|
+
if (result && typeof result === "object" && "then" in result) await Promise.resolve(result).catch((e) => {
|
|
178
|
+
error = e instanceof Error ? e : new Error(String(e));
|
|
179
|
+
});
|
|
180
|
+
} catch (e) {
|
|
181
|
+
error = e instanceof Error ? e : new Error(String(e));
|
|
182
|
+
} finally {
|
|
183
|
+
await new Promise((resolve) => setTimeout(resolve, settleTimeMs));
|
|
184
|
+
process.off("unhandledRejection", captureRejection);
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
envVars: sniffer.getEnvironmentVariables(),
|
|
188
|
+
error,
|
|
189
|
+
unhandledRejections
|
|
190
|
+
};
|
|
191
|
+
}
|
|
137
192
|
|
|
138
193
|
//#endregion
|
|
139
|
-
export { SnifferEnvironmentParser };
|
|
194
|
+
export { SnifferEnvironmentParser, sniffWithFireAndForget };
|
|
140
195
|
//# sourceMappingURL=SnifferEnvironmentParser.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnifferEnvironmentParser.mjs","names":["schema: z.ZodType","name: string","result: Record<string, unknown>","builder: (get: EnvFetcher) => TReturn","config: T","result: EmptyObject"],"sources":["../src/SnifferEnvironmentParser.ts"],"sourcesContent":["import { z } from 'zod/v4';\nimport {\n\tConfigParser,\n\ttype EmptyObject,\n\ttype EnvFetcher,\n} from './EnvironmentParser';\n\n/**\n * A specialized EnvironmentParser for build-time analysis that tracks\n * which environment variables are accessed without requiring actual values.\n *\n * Unlike the regular EnvironmentParser, the sniffer:\n * - Always returns mock values from .parse() and .safeParse()\n * - Never throws validation errors\n * - Tracks all accessed environment variable names\n *\n * This allows service registration to succeed during build-time analysis\n * even when environment variables are not set.\n *\n * @example\n * ```typescript\n * const sniffer = new SnifferEnvironmentParser();\n * await service.register(sniffer); // Always succeeds\n * const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'API_KEY']\n * ```\n */\nexport class SnifferEnvironmentParser<_T extends EmptyObject = EmptyObject> {\n\tprivate readonly accessedVars: Set<string> = new Set();\n\n\t/**\n\t * Wraps a Zod schema to always return mock values.\n\t * This ensures .parse() and .safeParse() never fail.\n\t */\n\tprivate wrapSchema = (schema: z.ZodType, name: string): z.ZodType => {\n\t\treturn new Proxy(schema, {\n\t\t\tget: (target, prop) => {\n\t\t\t\tif (prop === 'parse') {\n\t\t\t\t\treturn () => this.getMockValue(target);\n\t\t\t\t}\n\n\t\t\t\tif (prop === 'safeParse') {\n\t\t\t\t\treturn () => ({\n\t\t\t\t\t\tsuccess: true as const,\n\t\t\t\t\t\tdata: this.getMockValue(target),\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst originalProp = target[prop as keyof typeof target];\n\t\t\t\tif (typeof originalProp === 'function') {\n\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\tconst result = originalProp.apply(target, args);\n\t\t\t\t\t\tif (result && typeof result === 'object' && 'parse' in result) {\n\t\t\t\t\t\t\treturn this.wrapSchema(result, name);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\treturn originalProp;\n\t\t\t},\n\t\t});\n\t};\n\n\t/**\n\t * Returns a mock value based on the Zod schema type.\n\t */\n\tprivate getMockValue(schema: z.ZodType): unknown {\n\t\t// Return type-appropriate mock values\n\t\tif (schema instanceof z.ZodString) return '';\n\t\tif (schema instanceof z.ZodNumber) return 0;\n\t\tif (schema instanceof z.ZodBoolean) return false;\n\t\tif (schema instanceof z.ZodArray) return [];\n\t\tif (schema instanceof z.ZodOptional) return undefined;\n\t\tif (schema instanceof z.ZodNullable) return null;\n\n\t\t// For object schemas, build mock object from shape\n\t\tif (schema instanceof z.ZodObject && schema.shape) {\n\t\t\tconst result: Record<string, unknown> = {};\n\t\t\tfor (const [key, value] of Object.entries(schema.shape)) {\n\t\t\t\tif (value instanceof z.ZodType) {\n\t\t\t\t\tresult[key] = this.getMockValue(value);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\n\t\treturn '';\n\t}\n\n\t/**\n\t * Creates a proxied Zod getter that tracks environment variable access.\n\t */\n\tprivate getZodGetter = (name: string) => {\n\t\tthis.accessedVars.add(name);\n\n\t\treturn new Proxy(\n\t\t\t{ ...z },\n\t\t\t{\n\t\t\t\tget: (target, prop) => {\n\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\tconst value = target[prop];\n\n\t\t\t\t\tif (typeof value === 'function') {\n\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\tconst schema = value(...args);\n\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\tif (value && typeof value === 'object') {\n\t\t\t\t\t\treturn new Proxy(value, {\n\t\t\t\t\t\t\tget: (nestedTarget, nestedProp) => {\n\t\t\t\t\t\t\t\tconst nestedValue =\n\t\t\t\t\t\t\t\t\tnestedTarget[nestedProp as keyof typeof nestedTarget];\n\t\t\t\t\t\t\t\tif (typeof nestedValue === 'function') {\n\t\t\t\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\t\t\t\tconst schema = nestedValue(...args);\n\t\t\t\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn nestedValue;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\treturn value;\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t};\n\n\t/**\n\t * Creates a ConfigParser that will return mock values when parsed.\n\t */\n\tcreate<TReturn extends EmptyObject>(\n\t\tbuilder: (get: EnvFetcher) => TReturn,\n\t): ConfigParser<TReturn> {\n\t\tconst config = builder(this.getZodGetter);\n\t\treturn new SnifferConfigParser(config, this.accessedVars);\n\t}\n\n\t/**\n\t * Returns all environment variable names that were accessed.\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.accessedVars).sort();\n\t}\n}\n\n/**\n * A ConfigParser that always succeeds with mock values.\n */\nclass SnifferConfigParser<\n\tTResponse extends EmptyObject,\n> extends ConfigParser<TResponse> {\n\toverride parse(): any {\n\t\treturn this.parseWithMocks(this.getConfig());\n\t}\n\n\tprivate getConfig(): TResponse {\n\t\t// Access the private config via any cast\n\t\treturn (this as any).config;\n\t}\n\n\tprivate parseWithMocks<T>(config: T): any {\n\t\tconst result: EmptyObject = {};\n\n\t\tif (config && typeof config !== 'object') {\n\t\t\treturn config;\n\t\t}\n\n\t\tfor (const key in config) {\n\t\t\tconst schema = config[key];\n\n\t\t\tif (schema instanceof z.ZodType) {\n\t\t\t\t// Use safeParse which will return mock values from our wrapped schema\n\t\t\t\tconst parsed = schema.safeParse(undefined);\n\t\t\t\tresult[key] = parsed.success\n\t\t\t\t\t? parsed.data\n\t\t\t\t\t: this.getDefaultForSchema(schema);\n\t\t\t} else if (schema && typeof schema === 'object') {\n\t\t\t\tresult[key] = this.parseWithMocks(schema as EmptyObject);\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\tprivate getDefaultForSchema(schema: z.ZodType): unknown {\n\t\tif (schema instanceof z.ZodString) return '';\n\t\tif (schema instanceof z.ZodNumber) return 0;\n\t\tif (schema instanceof z.ZodBoolean) return false;\n\t\tif (schema instanceof z.ZodArray) return [];\n\t\tif (schema instanceof z.ZodOptional) return undefined;\n\t\tif (schema instanceof z.ZodNullable) return null;\n\n\t\tif (schema instanceof z.ZodObject && schema.shape) {\n\t\t\tconst result: Record<string, unknown> = {};\n\t\t\tfor (const [key, value] of Object.entries(schema.shape)) {\n\t\t\t\tif (value instanceof z.ZodType) {\n\t\t\t\t\tresult[key] = this.getDefaultForSchema(value);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\n\t\treturn '';\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA0BA,IAAa,2BAAb,MAA4E;CAC3E,AAAiB,+BA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kB,EAAE,SAAU,QAAO,CAAE;AAC3C,MAAI,kBAAkB,EAAE,YAAa;AACrC,MAAI,kBAAkB,EAAE,YAAa,QAAO;AAE5C,MAAI,kBAAkB,EAAE,aAAa,OAAO,OAAO;GAClD,MAAME,SAAkC,CAAE;AAC1C,QAAK,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,QAAQ,OAAO,MAAM,CACtD,KAAI,iBAAiB,EAAE,QACtB,QAAO,OAAO,KAAK,oBAAoB,MAAM;AAG/C,UAAO;EACP;AAED,SAAO;CACP;AACD"}
|
|
1
|
+
{"version":3,"file":"SnifferEnvironmentParser.mjs","names":["schema: z.ZodType","name: string","result: Record<string, unknown>","builder: (get: EnvFetcher) => TReturn","config: T","result: EmptyObject","sniffer: SnifferEnvironmentParser","operation: () => unknown | Promise<unknown>","options: SniffOptions","unhandledRejections: Error[]","reason: unknown","error: Error | undefined"],"sources":["../src/SnifferEnvironmentParser.ts"],"sourcesContent":["import { z } from 'zod/v4';\nimport {\n\tConfigParser,\n\ttype EmptyObject,\n\ttype EnvFetcher,\n} from './EnvironmentParser';\n\n/**\n * A specialized EnvironmentParser for build-time analysis that tracks\n * which environment variables are accessed without requiring actual values.\n *\n * Unlike the regular EnvironmentParser, the sniffer:\n * - Always returns mock values from .parse() and .safeParse()\n * - Never throws validation errors\n * - Tracks all accessed environment variable names\n *\n * This allows service registration to succeed during build-time analysis\n * even when environment variables are not set.\n *\n * @example\n * ```typescript\n * const sniffer = new SnifferEnvironmentParser();\n * await service.register(sniffer); // Always succeeds\n * const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'API_KEY']\n * ```\n */\nexport class SnifferEnvironmentParser<_T extends EmptyObject = EmptyObject> {\n\tprivate readonly accessedVars: Set<string> = new Set();\n\n\t/**\n\t * Wraps a Zod schema to always return mock values.\n\t * This ensures .parse() and .safeParse() never fail.\n\t */\n\tprivate wrapSchema = (schema: z.ZodType, name: string): z.ZodType => {\n\t\treturn new Proxy(schema, {\n\t\t\tget: (target, prop) => {\n\t\t\t\tif (prop === 'parse') {\n\t\t\t\t\treturn () => this.getMockValue(target);\n\t\t\t\t}\n\n\t\t\t\tif (prop === 'safeParse') {\n\t\t\t\t\treturn () => ({\n\t\t\t\t\t\tsuccess: true as const,\n\t\t\t\t\t\tdata: this.getMockValue(target),\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst originalProp = target[prop as keyof typeof target];\n\t\t\t\tif (typeof originalProp === 'function') {\n\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\tconst result = originalProp.apply(target, args);\n\t\t\t\t\t\tif (result && typeof result === 'object' && 'parse' in result) {\n\t\t\t\t\t\t\treturn this.wrapSchema(result, name);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\treturn originalProp;\n\t\t\t},\n\t\t});\n\t};\n\n\t/**\n\t * Returns a mock value based on the Zod schema type.\n\t */\n\tprivate getMockValue(schema: z.ZodType): unknown {\n\t\t// Return type-appropriate mock values\n\t\tif (schema instanceof z.ZodString) return '';\n\t\tif (schema instanceof z.ZodNumber) return 0;\n\t\tif (schema instanceof z.ZodBoolean) return false;\n\t\tif (schema instanceof z.ZodArray) return [];\n\t\tif (schema instanceof z.ZodOptional) return undefined;\n\t\tif (schema instanceof z.ZodNullable) return null;\n\n\t\t// For object schemas, build mock object from shape\n\t\tif (schema instanceof z.ZodObject && schema.shape) {\n\t\t\tconst result: Record<string, unknown> = {};\n\t\t\tfor (const [key, value] of Object.entries(schema.shape)) {\n\t\t\t\tif (value instanceof z.ZodType) {\n\t\t\t\t\tresult[key] = this.getMockValue(value);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\n\t\treturn '';\n\t}\n\n\t/**\n\t * Creates a proxied Zod getter that tracks environment variable access.\n\t */\n\tprivate getZodGetter = (name: string) => {\n\t\tthis.accessedVars.add(name);\n\n\t\treturn new Proxy(\n\t\t\t{ ...z },\n\t\t\t{\n\t\t\t\tget: (target, prop) => {\n\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\tconst value = target[prop];\n\n\t\t\t\t\tif (typeof value === 'function') {\n\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\tconst schema = value(...args);\n\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\tif (value && typeof value === 'object') {\n\t\t\t\t\t\treturn new Proxy(value, {\n\t\t\t\t\t\t\tget: (nestedTarget, nestedProp) => {\n\t\t\t\t\t\t\t\tconst nestedValue =\n\t\t\t\t\t\t\t\t\tnestedTarget[nestedProp as keyof typeof nestedTarget];\n\t\t\t\t\t\t\t\tif (typeof nestedValue === 'function') {\n\t\t\t\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\t\t\t\tconst schema = nestedValue(...args);\n\t\t\t\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn nestedValue;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\treturn value;\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t};\n\n\t/**\n\t * Creates a ConfigParser that will return mock values when parsed.\n\t */\n\tcreate<TReturn extends EmptyObject>(\n\t\tbuilder: (get: EnvFetcher) => TReturn,\n\t): ConfigParser<TReturn> {\n\t\tconst config = builder(this.getZodGetter);\n\t\treturn new SnifferConfigParser(config, this.accessedVars);\n\t}\n\n\t/**\n\t * Returns all environment variable names that were accessed.\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.accessedVars).sort();\n\t}\n}\n\n/**\n * A ConfigParser that always succeeds with mock values.\n */\nclass SnifferConfigParser<\n\tTResponse extends EmptyObject,\n> extends ConfigParser<TResponse> {\n\toverride parse(): any {\n\t\treturn this.parseWithMocks(this.getConfig());\n\t}\n\n\tprivate getConfig(): TResponse {\n\t\t// Access the private config via any cast\n\t\treturn (this as any).config;\n\t}\n\n\tprivate parseWithMocks<T>(config: T): any {\n\t\tconst result: EmptyObject = {};\n\n\t\tif (config && typeof config !== 'object') {\n\t\t\treturn config;\n\t\t}\n\n\t\tfor (const key in config) {\n\t\t\tconst schema = config[key];\n\n\t\t\tif (schema instanceof z.ZodType) {\n\t\t\t\t// Use safeParse which will return mock values from our wrapped schema\n\t\t\t\tconst parsed = schema.safeParse(undefined);\n\t\t\t\tresult[key] = parsed.success\n\t\t\t\t\t? parsed.data\n\t\t\t\t\t: this.getDefaultForSchema(schema);\n\t\t\t} else if (schema && typeof schema === 'object') {\n\t\t\t\tresult[key] = this.parseWithMocks(schema as EmptyObject);\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\tprivate getDefaultForSchema(schema: z.ZodType): unknown {\n\t\tif (schema instanceof z.ZodString) return '';\n\t\tif (schema instanceof z.ZodNumber) return 0;\n\t\tif (schema instanceof z.ZodBoolean) return false;\n\t\tif (schema instanceof z.ZodArray) return [];\n\t\tif (schema instanceof z.ZodOptional) return undefined;\n\t\tif (schema instanceof z.ZodNullable) return null;\n\n\t\tif (schema instanceof z.ZodObject && schema.shape) {\n\t\t\tconst result: Record<string, unknown> = {};\n\t\t\tfor (const [key, value] of Object.entries(schema.shape)) {\n\t\t\t\tif (value instanceof z.ZodType) {\n\t\t\t\t\tresult[key] = this.getDefaultForSchema(value);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\n\t\treturn '';\n\t}\n}\n\n/**\n * Result of sniffing with fire-and-forget handling.\n */\nexport interface SniffResult {\n\t/** Environment variables that were accessed during sniffing */\n\tenvVars: string[];\n\t/** Error thrown during sniffing (env vars may still be captured) */\n\terror?: Error;\n\t/** Unhandled promise rejections captured during sniffing */\n\tunhandledRejections: Error[];\n}\n\n/**\n * Options for sniffing with fire-and-forget handling.\n */\nexport interface SniffOptions {\n\t/**\n\t * Time in milliseconds to wait for fire-and-forget promises to settle.\n\t * Some libraries like better-auth create async operations that may reject\n\t * after the initial event loop tick.\n\t * @default 100\n\t */\n\tsettleTimeMs?: number;\n}\n\n/**\n * Executes a sniffing operation with fire-and-forget handling.\n *\n * This function:\n * 1. Captures unhandled promise rejections during the operation\n * 2. Waits for async operations to settle before returning\n * 3. Gracefully handles errors without throwing\n *\n * Use this when sniffing environment variables from code that may:\n * - Throw synchronous errors\n * - Create fire-and-forget promises that reject\n * - Have async initialization that may fail\n *\n * @param sniffer - The SnifferEnvironmentParser instance to use\n * @param operation - The async operation to execute (e.g., service.register)\n * @param options - Optional configuration\n * @returns SniffResult with env vars and any errors encountered\n *\n * @example\n * ```typescript\n * const sniffer = new SnifferEnvironmentParser();\n * const result = await sniffWithFireAndForget(sniffer, async () => {\n * await service.register({ envParser: sniffer });\n * });\n * console.log('Env vars:', result.envVars);\n * console.log('Error:', result.error);\n * console.log('Unhandled rejections:', result.unhandledRejections);\n * ```\n */\nexport async function sniffWithFireAndForget(\n\tsniffer: SnifferEnvironmentParser,\n\toperation: () => unknown | Promise<unknown>,\n\toptions: SniffOptions = {},\n): Promise<SniffResult> {\n\tconst { settleTimeMs = 100 } = options;\n\tconst unhandledRejections: Error[] = [];\n\n\t// Capture unhandled rejections during sniffing (fire-and-forget promises)\n\t// Libraries like better-auth create async operations that may reject after\n\t// the initial event loop tick\n\tconst captureRejection = (reason: unknown) => {\n\t\tconst err = reason instanceof Error ? reason : new Error(String(reason));\n\t\tunhandledRejections.push(err);\n\t};\n\tprocess.on('unhandledRejection', captureRejection);\n\n\tlet error: Error | undefined;\n\n\ttry {\n\t\tconst result = operation();\n\n\t\t// Handle async result\n\t\tif (result && typeof result === 'object' && 'then' in result) {\n\t\t\tawait Promise.resolve(result).catch((e) => {\n\t\t\t\terror = e instanceof Error ? e : new Error(String(e));\n\t\t\t});\n\t\t}\n\t} catch (e) {\n\t\terror = e instanceof Error ? e : new Error(String(e));\n\t} finally {\n\t\t// Wait for fire-and-forget promises to settle\n\t\tawait new Promise((resolve) => setTimeout(resolve, settleTimeMs));\n\t\tprocess.off('unhandledRejection', captureRejection);\n\t}\n\n\treturn {\n\t\tenvVars: sniffer.getEnvironmentVariables(),\n\t\terror,\n\t\tunhandledRejections,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA0BA,IAAa,2BAAb,MAA4E;CAC3E,AAAiB,+BAA4B,IAAI;;;;;CAMjD,AAAQ,aAAa,CAACA,QAAmBC,SAA4B;AACpE,SAAO,IAAI,MAAM,QAAQ,EACxB,KAAK,CAAC,QAAQ,SAAS;AACtB,OAAI,SAAS,QACZ,QAAO,MAAM,KAAK,aAAa,OAAO;AAGvC,OAAI,SAAS,YACZ,QAAO,OAAO;IACb,SAAS;IACT,MAAM,KAAK,aAAa,OAAO;GAC/B;GAGF,MAAM,eAAe,OAAO;AAC5B,cAAW,iBAAiB,WAC3B,QAAO,CAAC,GAAG,SAAgB;IAC1B,MAAM,SAAS,aAAa,MAAM,QAAQ,KAAK;AAC/C,QAAI,iBAAiB,WAAW,YAAY,WAAW,OACtD,QAAO,KAAK,WAAW,QAAQ,KAAK;AAErC,WAAO;GACP;AAGF,UAAO;EACP,EACD;CACD;;;;CAKD,AAAQ,aAAaD,QAA4B;AAEhD,MAAI,kBAAkB,EAAE,UAAW,QAAO;AAC1C,MAAI,kBAAkB,EAAE,UAAW,QAAO;AAC1C,MAAI,kBAAkB,EAAE,WAAY,QAAO;AAC3C,MAAI,kBAAkB,EAAE,SAAU,QAAO,CAAE;AAC3C,MAAI,kBAAkB,EAAE,YAAa;AACrC,MAAI,kBAAkB,EAAE,YAAa,QAAO;AAG5C,MAAI,kBAAkB,EAAE,aAAa,OAAO,OAAO;GAClD,MAAME,SAAkC,CAAE;AAC1C,QAAK,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,QAAQ,OAAO,MAAM,CACtD,KAAI,iBAAiB,EAAE,QACt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M,OAAO,EAAE;CACpD,UAAS;AAET,QAAM,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,aAAa;AAChE,UAAQ,IAAI,sBAAsB,iBAAiB;CACnD;AAED,QAAO;EACN,SAAS,QAAQ,yBAAyB;EAC1C;EACA;CACA;AACD"}
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { z } from "zod/v4";
|
|
2
|
-
|
|
3
1
|
//#region src/formatter.ts
|
|
4
2
|
/**
|
|
5
3
|
* ANSI color codes for terminal output.
|
|
@@ -57,7 +55,7 @@ function formatParseError(error, options = {}) {
|
|
|
57
55
|
const invalidVars = [];
|
|
58
56
|
for (const issue of error.issues) {
|
|
59
57
|
let envName = "";
|
|
60
|
-
if (issue.path.length > 0) envName =
|
|
58
|
+
if (issue.path.length > 0) envName = issue.path.map(String).join(".");
|
|
61
59
|
else {
|
|
62
60
|
const match = issue.message.match(/Environment variable "([^"]+)"/);
|
|
63
61
|
if (match?.[1]) envName = match[1];
|
|
@@ -110,4 +108,4 @@ function isDevelopment() {
|
|
|
110
108
|
|
|
111
109
|
//#endregion
|
|
112
110
|
export { formatParseError, isDevelopment };
|
|
113
|
-
//# sourceMappingURL=formatter-
|
|
111
|
+
//# sourceMappingURL=formatter-BRRrxQi3.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatter-BRRrxQi3.mjs","names":["error: z.ZodError","options: FormatOptions","missingVars: Array<{ name: string; message: string }>","invalidVars: Array<{ name: string; value: unknown; message: string }>","lines: string[]","message: string"],"sources":["../src/formatter.ts"],"sourcesContent":["import type { z } from 'zod/v4';\n\n/**\n * Options for formatting parse errors.\n */\nexport interface FormatOptions {\n\t/** Whether to use colors in output. Defaults to auto-detect TTY. */\n\tcolors?: boolean;\n}\n\n/**\n * ANSI color codes for terminal output.\n */\nconst colors = {\n\treset: '\\x1b[0m',\n\tred: '\\x1b[31m',\n\tyellow: '\\x1b[33m',\n\tcyan: '\\x1b[36m',\n\tdim: '\\x1b[2m',\n\tbold: '\\x1b[1m',\n};\n\n/**\n * Formats a ZodError into a user-friendly string for development.\n *\n * @param error - The ZodError to format\n * @param options - Formatting options\n * @returns Formatted error message\n *\n * @example\n * ```typescript\n * try {\n * config.parse();\n * } catch (error) {\n * if (error instanceof ZodError) {\n * console.error(formatParseError(error));\n * }\n * }\n * ```\n *\n * Output:\n * ```\n * Environment Configuration Failed\n *\n * Missing Variables:\n * DATABASE_URL - Required\n * JWT_SECRET - Required\n *\n * Invalid Values:\n * NODE_ENV = \"invalid\"\n * Expected: \"development\" | \"staging\" | \"production\"\n * ```\n */\nexport function formatParseError(\n\terror: z.ZodError,\n\toptions: FormatOptions = {},\n): string {\n\tconst useColors =\n\t\toptions.colors ?? (process.stdout?.isTTY && process.env.NO_COLOR == null);\n\n\tconst c = useColors\n\t\t? colors\n\t\t: { reset: '', red: '', yellow: '', cyan: '', dim: '', bold: '' };\n\n\tconst missingVars: Array<{ name: string; message: string }> = [];\n\tconst invalidVars: Array<{ name: string; value: unknown; message: string }> =\n\t\t[];\n\n\tfor (const issue of error.issues) {\n\t\t// Extract environment variable name from path or message\n\t\tlet envName = '';\n\t\tif (issue.path.length > 0) {\n\t\t\t// Join the full path with '.' to show nested config keys and env var name\n\t\t\tenvName = issue.path.map(String).join('.');\n\t\t} else {\n\t\t\t// Try to extract from message like 'Environment variable \"NAME\": ...'\n\t\t\tconst match = issue.message.match(/Environment variable \"([^\"]+)\"/);\n\t\t\tif (match?.[1]) {\n\t\t\t\tenvName = match[1];\n\t\t\t}\n\t\t}\n\n\t\t// Determine if this is a missing or invalid value\n\t\t// Use type guard for received property\n\t\tconst received = 'received' in issue ? issue.received : undefined;\n\t\tconst isMissing =\n\t\t\tissue.code === 'invalid_type' &&\n\t\t\t(received === 'undefined' || received === 'null');\n\n\t\tif (isMissing) {\n\t\t\tmissingVars.push({\n\t\t\t\tname: envName || 'Unknown',\n\t\t\t\tmessage: cleanMessage(issue.message),\n\t\t\t});\n\t\t} else {\n\t\t\tinvalidVars.push({\n\t\t\t\tname: envName || 'Unknown',\n\t\t\t\tvalue: received,\n\t\t\t\tmessage: cleanMessage(issue.message),\n\t\t\t});\n\t\t}\n\t}\n\n\tconst lines: string[] = [];\n\n\tlines.push('');\n\tlines.push(`${c.red}${c.bold}Environment Configuration Failed${c.reset}`);\n\tlines.push('');\n\n\tif (missingVars.length > 0) {\n\t\tlines.push(`${c.yellow}Missing Variables:${c.reset}`);\n\t\tfor (const v of missingVars) {\n\t\t\tlines.push(` ${c.cyan}${v.name}${c.reset} ${c.dim}- Required${c.reset}`);\n\t\t}\n\t\tlines.push('');\n\t}\n\n\tif (invalidVars.length > 0) {\n\t\tlines.push(`${c.yellow}Invalid Values:${c.reset}`);\n\t\tfor (const v of invalidVars) {\n\t\t\tconst valueStr =\n\t\t\t\tv.value !== undefined ? ` = ${JSON.stringify(v.value)}` : '';\n\t\t\tlines.push(` ${c.cyan}${v.name}${c.reset}${valueStr}`);\n\t\t\tlines.push(` ${c.dim}${v.message}${c.reset}`);\n\t\t}\n\t\tlines.push('');\n\t}\n\n\treturn lines.join('\\n');\n}\n\n/**\n * Cleans up a Zod error message by removing redundant prefixes.\n */\nfunction cleanMessage(message: string): string {\n\t// Remove \"Environment variable \"NAME\": \" prefix if present\n\treturn message.replace(/^Environment variable \"[^\"]+\": /, '');\n}\n\n/**\n * Checks if the current environment is development.\n */\nexport function isDevelopment(): boolean {\n\tconst nodeEnv = process.env.NODE_ENV?.toLowerCase();\n\treturn nodeEnv == null || nodeEnv === 'development' || nodeEnv === 'dev';\n}\n"],"mappings":";;;;AAaA,MAAM,SAAS;CACd,OAAO;CACP,KAAK;CACL,QAAQ;CACR,MAAM;CACN,KAAK;CACL,MAAM;AACN;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCD,SAAgB,iBACfA,OACAC,UAAyB,CAAE,GAClB;CACT,MAAM,YACL,QAAQ,WAAW,QAAQ,QAAQ,SAAS,QAAQ,IAAI,YAAY;CAEr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BAAuB,KAAK,KAAK,UAAU,EAAE,MAAM,CAAC,IAAI;AAC3D,SAAM,MAAM,IAAI,EAAE,KAAK,EAAE,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE;AACvD,SAAM,MAAM,MAAM,EAAE,IAAI,EAAE,EAAE,QAAQ,EAAE,EAAE,MAAM,EAAE;EAChD;AACD,QAAM,KAAK,GAAG;CACd;AAED,QAAO,MAAM,KAAK,KAAK;AACvB;;;;AAKD,SAAS,aAAaC,SAAyB;AAE9C,QAAO,QAAQ,QAAQ,mCAAmC,GAAG;AAC7D;;;;AAKD,SAAgB,gBAAyB;CACxC,MAAM,UAAU,QAAQ,IAAI,UAAU,aAAa;AACnD,QAAO,WAAW,QAAQ,YAAY,iBAAiB,YAAY;AACnE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatter-Cox0NGxT.d.mts","names":[],"sources":["../src/formatter.ts"],"sourcesContent":[],"mappings":";;;;;;AAKA;AAgDgB,UAhDC,aAAA,CAgDe;EAAA;EAAA,MACtB,CAAA,EAAA,OAAA;;AACkB;
|
|
1
|
+
{"version":3,"file":"formatter-Cox0NGxT.d.mts","names":[],"sources":["../src/formatter.ts"],"sourcesContent":[],"mappings":";;;;;;AAKA;AAgDgB,UAhDC,aAAA,CAgDe;EAAA;EAAA,MACtB,CAAA,EAAA,OAAA;;AACkB;AAuF5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAzFgB,gBAAA,QACR,CAAA,CAAE,oBACA;;;;iBAuFM,aAAA,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatter-D85aIkpd.d.cts","names":[],"sources":["../src/formatter.ts"],"sourcesContent":[],"mappings":";;;;;;AAKA;AAgDgB,UAhDC,aAAA,CAgDe;EAAA;EAAA,MACtB,CAAA,EAAA,OAAA;;AACkB;
|
|
1
|
+
{"version":3,"file":"formatter-D85aIkpd.d.cts","names":[],"sources":["../src/formatter.ts"],"sourcesContent":[],"mappings":";;;;;;AAKA;AAgDgB,UAhDC,aAAA,CAgDe;EAAA;EAAA,MACtB,CAAA,EAAA,OAAA;;AACkB;AAuF5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAzFgB,gBAAA,QACR,CAAA,CAAE,oBACA;;;;iBAuFM,aAAA,CAAA"}
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
-
const zod_v4 = require_chunk.__toESM(require("zod/v4"));
|
|
3
1
|
|
|
4
2
|
//#region src/formatter.ts
|
|
5
3
|
/**
|
|
@@ -58,7 +56,7 @@ function formatParseError(error, options = {}) {
|
|
|
58
56
|
const invalidVars = [];
|
|
59
57
|
for (const issue of error.issues) {
|
|
60
58
|
let envName = "";
|
|
61
|
-
if (issue.path.length > 0) envName =
|
|
59
|
+
if (issue.path.length > 0) envName = issue.path.map(String).join(".");
|
|
62
60
|
else {
|
|
63
61
|
const match = issue.message.match(/Environment variable "([^"]+)"/);
|
|
64
62
|
if (match?.[1]) envName = match[1];
|
|
@@ -122,4 +120,4 @@ Object.defineProperty(exports, 'isDevelopment', {
|
|
|
122
120
|
return isDevelopment;
|
|
123
121
|
}
|
|
124
122
|
});
|
|
125
|
-
//# sourceMappingURL=formatter-
|
|
123
|
+
//# sourceMappingURL=formatter-HxePpSy2.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatter-HxePpSy2.cjs","names":["error: z.ZodError","options: FormatOptions","missingVars: Array<{ name: string; message: string }>","invalidVars: Array<{ name: string; value: unknown; message: string }>","lines: string[]","message: string"],"sources":["../src/formatter.ts"],"sourcesContent":["import type { z } from 'zod/v4';\n\n/**\n * Options for formatting parse errors.\n */\nexport interface FormatOptions {\n\t/** Whether to use colors in output. Defaults to auto-detect TTY. */\n\tcolors?: boolean;\n}\n\n/**\n * ANSI color codes for terminal output.\n */\nconst colors = {\n\treset: '\\x1b[0m',\n\tred: '\\x1b[31m',\n\tyellow: '\\x1b[33m',\n\tcyan: '\\x1b[36m',\n\tdim: '\\x1b[2m',\n\tbold: '\\x1b[1m',\n};\n\n/**\n * Formats a ZodError into a user-friendly string for development.\n *\n * @param error - The ZodError to format\n * @param options - Formatting options\n * @returns Formatted error message\n *\n * @example\n * ```typescript\n * try {\n * config.parse();\n * } catch (error) {\n * if (error instanceof ZodError) {\n * console.error(formatParseError(error));\n * }\n * }\n * ```\n *\n * Output:\n * ```\n * Environment Configuration Failed\n *\n * Missing Variables:\n * DATABASE_URL - Required\n * JWT_SECRET - Required\n *\n * Invalid Values:\n * NODE_ENV = \"invalid\"\n * Expected: \"development\" | \"staging\" | \"production\"\n * ```\n */\nexport function formatParseError(\n\terror: z.ZodError,\n\toptions: FormatOptions = {},\n): string {\n\tconst useColors =\n\t\toptions.colors ?? (process.stdout?.isTTY && process.env.NO_COLOR == null);\n\n\tconst c = useColors\n\t\t? colors\n\t\t: { reset: '', red: '', yellow: '', cyan: '', dim: '', bold: '' };\n\n\tconst missingVars: Array<{ name: string; message: string }> = [];\n\tconst invalidVars: Array<{ name: string; value: unknown; message: string }> =\n\t\t[];\n\n\tfor (const issue of error.issues) {\n\t\t// Extract environment variable name from path or message\n\t\tlet envName = '';\n\t\tif (issue.path.length > 0) {\n\t\t\t// Join the full path with '.' to show nested config keys and env var name\n\t\t\tenvName = issue.path.map(String).join('.');\n\t\t} else {\n\t\t\t// Try to extract from message like 'Environment variable \"NAME\": ...'\n\t\t\tconst match = issue.message.match(/Environment variable \"([^\"]+)\"/);\n\t\t\tif (match?.[1]) {\n\t\t\t\tenvName = match[1];\n\t\t\t}\n\t\t}\n\n\t\t// Determine if this is a missing or invalid value\n\t\t// Use type guard for received property\n\t\tconst received = 'received' in issue ? issue.received : undefined;\n\t\tconst isMissing =\n\t\t\tissue.code === 'invalid_type' &&\n\t\t\t(received === 'undefined' || received === 'null');\n\n\t\tif (isMissing) {\n\t\t\tmissingVars.push({\n\t\t\t\tname: envName || 'Unknown',\n\t\t\t\tmessage: cleanMessage(issue.message),\n\t\t\t});\n\t\t} else {\n\t\t\tinvalidVars.push({\n\t\t\t\tname: envName || 'Unknown',\n\t\t\t\tvalue: received,\n\t\t\t\tmessage: cleanMessage(issue.message),\n\t\t\t});\n\t\t}\n\t}\n\n\tconst lines: string[] = [];\n\n\tlines.push('');\n\tlines.push(`${c.red}${c.bold}Environment Configuration Failed${c.reset}`);\n\tlines.push('');\n\n\tif (missingVars.length > 0) {\n\t\tlines.push(`${c.yellow}Missing Variables:${c.reset}`);\n\t\tfor (const v of missingVars) {\n\t\t\tlines.push(` ${c.cyan}${v.name}${c.reset} ${c.dim}- Required${c.reset}`);\n\t\t}\n\t\tlines.push('');\n\t}\n\n\tif (invalidVars.length > 0) {\n\t\tlines.push(`${c.yellow}Invalid Values:${c.reset}`);\n\t\tfor (const v of invalidVars) {\n\t\t\tconst valueStr =\n\t\t\t\tv.value !== undefined ? ` = ${JSON.stringify(v.value)}` : '';\n\t\t\tlines.push(` ${c.cyan}${v.name}${c.reset}${valueStr}`);\n\t\t\tlines.push(` ${c.dim}${v.message}${c.reset}`);\n\t\t}\n\t\tlines.push('');\n\t}\n\n\treturn lines.join('\\n');\n}\n\n/**\n * Cleans up a Zod error message by removing redundant prefixes.\n */\nfunction cleanMessage(message: string): string {\n\t// Remove \"Environment variable \"NAME\": \" prefix if present\n\treturn message.replace(/^Environment variable \"[^\"]+\": /, '');\n}\n\n/**\n * Checks if the current environment is development.\n */\nexport function isDevelopment(): boolean {\n\tconst nodeEnv = process.env.NODE_ENV?.toLowerCase();\n\treturn nodeEnv == null || nodeEnv === 'development' || nodeEnv === 'dev';\n}\n"],"mappings":";;;;;AAaA,MAAM,SAAS;CACd,OAAO;CACP,KAAK;CACL,QAAQ;CACR,MAAM;CACN,KAAK;CACL,MAAM;AACN;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCD,SAAgB,iBACfA,OACAC,UAAyB,CAAE,GAClB;CACT,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MAAM,EAAE;AAClD,OAAK,MAAM,KAAK,aAAa;GAC5B,MAAM,WACL,EAAE,oBAAuB,KAAK,KAAK,UAAU,EAAE,MAAM,CAAC,IAAI;AAC3D,SAAM,MAAM,IAAI,EAAE,KAAK,EAAE,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE;AACvD,SAAM,MAAM,MAAM,EAAE,IAAI,EAAE,EAAE,QAAQ,EAAE,EAAE,MAAM,EAAE;EAChD;AACD,QAAM,KAAK,GAAG;CACd;AAED,QAAO,MAAM,KAAK,KAAK;AACvB;;;;AAKD,SAAS,aAAaC,SAAyB;AAE9C,QAAO,QAAQ,QAAQ,mCAAmC,GAAG;AAC7D;;;;AAKD,SAAgB,gBAAyB;CACxC,MAAM,UAAU,QAAQ,IAAI,UAAU,aAAa;AACnD,QAAO,WAAW,QAAQ,YAAY,iBAAiB,YAAY;AACnE"}
|
package/dist/formatter.cjs
CHANGED
package/dist/formatter.mjs
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const require_EnvironmentBuilder = require('./EnvironmentBuilder-Djr1VsWM.cjs');
|
|
2
|
-
const require_formatter = require('./formatter-
|
|
3
|
-
const require_EnvironmentParser = require('./EnvironmentParser-
|
|
2
|
+
const require_formatter = require('./formatter-HxePpSy2.cjs');
|
|
3
|
+
const require_EnvironmentParser = require('./EnvironmentParser-DJdW7vOL.cjs');
|
|
4
4
|
|
|
5
5
|
exports.ConfigParser = require_EnvironmentParser.ConfigParser;
|
|
6
6
|
exports.EnvironmentBuilder = require_EnvironmentBuilder.EnvironmentBuilder;
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { EnvironmentBuilder, environmentCase } from "./EnvironmentBuilder-BSuHZm0y.mjs";
|
|
2
|
-
import { formatParseError, isDevelopment } from "./formatter-
|
|
3
|
-
import { ConfigParser, EnvironmentParser } from "./EnvironmentParser-
|
|
2
|
+
import { formatParseError, isDevelopment } from "./formatter-BRRrxQi3.mjs";
|
|
3
|
+
import { ConfigParser, EnvironmentParser } from "./EnvironmentParser-zMblItla.mjs";
|
|
4
4
|
|
|
5
5
|
export { ConfigParser, EnvironmentBuilder, EnvironmentParser, environmentCase, formatParseError, isDevelopment };
|
package/package.json
CHANGED
|
@@ -207,3 +207,100 @@ class SnifferConfigParser<
|
|
|
207
207
|
return '';
|
|
208
208
|
}
|
|
209
209
|
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Result of sniffing with fire-and-forget handling.
|
|
213
|
+
*/
|
|
214
|
+
export interface SniffResult {
|
|
215
|
+
/** Environment variables that were accessed during sniffing */
|
|
216
|
+
envVars: string[];
|
|
217
|
+
/** Error thrown during sniffing (env vars may still be captured) */
|
|
218
|
+
error?: Error;
|
|
219
|
+
/** Unhandled promise rejections captured during sniffing */
|
|
220
|
+
unhandledRejections: Error[];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Options for sniffing with fire-and-forget handling.
|
|
225
|
+
*/
|
|
226
|
+
export interface SniffOptions {
|
|
227
|
+
/**
|
|
228
|
+
* Time in milliseconds to wait for fire-and-forget promises to settle.
|
|
229
|
+
* Some libraries like better-auth create async operations that may reject
|
|
230
|
+
* after the initial event loop tick.
|
|
231
|
+
* @default 100
|
|
232
|
+
*/
|
|
233
|
+
settleTimeMs?: number;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Executes a sniffing operation with fire-and-forget handling.
|
|
238
|
+
*
|
|
239
|
+
* This function:
|
|
240
|
+
* 1. Captures unhandled promise rejections during the operation
|
|
241
|
+
* 2. Waits for async operations to settle before returning
|
|
242
|
+
* 3. Gracefully handles errors without throwing
|
|
243
|
+
*
|
|
244
|
+
* Use this when sniffing environment variables from code that may:
|
|
245
|
+
* - Throw synchronous errors
|
|
246
|
+
* - Create fire-and-forget promises that reject
|
|
247
|
+
* - Have async initialization that may fail
|
|
248
|
+
*
|
|
249
|
+
* @param sniffer - The SnifferEnvironmentParser instance to use
|
|
250
|
+
* @param operation - The async operation to execute (e.g., service.register)
|
|
251
|
+
* @param options - Optional configuration
|
|
252
|
+
* @returns SniffResult with env vars and any errors encountered
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```typescript
|
|
256
|
+
* const sniffer = new SnifferEnvironmentParser();
|
|
257
|
+
* const result = await sniffWithFireAndForget(sniffer, async () => {
|
|
258
|
+
* await service.register({ envParser: sniffer });
|
|
259
|
+
* });
|
|
260
|
+
* console.log('Env vars:', result.envVars);
|
|
261
|
+
* console.log('Error:', result.error);
|
|
262
|
+
* console.log('Unhandled rejections:', result.unhandledRejections);
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
export async function sniffWithFireAndForget(
|
|
266
|
+
sniffer: SnifferEnvironmentParser,
|
|
267
|
+
operation: () => unknown | Promise<unknown>,
|
|
268
|
+
options: SniffOptions = {},
|
|
269
|
+
): Promise<SniffResult> {
|
|
270
|
+
const { settleTimeMs = 100 } = options;
|
|
271
|
+
const unhandledRejections: Error[] = [];
|
|
272
|
+
|
|
273
|
+
// Capture unhandled rejections during sniffing (fire-and-forget promises)
|
|
274
|
+
// Libraries like better-auth create async operations that may reject after
|
|
275
|
+
// the initial event loop tick
|
|
276
|
+
const captureRejection = (reason: unknown) => {
|
|
277
|
+
const err = reason instanceof Error ? reason : new Error(String(reason));
|
|
278
|
+
unhandledRejections.push(err);
|
|
279
|
+
};
|
|
280
|
+
process.on('unhandledRejection', captureRejection);
|
|
281
|
+
|
|
282
|
+
let error: Error | undefined;
|
|
283
|
+
|
|
284
|
+
try {
|
|
285
|
+
const result = operation();
|
|
286
|
+
|
|
287
|
+
// Handle async result
|
|
288
|
+
if (result && typeof result === 'object' && 'then' in result) {
|
|
289
|
+
await Promise.resolve(result).catch((e) => {
|
|
290
|
+
error = e instanceof Error ? e : new Error(String(e));
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
} catch (e) {
|
|
294
|
+
error = e instanceof Error ? e : new Error(String(e));
|
|
295
|
+
} finally {
|
|
296
|
+
// Wait for fire-and-forget promises to settle
|
|
297
|
+
await new Promise((resolve) => setTimeout(resolve, settleTimeMs));
|
|
298
|
+
process.off('unhandledRejection', captureRejection);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return {
|
|
302
|
+
envVars: sniffer.getEnvironmentVariables(),
|
|
303
|
+
error,
|
|
304
|
+
unhandledRejections,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest';
|
|
2
2
|
import { z } from 'zod/v4';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
SnifferEnvironmentParser,
|
|
5
|
+
sniffWithFireAndForget,
|
|
6
|
+
} from '../SnifferEnvironmentParser';
|
|
4
7
|
|
|
5
8
|
describe('SnifferEnvironmentParser', () => {
|
|
6
9
|
describe('Environment variable tracking', () => {
|
|
@@ -384,3 +387,104 @@ describe('SnifferEnvironmentParser', () => {
|
|
|
384
387
|
});
|
|
385
388
|
});
|
|
386
389
|
});
|
|
390
|
+
|
|
391
|
+
describe('sniffWithFireAndForget', () => {
|
|
392
|
+
it('should capture environment variables from synchronous operations', async () => {
|
|
393
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
394
|
+
|
|
395
|
+
const result = await sniffWithFireAndForget(sniffer, () => {
|
|
396
|
+
sniffer.create((get) => ({
|
|
397
|
+
dbUrl: get('DATABASE_URL').string(),
|
|
398
|
+
apiKey: get('API_KEY').string(),
|
|
399
|
+
}));
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
expect(result.envVars).toEqual(['API_KEY', 'DATABASE_URL']);
|
|
403
|
+
expect(result.error).toBeUndefined();
|
|
404
|
+
expect(result.unhandledRejections).toEqual([]);
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it('should capture environment variables from async operations', async () => {
|
|
408
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
409
|
+
|
|
410
|
+
const result = await sniffWithFireAndForget(sniffer, async () => {
|
|
411
|
+
await Promise.resolve();
|
|
412
|
+
sniffer.create((get) => ({
|
|
413
|
+
redisUrl: get('REDIS_URL').string(),
|
|
414
|
+
}));
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
expect(result.envVars).toEqual(['REDIS_URL']);
|
|
418
|
+
expect(result.error).toBeUndefined();
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
it('should capture error when operation throws synchronously', async () => {
|
|
422
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
423
|
+
|
|
424
|
+
const result = await sniffWithFireAndForget(sniffer, () => {
|
|
425
|
+
sniffer.create((get) => ({
|
|
426
|
+
value: get('CAPTURED_BEFORE_ERROR').string(),
|
|
427
|
+
}));
|
|
428
|
+
throw new Error('Sync error');
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
// Should still capture env vars accessed before the error
|
|
432
|
+
expect(result.envVars).toEqual(['CAPTURED_BEFORE_ERROR']);
|
|
433
|
+
expect(result.error).toBeInstanceOf(Error);
|
|
434
|
+
expect(result.error?.message).toBe('Sync error');
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
it('should capture error when async operation rejects', async () => {
|
|
438
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
439
|
+
|
|
440
|
+
const result = await sniffWithFireAndForget(sniffer, async () => {
|
|
441
|
+
sniffer.create((get) => ({
|
|
442
|
+
value: get('CAPTURED_BEFORE_REJECT').string(),
|
|
443
|
+
}));
|
|
444
|
+
throw new Error('Async rejection');
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
expect(result.envVars).toEqual(['CAPTURED_BEFORE_REJECT']);
|
|
448
|
+
expect(result.error).toBeInstanceOf(Error);
|
|
449
|
+
expect(result.error?.message).toBe('Async rejection');
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
it('should convert non-Error throws to Error objects', async () => {
|
|
453
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
454
|
+
|
|
455
|
+
const result = await sniffWithFireAndForget(sniffer, () => {
|
|
456
|
+
throw 'string error';
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
expect(result.error).toBeInstanceOf(Error);
|
|
460
|
+
expect(result.error?.message).toBe('string error');
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
it('should use custom settle time', async () => {
|
|
464
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
465
|
+
const startTime = Date.now();
|
|
466
|
+
|
|
467
|
+
await sniffWithFireAndForget(
|
|
468
|
+
sniffer,
|
|
469
|
+
() => {},
|
|
470
|
+
{ settleTimeMs: 50 },
|
|
471
|
+
);
|
|
472
|
+
|
|
473
|
+
const elapsed = Date.now() - startTime;
|
|
474
|
+
// Should wait at least 50ms but not much longer
|
|
475
|
+
expect(elapsed).toBeGreaterThanOrEqual(50);
|
|
476
|
+
expect(elapsed).toBeLessThan(200);
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
it('should return empty arrays when no vars accessed and no errors', async () => {
|
|
480
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
481
|
+
|
|
482
|
+
const result = await sniffWithFireAndForget(sniffer, () => {
|
|
483
|
+
// Do nothing
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
expect(result.envVars).toEqual([]);
|
|
487
|
+
expect(result.error).toBeUndefined();
|
|
488
|
+
expect(result.unhandledRejections).toEqual([]);
|
|
489
|
+
});
|
|
490
|
+
});
|
|
@@ -102,6 +102,23 @@ describe('formatParseError', () => {
|
|
|
102
102
|
expect(formatted).toContain('Expected number, received string');
|
|
103
103
|
});
|
|
104
104
|
|
|
105
|
+
it('should join nested paths with dot', () => {
|
|
106
|
+
const error = new z.ZodError([
|
|
107
|
+
createIssue({
|
|
108
|
+
code: 'invalid_type',
|
|
109
|
+
expected: 'string',
|
|
110
|
+
received: 'undefined',
|
|
111
|
+
path: ['databaseUrl', 'DATABASE_URL'],
|
|
112
|
+
message: 'Environment variable "DATABASE_URL": Required',
|
|
113
|
+
}),
|
|
114
|
+
]);
|
|
115
|
+
|
|
116
|
+
const formatted = formatParseError(error, { colors: false });
|
|
117
|
+
|
|
118
|
+
// Should show the full path joined with '.'
|
|
119
|
+
expect(formatted).toContain('databaseUrl.DATABASE_URL');
|
|
120
|
+
});
|
|
121
|
+
|
|
105
122
|
it('should extract env name from message when path is empty', () => {
|
|
106
123
|
const error = new z.ZodError([
|
|
107
124
|
createIssue({
|
package/src/formatter.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { z } from 'zod/v4';
|
|
1
|
+
import type { z } from 'zod/v4';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Options for formatting parse errors.
|
|
@@ -70,7 +70,8 @@ export function formatParseError(
|
|
|
70
70
|
// Extract environment variable name from path or message
|
|
71
71
|
let envName = '';
|
|
72
72
|
if (issue.path.length > 0) {
|
|
73
|
-
|
|
73
|
+
// Join the full path with '.' to show nested config keys and env var name
|
|
74
|
+
envName = issue.path.map(String).join('.');
|
|
74
75
|
} else {
|
|
75
76
|
// Try to extract from message like 'Environment variable "NAME": ...'
|
|
76
77
|
const match = issue.message.match(/Environment variable "([^"]+)"/);
|
|
@@ -103,9 +104,7 @@ export function formatParseError(
|
|
|
103
104
|
const lines: string[] = [];
|
|
104
105
|
|
|
105
106
|
lines.push('');
|
|
106
|
-
lines.push(
|
|
107
|
-
`${c.red}${c.bold}Environment Configuration Failed${c.reset}`,
|
|
108
|
-
);
|
|
107
|
+
lines.push(`${c.red}${c.bold}Environment Configuration Failed${c.reset}`);
|
|
109
108
|
lines.push('');
|
|
110
109
|
|
|
111
110
|
if (missingVars.length > 0) {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"formatter-fz8V7x6i.mjs","names":["error: z.ZodError","options: FormatOptions","missingVars: Array<{ name: string; message: string }>","invalidVars: Array<{ name: string; value: unknown; message: string }>","lines: string[]","message: string"],"sources":["../src/formatter.ts"],"sourcesContent":["import { z } from 'zod/v4';\n\n/**\n * Options for formatting parse errors.\n */\nexport interface FormatOptions {\n\t/** Whether to use colors in output. Defaults to auto-detect TTY. */\n\tcolors?: boolean;\n}\n\n/**\n * ANSI color codes for terminal output.\n */\nconst colors = {\n\treset: '\\x1b[0m',\n\tred: '\\x1b[31m',\n\tyellow: '\\x1b[33m',\n\tcyan: '\\x1b[36m',\n\tdim: '\\x1b[2m',\n\tbold: '\\x1b[1m',\n};\n\n/**\n * Formats a ZodError into a user-friendly string for development.\n *\n * @param error - The ZodError to format\n * @param options - Formatting options\n * @returns Formatted error message\n *\n * @example\n * ```typescript\n * try {\n * config.parse();\n * } catch (error) {\n * if (error instanceof ZodError) {\n * console.error(formatParseError(error));\n * }\n * }\n * ```\n *\n * Output:\n * ```\n * Environment Configuration Failed\n *\n * Missing Variables:\n * DATABASE_URL - Required\n * JWT_SECRET - Required\n *\n * Invalid Values:\n * NODE_ENV = \"invalid\"\n * Expected: \"development\" | \"staging\" | \"production\"\n * ```\n */\nexport function formatParseError(\n\terror: z.ZodError,\n\toptions: FormatOptions = {},\n): string {\n\tconst useColors =\n\t\toptions.colors ?? (process.stdout?.isTTY && process.env.NO_COLOR == null);\n\n\tconst c = useColors\n\t\t? colors\n\t\t: { reset: '', red: '', yellow: '', cyan: '', dim: '', bold: '' };\n\n\tconst missingVars: Array<{ name: string; message: string }> = [];\n\tconst invalidVars: Array<{ name: string; value: unknown; message: string }> =\n\t\t[];\n\n\tfor (const issue of error.issues) {\n\t\t// Extract environment variable name from path or message\n\t\tlet envName = '';\n\t\tif (issue.path.length > 0) {\n\t\t\tenvName = String(issue.path[0]);\n\t\t} else {\n\t\t\t// Try to extract from message like 'Environment variable \"NAME\": ...'\n\t\t\tconst match = issue.message.match(/Environment variable \"([^\"]+)\"/);\n\t\t\tif (match?.[1]) {\n\t\t\t\tenvName = match[1];\n\t\t\t}\n\t\t}\n\n\t\t// Determine if this is a missing or invalid value\n\t\t// Use type guard for received property\n\t\tconst received = 'received' in issue ? issue.received : undefined;\n\t\tconst isMissing =\n\t\t\tissue.code === 'invalid_type' &&\n\t\t\t(received === 'undefined' || received === 'null');\n\n\t\tif (isMissing) {\n\t\t\tmissingVars.push({\n\t\t\t\tname: envName || 'Unknown',\n\t\t\t\tmessage: cleanMessage(issue.message),\n\t\t\t});\n\t\t} else {\n\t\t\tinvalidVars.push({\n\t\t\t\tname: envName || 'Unknown',\n\t\t\t\tvalue: received,\n\t\t\t\tmessage: cleanMessage(issue.message),\n\t\t\t});\n\t\t}\n\t}\n\n\tconst lines: string[] = [];\n\n\tlines.push('');\n\tlines.push(\n\t\t`${c.red}${c.bold}Environment Configuration Failed${c.reset}`,\n\t);\n\tlines.push('');\n\n\tif (missingVars.length > 0) {\n\t\tlines.push(`${c.yellow}Missing Variables:${c.reset}`);\n\t\tfor (const v of missingVars) {\n\t\t\tlines.push(` ${c.cyan}${v.name}${c.reset} ${c.dim}- Required${c.reset}`);\n\t\t}\n\t\tlines.push('');\n\t}\n\n\tif (invalidVars.length > 0) {\n\t\tlines.push(`${c.yellow}Invalid Values:${c.reset}`);\n\t\tfor (const v of invalidVars) {\n\t\t\tconst valueStr =\n\t\t\t\tv.value !== undefined ? ` = ${JSON.stringify(v.value)}` : '';\n\t\t\tlines.push(` ${c.cyan}${v.name}${c.reset}${valueStr}`);\n\t\t\tlines.push(` ${c.dim}${v.message}${c.reset}`);\n\t\t}\n\t\tlines.push('');\n\t}\n\n\treturn lines.join('\\n');\n}\n\n/**\n * Cleans up a Zod error message by removing redundant prefixes.\n */\nfunction cleanMessage(message: string): string {\n\t// Remove \"Environment variable \"NAME\": \" prefix if present\n\treturn message.replace(/^Environment variable \"[^\"]+\": /, '');\n}\n\n/**\n * Checks if the current environment is development.\n */\nexport function isDevelopment(): boolean {\n\tconst nodeEnv = process.env.NODE_ENV?.toLowerCase();\n\treturn nodeEnv == null || nodeEnv === 'development' || nodeEnv === 'dev';\n}\n"],"m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AAE,EAAE,MAAM,GAAG,EAAE,IAAI,YAAY,EAAE,MAAM,EAAE;AAE1E,QAAM,KAAK,GAAG;CACd;AAED,KAAI,YAAY,SAAS,GAAG;AAC3B,QAAM,MAAM,EAAE,EAAE,OAAO,iBAAiB,EAAE,MAAM,EAAE;AAClD,OAAK,MAAM,KAAK,aAAa;GAC5B,MAAM,WACL,EAAE,oBAAuB,KAAK,KAAK,UAAU,EAAE,MAAM,CAAC,IAAI;AAC3D,SAAM,MAAM,IAAI,EAAE,KAAK,EAAE,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE;AACvD,SAAM,MAAM,MAAM,EAAE,IAAI,EAAE,EAAE,QAAQ,EAAE,EAAE,MAAM,EAAE;EAChD;AACD,QAAM,KAAK,GAAG;CACd;AAED,QAAO,MAAM,KAAK,KAAK;AACvB;;;;AAKD,SAAS,aAAaC,SAAyB;AAE9C,QAAO,QAAQ,QAAQ,mCAAmC,GAAG;AAC7D;;;;AAKD,SAAgB,gBAAyB;CACxC,MAAM,UAAU,QAAQ,IAAI,UAAU,aAAa;AACnD,QAAO,WAAW,QAAQ,YAAY,iBAAiB,YAAY;AACnE"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"formatter-w8Tsccw4.cjs","names":["error: z.ZodError","options: FormatOptions","missingVars: Array<{ name: string; message: string }>","invalidVars: Array<{ name: string; value: unknown; message: string }>","lines: string[]","message: string"],"sources":["../src/formatter.ts"],"sourcesContent":["import { z } from 'zod/v4';\n\n/**\n * Options for formatting parse errors.\n */\nexport interface FormatOptions {\n\t/** Whether to use colors in output. Defaults to auto-detect TTY. */\n\tcolors?: boolean;\n}\n\n/**\n * ANSI color codes for terminal output.\n */\nconst colors = {\n\treset: '\\x1b[0m',\n\tred: '\\x1b[31m',\n\tyellow: '\\x1b[33m',\n\tcyan: '\\x1b[36m',\n\tdim: '\\x1b[2m',\n\tbold: '\\x1b[1m',\n};\n\n/**\n * Formats a ZodError into a user-friendly string for development.\n *\n * @param error - The ZodError to format\n * @param options - Formatting options\n * @returns Formatted error message\n *\n * @example\n * ```typescript\n * try {\n * config.parse();\n * } catch (error) {\n * if (error instanceof ZodError) {\n * console.error(formatParseError(error));\n * }\n * }\n * ```\n *\n * Output:\n * ```\n * Environment Configuration Failed\n *\n * Missing Variables:\n * DATABASE_URL - Required\n * JWT_SECRET - Required\n *\n * Invalid Values:\n * NODE_ENV = \"invalid\"\n * Expected: \"development\" | \"staging\" | \"production\"\n * ```\n */\nexport function formatParseError(\n\terror: z.ZodError,\n\toptions: FormatOptions = {},\n): string {\n\tconst useColors =\n\t\toptions.colors ?? (process.stdout?.isTTY && process.env.NO_COLOR == null);\n\n\tconst c = useColors\n\t\t? colors\n\t\t: { reset: '', red: '', yellow: '', cyan: '', dim: '', bold: '' };\n\n\tconst missingVars: Array<{ name: string; message: string }> = [];\n\tconst invalidVars: Array<{ name: string; value: unknown; message: string }> =\n\t\t[];\n\n\tfor (const issue of error.issues) {\n\t\t// Extract environment variable name from path or message\n\t\tlet envName = '';\n\t\tif (issue.path.length > 0) {\n\t\t\tenvName = String(issue.path[0]);\n\t\t} else {\n\t\t\t// Try to extract from message like 'Environment variable \"NAME\": ...'\n\t\t\tconst match = issue.message.match(/Environment variable \"([^\"]+)\"/);\n\t\t\tif (match?.[1]) {\n\t\t\t\tenvName = match[1];\n\t\t\t}\n\t\t}\n\n\t\t// Determine if this is a missing or invalid value\n\t\t// Use type guard for received property\n\t\tconst received = 'received' in issue ? issue.received : undefined;\n\t\tconst isMissing =\n\t\t\tissue.code === 'invalid_type' &&\n\t\t\t(received === 'undefined' || received === 'null');\n\n\t\tif (isMissing) {\n\t\t\tmissingVars.push({\n\t\t\t\tname: envName || 'Unknown',\n\t\t\t\tmessage: cleanMessage(issue.message),\n\t\t\t});\n\t\t} else {\n\t\t\tinvalidVars.push({\n\t\t\t\tname: envName || 'Unknown',\n\t\t\t\tvalue: received,\n\t\t\t\tmessage: cleanMessage(issue.message),\n\t\t\t});\n\t\t}\n\t}\n\n\tconst lines: string[] = [];\n\n\tlines.push('');\n\tlines.push(\n\t\t`${c.red}${c.bold}Environment Configuration Failed${c.reset}`,\n\t);\n\tlines.push('');\n\n\tif (missingVars.length > 0) {\n\t\tlines.push(`${c.yellow}Missing Variables:${c.reset}`);\n\t\tfor (const v of missingVars) {\n\t\t\tlines.push(` ${c.cyan}${v.name}${c.reset} ${c.dim}- Required${c.reset}`);\n\t\t}\n\t\tlines.push('');\n\t}\n\n\tif (invalidVars.length > 0) {\n\t\tlines.push(`${c.yellow}Invalid Values:${c.reset}`);\n\t\tfor (const v of invalidVars) {\n\t\t\tconst valueStr =\n\t\t\t\tv.value !== undefined ? ` = ${JSON.stringify(v.value)}` : '';\n\t\t\tlines.push(` ${c.cyan}${v.name}${c.reset}${valueStr}`);\n\t\t\tlines.push(` ${c.dim}${v.message}${c.reset}`);\n\t\t}\n\t\tlines.push('');\n\t}\n\n\treturn lines.join('\\n');\n}\n\n/**\n * Cleans up a Zod error message by removing redundant prefixes.\n */\nfunction cleanMessage(message: string): string {\n\t// Remove \"Environment variable \"NAME\": \" prefix if present\n\treturn message.replace(/^Environment variable \"[^\"]+\": /, '');\n}\n\n/**\n * Checks if the current environment is development.\n */\nexport function isDevelopment(): boolean {\n\tconst nodeEnv = process.env.NODE_ENV?.toLowerCase();\n\treturn nodeEnv == null || nodeEnv === 'development' || nodeEnv === 'dev';\n}\n"],"mappings":";;;;;;;AAaA,MAAM,SAAS;CACd,OAAO;CACP,KAAK;CACL,QAAQ;CACR,MAAM;CACN,KAAK;CACL,MAAM;AACN;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCD,SAAgB,iBACfA,OACAC,UAAyB,CAAE,GAClB;CACT,MAAM,YACL,QAAQ,WAAW,QAAQ,QAAQ,SAAS,QAAQ,IAAI,YAAY;CAErE,MAAM,IAAI,YACP,SACA;EAAE,OAAO;EAAI,KAAK;EAAI,QAAQ;EAAI,MAAM;EAAI,KAAK;E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