@zhin.js/console-contract 1.0.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 +28 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +3 -0
- package/lib/layout.d.ts +19 -0
- package/lib/layout.js +1 -0
- package/lib/navigation.d.ts +13 -0
- package/lib/navigation.js +1 -0
- package/lib/page.d.ts +35 -0
- package/lib/page.js +107 -0
- package/package.json +41 -0
- package/src/index.ts +3 -0
- package/src/layout.ts +23 -0
- package/src/navigation.ts +14 -0
- package/src/page.ts +148 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 凉菜
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# @zhin.js/console-contract
|
|
2
|
+
|
|
3
|
+
Console Plugin Runtime 的零依赖 wire contract。它定义 Page metadata、Page/Layout manifest、
|
|
4
|
+
canonical route、Navigation node 与 Shell slot props,不依赖 AI、React、Router 或构建器。
|
|
5
|
+
|
|
6
|
+
```tsx
|
|
7
|
+
import { definePage } from '@zhin.js/console-contract';
|
|
8
|
+
|
|
9
|
+
export const meta = definePage({
|
|
10
|
+
title: 'Service status',
|
|
11
|
+
icon: 'Activity',
|
|
12
|
+
order: 10,
|
|
13
|
+
requiredPermissions: ['status:read'],
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export default function StatusPage() {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`definePage()` 只验证并冻结 metadata,不注册页面,也不能覆盖 route。
|
|
22
|
+
`pageRoute(owner, root, localName)` 是路由的 SSOT:Root 页面为 `/p-<name>`,child 页面为
|
|
23
|
+
`/<plugin-path>/p-<name>`。模块 URL 与 hash 来自可选 Client Build adapter。
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pnpm --filter @zhin.js/console-contract build
|
|
27
|
+
pnpm --filter @zhin.js/console-contract test
|
|
28
|
+
```
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
package/lib/layout.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type LayoutSlot = 'nav' | 'footer';
|
|
2
|
+
export interface LayoutManifest {
|
|
3
|
+
readonly id: string;
|
|
4
|
+
readonly owner: string;
|
|
5
|
+
readonly slot: LayoutSlot;
|
|
6
|
+
readonly source: string;
|
|
7
|
+
readonly module: string;
|
|
8
|
+
readonly hash: string;
|
|
9
|
+
}
|
|
10
|
+
export interface NavSlotProps {
|
|
11
|
+
readonly tree: readonly NavNode[];
|
|
12
|
+
readonly current?: string;
|
|
13
|
+
navigate(path: string): void;
|
|
14
|
+
}
|
|
15
|
+
export interface FooterSlotProps {
|
|
16
|
+
readonly owner: string;
|
|
17
|
+
readonly route?: string;
|
|
18
|
+
}
|
|
19
|
+
import type { NavNode } from './navigation.js';
|
package/lib/layout.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface NavNode {
|
|
2
|
+
readonly id: string;
|
|
3
|
+
readonly type: 'plugin' | 'page';
|
|
4
|
+
readonly label: string;
|
|
5
|
+
readonly icon?: string;
|
|
6
|
+
readonly path?: string;
|
|
7
|
+
readonly order: number;
|
|
8
|
+
readonly children: readonly NavNode[];
|
|
9
|
+
}
|
|
10
|
+
export interface AccessSnapshot {
|
|
11
|
+
readonly permissions: readonly string[];
|
|
12
|
+
readonly roles: readonly string[];
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/page.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export interface PageMetadata {
|
|
2
|
+
readonly title?: string;
|
|
3
|
+
readonly icon?: string;
|
|
4
|
+
readonly order?: number;
|
|
5
|
+
readonly hideInNav?: boolean;
|
|
6
|
+
readonly requiredPermissions?: readonly string[];
|
|
7
|
+
readonly requiredRoles?: readonly string[];
|
|
8
|
+
}
|
|
9
|
+
export interface NormalizedPageMetadata {
|
|
10
|
+
readonly title: string;
|
|
11
|
+
readonly icon?: string;
|
|
12
|
+
readonly order: number;
|
|
13
|
+
readonly hideInNav: boolean;
|
|
14
|
+
readonly requiredPermissions: readonly string[];
|
|
15
|
+
readonly requiredRoles: readonly string[];
|
|
16
|
+
}
|
|
17
|
+
export interface ClientModuleArtifact {
|
|
18
|
+
readonly module: string;
|
|
19
|
+
readonly hash: string;
|
|
20
|
+
readonly metadata?: unknown;
|
|
21
|
+
}
|
|
22
|
+
export declare function normalizeClientModuleArtifact(value: unknown, label: string): Readonly<ClientModuleArtifact>;
|
|
23
|
+
export interface PageManifest extends NormalizedPageMetadata {
|
|
24
|
+
readonly id: string;
|
|
25
|
+
readonly owner: string;
|
|
26
|
+
readonly localName: string;
|
|
27
|
+
readonly source: string;
|
|
28
|
+
readonly module: string;
|
|
29
|
+
readonly hash: string;
|
|
30
|
+
readonly route: string;
|
|
31
|
+
}
|
|
32
|
+
export declare function definePage(metadata?: PageMetadata): Readonly<PageMetadata>;
|
|
33
|
+
export declare function normalizePageMetadata(localName: string, metadata: unknown): Readonly<NormalizedPageMetadata>;
|
|
34
|
+
export declare function pageRoute(owner: string, root: string, localName: string): string;
|
|
35
|
+
export declare function assertPageLocalName(value: string): void;
|
package/lib/page.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
export function normalizeClientModuleArtifact(value, label) {
|
|
2
|
+
const input = requireRecord(value, label);
|
|
3
|
+
const module = optionalString(input.module, `${label} module`);
|
|
4
|
+
const hash = optionalString(input.hash, `${label} hash`);
|
|
5
|
+
if (!module || !hash)
|
|
6
|
+
throw new TypeError(`${label} must include module and hash`);
|
|
7
|
+
return Object.freeze({ module, hash, metadata: input.metadata });
|
|
8
|
+
}
|
|
9
|
+
export function definePage(metadata = {}) {
|
|
10
|
+
return normalizePageInput(metadata);
|
|
11
|
+
}
|
|
12
|
+
export function normalizePageMetadata(localName, metadata) {
|
|
13
|
+
assertPageLocalName(localName);
|
|
14
|
+
const input = metadata === undefined ? {} : requireRecord(metadata, 'Page metadata');
|
|
15
|
+
assertKnownMetadataKeys(input);
|
|
16
|
+
const title = optionalString(input.title, 'Page title') ?? titleFromName(localName);
|
|
17
|
+
const icon = optionalString(input.icon, 'Page icon');
|
|
18
|
+
const order = optionalNumber(input.order, 'Page order') ?? 100;
|
|
19
|
+
const hideInNav = optionalBoolean(input.hideInNav, 'Page hideInNav') ?? false;
|
|
20
|
+
return Object.freeze({
|
|
21
|
+
title,
|
|
22
|
+
icon,
|
|
23
|
+
order,
|
|
24
|
+
hideInNav,
|
|
25
|
+
requiredPermissions: stringList(input.requiredPermissions, 'Page requiredPermissions'),
|
|
26
|
+
requiredRoles: stringList(input.requiredRoles, 'Page requiredRoles'),
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function assertKnownMetadataKeys(input) {
|
|
30
|
+
const known = new Set([
|
|
31
|
+
'title',
|
|
32
|
+
'icon',
|
|
33
|
+
'order',
|
|
34
|
+
'hideInNav',
|
|
35
|
+
'requiredPermissions',
|
|
36
|
+
'requiredRoles',
|
|
37
|
+
]);
|
|
38
|
+
const unknown = Object.keys(input).filter((key) => !known.has(key)).sort();
|
|
39
|
+
if (unknown.length > 0)
|
|
40
|
+
throw new TypeError(`Unknown Page metadata: ${unknown.join(', ')}`);
|
|
41
|
+
}
|
|
42
|
+
export function pageRoute(owner, root, localName) {
|
|
43
|
+
assertPageLocalName(localName);
|
|
44
|
+
if (owner !== root && !owner.startsWith(`${root}/`)) {
|
|
45
|
+
throw new Error(`Page owner ${owner} is outside Root ${root}`);
|
|
46
|
+
}
|
|
47
|
+
const relative = owner === root ? '' : owner.slice(root.length + 1);
|
|
48
|
+
return `/${relative ? `${relative}/` : ''}p-${localName}`;
|
|
49
|
+
}
|
|
50
|
+
export function assertPageLocalName(value) {
|
|
51
|
+
if (!/^[a-z0-9][a-z0-9-]*$/u.test(value)) {
|
|
52
|
+
throw new TypeError(`Invalid Page local name: ${value}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function normalizePageInput(metadata) {
|
|
56
|
+
const normalized = normalizePageMetadata('page', metadata);
|
|
57
|
+
return Object.freeze({
|
|
58
|
+
title: metadata.title === undefined ? undefined : normalized.title,
|
|
59
|
+
icon: normalized.icon,
|
|
60
|
+
order: metadata.order === undefined ? undefined : normalized.order,
|
|
61
|
+
hideInNav: metadata.hideInNav === undefined ? undefined : normalized.hideInNav,
|
|
62
|
+
requiredPermissions: normalized.requiredPermissions,
|
|
63
|
+
requiredRoles: normalized.requiredRoles,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
function titleFromName(name) {
|
|
67
|
+
return name.split('-').map((part) => `${part[0]?.toUpperCase() ?? ''}${part.slice(1)}`).join(' ');
|
|
68
|
+
}
|
|
69
|
+
function requireRecord(value, label) {
|
|
70
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
71
|
+
throw new TypeError(`${label} must be an object`);
|
|
72
|
+
}
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
function optionalString(value, label) {
|
|
76
|
+
if (value === undefined)
|
|
77
|
+
return undefined;
|
|
78
|
+
if (typeof value !== 'string' || !value.trim())
|
|
79
|
+
throw new TypeError(`${label} must be a non-empty string`);
|
|
80
|
+
return value;
|
|
81
|
+
}
|
|
82
|
+
function optionalNumber(value, label) {
|
|
83
|
+
if (value === undefined)
|
|
84
|
+
return undefined;
|
|
85
|
+
if (typeof value !== 'number' || !Number.isFinite(value))
|
|
86
|
+
throw new TypeError(`${label} must be finite`);
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
function optionalBoolean(value, label) {
|
|
90
|
+
if (value === undefined)
|
|
91
|
+
return undefined;
|
|
92
|
+
if (typeof value !== 'boolean')
|
|
93
|
+
throw new TypeError(`${label} must be boolean`);
|
|
94
|
+
return value;
|
|
95
|
+
}
|
|
96
|
+
function stringList(value, label) {
|
|
97
|
+
if (value === undefined)
|
|
98
|
+
return Object.freeze([]);
|
|
99
|
+
if (!Array.isArray(value))
|
|
100
|
+
throw new TypeError(`${label} must be an array`);
|
|
101
|
+
const result = value.map((item) => {
|
|
102
|
+
if (typeof item !== 'string' || !item.trim())
|
|
103
|
+
throw new TypeError(`${label} entries must be non-empty strings`);
|
|
104
|
+
return item;
|
|
105
|
+
});
|
|
106
|
+
return Object.freeze([...new Set(result)].sort());
|
|
107
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zhin.js/console-contract",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Zero-dependency Page, Layout, route and navigation contracts for Zhin.js plugins",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./lib/index.js",
|
|
7
|
+
"types": "./lib/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/index.d.ts",
|
|
11
|
+
"development": "./src/index.ts",
|
|
12
|
+
"import": "./lib/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"lib",
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "^26.1.0",
|
|
21
|
+
"typescript": "^6.0.3"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/zhinjs/zhin.git",
|
|
26
|
+
"directory": "packages/console/plugin-contract"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public",
|
|
30
|
+
"registry": "https://registry.npmjs.org"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsc",
|
|
38
|
+
"clean": "rimraf lib",
|
|
39
|
+
"test": "vitest run --root ../../.. packages/console/plugin-contract/tests"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/index.ts
ADDED
package/src/layout.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type LayoutSlot = 'nav' | 'footer';
|
|
2
|
+
|
|
3
|
+
export interface LayoutManifest {
|
|
4
|
+
readonly id: string;
|
|
5
|
+
readonly owner: string;
|
|
6
|
+
readonly slot: LayoutSlot;
|
|
7
|
+
readonly source: string;
|
|
8
|
+
readonly module: string;
|
|
9
|
+
readonly hash: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface NavSlotProps {
|
|
13
|
+
readonly tree: readonly NavNode[];
|
|
14
|
+
readonly current?: string;
|
|
15
|
+
navigate(path: string): void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface FooterSlotProps {
|
|
19
|
+
readonly owner: string;
|
|
20
|
+
readonly route?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
import type { NavNode } from './navigation.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface NavNode {
|
|
2
|
+
readonly id: string;
|
|
3
|
+
readonly type: 'plugin' | 'page';
|
|
4
|
+
readonly label: string;
|
|
5
|
+
readonly icon?: string;
|
|
6
|
+
readonly path?: string;
|
|
7
|
+
readonly order: number;
|
|
8
|
+
readonly children: readonly NavNode[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface AccessSnapshot {
|
|
12
|
+
readonly permissions: readonly string[];
|
|
13
|
+
readonly roles: readonly string[];
|
|
14
|
+
}
|
package/src/page.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
export interface PageMetadata {
|
|
2
|
+
readonly title?: string;
|
|
3
|
+
readonly icon?: string;
|
|
4
|
+
readonly order?: number;
|
|
5
|
+
readonly hideInNav?: boolean;
|
|
6
|
+
readonly requiredPermissions?: readonly string[];
|
|
7
|
+
readonly requiredRoles?: readonly string[];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface NormalizedPageMetadata {
|
|
11
|
+
readonly title: string;
|
|
12
|
+
readonly icon?: string;
|
|
13
|
+
readonly order: number;
|
|
14
|
+
readonly hideInNav: boolean;
|
|
15
|
+
readonly requiredPermissions: readonly string[];
|
|
16
|
+
readonly requiredRoles: readonly string[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ClientModuleArtifact {
|
|
20
|
+
readonly module: string;
|
|
21
|
+
readonly hash: string;
|
|
22
|
+
readonly metadata?: unknown;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function normalizeClientModuleArtifact(
|
|
26
|
+
value: unknown,
|
|
27
|
+
label: string,
|
|
28
|
+
): Readonly<ClientModuleArtifact> {
|
|
29
|
+
const input = requireRecord(value, label);
|
|
30
|
+
const module = optionalString(input.module, `${label} module`);
|
|
31
|
+
const hash = optionalString(input.hash, `${label} hash`);
|
|
32
|
+
if (!module || !hash) throw new TypeError(`${label} must include module and hash`);
|
|
33
|
+
return Object.freeze({ module, hash, metadata: input.metadata });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface PageManifest extends NormalizedPageMetadata {
|
|
37
|
+
readonly id: string;
|
|
38
|
+
readonly owner: string;
|
|
39
|
+
readonly localName: string;
|
|
40
|
+
readonly source: string;
|
|
41
|
+
readonly module: string;
|
|
42
|
+
readonly hash: string;
|
|
43
|
+
readonly route: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function definePage(metadata: PageMetadata = {}): Readonly<PageMetadata> {
|
|
47
|
+
return normalizePageInput(metadata);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function normalizePageMetadata(
|
|
51
|
+
localName: string,
|
|
52
|
+
metadata: unknown,
|
|
53
|
+
): Readonly<NormalizedPageMetadata> {
|
|
54
|
+
assertPageLocalName(localName);
|
|
55
|
+
const input = metadata === undefined ? {} : requireRecord(metadata, 'Page metadata');
|
|
56
|
+
assertKnownMetadataKeys(input);
|
|
57
|
+
const title = optionalString(input.title, 'Page title') ?? titleFromName(localName);
|
|
58
|
+
const icon = optionalString(input.icon, 'Page icon');
|
|
59
|
+
const order = optionalNumber(input.order, 'Page order') ?? 100;
|
|
60
|
+
const hideInNav = optionalBoolean(input.hideInNav, 'Page hideInNav') ?? false;
|
|
61
|
+
return Object.freeze({
|
|
62
|
+
title,
|
|
63
|
+
icon,
|
|
64
|
+
order,
|
|
65
|
+
hideInNav,
|
|
66
|
+
requiredPermissions: stringList(input.requiredPermissions, 'Page requiredPermissions'),
|
|
67
|
+
requiredRoles: stringList(input.requiredRoles, 'Page requiredRoles'),
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function assertKnownMetadataKeys(input: Record<string, unknown>): void {
|
|
72
|
+
const known = new Set([
|
|
73
|
+
'title',
|
|
74
|
+
'icon',
|
|
75
|
+
'order',
|
|
76
|
+
'hideInNav',
|
|
77
|
+
'requiredPermissions',
|
|
78
|
+
'requiredRoles',
|
|
79
|
+
]);
|
|
80
|
+
const unknown = Object.keys(input).filter((key) => !known.has(key)).sort();
|
|
81
|
+
if (unknown.length > 0) throw new TypeError(`Unknown Page metadata: ${unknown.join(', ')}`);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function pageRoute(owner: string, root: string, localName: string): string {
|
|
85
|
+
assertPageLocalName(localName);
|
|
86
|
+
if (owner !== root && !owner.startsWith(`${root}/`)) {
|
|
87
|
+
throw new Error(`Page owner ${owner} is outside Root ${root}`);
|
|
88
|
+
}
|
|
89
|
+
const relative = owner === root ? '' : owner.slice(root.length + 1);
|
|
90
|
+
return `/${relative ? `${relative}/` : ''}p-${localName}`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function assertPageLocalName(value: string): void {
|
|
94
|
+
if (!/^[a-z0-9][a-z0-9-]*$/u.test(value)) {
|
|
95
|
+
throw new TypeError(`Invalid Page local name: ${value}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function normalizePageInput(metadata: PageMetadata): Readonly<PageMetadata> {
|
|
100
|
+
const normalized = normalizePageMetadata('page', metadata);
|
|
101
|
+
return Object.freeze({
|
|
102
|
+
title: metadata.title === undefined ? undefined : normalized.title,
|
|
103
|
+
icon: normalized.icon,
|
|
104
|
+
order: metadata.order === undefined ? undefined : normalized.order,
|
|
105
|
+
hideInNav: metadata.hideInNav === undefined ? undefined : normalized.hideInNav,
|
|
106
|
+
requiredPermissions: normalized.requiredPermissions,
|
|
107
|
+
requiredRoles: normalized.requiredRoles,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function titleFromName(name: string): string {
|
|
112
|
+
return name.split('-').map((part) => `${part[0]?.toUpperCase() ?? ''}${part.slice(1)}`).join(' ');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
|
116
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
117
|
+
throw new TypeError(`${label} must be an object`);
|
|
118
|
+
}
|
|
119
|
+
return value as Record<string, unknown>;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function optionalString(value: unknown, label: string): string | undefined {
|
|
123
|
+
if (value === undefined) return undefined;
|
|
124
|
+
if (typeof value !== 'string' || !value.trim()) throw new TypeError(`${label} must be a non-empty string`);
|
|
125
|
+
return value;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function optionalNumber(value: unknown, label: string): number | undefined {
|
|
129
|
+
if (value === undefined) return undefined;
|
|
130
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) throw new TypeError(`${label} must be finite`);
|
|
131
|
+
return value;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function optionalBoolean(value: unknown, label: string): boolean | undefined {
|
|
135
|
+
if (value === undefined) return undefined;
|
|
136
|
+
if (typeof value !== 'boolean') throw new TypeError(`${label} must be boolean`);
|
|
137
|
+
return value;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function stringList(value: unknown, label: string): readonly string[] {
|
|
141
|
+
if (value === undefined) return Object.freeze([]);
|
|
142
|
+
if (!Array.isArray(value)) throw new TypeError(`${label} must be an array`);
|
|
143
|
+
const result = value.map((item) => {
|
|
144
|
+
if (typeof item !== 'string' || !item.trim()) throw new TypeError(`${label} entries must be non-empty strings`);
|
|
145
|
+
return item;
|
|
146
|
+
});
|
|
147
|
+
return Object.freeze([...new Set(result)].sort());
|
|
148
|
+
}
|