@ac-kit/app-config 0.2.1 → 0.3.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/CHANGELOG.md CHANGED
@@ -1,5 +1,31 @@
1
1
  # @ac-kit/app-config
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 20341eb: Environment variables read through a `zod/mini` schema, without ever quoting a
8
+ value in an error (`zod` is now a dependency):
9
+
10
+ - `parseEnvironment(schema, environment, { emptyAsUnset? })`: the variables
11
+ the object schema names, an empty one read as unset by default, checked
12
+ together; `EnvironmentError` lists every issue as `{ variable, path,
13
+ message }`.
14
+ - `parsedVariable(parse)`: a variable `parse` turns into a value, an
15
+ `InvalidValueError` it throws becoming the issue.
16
+ - `jsonVariable(schema)`: JSON checked by `schema`, each issue at its path;
17
+ text that is not JSON is `is not valid JSON`.
18
+ - `integerVariable({ minimum?, maximum? })`: a decimal integer within the
19
+ range its issue states.
20
+ - `composeIssuePath(path)`: an issue's path written as `.key[index]`.
21
+
22
+ ### Patch Changes
23
+
24
+ - Updated dependencies [20341eb]
25
+ - @ac-kit/core@0.9.0
26
+ - @ac-kit/node@0.8.1
27
+ - @ac-kit/format-editorconfig@0.1.8
28
+
3
29
  ## 0.2.1
4
30
 
5
31
  ### Patch Changes
package/README.md CHANGED
@@ -26,6 +26,56 @@ Also exposes `resolveExtendableConfig` and `resolveExtendsValue` for resolving
26
26
  an `extends` chain you already loaded yourself, and `editorconfig` helpers for
27
27
  reading `.editorconfig` settings.
28
28
 
29
+ ## Environment variables
30
+
31
+ A service configured through its environment reads it once, at start, through
32
+ a `zod/mini` schema: `parseEnvironment` reads the variables the schema names,
33
+ an empty one as unset, and throws an `EnvironmentError` listing every issue —
34
+ each naming its variable and where in its value, and none quoting a value, so
35
+ the error can be logged even when a variable holds a secret.
36
+
37
+ ```typescript
38
+ import {
39
+ integerVariable,
40
+ InvalidValueError,
41
+ jsonVariable,
42
+ parsedVariable,
43
+ parseEnvironment,
44
+ } from "@ac-kit/app-config";
45
+ import * as z from "zod/mini";
46
+
47
+ const config = parseEnvironment(
48
+ z.object({
49
+ PORT: z.prefault(integerVariable({ minimum: 1, maximum: 65_535 }), "8080"),
50
+ PUBLIC_URL: parsedVariable((value) => {
51
+ if (!URL.canParse(value)) {
52
+ throw new InvalidValueError("is not a URL");
53
+ }
54
+ return new URL(value);
55
+ }),
56
+ PROVIDERS: z.prefault(
57
+ jsonVariable(z.array(z.object({ id: z.string(), issuer: z.url() }))),
58
+ "[]",
59
+ ),
60
+ }),
61
+ process.env,
62
+ );
63
+ ```
64
+
65
+ - `parsedVariable(parse)` — a value with its own syntax; what `parse` throws as
66
+ an `InvalidValueError` becomes the issue, its message used as it is, so it
67
+ must not quote the value either.
68
+ - `jsonVariable(schema)` — JSON checked by a schema, each of its issues at its
69
+ path within the value (`PROVIDERS[0].issuer`); text that is not JSON is
70
+ reported without `JSON.parse`'s message, which quotes it.
71
+ - `integerVariable({ minimum?, maximum? })` — decimal digits and an optional
72
+ `-`, within the range, which its issue states.
73
+ - `composeIssuePath(path)` — a schema issue's path as `.key[index]`.
74
+
75
+ Any other schema of a string works too (`z.enum(["on", "off"])`); an unset
76
+ variable is `is required` unless its schema is wrapped in `z.optional` or
77
+ `z.prefault`.
78
+
29
79
  Node only: it reads the filesystem.
30
80
 
31
81
  See the
@@ -0,0 +1,62 @@
1
+ import type { Callable } from "@ac-kit/core";
2
+ import * as z from "zod/mini";
3
+ /**
4
+ * A variable's value refused by a {@link parsedVariable} parser. Its message
5
+ * becomes the variable's issue as it is, so it must not quote the value, which
6
+ * may be a secret.
7
+ */
8
+ export declare class InvalidValueError extends Error {
9
+ constructor(message: string);
10
+ }
11
+ /**
12
+ * The schema of a variable whose text `parse` turns into a value: what it
13
+ * throws as an {@link InvalidValueError} becomes the variable's issue, and any
14
+ * other error goes through. An unset variable's issue is `is required`; wrap
15
+ * the schema in `z.optional` or `z.prefault` for a variable that may be
16
+ * absent.
17
+ *
18
+ * Reach for it for a value with its own syntax (a URL, an address, a list);
19
+ * {@link jsonVariable} and {@link integerVariable} cover JSON and integers.
20
+ *
21
+ * @example
22
+ * const urlVariable = parsedVariable((value) => {
23
+ * if (!URL.canParse(value)) {
24
+ * throw new InvalidValueError("is not a URL");
25
+ * }
26
+ * return new URL(value);
27
+ * });
28
+ */
29
+ export declare function parsedVariable<Output>(parse: Callable<[value: string], Output>): z.ZodMiniPipe<z.ZodMiniString<string>, z.ZodMiniTransform<Awaited<Output>, string>>;
30
+ /**
31
+ * The schema of a variable holding JSON checked by `schema`: text that is not
32
+ * JSON is `is not valid JSON`, never quoting it as `JSON.parse`'s message
33
+ * would, and each of `schema`'s issues keeps its path within the value. Linear
34
+ * in the text's length, then `schema`'s own cost.
35
+ *
36
+ * @example
37
+ * const providers = jsonVariable(
38
+ * z.array(z.object({ id: z.string(), issuer: z.url() })),
39
+ * );
40
+ */
41
+ export declare function jsonVariable<Schema extends z.ZodMiniType>(schema: Schema): z.ZodMiniPipe<z.ZodMiniString<string>, z.ZodMiniTransform<Awaited<z.output<Schema>>, string>>;
42
+ /** The bounds of an {@link integerVariable}, both included. */
43
+ export type IntegerVariableRange = {
44
+ /** Default: `Number.MIN_SAFE_INTEGER`. */
45
+ readonly minimum?: number;
46
+ /** Default: `Number.MAX_SAFE_INTEGER`. */
47
+ readonly maximum?: number;
48
+ };
49
+ /**
50
+ * The schema of a variable holding an integer in decimal digits, with a `-` for
51
+ * a negative one — no `+`, exponent, fraction, separator or blank — from
52
+ * `range.minimum` to `range.maximum`. Its issue states the range, never the
53
+ * value: `must be a whole number from 1 to 65535`.
54
+ *
55
+ * @example
56
+ * const port = integerVariable({ minimum: 1, maximum: 65_535 });
57
+ *
58
+ * @throws {RangeError} When a bound is not a safe integer, or the minimum is
59
+ * above the maximum.
60
+ */
61
+ export declare function integerVariable(range?: IntegerVariableRange): z.ZodMiniPipe<z.ZodMiniString<string>, z.ZodMiniTransform<number, string>>;
62
+ //# sourceMappingURL=environment-variable.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"environment-variable.d.ts","sourceRoot":"","sources":["../../src/environment/environment-variable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,KAAK,CAAC,MAAM,UAAU,CAAC;AAE9B;;;;GAIG;AACH,qBAAa,iBAAkB,SAAQ,KAAK;IAC3C,YAAY,OAAO,EAAE,MAAM,EAG1B;CACD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,cAAc,CAAC,MAAM,EACpC,KAAK,EAAE,QAAQ,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,uFAoBxC;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,MAAM,SAAS,CAAC,CAAC,WAAW,EAAE,MAAM,EAAE,MAAM,iGA8BxE;AAED,+DAA+D;AAC/D,MAAM,MAAM,oBAAoB,GAAG;IAClC,0CAA0C;IAC1C,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,0CAA0C;IAC1C,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAIF;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAAC,KAAK,CAAC,EAAE,oBAAoB,8EAoB3D"}
@@ -0,0 +1,134 @@
1
+ import * as z from "zod/mini";
2
+ /**
3
+ * A variable's value refused by a {@link parsedVariable} parser. Its message
4
+ * becomes the variable's issue as it is, so it must not quote the value, which
5
+ * may be a secret.
6
+ */
7
+ export class InvalidValueError extends Error {
8
+ constructor(message) {
9
+ super(message);
10
+ this.name = "InvalidValueError";
11
+ }
12
+ }
13
+ /**
14
+ * The schema of a variable whose text `parse` turns into a value: what it
15
+ * throws as an {@link InvalidValueError} becomes the variable's issue, and any
16
+ * other error goes through. An unset variable's issue is `is required`; wrap
17
+ * the schema in `z.optional` or `z.prefault` for a variable that may be
18
+ * absent.
19
+ *
20
+ * Reach for it for a value with its own syntax (a URL, an address, a list);
21
+ * {@link jsonVariable} and {@link integerVariable} cover JSON and integers.
22
+ *
23
+ * @example
24
+ * const urlVariable = parsedVariable((value) => {
25
+ * if (!URL.canParse(value)) {
26
+ * throw new InvalidValueError("is not a URL");
27
+ * }
28
+ * return new URL(value);
29
+ * });
30
+ */
31
+ export function parsedVariable(parse) {
32
+ return z.pipe(z.string("is required"), z.transform((value, context) => {
33
+ try {
34
+ return parse(value);
35
+ }
36
+ catch (error) {
37
+ if (!(error instanceof InvalidValueError)) {
38
+ throw error;
39
+ }
40
+ context.issues.push({
41
+ code: "custom",
42
+ message: error.message,
43
+ input: value,
44
+ });
45
+ return z.NEVER;
46
+ }
47
+ }));
48
+ }
49
+ /**
50
+ * The schema of a variable holding JSON checked by `schema`: text that is not
51
+ * JSON is `is not valid JSON`, never quoting it as `JSON.parse`'s message
52
+ * would, and each of `schema`'s issues keeps its path within the value. Linear
53
+ * in the text's length, then `schema`'s own cost.
54
+ *
55
+ * @example
56
+ * const providers = jsonVariable(
57
+ * z.array(z.object({ id: z.string(), issuer: z.url() })),
58
+ * );
59
+ */
60
+ export function jsonVariable(schema) {
61
+ return z.pipe(z.string("is required"), z.transform((value, context) => {
62
+ let json;
63
+ try {
64
+ json = JSON.parse(value);
65
+ }
66
+ catch {
67
+ context.issues.push({
68
+ code: "custom",
69
+ message: "is not valid JSON",
70
+ input: value,
71
+ });
72
+ return z.NEVER;
73
+ }
74
+ const result = z.safeParse(schema, json);
75
+ if (!result.success) {
76
+ for (const issue of result.error.issues) {
77
+ context.issues.push({
78
+ code: "custom",
79
+ message: issue.message,
80
+ path: issue.path,
81
+ input: value,
82
+ });
83
+ }
84
+ return z.NEVER;
85
+ }
86
+ return result.data;
87
+ }));
88
+ }
89
+ const INTEGER_ = /^-?\d+$/;
90
+ /**
91
+ * The schema of a variable holding an integer in decimal digits, with a `-` for
92
+ * a negative one — no `+`, exponent, fraction, separator or blank — from
93
+ * `range.minimum` to `range.maximum`. Its issue states the range, never the
94
+ * value: `must be a whole number from 1 to 65535`.
95
+ *
96
+ * @example
97
+ * const port = integerVariable({ minimum: 1, maximum: 65_535 });
98
+ *
99
+ * @throws {RangeError} When a bound is not a safe integer, or the minimum is
100
+ * above the maximum.
101
+ */
102
+ export function integerVariable(range) {
103
+ const minimum = range?.minimum ?? Number.MIN_SAFE_INTEGER;
104
+ const maximum = range?.maximum ?? Number.MAX_SAFE_INTEGER;
105
+ if (!Number.isSafeInteger(minimum) ||
106
+ !Number.isSafeInteger(maximum) ||
107
+ minimum > maximum) {
108
+ throw new RangeError(`No whole number lies from ${minimum} to ${maximum}`);
109
+ }
110
+ const message = composeRangeMessage_(range);
111
+ return parsedVariable((value) => {
112
+ const number = INTEGER_.test(value) ? Number(value) : Number.NaN;
113
+ if (!(number >= minimum && number <= maximum)) {
114
+ throw new InvalidValueError(message);
115
+ }
116
+ // `-0` reads as `0`.
117
+ return number === 0 ? 0 : number;
118
+ });
119
+ }
120
+ function composeRangeMessage_(range) {
121
+ const minimum = range?.minimum;
122
+ const maximum = range?.maximum;
123
+ if (minimum !== undefined && maximum !== undefined) {
124
+ return `must be a whole number from ${minimum} to ${maximum}`;
125
+ }
126
+ if (minimum !== undefined) {
127
+ return `must be a whole number from ${minimum}`;
128
+ }
129
+ if (maximum !== undefined) {
130
+ return `must be a whole number up to ${maximum}`;
131
+ }
132
+ return "must be a whole number";
133
+ }
134
+ //# sourceMappingURL=environment-variable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"environment-variable.js","sourceRoot":"","sources":["../../src/environment/environment-variable.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,CAAC,MAAM,UAAU,CAAC;AAE9B;;;;GAIG;AACH,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAC3C,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IACjC,C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AAM,IAAI,OAAO,CAAC,EAAE,CAAC;YAC/C,MAAM,IAAI,iBAAiB,CAAC,OAAO,CAAC,CAAC;QACtC,CAAC;QACD,qBAAqB;QACrB,OAAO,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IAClC,CAAC,CAAC,CAAC;AACJ,CAAC;AAED,SAAS,oBAAoB,CAAC,KAAuC;IACpE,MAAM,OAAO,GAAG,KAAK,EAAE,OAAO,CAAC;IAC/B,MAAM,OAAO,GAAG,KAAK,EAAE,OAAO,CAAC;IAC/B,IAAI,OAAO,KAAK,SAAS,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QACpD,OAAO,+BAA+B,OAAO,OAAO,OAAO,EAAE,CAAC;IAC/D,CAAC;IACD,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC3B,OAAO,+BAA+B,OAAO,EAAE,CAAC;IACjD,CAAC;IACD,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC3B,OAAO,gCAAgC,OAAO,EAAE,CAAC;IAClD,CAAC;IACD,OAAO,wBAAwB,CAAC;AACjC,CAAC"}
@@ -0,0 +1,64 @@
1
+ import * as z from "zod/mini";
2
+ /** What is wrong with one variable, in words that never quote its value. */
3
+ export type EnvironmentIssue = {
4
+ /** The variable's name. */
5
+ readonly variable: string;
6
+ /**
7
+ * Where within its value, as {@link composeIssuePath} writes it; `""` for the
8
+ * value as a whole.
9
+ */
10
+ readonly path: string;
11
+ readonly message: string;
12
+ };
13
+ /**
14
+ * Environment variables {@link parseEnvironment} refused, every issue listed.
15
+ * Neither its message nor its issues quote a value, so it can be logged even
16
+ * where a variable holds a secret.
17
+ */
18
+ export declare class EnvironmentError extends Error {
19
+ readonly issues: readonly EnvironmentIssue[];
20
+ constructor(issues: readonly EnvironmentIssue[]);
21
+ }
22
+ /** Optional settings of {@link parseEnvironment}. */
23
+ export type ParseEnvironmentOptions = {
24
+ /**
25
+ * Whether a variable set to the empty string reads as unset, as most
26
+ * deployment tools write a variable left blank. Default: `true`.
27
+ */
28
+ readonly emptyAsUnset?: boolean;
29
+ };
30
+ /**
31
+ * The variables `schema` names, read from `environment` (`process.env`, or a
32
+ * test's record) and checked: each key of `schema` is a variable, its schema
33
+ * one of {@link parsedVariable}, {@link jsonVariable}, {@link integerVariable} or
34
+ * any schema of a string, wrapped in `z.optional` or `z.prefault` when it may
35
+ * be absent. The other variables are not read.
36
+ *
37
+ * Reach for it to read a service's configuration at start, failing with every
38
+ * issue at once.
39
+ *
40
+ * @example
41
+ * const config = parseEnvironment(
42
+ * z.object({
43
+ * PORT: z.prefault(
44
+ * integerVariable({ minimum: 1, maximum: 65_535 }),
45
+ * "8080",
46
+ * ),
47
+ * DATABASE_URL: parsedVariable(parseDatabaseUrl),
48
+ * }),
49
+ * process.env,
50
+ * );
51
+ *
52
+ * @throws {EnvironmentError} Listing each variable's issues, quoting no value.
53
+ */
54
+ export declare function parseEnvironment<Schema extends z.ZodMiniObject>(schema: Schema, environment: Readonly<Record<string, string | undefined>>, options?: ParseEnvironmentOptions): z.output<Schema>;
55
+ /**
56
+ * Where an issue lies within a value, from the path a schema reports: `.key`
57
+ * for a key, `[index]` for an index, `""` for the value itself. Linear in the
58
+ * path's length.
59
+ *
60
+ * @example
61
+ * composeIssuePath(["providers", 0, "issuer"]); // ".providers[0].issuer"
62
+ */
63
+ export declare function composeIssuePath(path: readonly PropertyKey[]): string;
64
+ //# sourceMappingURL=parse-environment.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-environment.d.ts","sourceRoot":"","sources":["../../src/environment/parse-environment.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,UAAU,CAAC;AAE9B,4EAA4E;AAC5E,MAAM,MAAM,gBAAgB,GAAG;IAC9B,2BAA2B;IAC3B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF;;;;GAIG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IAC9B,QAAQ,CAAC,MAAM,EAAE,SAAS,gBAAgB,EAAE;IAAxD,YAAqB,MAAM,EAAE,SAAS,gBAAgB,EAAE,EAOvD;CACD;AAED,qDAAqD;AACrD,MAAM,MAAM,uBAAuB,GAAG;IACrC;;;OAGG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,OAAO,CAAC;CAChC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,SAAS,CAAC,CAAC,aAAa,EAC9D,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,EACzD,OAAO,CAAC,EAAE,uBAAuB,GAC/B,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAyBlB;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,SAAS,WAAW,EAAE,GAAG,MAAM,CAIrE"}
@@ -0,0 +1,77 @@
1
+ import * as z from "zod/mini";
2
+ /**
3
+ * Environment variables {@link parseEnvironment} refused, every issue listed.
4
+ * Neither its message nor its issues quote a value, so it can be logged even
5
+ * where a variable holds a secret.
6
+ */
7
+ export class EnvironmentError extends Error {
8
+ issues;
9
+ constructor(issues) {
10
+ super(`Invalid environment: ${issues
11
+ .map(({ variable, path, message }) => `${variable}${path} ${message}`)
12
+ .join("; ")}`);
13
+ this.issues = issues;
14
+ this.name = "EnvironmentError";
15
+ }
16
+ }
17
+ /**
18
+ * The variables `schema` names, read from `environment` (`process.env`, or a
19
+ * test's record) and checked: each key of `schema` is a variable, its schema
20
+ * one of {@link parsedVariable}, {@link jsonVariable}, {@link integerVariable} or
21
+ * any schema of a string, wrapped in `z.optional` or `z.prefault` when it may
22
+ * be absent. The other variables are not read.
23
+ *
24
+ * Reach for it to read a service's configuration at start, failing with every
25
+ * issue at once.
26
+ *
27
+ * @example
28
+ * const config = parseEnvironment(
29
+ * z.object({
30
+ * PORT: z.prefault(
31
+ * integerVariable({ minimum: 1, maximum: 65_535 }),
32
+ * "8080",
33
+ * ),
34
+ * DATABASE_URL: parsedVariable(parseDatabaseUrl),
35
+ * }),
36
+ * process.env,
37
+ * );
38
+ *
39
+ * @throws {EnvironmentError} Listing each variable's issues, quoting no value.
40
+ */
41
+ export function parseEnvironment(schema, environment, options) {
42
+ const isEmptyUnset = options?.emptyAsUnset ?? true;
43
+ const variables = new Map();
44
+ for (const key of Object.keys(schema.shape)) {
45
+ const name = String(key);
46
+ const value = Object.hasOwn(environment, name)
47
+ ? environment[name]
48
+ : undefined;
49
+ variables.set(name, isEmptyUnset && value === "" ? undefined : value);
50
+ }
51
+ const result = z.safeParse(schema, Object.fromEntries(variables));
52
+ if (!result.success) {
53
+ throw new EnvironmentError(result.error.issues.map(({ path, message }) => {
54
+ const [variable, ...inner] = path;
55
+ return {
56
+ variable: String(variable),
57
+ path: composeIssuePath(inner),
58
+ message,
59
+ };
60
+ }));
61
+ }
62
+ return result.data;
63
+ }
64
+ /**
65
+ * Where an issue lies within a value, from the path a schema reports: `.key`
66
+ * for a key, `[index]` for an index, `""` for the value itself. Linear in the
67
+ * path's length.
68
+ *
69
+ * @example
70
+ * composeIssuePath(["providers", 0, "issuer"]); // ".providers[0].issuer"
71
+ */
72
+ export function composeIssuePath(path) {
73
+ return path
74
+ .map((key) => (typeof key === "number" ? `[${key}]` : `.${String(key)}`))
75
+ .join("");
76
+ }
77
+ //# sourceMappingURL=parse-environment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-environment.js","sourceRoot":"","sources":["../../src/environment/parse-environment.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,UAAU,CAAC;AAc9B;;;;GAIG;AACH,MAAM,OAAO,gBAAiB,SAAQ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I,EAAE,OAAO,EAAE,EAAE,EAAE;YAC7C,MAAM,CAAC,QAAQ,EAAE,GAAG,KAAK,CAAC,GAAG,IAAI,CAAC;YAClC,OAAO;gBACN,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAC;gBAC1B,IAAI,EAAE,gBAAgB,CAAC,KAAK,CAAC;gBAC7B,OAAO;aACP,CAAC;QACH,CAAC,CAAC,CACF,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC,IAAI,CAAC;AACpB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAA4B;IAC5D,OAAO,IAAI;SACT,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,GAAG,GAAG,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;SACxE,IAAI,CAAC,EAAE,CAAC,CAAC;AACZ,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from "./editorconfig.js";
2
+ export * from "./environment/environment-variable.js";
3
+ export * from "./environment/parse-environment.js";
2
4
  export * from "./extendable-config/resolve-extendable-config.js";
3
5
  export * from "./extendable-config/resolve-extends-value.js";
4
6
  export * from "./load-config.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,kDAAkD,CAAC;AACjE,cAAc,8CAA8C,CAAC;AAC7D,cAAc,kBAAkB,CAAC;AACjC,cAAc,2BAA2B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,uCAAuC,CAAC;AACtD,cAAc,oCAAoC,CAAC;AACnD,cAAc,kDAAkD,CAAC;AACjE,cAAc,8CAA8C,CAAC;AAC7D,cAAc,kBAAkB,CAAC;AACjC,cAAc,2BAA2B,CAAC"}
package/dist/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from "./editorconfig.js";
2
+ export * from "./environment/environment-variable.js";
3
+ export * from "./environment/parse-environment.js";
2
4
  export * from "./extendable-config/resolve-extendable-config.js";
3
5
  export * from "./extendable-config/resolve-extends-value.js";
4
6
  export * from "./load-config.js";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,kDAAkD,CAAC;AACjE,cAAc,8CAA8C,CAAC;AAC7D,cAAc,kBAAkB,CAAC;AACjC,cAAc,2BAA2B,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,uCAAuC,CAAC;AACtD,cAAc,oCAAoC,CAAC;AACnD,cAAc,kDAAkD,CAAC;AACjE,cAAc,8CAA8C,CAAC;AAC7D,cAAc,kBAAkB,CAAC;AACjC,cAAc,2BAA2B,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ac-kit/app-config",
3
- "version": "0.2.1",
4
- "description": "CLI configuration loading, built on cosmiconfig.",
3
+ "version": "0.3.0",
4
+ "description": "Application configuration: CLI configuration files found with cosmiconfig, and environment variables read through a schema without ever quoting a value.",
5
5
  "keywords": [
6
6
  "cli",
7
7
  "config",
@@ -38,22 +38,24 @@
38
38
  "prepack": "yarn build"
39
39
  },
40
40
  "dependencies": {
41
- "@ac-kit/core": "0.8.0",
42
- "@ac-kit/format-editorconfig": "^0.1.7",
43
- "@ac-kit/node": "0.8.0",
41
+ "@ac-kit/core": "0.9.0",
42
+ "@ac-kit/format-editorconfig": "^0.1.8",
43
+ "@ac-kit/node": "0.8.1",
44
44
  "@types/node": "^26.5.1",
45
45
  "cosmiconfig": "^10.0.0",
46
- "type-fest": "^5.8.0"
46
+ "type-fest": "^5.8.0",
47
+ "zod": "^4.5.4"
47
48
  },
48
49
  "devDependencies": {
49
50
  "@ac-kit/tsconfig": "0.1.0",
50
51
  "@ac-kit/typedoc-config": "0.1.0",
52
+ "@ac-kit/vitest-config": "0.1.1",
51
53
  "@typescript/native": "typescript@7.0.2",
52
54
  "rimraf": "6.1.4",
53
55
  "typedoc": "0.28.20",
54
56
  "typescript": "@typescript/typescript6@6.0.2",
55
57
  "vite": "8.3.8",
56
- "vitest": "5.1.0",
58
+ "vitest": "5.1.1",
57
59
  "vitest-mock-extended": "5.1.1"
58
60
  },
59
61
  "engines": {