@ebay-dt/bonfire-core 0.1.0-alpha.0 → 0.1.0-alpha.2
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/README.md +54 -6
- package/dist/agent.d.ts +48 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +96 -0
- package/dist/agent.js.map +1 -0
- package/dist/auth-model.d.ts.map +1 -1
- package/dist/auth-model.js.map +1 -1
- package/dist/config.d.ts +2 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +15 -1
- package/dist/config.js.map +1 -1
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/internal/types.d.ts.map +1 -1
- package/dist/internal/types.js.map +1 -1
- package/dist/internal/utils.d.ts.map +1 -1
- package/dist/internal/utils.js.map +1 -1
- package/dist/lifecycle-input.d.ts +10 -5
- package/dist/lifecycle-input.d.ts.map +1 -1
- package/dist/lifecycle-input.js +49 -25
- package/dist/lifecycle-input.js.map +1 -1
- package/dist/lifecycle.d.ts +23 -1
- package/dist/lifecycle.d.ts.map +1 -1
- package/dist/lifecycle.js +13 -0
- package/dist/lifecycle.js.map +1 -1
- package/dist/prototype-catalog.d.ts +4 -0
- package/dist/prototype-catalog.d.ts.map +1 -1
- package/dist/prototype-catalog.js.map +1 -1
- package/dist/prototype-metadata.d.ts +6 -1
- package/dist/prototype-metadata.d.ts.map +1 -1
- package/dist/prototype-metadata.js +17 -1
- package/dist/prototype-metadata.js.map +1 -1
- package/dist/prototype-ref.d.ts.map +1 -1
- package/dist/prototype-ref.js.map +1 -1
- package/dist/theme.d.ts +43 -0
- package/dist/theme.d.ts.map +1 -0
- package/dist/theme.js +210 -0
- package/dist/theme.js.map +1 -0
- package/package.json +2 -2
package/dist/theme.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { asRecord, deepFreeze, findUnknownKey } from "./internal/utils.js";
|
|
2
|
+
export const COLOR_SCHEMES = ["light", "dark", "system"];
|
|
3
|
+
const THEME_ID_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
4
|
+
const MAX_THEME_ID_LENGTH = 63;
|
|
5
|
+
export class ThemeValidationError extends Error {
|
|
6
|
+
path;
|
|
7
|
+
constructor(path, message) {
|
|
8
|
+
super(`Invalid theme configuration at ${path}: ${message}`);
|
|
9
|
+
this.name = "ThemeValidationError";
|
|
10
|
+
this.path = path;
|
|
11
|
+
this.reason = message;
|
|
12
|
+
}
|
|
13
|
+
reason;
|
|
14
|
+
}
|
|
15
|
+
function themeRecord(value, path) {
|
|
16
|
+
const record = asRecord(value);
|
|
17
|
+
if (record === null) {
|
|
18
|
+
throw new ThemeValidationError(path, "must be an object");
|
|
19
|
+
}
|
|
20
|
+
return record;
|
|
21
|
+
}
|
|
22
|
+
function rejectUnknownKeys(record, allowedKeys, path) {
|
|
23
|
+
const unknownKey = findUnknownKey(record, allowedKeys);
|
|
24
|
+
if (unknownKey) {
|
|
25
|
+
throw new ThemeValidationError(`${path}.${unknownKey}`, "is not supported");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function parseThemeId(value, path) {
|
|
29
|
+
if (typeof value !== "string") {
|
|
30
|
+
throw new ThemeValidationError(path, "must be a string");
|
|
31
|
+
}
|
|
32
|
+
if (value.length === 0 ||
|
|
33
|
+
value.length > MAX_THEME_ID_LENGTH ||
|
|
34
|
+
!THEME_ID_PATTERN.test(value)) {
|
|
35
|
+
throw new ThemeValidationError(path, "must contain lowercase letters, numbers, and single hyphens between segments");
|
|
36
|
+
}
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
function parseLabel(value, path) {
|
|
40
|
+
if (typeof value !== "string") {
|
|
41
|
+
throw new ThemeValidationError(path, "must be a string");
|
|
42
|
+
}
|
|
43
|
+
const label = value.trim();
|
|
44
|
+
if (label.length === 0) {
|
|
45
|
+
throw new ThemeValidationError(path, "must not be empty");
|
|
46
|
+
}
|
|
47
|
+
return label;
|
|
48
|
+
}
|
|
49
|
+
function parseColorScheme(value, path) {
|
|
50
|
+
if (typeof value !== "string" ||
|
|
51
|
+
!COLOR_SCHEMES.includes(value)) {
|
|
52
|
+
throw new ThemeValidationError(path, `must be one of ${COLOR_SCHEMES.join(", ")}`);
|
|
53
|
+
}
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
function parseColorSchemes(value, path) {
|
|
57
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
58
|
+
throw new ThemeValidationError(path, "must be a non-empty array");
|
|
59
|
+
}
|
|
60
|
+
const schemes = value.map((scheme, index) => parseColorScheme(scheme, `${path}[${index}]`));
|
|
61
|
+
if (new Set(schemes).size !== schemes.length) {
|
|
62
|
+
throw new ThemeValidationError(path, "must not contain duplicate values");
|
|
63
|
+
}
|
|
64
|
+
return schemes;
|
|
65
|
+
}
|
|
66
|
+
function parseThemeDefinition(value, index, definitionsPath) {
|
|
67
|
+
const path = `${definitionsPath}[${index}]`;
|
|
68
|
+
const record = themeRecord(value, path);
|
|
69
|
+
rejectUnknownKeys(record, ["id", "label", "allowedColorSchemes"], path);
|
|
70
|
+
return {
|
|
71
|
+
id: parseThemeId(record.id, `${path}.id`),
|
|
72
|
+
label: parseLabel(record.label, `${path}.label`),
|
|
73
|
+
allowedColorSchemes: parseColorSchemes(record.allowedColorSchemes, `${path}.allowedColorSchemes`),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function findTheme(definitions, id) {
|
|
77
|
+
return definitions.find((definition) => definition.id === id);
|
|
78
|
+
}
|
|
79
|
+
export function parseThemeRegistry(value, path = "themes") {
|
|
80
|
+
const record = themeRecord(value, path);
|
|
81
|
+
rejectUnknownKeys(record, ["definitions", "defaultTheme", "defaultColorScheme"], path);
|
|
82
|
+
if (!Array.isArray(record.definitions) || record.definitions.length === 0) {
|
|
83
|
+
throw new ThemeValidationError(`${path}.definitions`, "must be a non-empty array");
|
|
84
|
+
}
|
|
85
|
+
const definitions = record.definitions.map((definition, index) => {
|
|
86
|
+
const parsed = parseThemeDefinition(definition, index, `${path}.definitions`);
|
|
87
|
+
return parsed;
|
|
88
|
+
});
|
|
89
|
+
if (new Set(definitions.map((definition) => definition.id)).size !==
|
|
90
|
+
definitions.length) {
|
|
91
|
+
throw new ThemeValidationError(`${path}.definitions`, "must not contain duplicate theme IDs");
|
|
92
|
+
}
|
|
93
|
+
const defaultTheme = parseThemeId(record.defaultTheme, `${path}.defaultTheme`);
|
|
94
|
+
const defaultDefinition = findTheme(definitions, defaultTheme);
|
|
95
|
+
if (!defaultDefinition) {
|
|
96
|
+
throw new ThemeValidationError(`${path}.defaultTheme`, "must reference a defined theme");
|
|
97
|
+
}
|
|
98
|
+
const defaultColorScheme = parseColorScheme(record.defaultColorScheme, `${path}.defaultColorScheme`);
|
|
99
|
+
if (!defaultDefinition.allowedColorSchemes.includes(defaultColorScheme)) {
|
|
100
|
+
throw new ThemeValidationError(`${path}.defaultColorScheme`, `must be allowed by the default theme (${defaultTheme})`);
|
|
101
|
+
}
|
|
102
|
+
return deepFreeze({
|
|
103
|
+
definitions,
|
|
104
|
+
defaultTheme,
|
|
105
|
+
defaultColorScheme,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
function parseThemeIds(value, path) {
|
|
109
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
110
|
+
throw new ThemeValidationError(path, "must be a non-empty array");
|
|
111
|
+
}
|
|
112
|
+
const ids = value.map((id, index) => parseThemeId(id, `${path}[${index}]`));
|
|
113
|
+
if (new Set(ids).size !== ids.length) {
|
|
114
|
+
throw new ThemeValidationError(path, "must not contain duplicate values");
|
|
115
|
+
}
|
|
116
|
+
return ids;
|
|
117
|
+
}
|
|
118
|
+
export function createDefaultPrototypeTheme(registry) {
|
|
119
|
+
const allowedColorSchemes = [
|
|
120
|
+
...new Set(registry.definitions.flatMap((definition) => definition.allowedColorSchemes)),
|
|
121
|
+
];
|
|
122
|
+
return parsePrototypeTheme({
|
|
123
|
+
defaultTheme: registry.defaultTheme,
|
|
124
|
+
allowedThemes: registry.definitions.map((definition) => definition.id),
|
|
125
|
+
defaultColorScheme: registry.defaultColorScheme,
|
|
126
|
+
allowedColorSchemes,
|
|
127
|
+
}, "theme", registry);
|
|
128
|
+
}
|
|
129
|
+
function parseBoolean(value, path) {
|
|
130
|
+
if (typeof value !== "boolean") {
|
|
131
|
+
throw new ThemeValidationError(path, "must be a boolean");
|
|
132
|
+
}
|
|
133
|
+
return value;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Expands an author-facing selection (a chosen default plus two
|
|
137
|
+
* switching-permission flags) into a fully validated `PrototypeTheme`
|
|
138
|
+
* policy. This is the shape a create/edit authoring UI submits; it is
|
|
139
|
+
* simpler than hand-authoring `allowedThemes`/`allowedColorSchemes` arrays
|
|
140
|
+
* directly, since "allow switching" always means every registered theme (or
|
|
141
|
+
* every color scheme supported by the allowed themes) rather than an
|
|
142
|
+
* arbitrary subset.
|
|
143
|
+
*/
|
|
144
|
+
export function parsePrototypeThemeSelection(value, registry, path = "theme") {
|
|
145
|
+
const record = themeRecord(value, path);
|
|
146
|
+
rejectUnknownKeys(record, [
|
|
147
|
+
"defaultTheme",
|
|
148
|
+
"allowThemeSwitching",
|
|
149
|
+
"defaultColorScheme",
|
|
150
|
+
"allowColorSchemeSwitching",
|
|
151
|
+
], path);
|
|
152
|
+
const defaultTheme = parseThemeId(record.defaultTheme, `${path}.defaultTheme`);
|
|
153
|
+
const allowThemeSwitching = parseBoolean(record.allowThemeSwitching, `${path}.allowThemeSwitching`);
|
|
154
|
+
const defaultColorScheme = parseColorScheme(record.defaultColorScheme, `${path}.defaultColorScheme`);
|
|
155
|
+
const allowColorSchemeSwitching = parseBoolean(record.allowColorSchemeSwitching, `${path}.allowColorSchemeSwitching`);
|
|
156
|
+
const allowedThemes = allowThemeSwitching
|
|
157
|
+
? registry.definitions.map((definition) => definition.id)
|
|
158
|
+
: [defaultTheme];
|
|
159
|
+
const allowedDefinitions = registry.definitions.filter((definition) => allowedThemes.includes(definition.id));
|
|
160
|
+
const allowedColorSchemes = allowColorSchemeSwitching
|
|
161
|
+
? [
|
|
162
|
+
...new Set(allowedDefinitions.flatMap((definition) => definition.allowedColorSchemes)),
|
|
163
|
+
]
|
|
164
|
+
: [defaultColorScheme];
|
|
165
|
+
return parsePrototypeTheme({ defaultTheme, allowedThemes, defaultColorScheme, allowedColorSchemes }, path, registry);
|
|
166
|
+
}
|
|
167
|
+
export function parsePrototypeTheme(value, path = "theme", registry) {
|
|
168
|
+
const record = themeRecord(value, path);
|
|
169
|
+
rejectUnknownKeys(record, [
|
|
170
|
+
"defaultTheme",
|
|
171
|
+
"allowedThemes",
|
|
172
|
+
"defaultColorScheme",
|
|
173
|
+
"allowedColorSchemes",
|
|
174
|
+
], path);
|
|
175
|
+
const defaultTheme = parseThemeId(record.defaultTheme, `${path}.defaultTheme`);
|
|
176
|
+
const allowedThemes = parseThemeIds(record.allowedThemes, `${path}.allowedThemes`);
|
|
177
|
+
if (!allowedThemes.includes(defaultTheme)) {
|
|
178
|
+
throw new ThemeValidationError(`${path}.defaultTheme`, "must be included in allowedThemes");
|
|
179
|
+
}
|
|
180
|
+
const defaultColorScheme = parseColorScheme(record.defaultColorScheme, `${path}.defaultColorScheme`);
|
|
181
|
+
const allowedColorSchemes = parseColorSchemes(record.allowedColorSchemes, `${path}.allowedColorSchemes`);
|
|
182
|
+
if (!allowedColorSchemes.includes(defaultColorScheme)) {
|
|
183
|
+
throw new ThemeValidationError(`${path}.defaultColorScheme`, "must be included in allowedColorSchemes");
|
|
184
|
+
}
|
|
185
|
+
if (registry) {
|
|
186
|
+
for (const theme of allowedThemes) {
|
|
187
|
+
const definition = findTheme(registry.definitions, theme);
|
|
188
|
+
if (!definition) {
|
|
189
|
+
throw new ThemeValidationError(`${path}.allowedThemes`, `must reference defined theme IDs; ${theme} is not registered`);
|
|
190
|
+
}
|
|
191
|
+
if (!allowedColorSchemes.some((scheme) => definition.allowedColorSchemes.includes(scheme))) {
|
|
192
|
+
throw new ThemeValidationError(`${path}.allowedColorSchemes`, `must include a scheme supported by theme ${theme}`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const defaultDefinition = findTheme(registry.definitions, defaultTheme);
|
|
196
|
+
if (!defaultDefinition) {
|
|
197
|
+
throw new ThemeValidationError(`${path}.defaultTheme`, "must reference a defined theme");
|
|
198
|
+
}
|
|
199
|
+
if (!defaultDefinition.allowedColorSchemes.includes(defaultColorScheme)) {
|
|
200
|
+
throw new ThemeValidationError(`${path}.defaultColorScheme`, `must be allowed by theme ${defaultTheme}`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return deepFreeze({
|
|
204
|
+
defaultTheme,
|
|
205
|
+
allowedThemes,
|
|
206
|
+
defaultColorScheme,
|
|
207
|
+
allowedColorSchemes,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
//# sourceMappingURL=theme.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"theme.js","sourceRoot":"","sources":["../src/theme.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE3E,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAU,CAAC;AAuBlE,MAAM,gBAAgB,GAAG,4BAA4B,CAAC;AACtD,MAAM,mBAAmB,GAAG,EAAE,CAAC;AAE/B,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IACpC,IAAI,CAAS;IAEtB,YAAY,IAAY,EAAE,OAAe;QACvC,KAAK,CAAC,kCAAkC,IAAI,KAAK,OAAO,EAAE,CAAC,CAAC;QAC5D,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;QACnC,IAAI,CAAC,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AAC,mBAAmB,CAAC,QAAQ,CAAC,MAAM,CAAC,CAChD,EACD,CAAC;gBACD,MAAM,IAAI,oBAAoB,CAC5B,GAAG,IAAI,sBAAsB,EAC7B,4CAA4C,KAAK,EAAE,CACpD,CAAC;YACJ,CAAC;QACH,CAAC;QAED,MAAM,iBAAiB,GAAG,SAAS,CAAC,QAAQ,CAAC,WAAW,EAAE,YAAY,CAAC,CAAC;QACxE,IAAI,CAAC,iBAAiB,EAAE,CAAC;YACvB,MAAM,IAAI,oBAAoB,CAC5B,GAAG,IAAI,eAAe,EACtB,gCAAgC,CACjC,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,iBAAiB,CAAC,mBAAmB,CAAC,QAAQ,CAAC,kBAAkB,CAAC,EAAE,CAAC;YACxE,MAAM,IAAI,oBAAoB,CAC5B,GAAG,IAAI,qBAAqB,EAC5B,4BAA4B,YAAY,EAAE,CAC3C,CAAC;QACJ,CAAC;IACH,CAAC;IAED,OAAO,UAAU,CAAC;QAChB,YAAY;QACZ,aAAa;QACb,kBAAkB;QAClB,mBAAmB;KACpB,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { asRecord, deepFreeze, findUnknownKey } from \"./internal/utils.js\";\n\nexport const COLOR_SCHEMES = [\"light\", \"dark\", \"system\"] as const;\n\nexport type ColorScheme = (typeof COLOR_SCHEMES)[number];\n\nexport type ThemeDefinition = {\n id: string;\n label: string;\n allowedColorSchemes: readonly ColorScheme[];\n};\n\nexport type ThemeRegistry = {\n definitions: readonly ThemeDefinition[];\n defaultTheme: string;\n defaultColorScheme: ColorScheme;\n};\n\nexport type PrototypeTheme = {\n defaultTheme: string;\n allowedThemes: readonly string[];\n defaultColorScheme: ColorScheme;\n allowedColorSchemes: readonly ColorScheme[];\n};\n\nconst THEME_ID_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;\nconst MAX_THEME_ID_LENGTH = 63;\n\nexport class ThemeValidationError extends Error {\n readonly path: string;\n\n constructor(path: string, message: string) {\n super(`Invalid theme configuration at ${path}: ${message}`);\n this.name = \"ThemeValidationError\";\n this.path = path;\n this.reason = message;\n }\n\n readonly reason: string;\n}\n\nfunction themeRecord(value: unknown, path: string) {\n const record = asRecord(value);\n if (record === null) {\n throw new ThemeValidationError(path, \"must be an object\");\n }\n\n return record;\n}\n\nfunction rejectUnknownKeys(\n record: Record<string, unknown>,\n allowedKeys: readonly string[],\n path: string,\n) {\n const unknownKey = findUnknownKey(record, allowedKeys);\n if (unknownKey) {\n throw new ThemeValidationError(`${path}.${unknownKey}`, \"is not supported\");\n }\n}\n\nfunction parseThemeId(value: unknown, path: string) {\n if (typeof value !== \"string\") {\n throw new ThemeValidationError(path, \"must be a string\");\n }\n if (\n value.length === 0 ||\n value.length > MAX_THEME_ID_LENGTH ||\n !THEME_ID_PATTERN.test(value)\n ) {\n throw new ThemeValidationError(\n path,\n \"must contain lowercase letters, numbers, and single hyphens between segments\",\n );\n }\n\n return value;\n}\n\nfunction parseLabel(value: unknown, path: string) {\n if (typeof value !== \"string\") {\n throw new ThemeValidationError(path, \"must be a string\");\n }\n\n const label = value.trim();\n if (label.length === 0) {\n throw new ThemeValidationError(path, \"must not be empty\");\n }\n\n return label;\n}\n\nfunction parseColorScheme(value: unknown, path: string): ColorScheme {\n if (\n typeof value !== \"string\" ||\n !(COLOR_SCHEMES as readonly string[]).includes(value)\n ) {\n throw new ThemeValidationError(\n path,\n `must be one of ${COLOR_SCHEMES.join(\", \")}`,\n );\n }\n\n return value as ColorScheme;\n}\n\nfunction parseColorSchemes(value: unknown, path: string) {\n if (!Array.isArray(value) || value.length === 0) {\n throw new ThemeValidationError(path, \"must be a non-empty array\");\n }\n\n const schemes = value.map((scheme, index) =>\n parseColorScheme(scheme, `${path}[${index}]`),\n );\n if (new Set(schemes).size !== schemes.length) {\n throw new ThemeValidationError(path, \"must not contain duplicate values\");\n }\n\n return schemes;\n}\n\nfunction parseThemeDefinition(\n value: unknown,\n index: number,\n definitionsPath: string,\n): ThemeDefinition {\n const path = `${definitionsPath}[${index}]`;\n const record = themeRecord(value, path);\n rejectUnknownKeys(record, [\"id\", \"label\", \"allowedColorSchemes\"], path);\n\n return {\n id: parseThemeId(record.id, `${path}.id`),\n label: parseLabel(record.label, `${path}.label`),\n allowedColorSchemes: parseColorSchemes(\n record.allowedColorSchemes,\n `${path}.allowedColorSchemes`,\n ),\n };\n}\n\nfunction findTheme(definitions: readonly ThemeDefinition[], id: string) {\n return definitions.find((definition) => definition.id === id);\n}\n\nexport function parseThemeRegistry(\n value: unknown,\n path = \"themes\",\n): ThemeRegistry {\n const record = themeRecord(value, path);\n rejectUnknownKeys(\n record,\n [\"definitions\", \"defaultTheme\", \"defaultColorScheme\"],\n path,\n );\n\n if (!Array.isArray(record.definitions) || record.definitions.length === 0) {\n throw new ThemeValidationError(\n `${path}.definitions`,\n \"must be a non-empty array\",\n );\n }\n\n const definitions = record.definitions.map((definition, index) => {\n const parsed = parseThemeDefinition(\n definition,\n index,\n `${path}.definitions`,\n );\n return parsed;\n });\n\n if (\n new Set(definitions.map((definition) => definition.id)).size !==\n definitions.length\n ) {\n throw new ThemeValidationError(\n `${path}.definitions`,\n \"must not contain duplicate theme IDs\",\n );\n }\n\n const defaultTheme = parseThemeId(\n record.defaultTheme,\n `${path}.defaultTheme`,\n );\n const defaultDefinition = findTheme(definitions, defaultTheme);\n if (!defaultDefinition) {\n throw new ThemeValidationError(\n `${path}.defaultTheme`,\n \"must reference a defined theme\",\n );\n }\n\n const defaultColorScheme = parseColorScheme(\n record.defaultColorScheme,\n `${path}.defaultColorScheme`,\n );\n if (!defaultDefinition.allowedColorSchemes.includes(defaultColorScheme)) {\n throw new ThemeValidationError(\n `${path}.defaultColorScheme`,\n `must be allowed by the default theme (${defaultTheme})`,\n );\n }\n\n return deepFreeze({\n definitions,\n defaultTheme,\n defaultColorScheme,\n });\n}\n\nfunction parseThemeIds(value: unknown, path: string) {\n if (!Array.isArray(value) || value.length === 0) {\n throw new ThemeValidationError(path, \"must be a non-empty array\");\n }\n\n const ids = value.map((id, index) => parseThemeId(id, `${path}[${index}]`));\n if (new Set(ids).size !== ids.length) {\n throw new ThemeValidationError(path, \"must not contain duplicate values\");\n }\n\n return ids;\n}\n\nexport function createDefaultPrototypeTheme(\n registry: ThemeRegistry,\n): PrototypeTheme {\n const allowedColorSchemes = [\n ...new Set(\n registry.definitions.flatMap(\n (definition) => definition.allowedColorSchemes,\n ),\n ),\n ];\n\n return parsePrototypeTheme(\n {\n defaultTheme: registry.defaultTheme,\n allowedThemes: registry.definitions.map((definition) => definition.id),\n defaultColorScheme: registry.defaultColorScheme,\n allowedColorSchemes,\n },\n \"theme\",\n registry,\n );\n}\n\nexport type PrototypeThemeSelection = {\n defaultTheme: string;\n allowThemeSwitching: boolean;\n defaultColorScheme: ColorScheme;\n allowColorSchemeSwitching: boolean;\n};\n\nfunction parseBoolean(value: unknown, path: string): boolean {\n if (typeof value !== \"boolean\") {\n throw new ThemeValidationError(path, \"must be a boolean\");\n }\n\n return value;\n}\n\n/**\n * Expands an author-facing selection (a chosen default plus two\n * switching-permission flags) into a fully validated `PrototypeTheme`\n * policy. This is the shape a create/edit authoring UI submits; it is\n * simpler than hand-authoring `allowedThemes`/`allowedColorSchemes` arrays\n * directly, since \"allow switching\" always means every registered theme (or\n * every color scheme supported by the allowed themes) rather than an\n * arbitrary subset.\n */\nexport function parsePrototypeThemeSelection(\n value: unknown,\n registry: ThemeRegistry,\n path = \"theme\",\n): PrototypeTheme {\n const record = themeRecord(value, path);\n rejectUnknownKeys(\n record,\n [\n \"defaultTheme\",\n \"allowThemeSwitching\",\n \"defaultColorScheme\",\n \"allowColorSchemeSwitching\",\n ],\n path,\n );\n\n const defaultTheme = parseThemeId(\n record.defaultTheme,\n `${path}.defaultTheme`,\n );\n const allowThemeSwitching = parseBoolean(\n record.allowThemeSwitching,\n `${path}.allowThemeSwitching`,\n );\n const defaultColorScheme = parseColorScheme(\n record.defaultColorScheme,\n `${path}.defaultColorScheme`,\n );\n const allowColorSchemeSwitching = parseBoolean(\n record.allowColorSchemeSwitching,\n `${path}.allowColorSchemeSwitching`,\n );\n\n const allowedThemes = allowThemeSwitching\n ? registry.definitions.map((definition) => definition.id)\n : [defaultTheme];\n\n const allowedDefinitions = registry.definitions.filter((definition) =>\n allowedThemes.includes(definition.id),\n );\n\n const allowedColorSchemes = allowColorSchemeSwitching\n ? [\n ...new Set(\n allowedDefinitions.flatMap(\n (definition) => definition.allowedColorSchemes,\n ),\n ),\n ]\n : [defaultColorScheme];\n\n return parsePrototypeTheme(\n { defaultTheme, allowedThemes, defaultColorScheme, allowedColorSchemes },\n path,\n registry,\n );\n}\n\nexport function parsePrototypeTheme(\n value: unknown,\n path = \"theme\",\n registry?: ThemeRegistry,\n): PrototypeTheme {\n const record = themeRecord(value, path);\n rejectUnknownKeys(\n record,\n [\n \"defaultTheme\",\n \"allowedThemes\",\n \"defaultColorScheme\",\n \"allowedColorSchemes\",\n ],\n path,\n );\n\n const defaultTheme = parseThemeId(\n record.defaultTheme,\n `${path}.defaultTheme`,\n );\n const allowedThemes = parseThemeIds(\n record.allowedThemes,\n `${path}.allowedThemes`,\n );\n if (!allowedThemes.includes(defaultTheme)) {\n throw new ThemeValidationError(\n `${path}.defaultTheme`,\n \"must be included in allowedThemes\",\n );\n }\n\n const defaultColorScheme = parseColorScheme(\n record.defaultColorScheme,\n `${path}.defaultColorScheme`,\n );\n const allowedColorSchemes = parseColorSchemes(\n record.allowedColorSchemes,\n `${path}.allowedColorSchemes`,\n );\n if (!allowedColorSchemes.includes(defaultColorScheme)) {\n throw new ThemeValidationError(\n `${path}.defaultColorScheme`,\n \"must be included in allowedColorSchemes\",\n );\n }\n\n if (registry) {\n for (const theme of allowedThemes) {\n const definition = findTheme(registry.definitions, theme);\n if (!definition) {\n throw new ThemeValidationError(\n `${path}.allowedThemes`,\n `must reference defined theme IDs; ${theme} is not registered`,\n );\n }\n\n if (\n !allowedColorSchemes.some((scheme) =>\n definition.allowedColorSchemes.includes(scheme),\n )\n ) {\n throw new ThemeValidationError(\n `${path}.allowedColorSchemes`,\n `must include a scheme supported by theme ${theme}`,\n );\n }\n }\n\n const defaultDefinition = findTheme(registry.definitions, defaultTheme);\n if (!defaultDefinition) {\n throw new ThemeValidationError(\n `${path}.defaultTheme`,\n \"must reference a defined theme\",\n );\n }\n if (!defaultDefinition.allowedColorSchemes.includes(defaultColorScheme)) {\n throw new ThemeValidationError(\n `${path}.defaultColorScheme`,\n `must be allowed by theme ${defaultTheme}`,\n );\n }\n }\n\n return deepFreeze({\n defaultTheme,\n allowedThemes,\n defaultColorScheme,\n allowedColorSchemes,\n });\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ebay-dt/bonfire-core",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.2",
|
|
4
4
|
"description": "Framework-neutral Bonfire contracts.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"vitest": "^5.0.0"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
|
-
"build": "tsc -p tsconfig.build.json",
|
|
37
|
+
"build": "tsc -p tsconfig.build.json && node ../../scripts/embed-source-map-sources.mjs dist",
|
|
38
38
|
"dev": "tsc -p tsconfig.build.json --watch --preserveWatchOutput",
|
|
39
39
|
"check": "tsc -p tsconfig.json --noEmit",
|
|
40
40
|
"lint": "eslint .",
|