monorepo-config 0.0.1 → 0.0.3

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,14 @@
1
+ import type { ZodObject, input, output } from "zod";
2
+ export declare class ConfigError extends Error {
3
+ }
4
+ export type Config<ConfigSchema extends ZodObject> = {
5
+ _meta: {
6
+ isUsable: boolean;
7
+ canSet: boolean;
8
+ schema: ConfigSchema;
9
+ };
10
+ } & output<ConfigSchema>;
11
+ export declare function makeConfig<ConfigSchema extends ZodObject>(schema: ConfigSchema): Config<ConfigSchema>;
12
+ export declare function forceOverrideConfig<ConfigSchema extends ZodObject>(config: Config<ConfigSchema>, inputData: input<ConfigSchema>): void;
13
+ export declare function setConfig<ConfigSchema extends ZodObject>(config: Config<ConfigSchema>, inputData: input<ConfigSchema>): void;
14
+ //# sourceMappingURL=basic.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"basic.d.ts","sourceRoot":"","sources":["../src/basic.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,KAAK,CAAC;AAEpD,qBAAa,WAAY,SAAQ,KAAK;CAAI;AAE1C,MAAM,MAAM,MAAM,CAAC,YAAY,SAAS,SAAS,IAAI;IACnD,KAAK,EAAE;QACL,QAAQ,EAAE,OAAO,CAAC;QAClB,MAAM,EAAE,OAAO,CAAC;QAChB,MAAM,EAAE,YAAY,CAAC;KACtB,CAAA;CACF,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;AAgBzB,wBAAgB,UAAU,CAAC,YAAY,SAAS,SAAS,EAAE,MAAM,EAAE,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC,CAwBrG;AAED,wBAAgB,mBAAmB,CAAC,YAAY,SAAS,SAAS,EAAE,MAAM,EAAE,MAAM,CAAC,YAAY,CAAC,EAAE,SAAS,EAAE,KAAK,CAAC,YAAY,CAAC,QAK/H;AAED,wBAAgB,SAAS,CAAC,YAAY,SAAS,SAAS,EAAE,MAAM,EAAE,MAAM,CAAC,YAAY,CAAC,EAAE,SAAS,EAAE,KAAK,CAAC,YAAY,CAAC,QAMrH"}
package/dist/basic.js ADDED
@@ -0,0 +1,55 @@
1
+ export class ConfigError extends Error {
2
+ }
3
+ function wrapInProxy(obj) {
4
+ return new Proxy(obj, {
5
+ get(target, prop, receiver) {
6
+ if (prop === '_meta') {
7
+ return target._meta;
8
+ }
9
+ if (!target._meta.isUsable) {
10
+ throw new Error("Tried to access config but it is not usable yet. Did you forget to set or load the config?");
11
+ }
12
+ return target[prop];
13
+ }
14
+ });
15
+ }
16
+ export function makeConfig(schema) {
17
+ const zodDefaults = schema.safeParse({});
18
+ if (zodDefaults.success) {
19
+ // all attributes had defaults
20
+ return wrapInProxy({
21
+ _meta: {
22
+ schema: schema,
23
+ isUsable: true,
24
+ canSet: true,
25
+ },
26
+ ...zodDefaults.data
27
+ });
28
+ }
29
+ else {
30
+ // zod couldn't handle an empty object
31
+ // @ts-expect-error for simplicity, the types assume that config is always defined.
32
+ return wrapInProxy({
33
+ _meta: {
34
+ schema: schema,
35
+ isUsable: false,
36
+ canSet: true,
37
+ }
38
+ });
39
+ }
40
+ }
41
+ export function forceOverrideConfig(config, inputData) {
42
+ const outputData = config._meta.schema.parse(inputData);
43
+ Object.assign(config, outputData);
44
+ config._meta.isUsable = true;
45
+ config._meta.canSet = false;
46
+ }
47
+ export function setConfig(config, inputData) {
48
+ if (config._meta.canSet) {
49
+ forceOverrideConfig(config, inputData);
50
+ }
51
+ else {
52
+ throw new ConfigError("Can't override config as it is already set");
53
+ }
54
+ }
55
+ //# sourceMappingURL=basic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"basic.js","sourceRoot":"","sources":["../src/basic.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,WAAY,SAAQ,KAAK;CAAI;AAU1C,SAAS,WAAW,CAAiC,GAAyB;IAC5E,OAAO,IAAI,KAAK,CA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;AAED,MAAM,UAAU,SAAS,CAAiC,MAA4B,EAAE,SAA8B;IACpH,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;QACxB,mBAAmB,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IACzC,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,WAAW,CAAC,4CAA4C,CAAC,CAAC;IACtE,CAAC;AACH,CAAC"}
@@ -0,0 +1,16 @@
1
+ import type { ZodObject } from "zod";
2
+ import { Config } from "./basic.js";
3
+ export type FileConfig<ConfigSchema extends ZodObject> = Config<ConfigSchema> & {
4
+ _meta: {
5
+ configDir: string;
6
+ envVar: string;
7
+ };
8
+ };
9
+ export declare function makeFileConfig<ConfigSchema extends ZodObject>(opts: {
10
+ envVar: string;
11
+ directory: string;
12
+ schema: ConfigSchema;
13
+ preventAutoLoad?: boolean;
14
+ }): FileConfig<ConfigSchema>;
15
+ export declare function loadFileIntoConfig<ConfigSchema extends ZodObject>(config: FileConfig<ConfigSchema>): Promise<void>;
16
+ //# sourceMappingURL=filebased.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filebased.d.ts","sourceRoot":"","sources":["../src/filebased.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,KAAK,CAAC;AACrC,OAAO,EAAE,MAAM,EAA2B,MAAM,YAAY,CAAC;AAG7D,MAAM,MAAM,UAAU,CAAC,YAAY,SAAS,SAAS,IAAI,MAAM,CAAC,YAAY,CAAC,GAAG;IAC9E,KAAK,EAAE;QACL,SAAS,EAAE,MAAM,CAAC;QAClB,MAAM,EAAE,MAAM,CAAC;KAChB,CAAA;CACF,CAAC;AAEF,wBAAgB,cAAc,CAAC,YAAY,SAAS,SAAS,EAAE,IAAI,EAAE;IACnE,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,YAAY,CAAC;IAAC,eAAe,CAAC,EAAE,OAAO,CAAA;CACnF,GAAG,UAAU,CAAC,YAAY,CAAC,CAkB3B;AAED,wBAAsB,kBAAkB,CAAC,YAAY,SAAS,SAAS,EAAE,MAAM,EAAE,UAAU,CAAC,YAAY,CAAC,iBAUxG"}
@@ -0,0 +1,32 @@
1
+ import { ConfigError, makeConfig } from "./basic.js";
2
+ import { join } from "path";
3
+ export function makeFileConfig(opts) {
4
+ const cfg = makeConfig(opts.schema);
5
+ // imperative overrides here to not lose the Proxy
6
+ cfg._meta.configDir = opts.directory;
7
+ cfg._meta.envVar = opts.envVar;
8
+ if (!opts.preventAutoLoad) {
9
+ queueMicrotask(async () => {
10
+ try {
11
+ await loadFileIntoConfig(cfg);
12
+ }
13
+ catch (e) {
14
+ console.error(e);
15
+ process.exit(1);
16
+ }
17
+ });
18
+ }
19
+ return cfg;
20
+ }
21
+ export async function loadFileIntoConfig(config) {
22
+ const config_name = process.env[config._meta.envVar];
23
+ if (config_name === undefined) {
24
+ throw new ConfigError(`Please set the environment variable ${config._meta.envVar}`);
25
+ }
26
+ const path = join(config._meta.configDir, `${config_name}.js`);
27
+ await import(path);
28
+ if (!config._meta.isUsable) {
29
+ console.warn(`Imported ${path} but config was still not marked as usable`);
30
+ }
31
+ }
32
+ //# sourceMappingURL=filebased.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filebased.js","sourceRoot":"","sources":["../src/filebased.ts"],"names":[],"mappings":"AACA,OAAO,EAAU,WAAW,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7D,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAS5B,MAAM,UAAU,cAAc,CAAiC,IAE9D;IACC,MAAM,GAAG,GAAG,UAAU,CAAC,IAAI,CAAC,MAAM,CAA6B,CAAC;IAChE,kDAAkD;IAClD,GAAG,CAAC,KAAK,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC;IACrC,GAAG,CAAC,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;IAE/B,IAAI,CAAC,IAAI,CAAC,eAAe,EAAE,CAAC;QAC1B,cAAc,CAAC,KAAK,IAAI,EAAE;YACxB,IAAI,CAAC;gBACH,MAAM,kBAAkB,CAAC,GAAG,CAAC,CAAC;YAChC,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACX,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;gBACjB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YAClB,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,kBAAkB,CAAiC,MAAgC;IACvG,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IACrD,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;QAC9B,MAAM,IAAI,WAAW,CAAC,uCAAuC,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;IACtF,CAAC;IACD,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,GAAG,WAAW,KAAK,CAAC,CAAC;IAC/D,MAAM,MAAM,CAAC,IAAI,CAAC,CAAC;IACnB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;QAC3B,OAAO,CAAC,IAAI,CAAC,YAAY,IAAI,4CAA4C,CAAC,CAAC;IAC7E,CAAC;AACH,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { makeConfig, forceOverrideConfig, setConfig } from "./basic.js";
2
+ export { makeFileConfig, loadFileIntoConfig } from "./filebased.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,mBAAmB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACxE,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC"}
@@ -1,2 +1,3 @@
1
1
  export { makeConfig, forceOverrideConfig, setConfig } from "./basic.js";
2
2
  export { makeFileConfig, loadFileIntoConfig } from "./filebased.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,mBAAmB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACxE,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC"}
package/package.json CHANGED
@@ -1,22 +1,45 @@
1
1
  {
2
2
  "name": "monorepo-config",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "Managed and validated configs across packages (no deps, but bring your own zod)",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",
7
- "keywords": [],
7
+ "types": "./dist/index.d.ts",
8
+ "keywords": [
9
+ "monorepo",
10
+ "config",
11
+ "configuration",
12
+ "validation",
13
+ "zod",
14
+ "typescript"
15
+ ],
8
16
  "author": "Evert Heylen",
17
+ "homepage": "https://github.com/evertheylen/monorepo-config#readme",
18
+ "bugs": {
19
+ "url": "https://github.com/evertheylen/monorepo-config/issues"
20
+ },
9
21
  "license": "MIT",
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://github.com/evertheylen/monorepo-config"
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "README.md"
29
+ ],
30
+ "engines": {
31
+ "node": ">=18.0.0"
32
+ },
10
33
  "devDependencies": {
11
34
  "@types/node": "^25.0.3",
12
35
  "typescript": "^5.9.3",
13
36
  "zod": "^4.3.5"
14
37
  },
15
38
  "exports": {
39
+ ".": "./dist/index.js",
16
40
  "./*": "./dist/*"
17
41
  },
18
42
  "scripts": {
19
- "build": "rm -rf dist && tsc",
20
- "test": "vitest run"
43
+ "build": "rm -rf dist && tsc"
21
44
  }
22
45
  }
package/__test__/foo.js DELETED
@@ -1,11 +0,0 @@
1
- import { setConfig } from "../dist/index.js";
2
- import { FOO_CONFIG } from "./schema.js";
3
- import { BAR_CONFIG } from "./schema2.js";
4
-
5
- setConfig(FOO_CONFIG, {
6
- foo: 'foooo'
7
- });
8
-
9
- setConfig(BAR_CONFIG, {
10
- bar: 'baaaar'
11
- });
@@ -1,12 +0,0 @@
1
-
2
- import z from "zod";
3
- import { makeFileConfig } from "../dist/filebased.js";
4
-
5
- export const FOO_CONFIG = makeFileConfig({
6
- envVar: 'FOO_CONFIG',
7
- directory: import.meta.dirname,
8
- schema: z.object({
9
- foo: z.string()
10
- })
11
- });
12
-
@@ -1,11 +0,0 @@
1
-
2
- import z from "zod";
3
- import { makeConfig } from "../dist/index.js";
4
-
5
- export const BAR_CONFIG = makeConfig(z.object({
6
- bar: z.string().default('this is default'),
7
- optionalBar: z.string().optional(),
8
- }));
9
-
10
- console.assert(BAR_CONFIG.bar === 'this is default', 'bar default');
11
- console.assert(BAR_CONFIG.optionalBar === undefined, 'bar optional');
package/__test__/test.js DELETED
@@ -1,23 +0,0 @@
1
-
2
- import { loadFileIntoConfig } from "../dist/filebased.js";
3
- import { ConfigError, setConfig } from "../dist/index.js";
4
- import { FOO_CONFIG } from "./schema.js";
5
- import { BAR_CONFIG } from "./schema2.js";
6
-
7
- await loadFileIntoConfig(FOO_CONFIG);
8
-
9
- console.assert(FOO_CONFIG.foo === 'foooo', 'foo');
10
- console.assert(BAR_CONFIG.bar === 'baaaar', 'bar');
11
-
12
- let hadError = false;
13
- try {
14
- setConfig(FOO_CONFIG, {
15
- foo: 'boo'
16
- });
17
- } catch (e) {
18
- hadError = true;
19
- console.assert(e instanceof ConfigError, `error correct type: ${e}`);
20
- }
21
- console.assert(hadError, 'had error');
22
-
23
- console.log("no failures? everything is ok!");
@@ -1,2 +0,0 @@
1
- onlyBuiltDependencies:
2
- - esbuild
package/src/basic.ts DELETED
@@ -1,67 +0,0 @@
1
- import type { ZodObject, input, output } from "zod";
2
-
3
- export class ConfigError extends Error { }
4
-
5
- export type Config<ConfigSchema extends ZodObject> = {
6
- _meta: {
7
- isUsable: boolean,
8
- canSet: boolean,
9
- schema: ConfigSchema,
10
- }
11
- } & output<ConfigSchema>;
12
-
13
- function wrapInProxy<ConfigSchema extends ZodObject>(obj: Config<ConfigSchema>) {
14
- return new Proxy(obj, {
15
- get(target, prop, receiver) {
16
- if (prop === '_meta') {
17
- return target._meta;
18
- }
19
- if (!target._meta.isUsable) {
20
- throw new Error("Tried to access config but it is not usable yet. Did you forget to set or load the config?");
21
- }
22
- return (target as any)[prop];
23
- }
24
- });
25
- }
26
-
27
- export function makeConfig<ConfigSchema extends ZodObject>(schema: ConfigSchema): Config<ConfigSchema> {
28
- const zodDefaults = schema.safeParse({});
29
-
30
- if (zodDefaults.success) {
31
- // all attributes had defaults
32
- return wrapInProxy({
33
- _meta: {
34
- schema: schema,
35
- isUsable: true,
36
- canSet: true,
37
- },
38
- ...zodDefaults.data
39
- });
40
- } else {
41
- // zod couldn't handle an empty object
42
- // @ts-expect-error for simplicity, the types assume that config is always defined.
43
- return wrapInProxy({
44
- _meta: {
45
- schema: schema,
46
- isUsable: false,
47
- canSet: true,
48
- }
49
- });
50
- }
51
- }
52
-
53
- export function forceOverrideConfig<ConfigSchema extends ZodObject>(config: Config<ConfigSchema>, inputData: input<ConfigSchema>) {
54
- const outputData = config._meta.schema.parse(inputData);
55
- Object.assign(config, outputData);
56
- config._meta.isUsable = true;
57
- config._meta.canSet = false;
58
- }
59
-
60
- export function setConfig<ConfigSchema extends ZodObject>(config: Config<ConfigSchema>, inputData: input<ConfigSchema>) {
61
- if (config._meta.canSet) {
62
- forceOverrideConfig(config, inputData);
63
- } else {
64
- throw new ConfigError("Can't override config as it is already set");
65
- }
66
- }
67
-
package/src/filebased.ts DELETED
@@ -1,45 +0,0 @@
1
- import type { ZodObject } from "zod";
2
- import { Config, ConfigError, makeConfig } from "./basic.js";
3
- import { join } from "path";
4
-
5
- export type FileConfig<ConfigSchema extends ZodObject> = Config<ConfigSchema> & {
6
- _meta: {
7
- configDir: string;
8
- envVar: string;
9
- }
10
- };
11
-
12
- export function makeFileConfig<ConfigSchema extends ZodObject>(opts: {
13
- envVar: string, directory: string, schema: ConfigSchema, preventAutoLoad?: boolean
14
- }): FileConfig<ConfigSchema> {
15
- const cfg = makeConfig(opts.schema) as FileConfig<ConfigSchema>;
16
- // imperative overrides here to not lose the Proxy
17
- cfg._meta.configDir = opts.directory;
18
- cfg._meta.envVar = opts.envVar;
19
-
20
- if (!opts.preventAutoLoad) {
21
- queueMicrotask(async () => {
22
- try {
23
- await loadFileIntoConfig(cfg);
24
- } catch (e) {
25
- console.error(e);
26
- process.exit(1);
27
- }
28
- });
29
- }
30
-
31
- return cfg;
32
- }
33
-
34
- export async function loadFileIntoConfig<ConfigSchema extends ZodObject>(config: FileConfig<ConfigSchema>) {
35
- const config_name = process.env[config._meta.envVar];
36
- if (config_name === undefined) {
37
- throw new ConfigError(`Please set the environment variable ${config._meta.envVar}`);
38
- }
39
- const path = join(config._meta.configDir, `${config_name}.js`);
40
- await import(path);
41
- if (!config._meta.isUsable) {
42
- console.warn(`Imported ${path} but config was still not marked as usable`);
43
- }
44
- }
45
-
package/tsconfig.json DELETED
@@ -1,27 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "lib": [
4
- "es2024",
5
- "ESNext.Array",
6
- "ESNext.Collection",
7
- "ESNext.Iterator",
8
- "ESNext.Promise"
9
- ],
10
- "module": "nodenext",
11
- "target": "es2024",
12
-
13
- "strict": true,
14
- "esModuleInterop": true,
15
- "skipLibCheck": true,
16
- "moduleResolution": "node16",
17
-
18
- "declaration": true,
19
- "sourceMap": true,
20
- "declarationMap": true,
21
-
22
- "outDir": "./dist",
23
- "rootDir": "./src"
24
- },
25
- "include": ["src/**/*", "__test__"],
26
- "exclude": ["node_modules", "dist"]
27
- }