@jondotsoy/configs 0.1.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/LICENSE +21 -0
- package/README.md +296 -0
- package/dist/config.types.d.ts +177 -0
- package/dist/configs.d.ts +21 -0
- package/dist/configs.js +847 -0
- package/dist/errors.d.ts +2 -0
- package/dist/sources/env.d.ts +36 -0
- package/dist/sources/env.js +190 -0
- package/dist/sources/fetch.d.ts +15 -0
- package/dist/sources/fetch.js +182 -0
- package/dist/sources/file.d.ts +28 -0
- package/dist/sources/file.js +278 -0
- package/dist/sources/literal.d.ts +3 -0
- package/dist/sources/literal.js +149 -0
- package/dist/sources/source.d.ts +24 -0
- package/dist/sources/sse.d.ts +22 -0
- package/dist/sources/sse.js +236 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/schema.d.ts +1 -0
- package/dist/types/source.d.ts +19 -0
- package/dist/utils/data-types.d.ts +20 -0
- package/dist/utils/dotenv.d.ts +13 -0
- package/dist/utils/store.d.ts +44 -0
- package/package.json +87 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Passed to `start()`, mirroring a `ReadableStreamDefaultController`. */
|
|
2
|
+
export interface SourceControl<T> {
|
|
3
|
+
/** Publishes a new full-tree snapshot. */
|
|
4
|
+
set(value: T): void;
|
|
5
|
+
/** Marks the source as done; no further `set()` calls are expected. */
|
|
6
|
+
close(): void;
|
|
7
|
+
}
|
|
8
|
+
/** The object passed to `new Source(...)`, mirroring an `UnderlyingSource` for `ReadableStream`. */
|
|
9
|
+
export interface UnderlyingSource<T> {
|
|
10
|
+
start(control: SourceControl<T>): void | Promise<void>;
|
|
11
|
+
close?(): void | Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* When present, every `control.set(incoming)` call runs `incoming` through `reduce` (along with
|
|
14
|
+
* the last published value, `null` before the first `set()`) instead of publishing it as-is —
|
|
15
|
+
* so a source that only ever hands `start()` a partial patch, like an SSE message, can publish
|
|
16
|
+
* the merged result without keeping its own accumulator variable around.
|
|
17
|
+
*/
|
|
18
|
+
reduce?(incoming: T, previous: T | null): T;
|
|
19
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface DataType<T> {
|
|
2
|
+
from(value: unknown): T;
|
|
3
|
+
}
|
|
4
|
+
declare const dataTypesRegistry: {
|
|
5
|
+
number: DataType<number>;
|
|
6
|
+
string: DataType<string>;
|
|
7
|
+
boolean: DataType<boolean>;
|
|
8
|
+
list: DataType<string[]>;
|
|
9
|
+
};
|
|
10
|
+
export type DataTypeName = keyof typeof dataTypesRegistry;
|
|
11
|
+
/** Looks up a converter by name (e.g. from a schema's `field.type`); an unknown name throws. */
|
|
12
|
+
declare function factory(type: string): (typeof dataTypesRegistry)[DataTypeName];
|
|
13
|
+
export declare const DataTypes: {
|
|
14
|
+
factory: typeof factory;
|
|
15
|
+
number: DataType<number>;
|
|
16
|
+
string: DataType<string>;
|
|
17
|
+
boolean: DataType<boolean>;
|
|
18
|
+
list: DataType<string[]>;
|
|
19
|
+
};
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses dotenv-formatted text (`KEY=VALUE` lines) into a flat string map, following
|
|
3
|
+
* motdotla/dotenv's syntax (the reference implementation dotenv.org points to). Tolerates
|
|
4
|
+
* whitespace around `=` (`FOO = bar`, `FOO= bar`, `FOO=bar`), a single-, double-, or
|
|
5
|
+
* backtick-quoted value (quotes stripped; `\n`/`\r` expand to real newline/carriage-return
|
|
6
|
+
* characters, but only inside double quotes), and a trailing `# comment` on an unquoted value.
|
|
7
|
+
* Blank lines, whole-line `#` comments, and a line with no `=` are skipped.
|
|
8
|
+
*/
|
|
9
|
+
declare function parse(text: string): Record<string, string>;
|
|
10
|
+
export declare const DotEnv: {
|
|
11
|
+
parse: typeof parse;
|
|
12
|
+
};
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** Return `Unsubscribe` for cleanup run when this subscriber unsubscribes; any other return value is ignored. */
|
|
2
|
+
export type Subscriber<T> = (value: T) => unknown;
|
|
3
|
+
export type Unsubscribe = () => void;
|
|
4
|
+
export type EventListener = () => void;
|
|
5
|
+
export declare class Store<T> {
|
|
6
|
+
private value;
|
|
7
|
+
private readonly subscribers;
|
|
8
|
+
private readonly cleanups;
|
|
9
|
+
private readonly mountListeners;
|
|
10
|
+
private readonly unmountListeners;
|
|
11
|
+
constructor(initial: T);
|
|
12
|
+
get(): T;
|
|
13
|
+
set(next: T): void;
|
|
14
|
+
/** Calls `subscriber` immediately with the current value, then on every subsequent `set`. */
|
|
15
|
+
subscribe(subscriber: Subscriber<T>): Unsubscribe;
|
|
16
|
+
/** Calls `subscriber` only on subsequent `set` calls, not with the current value. */
|
|
17
|
+
listen(subscriber: Subscriber<T>): Unsubscribe;
|
|
18
|
+
/** Runs `subscriber` and stores its returned cleanup, if any, to run on unsubscribe. */
|
|
19
|
+
private runSubscriber;
|
|
20
|
+
/** Fires once a first subscriber is registered (via `subscribe` or `listen`). */
|
|
21
|
+
private onMount;
|
|
22
|
+
/** Fires once the last subscriber is removed. */
|
|
23
|
+
private onUnmount;
|
|
24
|
+
private addSubscriber;
|
|
25
|
+
private removeSubscriber;
|
|
26
|
+
/**
|
|
27
|
+
* Runs `callback` on mount; if it returns a function, that function runs on unmount.
|
|
28
|
+
* Returns an unsubscribe that tears down the wiring (running any pending cleanup first).
|
|
29
|
+
*/
|
|
30
|
+
static onMount<T>(store: Store<T>, callback: () => void | Unsubscribe): Unsubscribe;
|
|
31
|
+
}
|
|
32
|
+
declare function create<T>(initial: T): Store<T>;
|
|
33
|
+
/**
|
|
34
|
+
* Derives a read-through `Store<R>` from `source` via `selector`. It only subscribes to
|
|
35
|
+
* `source` while it has subscribers of its own, so mounting/unmounting the computed store
|
|
36
|
+
* mounts/unmounts `source` in turn.
|
|
37
|
+
*/
|
|
38
|
+
declare function computed<T, R>(source: Store<T>, selector: (value: T) => R): Store<R>;
|
|
39
|
+
export declare const store: {
|
|
40
|
+
create: typeof create;
|
|
41
|
+
computed: typeof computed;
|
|
42
|
+
onMount: typeof Store.onMount;
|
|
43
|
+
};
|
|
44
|
+
export default store;
|
package/package.json
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jondotsoy/configs",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A reactive, typed configuration library backed by pluggable sources (env, fetch, sse, file, literal).",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"config",
|
|
7
|
+
"configuration",
|
|
8
|
+
"env",
|
|
9
|
+
"environment-variables",
|
|
10
|
+
"reactive",
|
|
11
|
+
"typescript",
|
|
12
|
+
"source",
|
|
13
|
+
"sse",
|
|
14
|
+
"fetch",
|
|
15
|
+
"file",
|
|
16
|
+
"json",
|
|
17
|
+
"dotenv"
|
|
18
|
+
],
|
|
19
|
+
"module": "src/configs.ts",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"author": {
|
|
24
|
+
"name": "Jonathan Delgado",
|
|
25
|
+
"email": "hi@jon.soy",
|
|
26
|
+
"url": "https://jon.soy"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/JonDotsoy/configs#readme",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/JonDotsoy/configs.git"
|
|
32
|
+
},
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/JonDotsoy/configs/issues"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist"
|
|
41
|
+
],
|
|
42
|
+
"exports": {
|
|
43
|
+
".": {
|
|
44
|
+
"_entryPoint": "./src/configs.ts",
|
|
45
|
+
"types": "./dist/configs.d.ts",
|
|
46
|
+
"import": "./dist/configs.js"
|
|
47
|
+
},
|
|
48
|
+
"./sources/env": {
|
|
49
|
+
"_entryPoint": "./src/sources/env.ts",
|
|
50
|
+
"types": "./dist/sources/env.d.ts",
|
|
51
|
+
"import": "./dist/sources/env.js"
|
|
52
|
+
},
|
|
53
|
+
"./sources/fetch": {
|
|
54
|
+
"_entryPoint": "./src/sources/fetch.ts",
|
|
55
|
+
"types": "./dist/sources/fetch.d.ts",
|
|
56
|
+
"import": "./dist/sources/fetch.js"
|
|
57
|
+
},
|
|
58
|
+
"./sources/sse": {
|
|
59
|
+
"_entryPoint": "./src/sources/sse.ts",
|
|
60
|
+
"types": "./dist/sources/sse.d.ts",
|
|
61
|
+
"import": "./dist/sources/sse.js"
|
|
62
|
+
},
|
|
63
|
+
"./sources/file": {
|
|
64
|
+
"_entryPoint": "./src/sources/file.ts",
|
|
65
|
+
"types": "./dist/sources/file.d.ts",
|
|
66
|
+
"import": "./dist/sources/file.js"
|
|
67
|
+
},
|
|
68
|
+
"./sources/literal": {
|
|
69
|
+
"_entryPoint": "./src/sources/literal.ts",
|
|
70
|
+
"types": "./dist/sources/literal.d.ts",
|
|
71
|
+
"import": "./dist/sources/literal.js"
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
"scripts": {
|
|
75
|
+
"build": "bash scripts/build.sh",
|
|
76
|
+
"build:types": "tsc -p tsconfig.build.json",
|
|
77
|
+
"test:pack": "bun run scripts/manual-pack-test.ts",
|
|
78
|
+
"test:integration": "bun test ./test/integration/runtime-imports.ts"
|
|
79
|
+
},
|
|
80
|
+
"devDependencies": {
|
|
81
|
+
"@types/bun": "latest",
|
|
82
|
+
"typescript": "^5"
|
|
83
|
+
},
|
|
84
|
+
"peerDependencies": {
|
|
85
|
+
"typescript": "^5"
|
|
86
|
+
}
|
|
87
|
+
}
|