@nuvio/shared 1.0.0 → 1.1.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.
@@ -0,0 +1,133 @@
1
+ // src/coverage-contract.ts
2
+ import { z } from "zod";
3
+ var PCC_SUPPORTED_CATEGORIES = [
4
+ "card",
5
+ "button",
6
+ "heading",
7
+ "text",
8
+ "table",
9
+ "form",
10
+ "badge",
11
+ "nav"
12
+ ];
13
+ var PCC_REJECTED_CATEGORIES = ["media", "layout"];
14
+ var PCC_BRANDABLE_CATEGORIES = [
15
+ "card",
16
+ "button",
17
+ "heading",
18
+ "text",
19
+ "table",
20
+ "form",
21
+ "badge"
22
+ ];
23
+ var pccCategoryConfigSchema = z.object({
24
+ required: z.boolean().optional().default(true),
25
+ hosts: z.array(z.string().min(1)).min(1, "category hosts must not be empty")
26
+ });
27
+ var pccManifestSchema = z.object({
28
+ page: z.string().min(1),
29
+ route: z.string().min(1),
30
+ description: z.string().optional(),
31
+ categories: z.record(pccCategoryConfigSchema)
32
+ });
33
+ function isPccSupportedCategory(key) {
34
+ return PCC_SUPPORTED_CATEGORIES.includes(key);
35
+ }
36
+ function isPccRejectedCategory(key) {
37
+ return PCC_REJECTED_CATEGORIES.includes(key);
38
+ }
39
+ function isPccBrandableCategory(category) {
40
+ return PCC_BRANDABLE_CATEGORIES.includes(category);
41
+ }
42
+ function parsePccManifest(input) {
43
+ const parsed = pccManifestSchema.safeParse(input);
44
+ if (!parsed.success) {
45
+ return {
46
+ ok: false,
47
+ error: {
48
+ code: "invalid_manifest",
49
+ message: parsed.error.issues.map((i) => i.message).join("; ")
50
+ }
51
+ };
52
+ }
53
+ const categories = {};
54
+ const seenHosts = /* @__PURE__ */ new Map();
55
+ for (const [rawKey, rawConfig] of Object.entries(parsed.data.categories)) {
56
+ if (isPccRejectedCategory(rawKey)) {
57
+ return {
58
+ ok: false,
59
+ error: {
60
+ code: "invalid_manifest",
61
+ message: `Category "${rawKey}" is excluded from PCC (media/layout are not supported)`
62
+ }
63
+ };
64
+ }
65
+ if (!isPccSupportedCategory(rawKey)) {
66
+ return {
67
+ ok: false,
68
+ error: {
69
+ code: "invalid_manifest",
70
+ message: `Unknown PCC category "${rawKey}"`
71
+ }
72
+ };
73
+ }
74
+ const config = {
75
+ required: rawConfig.required ?? true,
76
+ hosts: [...rawConfig.hosts]
77
+ };
78
+ if (config.required && config.hosts.length === 0) {
79
+ return {
80
+ ok: false,
81
+ error: {
82
+ code: "invalid_manifest",
83
+ message: `Required category "${rawKey}" has no hosts`
84
+ }
85
+ };
86
+ }
87
+ for (const hostId of config.hosts) {
88
+ const prev = seenHosts.get(hostId);
89
+ if (prev) {
90
+ return {
91
+ ok: false,
92
+ error: {
93
+ code: "invalid_manifest",
94
+ message: `Duplicate host "${hostId}" in categories "${prev}" and "${rawKey}"`
95
+ }
96
+ };
97
+ }
98
+ seenHosts.set(hostId, rawKey);
99
+ }
100
+ categories[rawKey] = config;
101
+ }
102
+ return {
103
+ ok: true,
104
+ manifest: {
105
+ page: parsed.data.page,
106
+ route: parsed.data.route,
107
+ description: parsed.data.description,
108
+ categories
109
+ }
110
+ };
111
+ }
112
+ function defaultPccManifestPath(projectRoot, page) {
113
+ return `${projectRoot.replace(/\/$/, "")}/nuvio/pages/${page}.pcc.yaml`;
114
+ }
115
+ function pccHostsForCategory(manifest, category) {
116
+ return manifest.categories[category]?.hosts ?? [];
117
+ }
118
+ function pccCategoryLabel(category) {
119
+ return category.charAt(0).toUpperCase() + category.slice(1) + "s";
120
+ }
121
+
122
+ export {
123
+ PCC_SUPPORTED_CATEGORIES,
124
+ PCC_REJECTED_CATEGORIES,
125
+ PCC_BRANDABLE_CATEGORIES,
126
+ isPccSupportedCategory,
127
+ isPccRejectedCategory,
128
+ isPccBrandableCategory,
129
+ parsePccManifest,
130
+ defaultPccManifestPath,
131
+ pccHostsForCategory,
132
+ pccCategoryLabel
133
+ };