@cms0/shared 0.2.19 → 0.2.21

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 @@
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.ts"],"names":[],"mappings":"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"}
@@ -1,10 +1,16 @@
1
- import { z } from "zod";
1
+ import { z, ZodType } from "zod";
2
2
  // Map custom primitive-models (or types) you defined
3
3
  const customTypeSchemas = {
4
4
  Asset: z.object({ id: z.string(), url: z.string().url() }),
5
5
  RichText: z.object({ html: z.string(), delta: z.array(z.any()) }),
6
6
  Url: z.string().url(),
7
7
  };
8
+ const PRIMITIVE_DESCRIPTOR_TYPES = new Set([
9
+ "string",
10
+ "number",
11
+ "boolean",
12
+ "json",
13
+ ]);
8
14
  function convertDescriptorToZod(desc, modelZodSchemas, opts = {}) {
9
15
  const applyOptionality = (schema) => {
10
16
  let out = schema;
@@ -14,8 +20,12 @@ function convertDescriptorToZod(desc, modelZodSchemas, opts = {}) {
14
20
  out = out.optional();
15
21
  return out;
16
22
  };
17
- if (desc.kind === "primitive") {
18
- switch (desc.type) {
23
+ const type = desc.type;
24
+ const primitiveType = desc.kind === "primitive" || PRIMITIVE_DESCRIPTOR_TYPES.has(type)
25
+ ? type
26
+ : null;
27
+ if (primitiveType) {
28
+ switch (primitiveType) {
19
29
  case "string":
20
30
  return applyOptionality(z.string());
21
31
  case "number":
package/package.json CHANGED
@@ -1,50 +1,38 @@
1
1
  {
2
2
  "name": "@cms0/shared",
3
- "version": "0.2.19",
4
- "type": "module",
5
- "private": false,
3
+ "version": "0.2.21",
6
4
  "publishConfig": {
7
5
  "access": "restricted"
8
6
  },
9
- "main": "./dist/cjs/index.js",
10
- "module": "./dist/esm/index.js",
11
- "types": "./dist/types/index.d.ts",
12
- "files": [
13
- "dist",
14
- "src/responsive-break.css"
15
- ],
7
+ "type": "module",
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
16
10
  "exports": {
17
11
  ".": {
18
- "types": "./dist/types/index.d.ts",
19
- "import": "./dist/esm/index.js",
20
- "require": "./dist/cjs/index.js"
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
21
14
  },
22
- "./validation": {
23
- "types": "./dist/types/validation.d.ts",
24
- "import": "./dist/esm/validation.js",
25
- "require": "./dist/cjs/validation.js"
15
+ "./saas": {
16
+ "types": "./dist/saas.d.ts",
17
+ "default": "./dist/saas.js"
26
18
  },
27
- "./responsive-break.css": "./src/responsive-break.css"
19
+ "./responsive-break.css": {
20
+ "style": "./src/responsive-break.css",
21
+ "default": "./src/responsive-break.css"
22
+ }
28
23
  },
24
+ "files": [
25
+ "dist",
26
+ "src/responsive-break.css"
27
+ ],
29
28
  "dependencies": {
30
29
  "zod": "^4.1.12"
31
30
  },
32
- "devDependencies": {
33
- "@types/node": "^20.0.0",
34
- "typescript": "^5.0.0",
35
- "@cms0/typescript-config": "0.0.1"
36
- },
37
31
  "scripts": {
38
- "dev": "pnpm run dev:setup && pnpm run dev:esm",
39
- "dev:setup": "node -e \"const fs=require('fs');fs.mkdirSync('dist/cjs',{recursive:true});fs.writeFileSync('dist/cjs/package.json','{\\\"type\\\":\\\"commonjs\\\"}\\n');fs.mkdirSync('dist',{recursive:true});fs.copyFileSync('src/responsive-break.css','dist/responsive-break.css');\"",
40
- "dev:esm": "tsc -w -p tsconfig.json --preserveWatchOutput",
41
- "dev:cjs": "tsc -w -p tsconfig.cjs.json --preserveWatchOutput",
42
- "dev:css": "node -e \"const fs=require('node:fs');const path=require('node:path');const source=path.join('src','responsive-break.css');const target=path.join('dist','responsive-break.css');const sync=()=>{fs.mkdirSync(path.dirname(target),{recursive:true});fs.copyFileSync(source,target);console.log('[shared] synced responsive-break.css');};sync();fs.watchFile(source,{interval:250},(curr,prev)=>{if(curr.mtimeMs!==prev.mtimeMs)sync();});const stop=()=>{fs.unwatchFile(source);process.exit(0);};process.on('SIGINT',stop);process.on('SIGTERM',stop);\"",
43
- "build": "pnpm run build:clean && pnpm run build:esm && pnpm run build:cjs && pnpm run build:post",
44
- "build:clean": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true });\"",
45
- "build:esm": "tsc -p tsconfig.json",
46
- "build:cjs": "tsc -p tsconfig.cjs.json",
47
- "build:post": "node -e \"const fs=require('fs');fs.mkdirSync('dist/cjs',{recursive:true});fs.writeFileSync('dist/cjs/package.json','{\\\"type\\\":\\\"commonjs\\\"}\\\\n');fs.copyFileSync('src/responsive-break.css','dist/responsive-break.css');\"",
48
- "build:pack": "pnpm run build:esm && pnpm run build:cjs && pnpm run build:post"
32
+ "build": "tsc -p tsconfig.json",
33
+ "typecheck": "tsc -p tsconfig.json --noEmit",
34
+ "lint": "tsc -p tsconfig.json --noEmit",
35
+ "test:unit": "pnpm -C ../.. exec vitest run --config vitest.config.ts --project shared-unit",
36
+ "test": "pnpm test:unit"
49
37
  }
50
38
  }
@@ -1,404 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isPrimitiveDescriptor = isPrimitiveDescriptor;
4
- exports.isEnumDescriptor = isEnumDescriptor;
5
- exports.isUnionDescriptor = isUnionDescriptor;
6
- exports.isModelRefDescriptor = isModelRefDescriptor;
7
- exports.isObjectDescriptor = isObjectDescriptor;
8
- exports.isArrayDescriptor = isArrayDescriptor;
9
- exports.getModelDescriptor = getModelDescriptor;
10
- exports.getDescriptorPropertyEntries = getDescriptorPropertyEntries;
11
- exports.resolveAssetKindFromModelDescriptor = resolveAssetKindFromModelDescriptor;
12
- exports.resolveDescriptorCapability = resolveDescriptorCapability;
13
- exports.getDescriptorDefaultValue = getDescriptorDefaultValue;
14
- exports.normalizeEnumValue = normalizeEnumValue;
15
- exports.formatEnumOptionValue = formatEnumOptionValue;
16
- exports.parseEnumOptionValue = parseEnumOptionValue;
17
- exports.resolveUnionBranchIndex = resolveUnionBranchIndex;
18
- exports.normalizeUnionValue = normalizeUnionValue;
19
- exports.getDescriptorDisplayName = getDescriptorDisplayName;
20
- const union_js_1 = require("./union.js");
21
- function isPlainRecord(value) {
22
- return !!value && typeof value === "object" && !Array.isArray(value);
23
- }
24
- function isPrimitiveDescriptor(descriptor) {
25
- if (!descriptor)
26
- return false;
27
- const type = descriptor.type;
28
- return (descriptor.kind === "primitive" ||
29
- (!descriptor.kind && type !== "object" && type !== "array"));
30
- }
31
- function isEnumDescriptor(descriptor) {
32
- return descriptor?.kind === "enum";
33
- }
34
- function isUnionDescriptor(descriptor) {
35
- return descriptor?.kind === "union";
36
- }
37
- function isModelRefDescriptor(descriptor) {
38
- return descriptor?.kind === "modelRef";
39
- }
40
- function isObjectDescriptor(descriptor) {
41
- return !!descriptor && descriptor.type === "object";
42
- }
43
- function isArrayDescriptor(descriptor) {
44
- return !!descriptor && descriptor.type === "array";
45
- }
46
- function getModelDescriptor(fullDescriptor, modelName) {
47
- if (!fullDescriptor || !modelName)
48
- return undefined;
49
- return fullDescriptor.models?.[modelName];
50
- }
51
- function getDescriptorPropertyEntries(descriptor) {
52
- const properties = descriptor.properties ?? {};
53
- const order = Array.isArray(descriptor.propertyOrder)
54
- ? descriptor.propertyOrder
55
- : [];
56
- const ordered = order
57
- .filter((key) => Object.prototype.hasOwnProperty.call(properties, key))
58
- .map((key) => [key, properties[key]]);
59
- const remaining = Object.entries(properties)
60
- .filter(([key]) => !order.includes(key))
61
- .sort(([left], [right]) => left.localeCompare(right));
62
- return [...ordered, ...remaining];
63
- }
64
- function resolveAssetKindFromModelDescriptor(modelName, modelDescriptor) {
65
- const presentation = modelDescriptor?.presentation;
66
- if (presentation?.kind === "asset") {
67
- if (presentation.assetKind === "image")
68
- return "image";
69
- if (presentation.assetKind === "video")
70
- return "video";
71
- return "file";
72
- }
73
- if (modelName === "Image")
74
- return "image";
75
- if (modelName === "Video")
76
- return "video";
77
- if (modelName === "File")
78
- return "file";
79
- return null;
80
- }
81
- function resolveDescriptorCapability(descriptor, fullDescriptor) {
82
- if (isUnionDescriptor(descriptor)) {
83
- const keys = (0, union_js_1.getUnionBranchKeys)(descriptor);
84
- const branches = Array.isArray(descriptor.anyOf) ? descriptor.anyOf : [];
85
- return {
86
- kind: "union",
87
- discriminatorKey: descriptor.discriminator?.key,
88
- branches: branches.map((branch, index) => ({
89
- index,
90
- key: keys[index] ?? (0, union_js_1.getUnionBranchKeyAt)(descriptor, index) ?? `branch_${index + 1}`,
91
- descriptor: branch,
92
- capability: resolveDescriptorCapability(branch, fullDescriptor),
93
- label: getDescriptorDisplayName(branch, index),
94
- })),
95
- };
96
- }
97
- if (isModelRefDescriptor(descriptor)) {
98
- const modelDescriptor = getModelDescriptor(fullDescriptor, descriptor.model);
99
- const assetKind = resolveAssetKindFromModelDescriptor(descriptor.model, modelDescriptor);
100
- if (assetKind) {
101
- return {
102
- kind: "asset-ref",
103
- modelName: descriptor.model,
104
- assetKind,
105
- modelDescriptor,
106
- };
107
- }
108
- return {
109
- kind: "model-ref",
110
- modelName: descriptor.model,
111
- modelDescriptor,
112
- };
113
- }
114
- if (isEnumDescriptor(descriptor)) {
115
- return {
116
- kind: "enum",
117
- valueType: descriptor.valueType,
118
- options: Array.isArray(descriptor.values) ? descriptor.values.slice() : [],
119
- };
120
- }
121
- if (isObjectDescriptor(descriptor)) {
122
- return {
123
- kind: "object-inline",
124
- properties: getDescriptorPropertyEntries(descriptor),
125
- };
126
- }
127
- if (isArrayDescriptor(descriptor)) {
128
- return {
129
- kind: "collection-inline",
130
- itemDescriptor: descriptor.items,
131
- };
132
- }
133
- if (!isPrimitiveDescriptor(descriptor)) {
134
- return {
135
- kind: "unsupported",
136
- reason: "Unsupported descriptor kind.",
137
- };
138
- }
139
- if (descriptor.customType === "LocalizedString") {
140
- return { kind: "localized-string" };
141
- }
142
- if (descriptor.customType === "RichText") {
143
- return { kind: "rich-text" };
144
- }
145
- if (descriptor.customType === "LocalizedRichText") {
146
- return { kind: "localized-rich-text" };
147
- }
148
- return {
149
- kind: "scalar",
150
- primitiveType: descriptor.type,
151
- customType: descriptor.customType,
152
- };
153
- }
154
- function getDescriptorDefaultValue(descriptor, options = {}) {
155
- const capability = resolveDescriptorCapability(descriptor);
156
- if (capability.kind === "localized-string") {
157
- const locales = options.locales?.length ? [...options.locales] : [options.defaultLocale ?? "en"];
158
- const defaultLocale = options.defaultLocale ?? locales[0] ?? "en";
159
- return {
160
- defaultLocale,
161
- locales: Object.fromEntries(locales.map((locale) => [locale, ""])),
162
- };
163
- }
164
- if (capability.kind === "rich-text") {
165
- return { value: {}, html: "" };
166
- }
167
- if (capability.kind === "localized-rich-text") {
168
- const locales = options.locales?.length ? [...options.locales] : [options.defaultLocale ?? "en"];
169
- const defaultLocale = options.defaultLocale ?? locales[0] ?? "en";
170
- return {
171
- defaultLocale,
172
- locales: Object.fromEntries(locales.map((locale) => [locale, { value: {}, html: "" }])),
173
- };
174
- }
175
- if (capability.kind === "model-ref" || capability.kind === "asset-ref") {
176
- return null;
177
- }
178
- if (capability.kind === "enum") {
179
- return capability.options[0] ?? null;
180
- }
181
- if (capability.kind === "union") {
182
- const firstBranch = capability.branches[0];
183
- if (!firstBranch)
184
- return null;
185
- return (0, union_js_1.createTaggedUnionValue)(descriptor, 0, getDescriptorDefaultValue(firstBranch.descriptor, options));
186
- }
187
- if (capability.kind === "object-inline") {
188
- return Object.fromEntries(capability.properties.map(([key, propertyDescriptor]) => [
189
- key,
190
- getDescriptorDefaultValue(propertyDescriptor, options),
191
- ]));
192
- }
193
- if (capability.kind === "collection-inline") {
194
- return [];
195
- }
196
- if (capability.kind === "scalar") {
197
- if (capability.primitiveType === "string")
198
- return "";
199
- if (capability.primitiveType === "number")
200
- return 0;
201
- if (capability.primitiveType === "boolean")
202
- return false;
203
- return null;
204
- }
205
- return null;
206
- }
207
- function normalizeEnumValue(descriptor, value) {
208
- const valueType = (descriptor.valueType ?? "string");
209
- const values = Array.isArray(descriptor.values) ? descriptor.values : [];
210
- let normalized = value;
211
- if (valueType === "string") {
212
- normalized = value == null ? "" : String(value);
213
- }
214
- else if (valueType === "number") {
215
- if (typeof value === "number" && Number.isFinite(value)) {
216
- normalized = value;
217
- }
218
- else {
219
- const parsed = Number(value);
220
- normalized = Number.isFinite(parsed) ? parsed : null;
221
- }
222
- }
223
- else if (valueType === "boolean") {
224
- if (value === true || value === "true" || value === 1 || value === "1") {
225
- normalized = true;
226
- }
227
- else if (value === false || value === "false" || value === 0 || value === "0") {
228
- normalized = false;
229
- }
230
- else {
231
- normalized = Boolean(value);
232
- }
233
- }
234
- if (!values.length)
235
- return normalized;
236
- if (values.some((entry) => Object.is(entry, normalized))) {
237
- return normalized;
238
- }
239
- return values[0];
240
- }
241
- function formatEnumOptionValue(value) {
242
- return JSON.stringify(value);
243
- }
244
- function parseEnumOptionValue(raw, valueType) {
245
- try {
246
- const parsed = JSON.parse(raw);
247
- if (valueType === "string")
248
- return String(parsed ?? "");
249
- if (valueType === "number") {
250
- const num = Number(parsed);
251
- return Number.isFinite(num) ? num : null;
252
- }
253
- return Boolean(parsed);
254
- }
255
- catch {
256
- if (valueType === "number") {
257
- const num = Number(raw);
258
- return Number.isFinite(num) ? num : null;
259
- }
260
- if (valueType === "boolean") {
261
- return raw === "true";
262
- }
263
- return raw;
264
- }
265
- }
266
- function isUrlLikeString(input) {
267
- return (typeof input === "string" &&
268
- (input.includes("://") || input.startsWith("/") || input.startsWith("./")));
269
- }
270
- function isUuidLikeId(input) {
271
- return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(input);
272
- }
273
- function normalizeModelRefValue(value) {
274
- if (typeof value === "string" || typeof value === "number")
275
- return value;
276
- if (!isPlainRecord(value))
277
- return null;
278
- const id = value.id;
279
- return typeof id === "string" || typeof id === "number" ? id : null;
280
- }
281
- function descriptorMatchScore(descriptor, value) {
282
- if (isPrimitiveDescriptor(descriptor)) {
283
- if (descriptor.type === "json")
284
- return 1;
285
- if (descriptor.type === "string")
286
- return typeof value === "string" ? 4 : 0;
287
- if (descriptor.type === "number")
288
- return typeof value === "number" ? 4 : 0;
289
- if (descriptor.type === "boolean")
290
- return typeof value === "boolean" ? 4 : 0;
291
- return 0;
292
- }
293
- if (isEnumDescriptor(descriptor)) {
294
- const values = Array.isArray(descriptor.values) ? descriptor.values : [];
295
- if (!values.length)
296
- return 2;
297
- return values.some((entry) => Object.is(entry, value)) ? 6 : 0;
298
- }
299
- if (isModelRefDescriptor(descriptor)) {
300
- if (typeof value === "string") {
301
- const normalized = value.trim();
302
- if (!normalized)
303
- return 0;
304
- if (isUrlLikeString(normalized))
305
- return 1;
306
- return isUuidLikeId(normalized) ? 6 : 2;
307
- }
308
- if (typeof value === "number") {
309
- return Number.isFinite(value) && value !== 0 ? 4 : 0;
310
- }
311
- const candidate = normalizeModelRefValue(value);
312
- if (candidate == null)
313
- return 0;
314
- return typeof candidate === "string" && isUuidLikeId(candidate) ? 5 : 2;
315
- }
316
- if (isArrayDescriptor(descriptor)) {
317
- return Array.isArray(value) ? 3 : 0;
318
- }
319
- if (isObjectDescriptor(descriptor)) {
320
- if (!isPlainRecord(value))
321
- return 0;
322
- const keys = Object.keys(descriptor.properties ?? {});
323
- const matched = keys.filter((key) => Object.prototype.hasOwnProperty.call(value, key)).length;
324
- return 2 + matched;
325
- }
326
- if (isUnionDescriptor(descriptor)) {
327
- const branches = Array.isArray(descriptor.anyOf) ? descriptor.anyOf : [];
328
- if (!branches.length)
329
- return 0;
330
- return Math.max(...branches.map((branch) => descriptorMatchScore(branch, value)));
331
- }
332
- return 0;
333
- }
334
- function resolveUnionBranchIndex(descriptor, value) {
335
- const branches = Array.isArray(descriptor.anyOf) ? descriptor.anyOf : [];
336
- if (!branches.length)
337
- return 0;
338
- const discriminatorKey = descriptor.discriminator?.key;
339
- if (discriminatorKey && isPlainRecord(value)) {
340
- const discriminatorValue = value[discriminatorKey];
341
- for (let index = 0; index < branches.length; index += 1) {
342
- const branch = branches[index];
343
- const expected = branch?.properties?.[discriminatorKey]?.values?.[0];
344
- if (expected !== undefined && Object.is(expected, discriminatorValue)) {
345
- return index;
346
- }
347
- }
348
- }
349
- let bestIndex = 0;
350
- let bestScore = -1;
351
- for (let index = 0; index < branches.length; index += 1) {
352
- const score = descriptorMatchScore(branches[index], value);
353
- if (score > bestScore) {
354
- bestScore = score;
355
- bestIndex = index;
356
- }
357
- }
358
- return bestIndex;
359
- }
360
- function normalizeUnionValue(descriptor, value, options = {}) {
361
- if (value == null) {
362
- return null;
363
- }
364
- if ((0, union_js_1.isTaggedUnionValue)(value)) {
365
- return value;
366
- }
367
- const branches = Array.isArray(descriptor.anyOf) ? descriptor.anyOf : [];
368
- if (!branches.length)
369
- return null;
370
- const branchIndex = resolveUnionBranchIndex(descriptor, value);
371
- const branchKey = (0, union_js_1.getUnionBranchKeyAt)(descriptor, branchIndex);
372
- if (!branchKey)
373
- return null;
374
- const branchDescriptor = branches[branchIndex];
375
- const branchValue = value == null
376
- ? getDescriptorDefaultValue(branchDescriptor, options)
377
- : value;
378
- return (0, union_js_1.createTaggedUnionValue)(descriptor, branchIndex, branchValue);
379
- }
380
- function getDescriptorDisplayName(descriptor, index = 0) {
381
- const customType = descriptor?.customType;
382
- if (typeof customType === "string" && customType.length > 0) {
383
- return humanizeVisualText(customType);
384
- }
385
- if (isModelRefDescriptor(descriptor)) {
386
- return humanizeVisualText(descriptor.model || `Branch ${index + 1}`);
387
- }
388
- if (isEnumDescriptor(descriptor))
389
- return "Enum";
390
- if (isUnionDescriptor(descriptor))
391
- return "Union";
392
- const descriptorType = descriptor?.type;
393
- if (typeof descriptorType === "string") {
394
- return humanizeVisualText(descriptorType);
395
- }
396
- return `Branch ${index + 1}`;
397
- }
398
- function humanizeVisualText(value) {
399
- return value
400
- .replace(/([a-z0-9])([A-Z])/g, "$1 $2")
401
- .replace(/[-_]+/g, " ")
402
- .trim()
403
- .replace(/\b\w/g, (match) => match.toUpperCase());
404
- }
package/dist/cjs/index.js DELETED
@@ -1,22 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.delay = void 0;
18
- __exportStar(require("./validation.js"), exports);
19
- __exportStar(require("./union.js"), exports);
20
- __exportStar(require("./descriptor-capabilities.js"), exports);
21
- const delay = (ms) => new Promise((res) => setTimeout(res, ms));
22
- exports.delay = delay;
@@ -1 +0,0 @@
1
- {"type":"commonjs"}