@12-apps/tenant 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.
- package/ADOPTING.md +137 -1
- package/README.md +10 -4
- package/package.json +1 -1
- package/src/index.ts +44 -2
- package/src/port.ts +84 -3
- package/src/server/index.ts +67 -8
- package/src/slugs/index.ts +125 -0
- package/src/slugs/memory-options.ts +39 -0
- package/src/slugs/memory.ts +373 -0
- package/src/slugs/port.ts +36 -0
- package/src/tree/index.ts +147 -0
- package/src/tree/path.ts +103 -0
- package/src/validate.ts +44 -1
package/src/validate.ts
CHANGED
|
@@ -11,7 +11,8 @@ import {
|
|
|
11
11
|
type TenantCoreConfig,
|
|
12
12
|
type TenantIds,
|
|
13
13
|
} from './config';
|
|
14
|
-
import { TENANT_PORT_METHODS, type TenantPort } from './port';
|
|
14
|
+
import { TENANT_PORT_METHODS, TENANT_TREE_PORT_METHODS, type TenantPort, type TenantTreePort } from './port';
|
|
15
|
+
import { TENANT_SLUG_PORT_METHODS } from './slugs/port';
|
|
15
16
|
|
|
16
17
|
/** Thrown at startup when the config is missing a piece or carries a bad one. */
|
|
17
18
|
export class TenantConfigError extends Error {
|
|
@@ -202,3 +203,45 @@ export function validatePort(value: unknown): asserts value is TenantPort {
|
|
|
202
203
|
}
|
|
203
204
|
}
|
|
204
205
|
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* The tenant tree's port, when the host gives one: every member required. Left
|
|
209
|
+
* out (`undefined`), there is no tree and nothing to check; anything else that
|
|
210
|
+
* is not a port with all four members fails at startup, named.
|
|
211
|
+
*/
|
|
212
|
+
export function validateTreePort(value: unknown): asserts value is TenantTreePort {
|
|
213
|
+
if (value === null || !isBag(value)) {
|
|
214
|
+
throw new TenantConfigError('tree', 'must be the tree port object, or be left out');
|
|
215
|
+
}
|
|
216
|
+
for (const method of TENANT_TREE_PORT_METHODS) {
|
|
217
|
+
if (value[method] === undefined || value[method] === null) {
|
|
218
|
+
throw new TenantConfigError(`tree.${method}`, 'is required and missing');
|
|
219
|
+
}
|
|
220
|
+
if (typeof value[method] !== 'function') {
|
|
221
|
+
throw new TenantConfigError(`tree.${method}`, 'must be a function');
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** A member that must be present and a function, named by its dotted key. */
|
|
227
|
+
function requireMethod(bag: Bag, method: string, key: string): void {
|
|
228
|
+
if (bag[method] === undefined || bag[method] === null) {
|
|
229
|
+
throw new TenantConfigError(key, 'is required and missing');
|
|
230
|
+
}
|
|
231
|
+
if (typeof bag[method] !== 'function') throw new TenantConfigError(key, 'must be a function');
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Slug resolution, when the host opts in: the slug port (both members) and a
|
|
236
|
+
* kept memory (`createKeptMemory`, validated when it was made). Left out
|
|
237
|
+
* (`undefined`), there is nothing to check.
|
|
238
|
+
*/
|
|
239
|
+
export function validateSlugs(value: unknown): void {
|
|
240
|
+
if (value === null || !isBag(value)) {
|
|
241
|
+
throw new TenantConfigError('slugs', 'must be the slug config object ({ port, memory }), or be left out');
|
|
242
|
+
}
|
|
243
|
+
const port = requireBag(value.port, 'slugs.port');
|
|
244
|
+
for (const method of TENANT_SLUG_PORT_METHODS) requireMethod(port, method, `slugs.port.${method}`);
|
|
245
|
+
const memory = requireBag(value.memory, 'slugs.memory');
|
|
246
|
+
requireMethod(memory, 'read', 'slugs.memory.read');
|
|
247
|
+
}
|