@openbrf/plugin-sdk 1.0.0 → 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.
Files changed (100) hide show
  1. package/README.md +95 -0
  2. package/dist/abort-signal.d.ts +23 -0
  3. package/dist/abort-signal.d.ts.map +1 -0
  4. package/dist/abort-signal.js +2 -0
  5. package/dist/abort-signal.js.map +1 -0
  6. package/dist/action-refusal.d.ts +101 -0
  7. package/dist/action-refusal.d.ts.map +1 -0
  8. package/dist/action-refusal.js +96 -0
  9. package/dist/action-refusal.js.map +1 -0
  10. package/dist/action-schema.d.ts.map +1 -1
  11. package/dist/action-schema.js +4 -1
  12. package/dist/action-schema.js.map +1 -1
  13. package/dist/actions.d.ts +94 -6
  14. package/dist/actions.d.ts.map +1 -1
  15. package/dist/actions.js.map +1 -1
  16. package/dist/blocks.d.ts +310 -0
  17. package/dist/blocks.d.ts.map +1 -0
  18. package/dist/blocks.js +185 -0
  19. package/dist/blocks.js.map +1 -0
  20. package/dist/catalog.d.ts +399 -7
  21. package/dist/catalog.d.ts.map +1 -1
  22. package/dist/catalog.js +373 -9
  23. package/dist/catalog.js.map +1 -1
  24. package/dist/entitlements.d.ts +117 -0
  25. package/dist/entitlements.d.ts.map +1 -0
  26. package/dist/entitlements.js +62 -0
  27. package/dist/entitlements.js.map +1 -0
  28. package/dist/extensions.d.ts +183 -0
  29. package/dist/extensions.d.ts.map +1 -0
  30. package/dist/extensions.js +67 -0
  31. package/dist/extensions.js.map +1 -0
  32. package/dist/finances.d.ts +294 -0
  33. package/dist/finances.d.ts.map +1 -0
  34. package/dist/finances.js +56 -0
  35. package/dist/finances.js.map +1 -0
  36. package/dist/findings.d.ts +90 -1
  37. package/dist/findings.d.ts.map +1 -1
  38. package/dist/findings.js +89 -0
  39. package/dist/findings.js.map +1 -1
  40. package/dist/host.d.ts +621 -2
  41. package/dist/host.d.ts.map +1 -1
  42. package/dist/host.js +39 -0
  43. package/dist/host.js.map +1 -1
  44. package/dist/imports.d.ts +104 -0
  45. package/dist/imports.d.ts.map +1 -0
  46. package/dist/imports.js +70 -0
  47. package/dist/imports.js.map +1 -0
  48. package/dist/index.d.ts +17 -7
  49. package/dist/index.d.ts.map +1 -1
  50. package/dist/index.js +15 -6
  51. package/dist/index.js.map +1 -1
  52. package/dist/localized-text.d.ts +18 -0
  53. package/dist/localized-text.d.ts.map +1 -0
  54. package/dist/localized-text.js +17 -0
  55. package/dist/localized-text.js.map +1 -0
  56. package/dist/manifest.d.ts +730 -0
  57. package/dist/manifest.d.ts.map +1 -1
  58. package/dist/manifest.js +805 -11
  59. package/dist/manifest.js.map +1 -1
  60. package/dist/permissions.d.ts +170 -11
  61. package/dist/permissions.d.ts.map +1 -1
  62. package/dist/permissions.js +210 -2
  63. package/dist/permissions.js.map +1 -1
  64. package/dist/recipients.d.ts +139 -0
  65. package/dist/recipients.d.ts.map +1 -0
  66. package/dist/recipients.js +223 -0
  67. package/dist/recipients.js.map +1 -0
  68. package/dist/records.d.ts +130 -0
  69. package/dist/records.d.ts.map +1 -0
  70. package/dist/records.js +85 -0
  71. package/dist/records.js.map +1 -0
  72. package/dist/require-capability.d.ts +37 -0
  73. package/dist/require-capability.d.ts.map +1 -0
  74. package/dist/require-capability.js +88 -0
  75. package/dist/require-capability.js.map +1 -0
  76. package/dist/settings-schema.d.ts +35 -0
  77. package/dist/settings-schema.d.ts.map +1 -1
  78. package/dist/settings-schema.js +55 -2
  79. package/dist/settings-schema.js.map +1 -1
  80. package/package.json +4 -4
  81. package/src/abort-signal.ts +22 -0
  82. package/src/action-refusal.ts +178 -0
  83. package/src/action-schema.ts +4 -1
  84. package/src/actions.ts +103 -6
  85. package/src/blocks.ts +289 -0
  86. package/src/catalog.ts +500 -57
  87. package/src/entitlements.ts +150 -0
  88. package/src/extensions.ts +212 -0
  89. package/src/finances.ts +317 -0
  90. package/src/findings.ts +92 -0
  91. package/src/host.ts +656 -2
  92. package/src/imports.ts +135 -0
  93. package/src/index.ts +193 -0
  94. package/src/localized-text.ts +19 -0
  95. package/src/manifest.ts +1039 -12
  96. package/src/permissions.ts +223 -3
  97. package/src/recipients.ts +294 -0
  98. package/src/records.ts +172 -0
  99. package/src/require-capability.ts +106 -0
  100. package/src/settings-schema.ts +64 -2
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
+ }
@@ -68,11 +68,28 @@ const selectFieldSchema = baseFieldSchema.extend({
68
68
  .max(50),
69
69
  });
70
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
+
71
87
  export const pluginSettingsFieldSchema = z.discriminatedUnion("type", [
72
88
  textFieldSchema,
73
89
  numberFieldSchema,
74
90
  booleanFieldSchema,
75
91
  selectFieldSchema,
92
+ secretFieldSchema,
76
93
  ]);
77
94
 
78
95
  export type PluginSettingsField = z.infer<typeof pluginSettingsFieldSchema>;
@@ -160,6 +177,16 @@ function checkFieldConstraints(
160
177
  }
161
178
  return;
162
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
+ }
163
190
  case "boolean":
164
191
  return;
165
192
  }
@@ -186,6 +213,35 @@ export type PluginSettingsSchema = z.infer<typeof pluginSettingsSchema>;
186
213
 
187
214
  export type PluginSettingsValues = Record<string, string | number | boolean>;
188
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
+
189
245
  /**
190
246
  * Builds the validator for one settings declaration.
191
247
  *
@@ -194,6 +250,10 @@ export type PluginSettingsValues = Record<string, string | number | boolean>;
194
250
  * about what is acceptable. Unknown keys are stripped: a plugin that drops a
195
251
  * setting in a new version must not leave the old value behind to be handed
196
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.
197
257
  */
198
258
  export function settingsValidator(
199
259
  schema: PluginSettingsSchema,
@@ -239,6 +299,8 @@ function fieldValidator(field: PluginSettingsField): z.ZodTypeAny {
239
299
  z.enum(field.options.map((option) => option.value)),
240
300
  field,
241
301
  );
302
+ case "secret":
303
+ return optionality(secretValueValidator(field), field);
242
304
  }
243
305
  }
244
306
 
@@ -252,7 +314,7 @@ function optionality(
252
314
  validator: z.ZodTypeAny,
253
315
  field: PluginSettingsField,
254
316
  ): z.ZodTypeAny {
255
- if (field.default !== undefined) {
317
+ if ("default" in field && field.default !== undefined) {
256
318
  return validator.default(field.default);
257
319
  }
258
320
  return field.required ? validator : validator.optional();
@@ -264,7 +326,7 @@ export function defaultSettings(
264
326
  ): PluginSettingsValues {
265
327
  const values: PluginSettingsValues = {};
266
328
  for (const field of schema.fields) {
267
- if (field.default !== undefined) {
329
+ if ("default" in field && field.default !== undefined) {
268
330
  values[field.key] = field.default;
269
331
  }
270
332
  }