@zerotal/settings 1.18.0 → 1.18.1
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 +25 -0
- package/README.md +2 -1
- package/api-surface.md +4 -4
- package/package.json +5 -5
- package/src/SettingsFake.ts +69 -7
- package/src/provider/SettingsProvider.ts +6 -0
- package/src/setting.ts +10 -1
package/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,31 @@ follows the Zerotal monorepo's unified versioning.
|
|
|
8
8
|
|
|
9
9
|
## [Unreleased]
|
|
10
10
|
|
|
11
|
+
## [1.18.1] — 2026-11-26
|
|
12
|
+
|
|
13
|
+
### Fixed
|
|
14
|
+
|
|
15
|
+
- **A `validate` check that throws refuses the value.** A check written for the declared shape
|
|
16
|
+
can throw on another one: `setFromText()` on a JSON setting parses `"null"` to `null`, and the
|
|
17
|
+
documented `(v) => (Array.isArray(v.days) ? …)` then threw a `TypeError`, a 500 from the
|
|
18
|
+
settings screen. It is an `InvalidSettingValueError` (422) now, its reason naming the check's
|
|
19
|
+
message.
|
|
20
|
+
- **A settings group works in a running app.** `SettingsProvider` bound `settings` lazily and
|
|
21
|
+
never resolved it, and the typed groups and the `Settings` facade read the binding with
|
|
22
|
+
`makeSync`, which refuses a singleton nothing has resolved. So with the provider registered,
|
|
23
|
+
every `billing.get(…)` in a request threw "Settings are not available: register
|
|
24
|
+
SettingsProvider". `SettingsFake` and the provider's tests used other paths and passed. The
|
|
25
|
+
provider resolves the manager while booting now.
|
|
26
|
+
|
|
27
|
+
### Added
|
|
28
|
+
|
|
29
|
+
- **`SettingsFake` checks keys and values at compile time when given the group.**
|
|
30
|
+
`SettingsFake.install(billing, { "billing.paymentTermsDays": 7 })` takes only the keys
|
|
31
|
+
`billing` declares, each with its declared type, in the overrides and in every later
|
|
32
|
+
`override()` and `release()` — the way the group's own `get()` and `set()` do. Pass several
|
|
33
|
+
groups as an array. `SettingsFake.install({ … })` without a group still takes any key and
|
|
34
|
+
rejects an unknown one when the test runs.
|
|
35
|
+
|
|
11
36
|
## [1.18.0] — 2026-11-26
|
|
12
37
|
|
|
13
38
|
### Added
|
package/README.md
CHANGED
|
@@ -59,7 +59,8 @@ In a test:
|
|
|
59
59
|
```ts
|
|
60
60
|
import { SettingsFake } from "@zerotal/settings";
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
// Given the group, keys and values are checked against its definitions.
|
|
63
|
+
const settings = SettingsFake.install(billing, { "billing.paymentTermsDays": 7 });
|
|
63
64
|
// …
|
|
64
65
|
settings.restore();
|
|
65
66
|
```
|
package/api-surface.md
CHANGED
|
@@ -52,16 +52,16 @@ class SettingsError = {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
class SettingsFake = {
|
|
55
|
-
new (app: Application, original: RegistryEntry | undefined, store: MemoryStore): SettingsFake
|
|
56
|
-
static install: (overrides?: Record<string, unknown>)
|
|
55
|
+
new <D extends SettingDefinitions = SettingDefinitions>(app: Application, original: RegistryEntry | undefined, store: MemoryStore): SettingsFake<D>
|
|
56
|
+
static install: { <G extends DefinesSettings>(group: G, overrides?: Partial<SettingValues<G['definitions']>> | undefined): SettingsFake<G['definitions']>; <const G extends readonly DefinesSettings[]>(groups: G, overrides?: Partial<SettingValues<MergedDefinitions<G>>> | undefined): SettingsFake<MergedDefinitions<G>>; (overrides?: Record<string, unknown> | undefined): SettingsFake;}
|
|
57
57
|
all: (scope?: SettingsScope) => Promise<ResolvedSetting[]>
|
|
58
58
|
definitions: () => [string, SettingDefinition][]
|
|
59
59
|
get: (key: string, scope?: SettingsScope) => Promise<unknown>
|
|
60
60
|
hide: (key: string, options?: SettingWriteOptions) => Promise<boolean>
|
|
61
61
|
history: (key: string, options?: SettingHistoryOptions) => Promise<SettingHistoryEntry[]>
|
|
62
|
-
override: (key:
|
|
62
|
+
override: <K extends keyof D & string>(key: K, value: SettingValue<D[K]>) => SettingsFake<D>
|
|
63
63
|
readonly memory: MemoryStore
|
|
64
|
-
release: (key:
|
|
64
|
+
release: <K extends keyof D & string>(key: K) => SettingsFake<D>
|
|
65
65
|
resolve: (key: string, scope?: SettingsScope) => Promise<ResolvedSetting>
|
|
66
66
|
resolveMany: (keys: readonly string[], scope?: SettingsScope) => Promise<ResolvedSetting[]>
|
|
67
67
|
restore: () => void
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zerotal/settings",
|
|
3
|
-
"version": "1.18.
|
|
3
|
+
"version": "1.18.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"maturity": "beta",
|
|
6
6
|
"maturityReview": "1.20.0",
|
|
@@ -31,12 +31,12 @@
|
|
|
31
31
|
"typecheck": "tsc --noEmit"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@zerotal/core": "1.18.
|
|
35
|
-
"@zerotal/orm": "1.18.
|
|
36
|
-
"@zerotal/tenancy": "1.18.
|
|
34
|
+
"@zerotal/core": "1.18.1",
|
|
35
|
+
"@zerotal/orm": "1.18.1",
|
|
36
|
+
"@zerotal/tenancy": "1.18.1"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@zerotal/audit": "1.18.
|
|
39
|
+
"@zerotal/audit": "1.18.1",
|
|
40
40
|
"typescript": "^5.8.0"
|
|
41
41
|
},
|
|
42
42
|
"description": "Per-tenant settings for Zerotal: typed defaults in code, overrides per tenant and per user, hidden but never deleted, audited, with an admin screen.",
|
package/src/SettingsFake.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Application, currentApp } from "@zerotal/core";
|
|
2
|
+
import type { SettingDefinitions, SettingValue, SettingValues } from "./defineSettings.ts";
|
|
2
3
|
import { _definitionOf } from "./registry.ts";
|
|
3
4
|
import { SettingsManager } from "./SettingsManager.ts";
|
|
4
5
|
import type { ResolvedSetting } from "./SettingsManager.ts";
|
|
@@ -9,6 +10,38 @@ import type { StoredSetting } from "./stores/SettingsStore.ts";
|
|
|
9
10
|
/** One entry of the container's registry, as `install()` saves it and `restore()` puts it back. */
|
|
10
11
|
type RegistryEntry = Application["container"]["registry"] extends Map<unknown, infer B> ? B : never;
|
|
11
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Anything carrying setting definitions: what `defineSettings()` returns, or a
|
|
15
|
+
* plain `{ definitions }`. Structural, so the fake does not depend on the group's
|
|
16
|
+
* methods.
|
|
17
|
+
*/
|
|
18
|
+
interface DefinesSettings<D extends SettingDefinitions = SettingDefinitions> {
|
|
19
|
+
readonly definitions: D;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Every key of several groups, with each key's own type. */
|
|
23
|
+
type MergedDefinitions<G extends readonly DefinesSettings[]> = _Intersect<G[number]["definitions"]>;
|
|
24
|
+
|
|
25
|
+
/** A union of definition sets, as one set holding every key. */
|
|
26
|
+
type _Intersect<U> = (U extends unknown ? (u: U) => void : never) extends (i: infer I) => void
|
|
27
|
+
? I extends SettingDefinitions
|
|
28
|
+
? I
|
|
29
|
+
: never
|
|
30
|
+
: never;
|
|
31
|
+
|
|
32
|
+
function _isGroup(value: unknown): value is DefinesSettings {
|
|
33
|
+
return (
|
|
34
|
+
typeof value === "object" &&
|
|
35
|
+
value !== null &&
|
|
36
|
+
!Array.isArray(value) &&
|
|
37
|
+
"definitions" in value &&
|
|
38
|
+
typeof value.definitions === "object" &&
|
|
39
|
+
value.definitions !== null &&
|
|
40
|
+
"get" in value &&
|
|
41
|
+
typeof value.get === "function"
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
12
45
|
/**
|
|
13
46
|
* Settings for a test: an in-memory store in place of the table, and a way to
|
|
14
47
|
* pin a setting to a value whoever reads it.
|
|
@@ -18,12 +51,19 @@ type RegistryEntry = Application["container"]["registry"] extends Map<unknown, i
|
|
|
18
51
|
* value is checked against its definition, and wins over every level until it is
|
|
19
52
|
* released or the fake is restored.
|
|
20
53
|
*
|
|
54
|
+
* Given the groups `defineSettings()` returned, the fake takes only their keys,
|
|
55
|
+
* and each value as its declared type, the way the groups themselves do. Given
|
|
56
|
+
* none, it takes any key and checks it when the test runs.
|
|
57
|
+
*
|
|
21
58
|
* @example
|
|
22
|
-
* const settings = SettingsFake.install({ "billing.paymentTermsDays": 7 });
|
|
59
|
+
* const settings = SettingsFake.install(billing, { "billing.paymentTermsDays": 7 });
|
|
23
60
|
* expect(await billing.get("billing.paymentTermsDays")).toBe(7);
|
|
61
|
+
* settings.override("billing.paymentTermsDays", "7"); // type error: a number
|
|
24
62
|
* settings.restore(); // in afterEach
|
|
25
63
|
*/
|
|
26
|
-
export class SettingsFake
|
|
64
|
+
export class SettingsFake<
|
|
65
|
+
D extends SettingDefinitions = SettingDefinitions,
|
|
66
|
+
> extends SettingsManager {
|
|
27
67
|
/** The store behind the fake, for asserting on what was written. */
|
|
28
68
|
readonly memory: MemoryStore;
|
|
29
69
|
private readonly _pins = new Map<string, unknown>();
|
|
@@ -37,12 +77,34 @@ export class SettingsFake extends SettingsManager {
|
|
|
37
77
|
this._original = original;
|
|
38
78
|
}
|
|
39
79
|
|
|
40
|
-
/**
|
|
41
|
-
|
|
80
|
+
/**
|
|
81
|
+
* Replace the `settings` binding with a fake, pinning `overrides` from the start.
|
|
82
|
+
*
|
|
83
|
+
* Pass the group `defineSettings()` returned (or several, in an array) first, and
|
|
84
|
+
* the overrides and every later `override()` and `release()` are checked against
|
|
85
|
+
* its keys and value types at compile time. With overrides alone, keys are checked
|
|
86
|
+
* when the test runs.
|
|
87
|
+
*/
|
|
88
|
+
static install<G extends DefinesSettings>(
|
|
89
|
+
group: G,
|
|
90
|
+
overrides?: Partial<SettingValues<G["definitions"]>> | undefined,
|
|
91
|
+
): SettingsFake<G["definitions"]>;
|
|
92
|
+
static install<const G extends readonly DefinesSettings[]>(
|
|
93
|
+
groups: G,
|
|
94
|
+
overrides?: Partial<SettingValues<MergedDefinitions<G>>> | undefined,
|
|
95
|
+
): SettingsFake<MergedDefinitions<G>>;
|
|
96
|
+
static install(overrides?: Record<string, unknown> | undefined): SettingsFake;
|
|
97
|
+
static install(
|
|
98
|
+
groupsOrOverrides?: DefinesSettings | readonly DefinesSettings[] | Record<string, unknown>,
|
|
99
|
+
overrides?: Record<string, unknown>,
|
|
100
|
+
): SettingsFake {
|
|
101
|
+
const typed = Array.isArray(groupsOrOverrides) || _isGroup(groupsOrOverrides);
|
|
102
|
+
const pins =
|
|
103
|
+
(typed ? overrides : (groupsOrOverrides as Record<string, unknown> | undefined)) ?? {};
|
|
42
104
|
const app = currentApp();
|
|
43
105
|
const original = app.container.registry.get("settings");
|
|
44
106
|
const fake = new SettingsFake(app, original, new MemoryStore());
|
|
45
|
-
for (const [key, value] of Object.entries(
|
|
107
|
+
for (const [key, value] of Object.entries(pins)) fake.override(key, value);
|
|
46
108
|
app.container.value("settings", fake);
|
|
47
109
|
return fake;
|
|
48
110
|
}
|
|
@@ -61,13 +123,13 @@ export class SettingsFake extends SettingsManager {
|
|
|
61
123
|
* must fit the setting's definition, so a test cannot pin what the app could
|
|
62
124
|
* never hold.
|
|
63
125
|
*/
|
|
64
|
-
override(key:
|
|
126
|
+
override<K extends keyof D & string>(key: K, value: SettingValue<D[K]>): this {
|
|
65
127
|
this._pins.set(key, _definitionOf(key).parse(value, key));
|
|
66
128
|
return this;
|
|
67
129
|
}
|
|
68
130
|
|
|
69
131
|
/** Release a pin, so the setting resolves normally again. */
|
|
70
|
-
release(key:
|
|
132
|
+
release<K extends keyof D & string>(key: K): this {
|
|
71
133
|
this._pins.delete(key);
|
|
72
134
|
return this;
|
|
73
135
|
}
|
|
@@ -39,6 +39,12 @@ export class SettingsProvider extends ServiceProvider {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
override async onBooting(): Promise<void> {
|
|
42
|
+
// Resolved here, not on first use: the typed groups and the `Settings` facade read
|
|
43
|
+
// the binding with `makeSync`, which refuses a singleton nothing has resolved yet.
|
|
44
|
+
// Left lazy, every `billing.get(…)` in an app threw "Settings are not available"
|
|
45
|
+
// with this provider registered.
|
|
46
|
+
await this.app.container.make("settings");
|
|
47
|
+
|
|
42
48
|
// The panel mounts its routes once booting finishes, so the screen is
|
|
43
49
|
// contributed here rather than in onBooted, which would race the mount.
|
|
44
50
|
_installSettingsAdmin(this.app);
|
package/src/setting.ts
CHANGED
|
@@ -114,7 +114,16 @@ function build<T>(
|
|
|
114
114
|
parse(value, key, fromText = false) {
|
|
115
115
|
const result = check(value, fromText);
|
|
116
116
|
if (isRefusal(result)) throw new InvalidSettingValueError(key, result[INVALID]);
|
|
117
|
-
|
|
117
|
+
// A check written for the declared shape can throw on a value of another one
|
|
118
|
+
// (`null` from a JSON field reaches `v.days`). That refuses the value too, rather
|
|
119
|
+
// than escaping as a 500 from the screen that sent it.
|
|
120
|
+
let reason: string | undefined | void;
|
|
121
|
+
try {
|
|
122
|
+
reason = options.validate?.(result);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
125
|
+
throw new InvalidSettingValueError(key, `its check failed (${message})`);
|
|
126
|
+
}
|
|
118
127
|
if (reason) throw new InvalidSettingValueError(key, reason);
|
|
119
128
|
return result;
|
|
120
129
|
},
|