@openbrf/plugin-sdk 0.0.0-stage → 1.2.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 +661 -0
- package/LICENSE-EXCEPTION.md +28 -0
- package/README.md +94 -2
- package/dist/abort-signal.d.ts +23 -0
- package/dist/abort-signal.d.ts.map +1 -0
- package/dist/abort-signal.js +2 -0
- package/dist/abort-signal.js.map +1 -0
- package/dist/action-refusal.d.ts +101 -0
- package/dist/action-refusal.d.ts.map +1 -0
- package/dist/action-refusal.js +96 -0
- package/dist/action-refusal.js.map +1 -0
- package/dist/action-schema.d.ts +42 -0
- package/dist/action-schema.d.ts.map +1 -0
- package/dist/action-schema.js +67 -0
- package/dist/action-schema.js.map +1 -0
- package/dist/actions.d.ts +287 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/actions.js +68 -0
- package/dist/actions.js.map +1 -0
- package/dist/api-version.d.ts +23 -0
- package/dist/api-version.d.ts.map +1 -0
- package/dist/api-version.js +27 -0
- package/dist/api-version.js.map +1 -0
- package/dist/blocks.d.ts +310 -0
- package/dist/blocks.d.ts.map +1 -0
- package/dist/blocks.js +185 -0
- package/dist/blocks.js.map +1 -0
- package/dist/catalog.d.ts +707 -0
- package/dist/catalog.d.ts.map +1 -0
- package/dist/catalog.js +567 -0
- package/dist/catalog.js.map +1 -0
- package/dist/define-plugin.d.ts +14 -0
- package/dist/define-plugin.d.ts.map +1 -0
- package/dist/define-plugin.js +15 -0
- package/dist/define-plugin.js.map +1 -0
- package/dist/entitlements.d.ts +117 -0
- package/dist/entitlements.d.ts.map +1 -0
- package/dist/entitlements.js +62 -0
- package/dist/entitlements.js.map +1 -0
- package/dist/extensions.d.ts +183 -0
- package/dist/extensions.d.ts.map +1 -0
- package/dist/extensions.js +67 -0
- package/dist/extensions.js.map +1 -0
- package/dist/finances.d.ts +294 -0
- package/dist/finances.d.ts.map +1 -0
- package/dist/finances.js +56 -0
- package/dist/finances.js.map +1 -0
- package/dist/findings.d.ts +156 -0
- package/dist/findings.d.ts.map +1 -0
- package/dist/findings.js +146 -0
- package/dist/findings.js.map +1 -0
- package/dist/host.d.ts +827 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +80 -0
- package/dist/host.js.map +1 -0
- package/dist/imports.d.ts +104 -0
- package/dist/imports.d.ts.map +1 -0
- package/dist/imports.js +70 -0
- package/dist/imports.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/integrity.d.ts +38 -0
- package/dist/integrity.d.ts.map +1 -0
- package/dist/integrity.js +105 -0
- package/dist/integrity.js.map +1 -0
- package/dist/localized-text.d.ts +18 -0
- package/dist/localized-text.d.ts.map +1 -0
- package/dist/localized-text.js +17 -0
- package/dist/localized-text.js.map +1 -0
- package/dist/manifest.d.ts +1122 -0
- package/dist/manifest.d.ts.map +1 -0
- package/dist/manifest.js +1118 -0
- package/dist/manifest.js.map +1 -0
- package/dist/module.d.ts +35 -0
- package/dist/module.d.ts.map +1 -0
- package/dist/module.js +2 -0
- package/dist/module.js.map +1 -0
- package/dist/package-check.d.ts +66 -0
- package/dist/package-check.d.ts.map +1 -0
- package/dist/package-check.js +755 -0
- package/dist/package-check.js.map +1 -0
- package/dist/permissions.d.ts +224 -0
- package/dist/permissions.d.ts.map +1 -0
- package/dist/permissions.js +277 -0
- package/dist/permissions.js.map +1 -0
- package/dist/recipients.d.ts +139 -0
- package/dist/recipients.d.ts.map +1 -0
- package/dist/recipients.js +223 -0
- package/dist/recipients.js.map +1 -0
- package/dist/records.d.ts +130 -0
- package/dist/records.d.ts.map +1 -0
- package/dist/records.js +85 -0
- package/dist/records.js.map +1 -0
- package/dist/require-capability.d.ts +37 -0
- package/dist/require-capability.d.ts.map +1 -0
- package/dist/require-capability.js +88 -0
- package/dist/require-capability.js.map +1 -0
- package/dist/settings-schema.d.ts +130 -0
- package/dist/settings-schema.d.ts.map +1 -0
- package/dist/settings-schema.js +272 -0
- package/dist/settings-schema.js.map +1 -0
- package/dist/standard-schema.d.ts +43 -0
- package/dist/standard-schema.d.ts.map +1 -0
- package/dist/standard-schema.js +2 -0
- package/dist/standard-schema.js.map +1 -0
- package/package.json +55 -3
- package/src/abort-signal.ts +22 -0
- package/src/action-refusal.ts +178 -0
- package/src/action-schema.ts +89 -0
- package/src/actions.ts +344 -0
- package/src/api-version.ts +28 -0
- package/src/blocks.ts +289 -0
- package/src/catalog.ts +679 -0
- package/src/define-plugin.ts +18 -0
- package/src/entitlements.ts +150 -0
- package/src/extensions.ts +212 -0
- package/src/finances.ts +317 -0
- package/src/findings.ts +163 -0
- package/src/host.ts +894 -0
- package/src/imports.ts +135 -0
- package/src/index.ts +316 -0
- package/src/integrity.ts +125 -0
- package/src/localized-text.ts +19 -0
- package/src/manifest.ts +1412 -0
- package/src/module.ts +38 -0
- package/src/package-check.ts +894 -0
- package/src/permissions.ts +295 -0
- package/src/recipients.ts +294 -0
- package/src/records.ts +172 -0
- package/src/require-capability.ts +106 -0
- package/src/settings-schema.ts +334 -0
- package/src/standard-schema.ts +39 -0
package/src/records.ts
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A plugin's own records (ADR 0033).
|
|
3
|
+
*
|
|
4
|
+
* A small keyed store: JSON values under a collection and a key, belonging to
|
|
5
|
+
* the plugin and to nothing else. It is for what a plugin has of its own to
|
|
6
|
+
* remember and cannot fit into a settings form - a list of screens, each with
|
|
7
|
+
* its own layout and rotation - and never for people. The data subject access
|
|
8
|
+
* report, purging, erasure and restriction all read the core's own tables, and
|
|
9
|
+
* a plugin's JSON is opaque to every one of them, so a name, an email address
|
|
10
|
+
* or anything copied out of the register kept here would be data the
|
|
11
|
+
* association holds and cannot report, purge or restrict. The host cannot tell
|
|
12
|
+
* a name from a label, so the rule is declared rather than enforced, with one
|
|
13
|
+
* exception: a personal identity number is refused on every write.
|
|
14
|
+
*
|
|
15
|
+
* The errors below are thrown by the host's copy of this package. A plugin
|
|
16
|
+
* imports the SDK for its types only, so it tells them apart by `name` rather
|
|
17
|
+
* than with `instanceof`.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** A value JSON can carry. */
|
|
21
|
+
export type JsonValue =
|
|
22
|
+
| null
|
|
23
|
+
| boolean
|
|
24
|
+
| number
|
|
25
|
+
| string
|
|
26
|
+
| readonly JsonValue[]
|
|
27
|
+
| { readonly [key: string]: JsonValue };
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A collection: lowercase letters, digits and single hyphens, at most 32
|
|
31
|
+
* characters, starting and ending with a letter or a digit.
|
|
32
|
+
*/
|
|
33
|
+
export const PLUGIN_RECORD_COLLECTION_PATTERN =
|
|
34
|
+
/^(?=.{1,32}$)[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
35
|
+
|
|
36
|
+
/** A key: 1 to 128 letters, digits, `.`, `_`, `:` or `-`. */
|
|
37
|
+
export const PLUGIN_RECORD_KEY_PATTERN = /^[A-Za-z0-9._:-]{1,128}$/;
|
|
38
|
+
|
|
39
|
+
/** What one plugin may hold, and the most one call reads. */
|
|
40
|
+
export const PLUGIN_RECORD_LIMITS = {
|
|
41
|
+
/** Bytes of one value, serialised as JSON in UTF-8. */
|
|
42
|
+
valueBytes: 64 * 1024,
|
|
43
|
+
/** Records per plugin, across every collection. */
|
|
44
|
+
records: 10_000,
|
|
45
|
+
/** Bytes per plugin, across every collection, counted as for one value. */
|
|
46
|
+
totalBytes: 16 * 1024 * 1024,
|
|
47
|
+
/** Records one `list` returns, and its default. */
|
|
48
|
+
listLimit: 200,
|
|
49
|
+
/**
|
|
50
|
+
* Writes of one plugin the host holds at once, the one under way and those
|
|
51
|
+
* waiting for their turn. A write sent past it is refused, not held.
|
|
52
|
+
*/
|
|
53
|
+
queuedWrites: 100,
|
|
54
|
+
} as const;
|
|
55
|
+
|
|
56
|
+
export interface PluginRecord<Value extends JsonValue = JsonValue> {
|
|
57
|
+
readonly collection: string;
|
|
58
|
+
readonly key: string;
|
|
59
|
+
readonly value: Value;
|
|
60
|
+
/**
|
|
61
|
+
* A counter, 1 when the record is written first and one more on every write
|
|
62
|
+
* after that. Not `updatedAt`, because two writes inside one millisecond
|
|
63
|
+
* carry the same time.
|
|
64
|
+
*/
|
|
65
|
+
readonly revision: number;
|
|
66
|
+
/** When the record was last written, as an ISO 8601 instant. */
|
|
67
|
+
readonly updatedAt: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The plugin's own records. Needs no permission, and refuses while the plugin
|
|
72
|
+
* is switched off, as every host service does.
|
|
73
|
+
*
|
|
74
|
+
* A collection or a key outside its pattern is refused with a RangeError, and a
|
|
75
|
+
* value JSON cannot carry exactly - `undefined`, a function, `NaN`, a `Date`, a
|
|
76
|
+
* class instance, a lone surrogate or a NUL character - with a TypeError.
|
|
77
|
+
*/
|
|
78
|
+
export interface PluginRecords {
|
|
79
|
+
get<Value extends JsonValue>(
|
|
80
|
+
collection: string,
|
|
81
|
+
key: string,
|
|
82
|
+
): Promise<PluginRecord<Value> | null>;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* In key order, from after `after` when it is given. At most 200 per call,
|
|
86
|
+
* which is also the default.
|
|
87
|
+
*/
|
|
88
|
+
list<Value extends JsonValue>(
|
|
89
|
+
collection: string,
|
|
90
|
+
options?: { after?: string; limit?: number },
|
|
91
|
+
): Promise<readonly PluginRecord<Value>[]>;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Writes a value. With `expectedRevision`, only over that revision, and
|
|
95
|
+
* `null` means only where there is no record yet; otherwise it throws
|
|
96
|
+
* PluginRecordConflictError.
|
|
97
|
+
*
|
|
98
|
+
* A value carrying a personal identity number, in a string, a property name
|
|
99
|
+
* or a number, is refused with PluginRecordIdentityNumberError. A value
|
|
100
|
+
* over 64 KiB, and a write that would take the plugin past 10 000 records
|
|
101
|
+
* or 16 MiB, is refused with PluginRecordLimitError, and so is any write,
|
|
102
|
+
* an overwrite too, sent while 100 of the plugin's writes are already held,
|
|
103
|
+
* the one under way and those waiting.
|
|
104
|
+
*/
|
|
105
|
+
put<Value extends JsonValue>(
|
|
106
|
+
collection: string,
|
|
107
|
+
key: string,
|
|
108
|
+
value: Value,
|
|
109
|
+
options?: { expectedRevision?: number | null },
|
|
110
|
+
): Promise<PluginRecord<Value>>;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* True when there was a record to delete. With `expectedRevision`, only that
|
|
114
|
+
* revision is deleted, and another one throws PluginRecordConflictError; no
|
|
115
|
+
* record at all is still `false`.
|
|
116
|
+
*/
|
|
117
|
+
delete(
|
|
118
|
+
collection: string,
|
|
119
|
+
key: string,
|
|
120
|
+
options?: { expectedRevision?: number },
|
|
121
|
+
): Promise<boolean>;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** A write's `expectedRevision` did not match the record it found. */
|
|
125
|
+
export class PluginRecordConflictError extends Error {
|
|
126
|
+
constructor(
|
|
127
|
+
readonly pluginId: string,
|
|
128
|
+
/** The revision the store holds, or null when it holds no record. */
|
|
129
|
+
readonly actualRevision: number | null,
|
|
130
|
+
) {
|
|
131
|
+
super(
|
|
132
|
+
`Plugin "${pluginId}" wrote a record over a revision it no longer has.`,
|
|
133
|
+
);
|
|
134
|
+
this.name = "PluginRecordConflictError";
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export type PluginRecordLimit =
|
|
139
|
+
"valueBytes" | "records" | "totalBytes" | "queuedWrites";
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* A write, or a delete queued behind the writes, was past one of
|
|
143
|
+
* {@link PLUGIN_RECORD_LIMITS}, and nothing was changed.
|
|
144
|
+
*/
|
|
145
|
+
export class PluginRecordLimitError extends Error {
|
|
146
|
+
constructor(
|
|
147
|
+
readonly pluginId: string,
|
|
148
|
+
readonly limit: PluginRecordLimit,
|
|
149
|
+
) {
|
|
150
|
+
super(
|
|
151
|
+
`Plugin "${pluginId}" was refused a record operation past its ${limit} ` +
|
|
152
|
+
`limit of ${String(PLUGIN_RECORD_LIMITS[limit])}.`,
|
|
153
|
+
);
|
|
154
|
+
this.name = "PluginRecordLimitError";
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* A write carried a personal identity number.
|
|
160
|
+
*
|
|
161
|
+
* Neither the collection, the key nor the number is in the message: the error
|
|
162
|
+
* reaches the log, and the number is what the refusal is keeping out of it.
|
|
163
|
+
*/
|
|
164
|
+
export class PluginRecordIdentityNumberError extends Error {
|
|
165
|
+
constructor(readonly pluginId: string) {
|
|
166
|
+
super(
|
|
167
|
+
`Plugin "${pluginId}" wrote a record carrying a personal identity ` +
|
|
168
|
+
"number, which a plugin's records may not hold.",
|
|
169
|
+
);
|
|
170
|
+
this.name = "PluginRecordIdentityNumberError";
|
|
171
|
+
}
|
|
172
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Raising a plugin route above the capability floor the host gives it.
|
|
3
|
+
*
|
|
4
|
+
* The host reads one metadata key off a controller and off each of its routes,
|
|
5
|
+
* and answers a request only for a person who holds every capability listed
|
|
6
|
+
* there. A plugin's controllers get a floor from the plugin's own permissions
|
|
7
|
+
* (see "Routes" in the plugin contract); `RequireCapability` is how a route
|
|
8
|
+
* asks for more.
|
|
9
|
+
*
|
|
10
|
+
* Written without `@nestjs/common` on purpose, for the reason `definePlugin` is
|
|
11
|
+
* an identity function: the SDK is bundled into the plugin, and an import of
|
|
12
|
+
* its own would have to be resolved from the data volume. NestJS's
|
|
13
|
+
* `SetMetadata` does no more than this, on the same global `Reflect` the host's
|
|
14
|
+
* copy of NestJS writes to.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** The metadata key the host reads a route's or a controller's capabilities from. */
|
|
18
|
+
export const REQUIRED_CAPABILITIES_KEY = "openbrf:required-capabilities";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Every capability name the host knows, which is what a route may require.
|
|
22
|
+
*
|
|
23
|
+
* Listed here, not derived, because the SDK cannot import the host. The host
|
|
24
|
+
* checks the list against its own capabilities in a test, so a capability
|
|
25
|
+
* added there is not missing here for long; until the SDK is released with it,
|
|
26
|
+
* a plugin that needs the new name is a type error rather than a typo caught
|
|
27
|
+
* at request time.
|
|
28
|
+
*/
|
|
29
|
+
export const PLUGIN_ROUTE_CAPABILITIES = [
|
|
30
|
+
"association:manage",
|
|
31
|
+
"association:read",
|
|
32
|
+
"addressBook:read",
|
|
33
|
+
"addressBook:write",
|
|
34
|
+
"memberRegister:read",
|
|
35
|
+
"apartmentRegister:read",
|
|
36
|
+
"protectedData:reveal",
|
|
37
|
+
"registerReport:export",
|
|
38
|
+
"invitation:send",
|
|
39
|
+
"signupRequest:decide",
|
|
40
|
+
"self:manage",
|
|
41
|
+
"residentDirectory:read",
|
|
42
|
+
"issues:handle",
|
|
43
|
+
"issues:report",
|
|
44
|
+
"issues:configure",
|
|
45
|
+
"documents:manage",
|
|
46
|
+
"site:manage",
|
|
47
|
+
"news:comment",
|
|
48
|
+
"boardPosition:manage",
|
|
49
|
+
"bookings:book",
|
|
50
|
+
"bookings:manage",
|
|
51
|
+
"bookings:configure",
|
|
52
|
+
"events:manage",
|
|
53
|
+
"events:attend",
|
|
54
|
+
"systemRole:manage",
|
|
55
|
+
"motions:submit",
|
|
56
|
+
"motions:handle",
|
|
57
|
+
"meetings:manage",
|
|
58
|
+
"memberCharges:manage",
|
|
59
|
+
"fees:manage",
|
|
60
|
+
"dataProtection:manage",
|
|
61
|
+
"boardMailbox:handle",
|
|
62
|
+
"sublets:apply",
|
|
63
|
+
"sublets:handle",
|
|
64
|
+
"keyOrders:place",
|
|
65
|
+
"keyOrders:handle",
|
|
66
|
+
"chat:participate",
|
|
67
|
+
"chat:moderate",
|
|
68
|
+
"apartmentBinder:manage",
|
|
69
|
+
] as const;
|
|
70
|
+
|
|
71
|
+
/** A capability name a plugin route may require. */
|
|
72
|
+
export type PluginRouteCapability = (typeof PLUGIN_ROUTE_CAPABILITIES)[number];
|
|
73
|
+
|
|
74
|
+
/** The slice of `Reflect` that `reflect-metadata` adds, which NestJS loads. */
|
|
75
|
+
interface MetadataReflect {
|
|
76
|
+
defineMetadata?: (key: string, value: unknown, target: object) => void;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Declares the capabilities a route requires. Several are combined with AND.
|
|
81
|
+
*
|
|
82
|
+
* Applied to a controller it covers every route in it. It can only add to the
|
|
83
|
+
* floor the host gives the plugin: a route cannot require less.
|
|
84
|
+
*/
|
|
85
|
+
export function RequireCapability(
|
|
86
|
+
...capabilities: PluginRouteCapability[]
|
|
87
|
+
): MethodDecorator & ClassDecorator {
|
|
88
|
+
return ((
|
|
89
|
+
target: object,
|
|
90
|
+
_key?: string | symbol,
|
|
91
|
+
descriptor?: PropertyDescriptor,
|
|
92
|
+
) => {
|
|
93
|
+
const define = (Reflect as unknown as MetadataReflect).defineMetadata;
|
|
94
|
+
if (define === undefined) {
|
|
95
|
+
throw new Error(
|
|
96
|
+
"RequireCapability needs reflect-metadata, which @nestjs/common loads: import it before the controller.",
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
if (descriptor !== undefined) {
|
|
100
|
+
define(REQUIRED_CAPABILITIES_KEY, capabilities, descriptor.value);
|
|
101
|
+
return descriptor;
|
|
102
|
+
}
|
|
103
|
+
define(REQUIRED_CAPABILITIES_KEY, capabilities, target);
|
|
104
|
+
return target;
|
|
105
|
+
}) as MethodDecorator & ClassDecorator;
|
|
106
|
+
}
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A plugin's settings form, declared rather than drawn.
|
|
5
|
+
*
|
|
6
|
+
* The host renders it, validates against it, and stores the result. A plugin
|
|
7
|
+
* therefore never ships a settings screen of its own, which is what keeps
|
|
8
|
+
* plugin settings looking like the rest of the product and keeps the values
|
|
9
|
+
* validated on the server rather than only in a form the plugin drew.
|
|
10
|
+
*
|
|
11
|
+
* Labels are i18n KEYS, resolved in the plugin's own namespace
|
|
12
|
+
* (`plugin-<id>`, merged at runtime from its locales/{sv,en}.json). Inline
|
|
13
|
+
* label text is deliberately not accepted: a manifest carrying English prose
|
|
14
|
+
* would put an untranslatable string on a Swedish-first screen, and the
|
|
15
|
+
* runtime merge already exists to hold that text.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const settingsKeySchema = z
|
|
19
|
+
.string()
|
|
20
|
+
.min(1)
|
|
21
|
+
.max(64)
|
|
22
|
+
// A settings key becomes a JSON object key and a form field name. Keeping it
|
|
23
|
+
// to an identifier shape means a stored value can never collide with a
|
|
24
|
+
// prototype member or need escaping in either place.
|
|
25
|
+
.regex(/^[a-z][a-zA-Z0-9_]*$/, "must be a lowerCamelCase identifier");
|
|
26
|
+
|
|
27
|
+
const translationKeySchema = z.string().min(1).max(200);
|
|
28
|
+
|
|
29
|
+
const baseFieldSchema = z.object({
|
|
30
|
+
key: settingsKeySchema,
|
|
31
|
+
labelKey: translationKeySchema,
|
|
32
|
+
/** Help text under the field. Never the only carrier of a requirement. */
|
|
33
|
+
hintKey: translationKeySchema.optional(),
|
|
34
|
+
required: z.boolean().default(false),
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const textFieldSchema = baseFieldSchema.extend({
|
|
38
|
+
type: z.literal("text"),
|
|
39
|
+
default: z.string().optional(),
|
|
40
|
+
minLength: z.int().min(0).optional(),
|
|
41
|
+
maxLength: z.int().min(1).max(10_000).optional(),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const numberFieldSchema = baseFieldSchema.extend({
|
|
45
|
+
type: z.literal("number"),
|
|
46
|
+
default: z.number().optional(),
|
|
47
|
+
min: z.number().optional(),
|
|
48
|
+
max: z.number().optional(),
|
|
49
|
+
integer: z.boolean().default(false),
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const booleanFieldSchema = baseFieldSchema.extend({
|
|
53
|
+
type: z.literal("boolean"),
|
|
54
|
+
default: z.boolean().optional(),
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const selectFieldSchema = baseFieldSchema.extend({
|
|
58
|
+
type: z.literal("select"),
|
|
59
|
+
default: z.string().optional(),
|
|
60
|
+
options: z
|
|
61
|
+
.array(
|
|
62
|
+
z.object({
|
|
63
|
+
value: z.string().min(1).max(200),
|
|
64
|
+
labelKey: translationKeySchema,
|
|
65
|
+
}),
|
|
66
|
+
)
|
|
67
|
+
.min(1)
|
|
68
|
+
.max(50),
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A credential: an API key, a token, a customer number the plugin signs in
|
|
73
|
+
* with (ADR 0037).
|
|
74
|
+
*
|
|
75
|
+
* Write-only from the admin screen: the host never returns the value to the
|
|
76
|
+
* browser, only whether one is set, and the board replaces or clears it. It is
|
|
77
|
+
* stored encrypted, apart from the other values, and handed back in clear only
|
|
78
|
+
* by `host.settings.read()` on the server. No default, because a credential
|
|
79
|
+
* written in a published manifest is not a secret.
|
|
80
|
+
*/
|
|
81
|
+
const secretFieldSchema = baseFieldSchema.extend({
|
|
82
|
+
type: z.literal("secret"),
|
|
83
|
+
minLength: z.int().min(1).optional(),
|
|
84
|
+
maxLength: z.int().min(1).max(10_000).optional(),
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
export const pluginSettingsFieldSchema = z.discriminatedUnion("type", [
|
|
88
|
+
textFieldSchema,
|
|
89
|
+
numberFieldSchema,
|
|
90
|
+
booleanFieldSchema,
|
|
91
|
+
selectFieldSchema,
|
|
92
|
+
secretFieldSchema,
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
export type PluginSettingsField = z.infer<typeof pluginSettingsFieldSchema>;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Checks a field's bounds against each other and against its default.
|
|
99
|
+
*
|
|
100
|
+
* Done while the manifest is parsed rather than left to the validator, because
|
|
101
|
+
* neither of the two paths a default takes goes through the field's
|
|
102
|
+
* constraints. `.default(value)` short-circuits an absent value and returns it
|
|
103
|
+
* without applying anything downstream, and `defaultSettings` reads the
|
|
104
|
+
* declaration directly. A number field declaring `min: 10, default: 5` would
|
|
105
|
+
* otherwise hand the plugin a 5 it said it would never see, and
|
|
106
|
+
* `minLength: 10, maxLength: 2` would declare a field no submitted value can
|
|
107
|
+
* satisfy - both of which are the plugin's mistake to hear about at install
|
|
108
|
+
* time rather than the board's to discover at a save.
|
|
109
|
+
*/
|
|
110
|
+
function checkFieldConstraints(
|
|
111
|
+
field: PluginSettingsField,
|
|
112
|
+
index: number,
|
|
113
|
+
ctx: z.RefinementCtx,
|
|
114
|
+
): void {
|
|
115
|
+
const reject = (message: string, key: string): void => {
|
|
116
|
+
ctx.addIssue({
|
|
117
|
+
code: "custom",
|
|
118
|
+
message,
|
|
119
|
+
path: ["fields", index, key],
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
switch (field.type) {
|
|
124
|
+
case "text": {
|
|
125
|
+
if (
|
|
126
|
+
field.minLength !== undefined &&
|
|
127
|
+
field.maxLength !== undefined &&
|
|
128
|
+
field.minLength > field.maxLength
|
|
129
|
+
) {
|
|
130
|
+
reject("minLength must not be greater than maxLength", "minLength");
|
|
131
|
+
}
|
|
132
|
+
if (field.default === undefined) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (
|
|
136
|
+
field.minLength !== undefined &&
|
|
137
|
+
field.default.length < field.minLength
|
|
138
|
+
) {
|
|
139
|
+
reject("the default is shorter than minLength", "default");
|
|
140
|
+
}
|
|
141
|
+
if (
|
|
142
|
+
field.maxLength !== undefined &&
|
|
143
|
+
field.default.length > field.maxLength
|
|
144
|
+
) {
|
|
145
|
+
reject("the default is longer than maxLength", "default");
|
|
146
|
+
}
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
case "number": {
|
|
150
|
+
if (
|
|
151
|
+
field.min !== undefined &&
|
|
152
|
+
field.max !== undefined &&
|
|
153
|
+
field.min > field.max
|
|
154
|
+
) {
|
|
155
|
+
reject("min must not be greater than max", "min");
|
|
156
|
+
}
|
|
157
|
+
if (field.default === undefined) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (field.integer && !Number.isInteger(field.default)) {
|
|
161
|
+
reject("the default is not an integer", "default");
|
|
162
|
+
}
|
|
163
|
+
if (field.min !== undefined && field.default < field.min) {
|
|
164
|
+
reject("the default is below min", "default");
|
|
165
|
+
}
|
|
166
|
+
if (field.max !== undefined && field.default > field.max) {
|
|
167
|
+
reject("the default is above max", "default");
|
|
168
|
+
}
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
case "select": {
|
|
172
|
+
if (
|
|
173
|
+
field.default !== undefined &&
|
|
174
|
+
!field.options.some((option) => option.value === field.default)
|
|
175
|
+
) {
|
|
176
|
+
reject("the default is not one of the declared options", "default");
|
|
177
|
+
}
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
case "secret": {
|
|
181
|
+
if (
|
|
182
|
+
field.minLength !== undefined &&
|
|
183
|
+
field.maxLength !== undefined &&
|
|
184
|
+
field.minLength > field.maxLength
|
|
185
|
+
) {
|
|
186
|
+
reject("minLength must not be greater than maxLength", "minLength");
|
|
187
|
+
}
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
case "boolean":
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export const pluginSettingsSchema = z
|
|
196
|
+
.object({
|
|
197
|
+
fields: z
|
|
198
|
+
.array(pluginSettingsFieldSchema)
|
|
199
|
+
.max(50)
|
|
200
|
+
.refine(
|
|
201
|
+
(fields) =>
|
|
202
|
+
new Set(fields.map((field) => field.key)).size === fields.length,
|
|
203
|
+
{ message: "field keys must be unique" },
|
|
204
|
+
),
|
|
205
|
+
})
|
|
206
|
+
.superRefine((declaration, ctx) => {
|
|
207
|
+
declaration.fields.forEach((field, index) => {
|
|
208
|
+
checkFieldConstraints(field, index, ctx);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
export type PluginSettingsSchema = z.infer<typeof pluginSettingsSchema>;
|
|
213
|
+
|
|
214
|
+
export type PluginSettingsValues = Record<string, string | number | boolean>;
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* The declaration without its secret fields: what the admin screen is given
|
|
218
|
+
* values for, and what the host validates the stored plain values against.
|
|
219
|
+
*/
|
|
220
|
+
export function withoutSecretFields(
|
|
221
|
+
schema: PluginSettingsSchema,
|
|
222
|
+
): PluginSettingsSchema {
|
|
223
|
+
return {
|
|
224
|
+
fields: schema.fields.filter((field) => field.type !== "secret"),
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* The validator for one secret value, as it is written.
|
|
230
|
+
*
|
|
231
|
+
* The bounds the field declares, and never the empty string: a secret is
|
|
232
|
+
* cleared by saying so rather than by storing nothing as a value, so a
|
|
233
|
+
* plugin reading it is never handed "" where it expects a key.
|
|
234
|
+
*/
|
|
235
|
+
export function secretValueValidator(
|
|
236
|
+
field: Extract<PluginSettingsField, { type: "secret" }>,
|
|
237
|
+
): z.ZodString {
|
|
238
|
+
let secret = z.string().min(Math.max(1, field.minLength ?? 1));
|
|
239
|
+
if (field.maxLength !== undefined) {
|
|
240
|
+
secret = secret.max(field.maxLength);
|
|
241
|
+
}
|
|
242
|
+
return secret;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Builds the validator for one settings declaration.
|
|
247
|
+
*
|
|
248
|
+
* Produced from the declaration rather than written by the plugin, so the
|
|
249
|
+
* form the host renders and the values the host stores can never disagree
|
|
250
|
+
* about what is acceptable. Unknown keys are stripped: a plugin that drops a
|
|
251
|
+
* setting in a new version must not leave the old value behind to be handed
|
|
252
|
+
* back to it as something it no longer understands.
|
|
253
|
+
*
|
|
254
|
+
* A secret field reads as a string, which is how `host.settings.read()` hands
|
|
255
|
+
* it to the plugin. What the admin screen writes it with is the host's own
|
|
256
|
+
* rule, since the screen never holds the value it would be validating.
|
|
257
|
+
*/
|
|
258
|
+
export function settingsValidator(
|
|
259
|
+
schema: PluginSettingsSchema,
|
|
260
|
+
): z.ZodType<PluginSettingsValues> {
|
|
261
|
+
const shape: Record<string, z.ZodTypeAny> = {};
|
|
262
|
+
|
|
263
|
+
for (const field of schema.fields) {
|
|
264
|
+
shape[field.key] = fieldValidator(field);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return z.object(shape).strip() as unknown as z.ZodType<PluginSettingsValues>;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function fieldValidator(field: PluginSettingsField): z.ZodTypeAny {
|
|
271
|
+
switch (field.type) {
|
|
272
|
+
case "text": {
|
|
273
|
+
let text = z.string();
|
|
274
|
+
if (field.minLength !== undefined) {
|
|
275
|
+
text = text.min(field.minLength);
|
|
276
|
+
}
|
|
277
|
+
if (field.maxLength !== undefined) {
|
|
278
|
+
text = text.max(field.maxLength);
|
|
279
|
+
}
|
|
280
|
+
// A required text field rejects the empty string. Without this a form
|
|
281
|
+
// submitted with the field untouched would satisfy "required" with a
|
|
282
|
+
// value the plugin has to treat as missing anyway.
|
|
283
|
+
return optionality(field.required ? text.min(1) : text, field);
|
|
284
|
+
}
|
|
285
|
+
case "number": {
|
|
286
|
+
let number = field.integer ? z.number().int() : z.number();
|
|
287
|
+
if (field.min !== undefined) {
|
|
288
|
+
number = number.min(field.min);
|
|
289
|
+
}
|
|
290
|
+
if (field.max !== undefined) {
|
|
291
|
+
number = number.max(field.max);
|
|
292
|
+
}
|
|
293
|
+
return optionality(number, field);
|
|
294
|
+
}
|
|
295
|
+
case "boolean":
|
|
296
|
+
return optionality(z.boolean(), field);
|
|
297
|
+
case "select":
|
|
298
|
+
return optionality(
|
|
299
|
+
z.enum(field.options.map((option) => option.value)),
|
|
300
|
+
field,
|
|
301
|
+
);
|
|
302
|
+
case "secret":
|
|
303
|
+
return optionality(secretValueValidator(field), field);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Applies the declared default and, for an optional field, tolerates absence.
|
|
309
|
+
*
|
|
310
|
+
* A default makes the field satisfied whether or not it was submitted, which
|
|
311
|
+
* is why it is applied before the optional wrapper rather than after.
|
|
312
|
+
*/
|
|
313
|
+
function optionality(
|
|
314
|
+
validator: z.ZodTypeAny,
|
|
315
|
+
field: PluginSettingsField,
|
|
316
|
+
): z.ZodTypeAny {
|
|
317
|
+
if ("default" in field && field.default !== undefined) {
|
|
318
|
+
return validator.default(field.default);
|
|
319
|
+
}
|
|
320
|
+
return field.required ? validator : validator.optional();
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** The values a fresh installation starts with, from the declared defaults. */
|
|
324
|
+
export function defaultSettings(
|
|
325
|
+
schema: PluginSettingsSchema,
|
|
326
|
+
): PluginSettingsValues {
|
|
327
|
+
const values: PluginSettingsValues = {};
|
|
328
|
+
for (const field of schema.fields) {
|
|
329
|
+
if ("default" in field && field.default !== undefined) {
|
|
330
|
+
values[field.key] = field.default;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return values;
|
|
334
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Standard Schema interface, version 1, as this package needs it.
|
|
3
|
+
*
|
|
4
|
+
* Declared here rather than imported, for the reason the rest of this package
|
|
5
|
+
* declares its own types: what an action's input and output are validated by is
|
|
6
|
+
* the host's business, and naming a validator in the published contract would
|
|
7
|
+
* make every plugin author's build depend on the one the host happens to use
|
|
8
|
+
* today. A structural interface says what the host requires - something that can
|
|
9
|
+
* validate an unknown value and report issues - and any library implementing the
|
|
10
|
+
* standard satisfies it, zod included.
|
|
11
|
+
*
|
|
12
|
+
* It is a subset: the parts the registry actually calls. `validate` is the one
|
|
13
|
+
* that matters, and note that it RETURNS its issues rather than throwing, which
|
|
14
|
+
* is the difference between this and a validator's own `parse`.
|
|
15
|
+
*
|
|
16
|
+
* The full specification is at standardschema.dev.
|
|
17
|
+
*/
|
|
18
|
+
export interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
19
|
+
readonly "~standard": {
|
|
20
|
+
/** The version of the standard this schema implements. */
|
|
21
|
+
readonly version: 1;
|
|
22
|
+
/** The library that produced the schema, for diagnostics. */
|
|
23
|
+
readonly vendor: string;
|
|
24
|
+
readonly validate: (
|
|
25
|
+
value: unknown,
|
|
26
|
+
) => StandardSchemaResult<Output> | Promise<StandardSchemaResult<Output>>;
|
|
27
|
+
readonly types?:
|
|
28
|
+
{ readonly input: Input; readonly output: Output } | undefined;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type StandardSchemaResult<Output> =
|
|
33
|
+
| { readonly value: Output; readonly issues?: undefined }
|
|
34
|
+
| { readonly issues: readonly StandardSchemaIssue[] };
|
|
35
|
+
|
|
36
|
+
export interface StandardSchemaIssue {
|
|
37
|
+
readonly message: string;
|
|
38
|
+
readonly path?: readonly (PropertyKey | { readonly key: PropertyKey })[];
|
|
39
|
+
}
|