@abinnovision/nestjs-configx 2.0.1 → 2.0.2-beta.22

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.
@@ -0,0 +1,20 @@
1
+ const require_resolver = require("./resolver.cjs");
2
+ //#region src/create.ts
3
+ /**
4
+ * Creates a new Configx class based on the provided schema.
5
+ *
6
+ * @param schema The schema of the Configx class (Zod, ArkType, or any StandardSchema).
7
+ */ function configx(schema) {
8
+ return class ConfigxUsage {
9
+ static schema = schema;
10
+ constructor() {
11
+ const config = require_resolver.resolveConfig({
12
+ schema,
13
+ resolveEnv: () => process.env
14
+ });
15
+ Object.assign(this, config);
16
+ }
17
+ };
18
+ }
19
+ //#endregion
20
+ exports.configx = configx;
@@ -0,0 +1,11 @@
1
+ import { ConfigxSchema, ConfigxType } from "./types.cjs";
2
+
3
+ //#region src/create.d.ts
4
+ /**
5
+ * Creates a new Configx class based on the provided schema.
6
+ *
7
+ * @param schema The schema of the Configx class (Zod, ArkType, or any StandardSchema).
8
+ */
9
+ declare function configx<T extends ConfigxSchema>(schema: T): ConfigxType<T>;
10
+ //#endregion
11
+ export { configx };
@@ -0,0 +1,11 @@
1
+ import { ConfigxSchema, ConfigxType } from "./types.mjs";
2
+
3
+ //#region src/create.d.ts
4
+ /**
5
+ * Creates a new Configx class based on the provided schema.
6
+ *
7
+ * @param schema The schema of the Configx class (Zod, ArkType, or any StandardSchema).
8
+ */
9
+ declare function configx<T extends ConfigxSchema>(schema: T): ConfigxType<T>;
10
+ //#endregion
11
+ export { configx };
@@ -0,0 +1,20 @@
1
+ import { resolveConfig } from "./resolver.mjs";
2
+ //#region src/create.ts
3
+ /**
4
+ * Creates a new Configx class based on the provided schema.
5
+ *
6
+ * @param schema The schema of the Configx class (Zod, ArkType, or any StandardSchema).
7
+ */ function configx(schema) {
8
+ return class ConfigxUsage {
9
+ static schema = schema;
10
+ constructor() {
11
+ const config = resolveConfig({
12
+ schema,
13
+ resolveEnv: () => process.env
14
+ });
15
+ Object.assign(this, config);
16
+ }
17
+ };
18
+ }
19
+ //#endregion
20
+ export { configx };
@@ -0,0 +1,40 @@
1
+ //#region src/errors.ts
2
+ /**
3
+ * Formats an issue for the ConfigxError.
4
+ *
5
+ * @param issue The issue to format.
6
+ * @returns The formatted issue.
7
+ */ const formatIssue = (issue) => {
8
+ let result = "";
9
+ if ((issue.path?.length ?? 0) > 0) {
10
+ const path = (issue.path ?? []).map((segment) => {
11
+ if (typeof segment === "object" && "key" in segment) return segment.key.toString();
12
+ return segment.toString();
13
+ }).join(".");
14
+ result += `'${path}': `;
15
+ } else result += "@: ";
16
+ result += issue.message;
17
+ return result;
18
+ };
19
+ /**
20
+ * Base class for all Configx errors.
21
+ */ var ConfigxError = class extends Error {
22
+ constructor(message) {
23
+ super(message);
24
+ this.name = "ConfigxError";
25
+ }
26
+ };
27
+ /**
28
+ * Error thrown when the configuration is invalid.
29
+ */ var InvalidConfigError = class InvalidConfigError extends ConfigxError {
30
+ constructor(message) {
31
+ super(message);
32
+ this.name = "InvalidConfigError";
33
+ }
34
+ static fromSchemaIssues(issues) {
35
+ return new InvalidConfigError(issues.map(formatIssue).join("; "));
36
+ }
37
+ };
38
+ //#endregion
39
+ exports.ConfigxError = ConfigxError;
40
+ exports.InvalidConfigError = InvalidConfigError;
@@ -0,0 +1,18 @@
1
+ import { StandardSchemaV1 } from "@standard-schema/spec";
2
+
3
+ //#region src/errors.d.ts
4
+ /**
5
+ * Base class for all Configx errors.
6
+ */
7
+ declare class ConfigxError extends Error {
8
+ constructor(message: string);
9
+ }
10
+ /**
11
+ * Error thrown when the configuration is invalid.
12
+ */
13
+ declare class InvalidConfigError extends ConfigxError {
14
+ constructor(message: string);
15
+ static fromSchemaIssues(issues: readonly StandardSchemaV1.Issue[]): InvalidConfigError;
16
+ }
17
+ //#endregion
18
+ export { ConfigxError, InvalidConfigError };
@@ -0,0 +1,18 @@
1
+ import { StandardSchemaV1 } from "@standard-schema/spec";
2
+
3
+ //#region src/errors.d.ts
4
+ /**
5
+ * Base class for all Configx errors.
6
+ */
7
+ declare class ConfigxError extends Error {
8
+ constructor(message: string);
9
+ }
10
+ /**
11
+ * Error thrown when the configuration is invalid.
12
+ */
13
+ declare class InvalidConfigError extends ConfigxError {
14
+ constructor(message: string);
15
+ static fromSchemaIssues(issues: readonly StandardSchemaV1.Issue[]): InvalidConfigError;
16
+ }
17
+ //#endregion
18
+ export { ConfigxError, InvalidConfigError };
@@ -0,0 +1,39 @@
1
+ //#region src/errors.ts
2
+ /**
3
+ * Formats an issue for the ConfigxError.
4
+ *
5
+ * @param issue The issue to format.
6
+ * @returns The formatted issue.
7
+ */ const formatIssue = (issue) => {
8
+ let result = "";
9
+ if ((issue.path?.length ?? 0) > 0) {
10
+ const path = (issue.path ?? []).map((segment) => {
11
+ if (typeof segment === "object" && "key" in segment) return segment.key.toString();
12
+ return segment.toString();
13
+ }).join(".");
14
+ result += `'${path}': `;
15
+ } else result += "@: ";
16
+ result += issue.message;
17
+ return result;
18
+ };
19
+ /**
20
+ * Base class for all Configx errors.
21
+ */ var ConfigxError = class extends Error {
22
+ constructor(message) {
23
+ super(message);
24
+ this.name = "ConfigxError";
25
+ }
26
+ };
27
+ /**
28
+ * Error thrown when the configuration is invalid.
29
+ */ var InvalidConfigError = class InvalidConfigError extends ConfigxError {
30
+ constructor(message) {
31
+ super(message);
32
+ this.name = "InvalidConfigError";
33
+ }
34
+ static fromSchemaIssues(issues) {
35
+ return new InvalidConfigError(issues.map(formatIssue).join("; "));
36
+ }
37
+ };
38
+ //#endregion
39
+ export { ConfigxError, InvalidConfigError };
package/dist/index.cjs ADDED
@@ -0,0 +1,6 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_errors = require("./errors.cjs");
3
+ const require_create = require("./create.cjs");
4
+ exports.ConfigxError = require_errors.ConfigxError;
5
+ exports.InvalidConfigError = require_errors.InvalidConfigError;
6
+ exports.configx = require_create.configx;
@@ -0,0 +1,4 @@
1
+ import { ConfigxError, InvalidConfigError } from "./errors.cjs";
2
+ import { ConfigxSchema, ConfigxType } from "./types.cjs";
3
+ import { configx } from "./create.cjs";
4
+ export { ConfigxError, ConfigxSchema, ConfigxType, InvalidConfigError, configx };
@@ -0,0 +1,4 @@
1
+ import { ConfigxError, InvalidConfigError } from "./errors.mjs";
2
+ import { ConfigxSchema, ConfigxType } from "./types.mjs";
3
+ import { configx } from "./create.mjs";
4
+ export { ConfigxError, ConfigxSchema, ConfigxType, InvalidConfigError, configx };
package/dist/index.mjs ADDED
@@ -0,0 +1,3 @@
1
+ import { ConfigxError, InvalidConfigError } from "./errors.mjs";
2
+ import { configx } from "./create.mjs";
3
+ export { ConfigxError, InvalidConfigError, configx };
@@ -0,0 +1,20 @@
1
+ const require_errors = require("./errors.cjs");
2
+ //#region src/resolver.ts
3
+ /**
4
+ * Resolves and validates configuration from environment variables using the provided schema.
5
+ *
6
+ * @param args The resolution arguments.
7
+ * @param args.schema A Standard Schema V1 compatible schema (Zod, ArkType, etc.).
8
+ * @param args.resolveEnv Function that returns the environment variables object.
9
+ * @returns The validated and transformed configuration object.
10
+ * @throws {ConfigxError} If the schema uses asynchronous validation.
11
+ * @throws {InvalidConfigError} If the environment variables fail schema validation.
12
+ */ const resolveConfig = (args) => {
13
+ const parsableObject = args.resolveEnv();
14
+ const result = args.schema["~standard"].validate(parsableObject);
15
+ if (result instanceof Promise) throw new require_errors.ConfigxError("Asynchronous schemas are not supported");
16
+ if (result.issues === void 0) return result.value;
17
+ else throw require_errors.InvalidConfigError.fromSchemaIssues(result.issues);
18
+ };
19
+ //#endregion
20
+ exports.resolveConfig = resolveConfig;
@@ -0,0 +1,20 @@
1
+ import { ConfigxError, InvalidConfigError } from "./errors.mjs";
2
+ //#region src/resolver.ts
3
+ /**
4
+ * Resolves and validates configuration from environment variables using the provided schema.
5
+ *
6
+ * @param args The resolution arguments.
7
+ * @param args.schema A Standard Schema V1 compatible schema (Zod, ArkType, etc.).
8
+ * @param args.resolveEnv Function that returns the environment variables object.
9
+ * @returns The validated and transformed configuration object.
10
+ * @throws {ConfigxError} If the schema uses asynchronous validation.
11
+ * @throws {InvalidConfigError} If the environment variables fail schema validation.
12
+ */ const resolveConfig = (args) => {
13
+ const parsableObject = args.resolveEnv();
14
+ const result = args.schema["~standard"].validate(parsableObject);
15
+ if (result instanceof Promise) throw new ConfigxError("Asynchronous schemas are not supported");
16
+ if (result.issues === void 0) return result.value;
17
+ else throw InvalidConfigError.fromSchemaIssues(result.issues);
18
+ };
19
+ //#endregion
20
+ export { resolveConfig };
@@ -0,0 +1,22 @@
1
+ import { StandardSchemaV1 } from "@standard-schema/spec";
2
+
3
+ //#region src/types.d.ts
4
+ /**
5
+ * Describes the allowed types for a Configx schema.
6
+ */
7
+ type ConfigxSchema = StandardSchemaV1<Record<string, string | undefined>, Record<string, any>>;
8
+ /**
9
+ * Describes a Configx class.
10
+ */
11
+ interface ConfigxType<T extends ConfigxSchema = any> {
12
+ /**
13
+ * Standard Schema V1 schema of the Configx class.
14
+ */
15
+ schema: T;
16
+ /**
17
+ * Instantiates a new Configx instance.
18
+ */
19
+ new (): StandardSchemaV1.InferOutput<T>;
20
+ }
21
+ //#endregion
22
+ export { ConfigxSchema, ConfigxType };
@@ -0,0 +1,22 @@
1
+ import { StandardSchemaV1 } from "@standard-schema/spec";
2
+
3
+ //#region src/types.d.ts
4
+ /**
5
+ * Describes the allowed types for a Configx schema.
6
+ */
7
+ type ConfigxSchema = StandardSchemaV1<Record<string, string | undefined>, Record<string, any>>;
8
+ /**
9
+ * Describes a Configx class.
10
+ */
11
+ interface ConfigxType<T extends ConfigxSchema = any> {
12
+ /**
13
+ * Standard Schema V1 schema of the Configx class.
14
+ */
15
+ schema: T;
16
+ /**
17
+ * Instantiates a new Configx instance.
18
+ */
19
+ new (): StandardSchemaV1.InferOutput<T>;
20
+ }
21
+ //#endregion
22
+ export { ConfigxSchema, ConfigxType };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@abinnovision/nestjs-configx",
4
- "version": "2.0.1",
4
+ "version": "2.0.2-beta.22",
5
5
  "keywords": [
6
6
  "nestjs",
7
7
  "config",
@@ -10,7 +10,7 @@
10
10
  ],
11
11
  "repository": {
12
12
  "type": "git",
13
- "url": "https://github.com/abinnovision/nestjs-commons.git",
13
+ "url": "git+https://github.com/abinnovision/nestjs-commons.git",
14
14
  "directory": "packages/configx"
15
15
  },
16
16
  "license": "Apache-2.0",
@@ -19,20 +19,28 @@
19
19
  "email": "info@abinnovision.com",
20
20
  "url": "https://abinnovision.com/"
21
21
  },
22
+ "type": "module",
22
23
  "exports": {
23
24
  ".": {
24
- "types": "./dist/index.d.ts",
25
- "require": "./dist/index.js",
26
- "default": "./dist/index.js"
25
+ "import": {
26
+ "types": "./dist/index.d.mts",
27
+ "default": "./dist/index.mjs"
28
+ },
29
+ "require": {
30
+ "types": "./dist/index.d.cts",
31
+ "default": "./dist/index.cjs"
32
+ }
27
33
  }
28
34
  },
35
+ "main": "./dist/index.cjs",
36
+ "types": "./dist/index.d.cts",
29
37
  "files": [
30
38
  "dist",
31
39
  "LICENSE",
32
40
  "README.md"
33
41
  ],
34
42
  "scripts": {
35
- "build": "tsc --project tsconfig.build.json",
43
+ "build": "tsdown",
36
44
  "format:check": "prettier --check 'src/**/*.ts' '*.{json{,5},md,y{,a}ml}'",
37
45
  "format:fix": "prettier --write 'src/**/*.ts' '*.{json{,5},md,y{,a}ml}'",
38
46
  "lint:check": "eslint 'src/**/*.ts'",
@@ -52,17 +60,20 @@
52
60
  "@standard-schema/spec": "^1.0.0"
53
61
  },
54
62
  "devDependencies": {
55
- "@abinnovision/eslint-config-base": "^3.0.2",
63
+ "@abinnovision/eslint-config-base": "^3.2.0",
56
64
  "@abinnovision/prettier-config": "^2.1.5",
57
- "@swc/core": "^1.11.5",
65
+ "@arethetypeswrong/core": "^0.18.2",
66
+ "@swc/core": "^1.15.18",
58
67
  "arktype": "^2.1.9",
59
- "eslint": "^9.39.1",
60
- "globals": "^16.5.0",
61
- "prettier": "^3.7.4",
68
+ "eslint": "^9.39.4",
69
+ "prettier": "^3.8.1",
70
+ "publint": "^0.3.18",
62
71
  "reflect-metadata": "^0.2.2",
63
72
  "rxjs": "^7.8.1",
64
- "typescript": "^5.8.2",
65
- "vitest": "^4.0.15",
73
+ "tsdown": "^0.21.3",
74
+ "typescript": "^5.9.3",
75
+ "unplugin-swc": "^1.5.9",
76
+ "vitest": "^4.1.0",
66
77
  "zod": "^3.24.1"
67
78
  },
68
79
  "publishConfig": {
@@ -1,4 +0,0 @@
1
- /**
2
- * Symbol key used to store resolved config values on the Configx class.
3
- */
4
- export declare const CONFIGX_RESOLVED: unique symbol;
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CONFIGX_RESOLVED = void 0;
4
- /**
5
- * Symbol key used to store resolved config values on the Configx class.
6
- */
7
- exports.CONFIGX_RESOLVED = Symbol("CONFIGX_RESOLVED");
@@ -1,8 +0,0 @@
1
- import type { StandardSchemaV1 } from "@standard-schema/spec";
2
- /**
3
- * Generic error class for Configx.
4
- */
5
- export declare class ConfigxError extends Error {
6
- constructor(message: string);
7
- static fromSchemaIssues(issues: readonly StandardSchemaV1.Issue[]): ConfigxError;
8
- }
@@ -1,48 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ConfigxError = void 0;
4
- /**
5
- * Formats an issue for the ConfigxError.
6
- *
7
- * @param issue The issue to format.
8
- * @returns The formatted issue.
9
- */
10
- const formatIssue = (issue) => {
11
- let result = "";
12
- if ((issue.path?.length ?? 0) > 0) {
13
- // Format the path segments.
14
- const path = (issue.path ?? [])
15
- .map((segment) => {
16
- // If the segment is an object with a key property, use that.
17
- if (typeof segment === "object" && "key" in segment) {
18
- return segment.key.toString();
19
- }
20
- return segment.toString();
21
- })
22
- .join(".");
23
- // Add the path to the result.
24
- result += `${path}: `;
25
- }
26
- else {
27
- // If there is no path, the issue is a root issue.
28
- result += "@: ";
29
- }
30
- // Add the message.
31
- result += issue.message;
32
- return result;
33
- };
34
- /**
35
- * Generic error class for Configx.
36
- */
37
- class ConfigxError extends Error {
38
- constructor(message) {
39
- super(message);
40
- this.name = "ConfigxError";
41
- }
42
- static fromSchemaIssues(issues) {
43
- const messageHeader = "Invalid config:\n";
44
- const messageBody = issues.map((it) => `- ${formatIssue(it)}`).join("\n");
45
- return new ConfigxError(`${messageHeader}${messageBody}`);
46
- }
47
- }
48
- exports.ConfigxError = ConfigxError;
@@ -1,7 +0,0 @@
1
- import type { ConfigxSchema, ConfigxType } from "./configx.types";
2
- /**
3
- * Creates a new Configx class.
4
- *
5
- * @param schema The schema of the Configx class (Zod, ArkType, or any StandardSchema).
6
- */
7
- export declare function configx<T extends ConfigxSchema>(schema: T): ConfigxType<T>;
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.configx = configx;
4
- const configx_resolver_1 = require("./configx.resolver");
5
- /**
6
- * Creates a new Configx class.
7
- *
8
- * @param schema The schema of the Configx class (Zod, ArkType, or any StandardSchema).
9
- */
10
- function configx(schema) {
11
- // eslint-disable-next-line @typescript-eslint/no-extraneous-class
12
- class ConfigxUsage {
13
- static schema = schema;
14
- constructor() {
15
- // Resolve the configuration.
16
- const config = (0, configx_resolver_1.resolveConfig)({
17
- schema,
18
- resolveEnv: () => process.env,
19
- });
20
- // Assign the resolved configuration to the instance.
21
- Object.assign(this, config);
22
- }
23
- }
24
- return ConfigxUsage;
25
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,19 +0,0 @@
1
- import type { ConfigxSchema } from "./configx.types";
2
- import type { StandardSchemaV1 } from "@standard-schema/spec";
3
- interface ResolveConfigArgs<T extends ConfigxSchema> {
4
- /**
5
- * Schema of the Configx class.
6
- */
7
- schema: T;
8
- /**
9
- * Resolves the environment variables object.
10
- */
11
- resolveEnv: () => Record<string, string | undefined>;
12
- }
13
- /**
14
- * Resolves the configuration with the given ConfigxConfig.
15
- *
16
- * @param args The arguments for the function.
17
- */
18
- export declare const resolveConfig: <T extends ConfigxSchema>(args: ResolveConfigArgs<T>) => StandardSchemaV1.InferOutput<T>;
19
- export {};
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveConfig = void 0;
4
- const configx_errors_1 = require("./configx.errors");
5
- /**
6
- * Resolves the configuration with the given ConfigxConfig.
7
- *
8
- * @param args The arguments for the function.
9
- */
10
- const resolveConfig = (args) => {
11
- // The object which will later be parsed by the Zod schema.
12
- const parsableObject = args.resolveEnv();
13
- const result = args.schema["~standard"].validate(parsableObject);
14
- if (result instanceof Promise) {
15
- throw new Error("Asynchronous schemas are not supported");
16
- }
17
- if (result.issues === undefined) {
18
- return result.value;
19
- }
20
- else {
21
- throw configx_errors_1.ConfigxError.fromSchemaIssues(result.issues);
22
- }
23
- };
24
- exports.resolveConfig = resolveConfig;
@@ -1 +0,0 @@
1
- export declare const CONFIGX_RESOLVER_FN: unique symbol;
@@ -1,4 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CONFIGX_RESOLVER_FN = void 0;
4
- exports.CONFIGX_RESOLVER_FN = Symbol("CONFIGX_RESOLVER_FN");
@@ -1,23 +0,0 @@
1
- import type { StandardSchemaV1 } from "@standard-schema/spec";
2
- /**
3
- * Describes the allowed types for a Configx schema.
4
- */
5
- export type ConfigxSchema = StandardSchemaV1<Record<string, string | undefined>, Record<string, any>>;
6
- /**
7
- * Describes a Configx class.
8
- * The instance of the class is used to access the configuration.
9
- */
10
- export interface ConfigxType<T extends ConfigxSchema = any> {
11
- /**
12
- * Standard Schema V1 schema of the Configx class.
13
- */
14
- schema: T;
15
- /**
16
- * Instantiates a new Configx instance.
17
- */
18
- new (): ConfigxValue<T>;
19
- }
20
- /**
21
- * Describes the value of the Configx class.
22
- */
23
- export type ConfigxValue<T extends ConfigxSchema> = StandardSchemaV1.InferOutput<T>;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
package/dist/create.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import type { ConfigxSchema, ConfigxType } from "./types";
2
- /**
3
- * Creates a new Configx class based on the provided schema.
4
- *
5
- * @param schema The schema of the Configx class (Zod, ArkType, or any StandardSchema).
6
- */
7
- export declare function configx<T extends ConfigxSchema>(schema: T): ConfigxType<T>;
package/dist/create.js DELETED
@@ -1,26 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.configx = configx;
4
- const resolver_1 = require("./resolver");
5
- /**
6
- * Creates a new Configx class based on the provided schema.
7
- *
8
- * @param schema The schema of the Configx class (Zod, ArkType, or any StandardSchema).
9
- */
10
- function configx(schema) {
11
- // eslint-disable-next-line @typescript-eslint/no-extraneous-class
12
- class ConfigxUsage {
13
- static schema = schema;
14
- constructor() {
15
- // Resolve the configuration.
16
- const config = (0, resolver_1.resolveConfig)({
17
- schema,
18
- resolveEnv: () => process.env,
19
- });
20
- // Assign the resolved configuration to the instance.
21
- // This is currently the most efficient way to do this without relying on proxies.
22
- Object.assign(this, config);
23
- }
24
- }
25
- return ConfigxUsage;
26
- }
package/dist/errors.d.ts DELETED
@@ -1,14 +0,0 @@
1
- import type { StandardSchemaV1 } from "@standard-schema/spec";
2
- /**
3
- * Base class for all Configx errors.
4
- */
5
- export declare class ConfigxError extends Error {
6
- constructor(message: string);
7
- }
8
- /**
9
- * Error thrown when the configuration is invalid.
10
- */
11
- export declare class InvalidConfigError extends ConfigxError {
12
- constructor(message: string);
13
- static fromSchemaIssues(issues: readonly StandardSchemaV1.Issue[]): InvalidConfigError;
14
- }
package/dist/errors.js DELETED
@@ -1,57 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InvalidConfigError = exports.ConfigxError = void 0;
4
- /**
5
- * Formats an issue for the ConfigxError.
6
- *
7
- * @param issue The issue to format.
8
- * @returns The formatted issue.
9
- */
10
- const formatIssue = (issue) => {
11
- let result = "";
12
- if ((issue.path?.length ?? 0) > 0) {
13
- // Format the path segments.
14
- const path = (issue.path ?? [])
15
- .map((segment) => {
16
- // If the segment is an object with a key property, use that.
17
- if (typeof segment === "object" && "key" in segment) {
18
- return segment.key.toString();
19
- }
20
- return segment.toString();
21
- })
22
- .join(".");
23
- // Add the path to the result.
24
- result += `'${path}': `;
25
- }
26
- else {
27
- // If there is no path, the issue is a root issue.
28
- result += "@: ";
29
- }
30
- // Add the message.
31
- result += issue.message;
32
- return result;
33
- };
34
- /**
35
- * Base class for all Configx errors.
36
- */
37
- class ConfigxError extends Error {
38
- constructor(message) {
39
- super(message);
40
- this.name = "ConfigxError";
41
- }
42
- }
43
- exports.ConfigxError = ConfigxError;
44
- /**
45
- * Error thrown when the configuration is invalid.
46
- */
47
- class InvalidConfigError extends ConfigxError {
48
- constructor(message) {
49
- super(message);
50
- this.name = "InvalidConfigError";
51
- }
52
- static fromSchemaIssues(issues) {
53
- const messageBody = issues.map(formatIssue).join("; ");
54
- return new InvalidConfigError(messageBody);
55
- }
56
- }
57
- exports.InvalidConfigError = InvalidConfigError;
package/dist/helper.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import type { ConfigxSchema, ConfigxType } from "./types";
2
- /**
3
- * Creates a new Configx class based on the provided schema.
4
- *
5
- * @param schema The schema of the Configx class (Zod, ArkType, or any StandardSchema).
6
- */
7
- export declare function configx<T extends ConfigxSchema>(schema: T): ConfigxType<T>;
package/dist/helper.js DELETED
@@ -1,26 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.configx = configx;
4
- const resolver_1 = require("./resolver");
5
- /**
6
- * Creates a new Configx class based on the provided schema.
7
- *
8
- * @param schema The schema of the Configx class (Zod, ArkType, or any StandardSchema).
9
- */
10
- function configx(schema) {
11
- // eslint-disable-next-line @typescript-eslint/no-extraneous-class
12
- class ConfigxUsage {
13
- static schema = schema;
14
- constructor() {
15
- // Resolve the configuration.
16
- const config = (0, resolver_1.resolveConfig)({
17
- schema,
18
- resolveEnv: () => process.env,
19
- });
20
- // Assign the resolved configuration to the instance.
21
- // This is currently the most efficient way to do this without relying on proxies.
22
- Object.assign(this, config);
23
- }
24
- }
25
- return ConfigxUsage;
26
- }
package/dist/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from "./errors";
2
- export * from "./create";
3
- export * from "./types";
package/dist/index.js DELETED
@@ -1,19 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./errors"), exports);
18
- __exportStar(require("./create"), exports);
19
- __exportStar(require("./types"), exports);
@@ -1,24 +0,0 @@
1
- import type { ConfigxSchema } from "./types";
2
- import type { StandardSchemaV1 } from "@standard-schema/spec";
3
- interface ResolveConfigArgs<T extends ConfigxSchema> {
4
- /**
5
- * Schema of the Configx class.
6
- */
7
- schema: T;
8
- /**
9
- * Resolves the environment variables object.
10
- */
11
- resolveEnv: () => Record<string, string | undefined>;
12
- }
13
- /**
14
- * Resolves and validates configuration from environment variables using the provided schema.
15
- *
16
- * @param args The resolution arguments.
17
- * @param args.schema A Standard Schema V1 compatible schema (Zod, ArkType, etc.).
18
- * @param args.resolveEnv Function that returns the environment variables object.
19
- * @returns The validated and transformed configuration object.
20
- * @throws {ConfigxError} If the schema uses asynchronous validation.
21
- * @throws {InvalidConfigError} If the environment variables fail schema validation.
22
- */
23
- export declare const resolveConfig: <T extends ConfigxSchema>(args: ResolveConfigArgs<T>) => StandardSchemaV1.InferOutput<T>;
24
- export {};
package/dist/resolver.js DELETED
@@ -1,29 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveConfig = void 0;
4
- const errors_1 = require("./errors");
5
- /**
6
- * Resolves and validates configuration from environment variables using the provided schema.
7
- *
8
- * @param args The resolution arguments.
9
- * @param args.schema A Standard Schema V1 compatible schema (Zod, ArkType, etc.).
10
- * @param args.resolveEnv Function that returns the environment variables object.
11
- * @returns The validated and transformed configuration object.
12
- * @throws {ConfigxError} If the schema uses asynchronous validation.
13
- * @throws {InvalidConfigError} If the environment variables fail schema validation.
14
- */
15
- const resolveConfig = (args) => {
16
- // The object which will later be parsed by the schema.
17
- const parsableObject = args.resolveEnv();
18
- const result = args.schema["~standard"].validate(parsableObject);
19
- if (result instanceof Promise) {
20
- throw new errors_1.ConfigxError("Asynchronous schemas are not supported");
21
- }
22
- if (result.issues === undefined) {
23
- return result.value;
24
- }
25
- else {
26
- throw errors_1.InvalidConfigError.fromSchemaIssues(result.issues);
27
- }
28
- };
29
- exports.resolveConfig = resolveConfig;
package/dist/types.d.ts DELETED
@@ -1,18 +0,0 @@
1
- import type { StandardSchemaV1 } from "@standard-schema/spec";
2
- /**
3
- * Describes the allowed types for a Configx schema.
4
- */
5
- export type ConfigxSchema = StandardSchemaV1<Record<string, string | undefined>, Record<string, any>>;
6
- /**
7
- * Describes a Configx class.
8
- */
9
- export interface ConfigxType<T extends ConfigxSchema = any> {
10
- /**
11
- * Standard Schema V1 schema of the Configx class.
12
- */
13
- schema: T;
14
- /**
15
- * Instantiates a new Configx instance.
16
- */
17
- new (): StandardSchemaV1.InferOutput<T>;
18
- }
package/dist/types.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });