@supernova-studio/client 0.2.2 → 0.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supernova-studio/client",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Supernova Data Models",
5
5
  "source": "src/index.ts",
6
6
  "main": "dist/index.js",
@@ -5,9 +5,9 @@ import {
5
5
  PageBlockText,
6
6
  PageBlockTextSpan,
7
7
  PageBlockTextSpanAttribute,
8
- PageBlockItemRichTextPropertyValue,
8
+ PageBlockItemRichTextValue,
9
9
  PageBlockCalloutType,
10
- PageBlockItemMultiRichTextPropertyValue,
10
+ PageBlockItemMultiRichTextValue,
11
11
  } from "@supernova-studio/model";
12
12
  import { PageBlockEditorModel } from "./model/block";
13
13
  import { ProsemirrorNode, ProsemirrorMark } from "./prosemirror/types";
@@ -30,11 +30,11 @@ type InputWithProperty = BaseInput & {
30
30
  };
31
31
 
32
32
  type RichTextInputWithValue = InputWithProperty & {
33
- richTextPropertyValue: PageBlockItemRichTextPropertyValue;
33
+ richTextPropertyValue: PageBlockItemRichTextValue;
34
34
  };
35
35
 
36
36
  type MultiRichTextInputWithValue = InputWithProperty & {
37
- multiRichTextPropertyValue: PageBlockItemMultiRichTextPropertyValue;
37
+ multiRichTextPropertyValue: PageBlockItemMultiRichTextValue;
38
38
  };
39
39
 
40
40
  //
@@ -1,4 +1,4 @@
1
- import { PageBlockItemUntypedPropertyValue } from "@supernova-studio/model";
1
+ import { PageBlockItemUntypedValue } from "@supernova-studio/model";
2
2
 
3
3
  export type ProsemirrorNode = {
4
4
  type: string;
@@ -15,5 +15,5 @@ export type ProsemirrorMark = {
15
15
 
16
16
  export type ProsemirrorBlockItem = {
17
17
  id: string;
18
- props: Record<string, PageBlockItemUntypedPropertyValue>;
18
+ props: Record<string, PageBlockItemUntypedValue>;
19
19
  };
@@ -7,13 +7,33 @@ import {
7
7
  PageBlockTextSpanAttribute,
8
8
  PageBlockDefinitionProperty,
9
9
  PageBlockCalloutType,
10
- PageBlockItemRichTextPropertyValue,
11
- PageBlockItemMultiRichTextPropertyValue,
12
- PageBlockItemEmbedPropertyValue,
10
+ PageBlockItemEmbedValue,
13
11
  PageBlockItemV2,
14
- PageBlockItemTextPropertyValue,
15
- PageBlockItemUntypedPropertyValue,
12
+ PageBlockItemTextValue,
16
13
  PageBlockItemImageValue,
14
+ PageBlockItemRichTextValue,
15
+ PageBlockDefinitionPropertyType,
16
+ PageBlockItemMultiRichTextValue,
17
+ PageBlockItemBooleanValue,
18
+ PageBlockItemNumberValue,
19
+ PageBlockItemSingleSelectValue,
20
+ PageBlockItemMultiSelectValue,
21
+ PageBlockItemTokenValue,
22
+ PageBlockItemTokenTypeValue,
23
+ PageBlockItemTokenPropertyValue,
24
+ PageBlockItemComponentValue,
25
+ PageBlockItemComponentPropertyValue,
26
+ PageBlockItemAssetValue,
27
+ PageBlockItemAssetPropertyValue,
28
+ PageBlockItemUrlValue,
29
+ PageBlockItemMarkdownValue,
30
+ PageBlockItemCodeValue,
31
+ PageBlockItemSandboxValue,
32
+ PageBlockItemTableValue,
33
+ PageBlockItemDividerValue,
34
+ PageBlockItemStorybookValue,
35
+ PageBlockItemColorValue,
36
+ PageBlockItemUntypedValue,
17
37
  } from "@supernova-studio/model";
18
38
  import { PageBlockEditorModel } from "./model/block";
19
39
  import { DocumentationPageEditorModel } from "./model/page";
@@ -109,7 +129,7 @@ function parseAsRichText(
109
129
 
110
130
  // Optional
111
131
  ...(calloutType && { calloutType: calloutType }),
112
- } satisfies PageBlockItemRichTextPropertyValue,
132
+ } satisfies PageBlockItemRichTextValue,
113
133
  },
114
134
  },
115
135
  ],
@@ -170,7 +190,7 @@ function parseAsMultiRichText(
170
190
  return parseRichText(paragraph.content ?? []);
171
191
  })
172
192
  .filter(nonNullFilter),
173
- } satisfies PageBlockItemMultiRichTextPropertyValue,
193
+ } satisfies PageBlockItemMultiRichTextValue,
174
194
  },
175
195
  },
176
196
  ],
@@ -256,7 +276,7 @@ function parseAsEmbed(
256
276
  value: url,
257
277
  caption: caption,
258
278
  height: height,
259
- } as PageBlockItemEmbedPropertyValue,
279
+ } as PageBlockItemEmbedValue,
260
280
  },
261
281
  },
262
282
  ],
@@ -303,7 +323,7 @@ function parseAsCustomBlock(
303
323
  return null;
304
324
  }
305
325
 
306
- const parsedItems = itemsJson.map(i => parseItem(i, definition));
326
+ const parsedItems = itemsJson.map(i => parseItem(i, definition)).filter(nonNullFilter);
307
327
 
308
328
  return {
309
329
  id: id,
@@ -324,33 +344,20 @@ function parseItem(rawItem: unknown, definition: PageBlockDefinition): PageBlock
324
344
  return null;
325
345
  }
326
346
 
327
- const sanitizedProps: Record<string, PageBlockItemUntypedPropertyValue> = {};
347
+ const sanitizedProps: Record<string, PageBlockItemUntypedValue> = {};
328
348
 
329
349
  for (const property of definition.item.properties) {
330
350
  const value = parsedItem.data.props[property.id];
331
351
  if (!value) {
332
- return null;
352
+ continue;
333
353
  }
334
354
 
335
- // Validate
336
- switch (property.type) {
337
- case "Text":
338
- PageBlockItemTextPropertyValue.parse(value);
339
- break;
355
+ const valueSchema = valueSchemaForPropertyType(property.type);
356
+ const validationResult = valueSchema.safeParse(value);
340
357
 
341
- case "EmbedURL":
342
- PageBlockItemEmbedPropertyValue.parse(value);
343
- break;
344
-
345
- case "Image":
346
- PageBlockItemImageValue.parse(value);
347
- break;
348
-
349
- default:
350
- return null;
358
+ if (validationResult.success) {
359
+ sanitizedProps[property.id] = validationResult.data;
351
360
  }
352
-
353
- sanitizedProps[property.id] = value;
354
361
  }
355
362
 
356
363
  return {
@@ -360,6 +367,59 @@ function parseItem(rawItem: unknown, definition: PageBlockDefinition): PageBlock
360
367
  };
361
368
  }
362
369
 
370
+ function valueSchemaForPropertyType(type: PageBlockDefinitionPropertyType) {
371
+ switch (type) {
372
+ case "RichText":
373
+ return PageBlockItemRichTextValue;
374
+ case "MultiRichText":
375
+ return PageBlockItemMultiRichTextValue;
376
+ case "Text":
377
+ return PageBlockItemTextValue;
378
+ case "Boolean":
379
+ return PageBlockItemBooleanValue;
380
+ case "Number":
381
+ return PageBlockItemNumberValue;
382
+ case "SingleSelect":
383
+ return PageBlockItemSingleSelectValue;
384
+ case "MultiSelect":
385
+ return PageBlockItemMultiSelectValue;
386
+ case "Image":
387
+ return PageBlockItemImageValue;
388
+ case "Token":
389
+ return PageBlockItemTokenValue;
390
+ case "TokenType":
391
+ return PageBlockItemTokenTypeValue;
392
+ case "TokenProperty":
393
+ return PageBlockItemTokenPropertyValue;
394
+ case "Component":
395
+ return PageBlockItemComponentValue;
396
+ case "ComponentProperty":
397
+ return PageBlockItemComponentPropertyValue;
398
+ case "Asset":
399
+ return PageBlockItemAssetValue;
400
+ case "AssetProperty":
401
+ return PageBlockItemAssetPropertyValue;
402
+ case "EmbedURL":
403
+ return PageBlockItemEmbedValue;
404
+ case "URL":
405
+ return PageBlockItemUrlValue;
406
+ case "Markdown":
407
+ return PageBlockItemMarkdownValue;
408
+ case "Code":
409
+ return PageBlockItemCodeValue;
410
+ case "CodeSandbox":
411
+ return PageBlockItemSandboxValue;
412
+ case "Table":
413
+ return PageBlockItemTableValue;
414
+ case "Divider":
415
+ return PageBlockItemDividerValue;
416
+ case "Storybook":
417
+ return PageBlockItemStorybookValue;
418
+ case "Color":
419
+ return PageBlockItemColorValue;
420
+ }
421
+ }
422
+
363
423
  //
364
424
  // Attributes
365
425
  //
@@ -2,10 +2,10 @@ import { PageBlockEditorModel } from "./model/block";
2
2
  import {
3
3
  PageBlockDefinition,
4
4
  PageBlockDefinitionPropertyType,
5
- PageBlockItemEmbedPropertyValue,
6
- PageBlockItemMultiRichTextPropertyValue,
7
- PageBlockItemRichTextPropertyValue,
8
- PageBlockItemUntypedPropertyValue,
5
+ PageBlockItemEmbedValue,
6
+ PageBlockItemMultiRichTextValue,
7
+ PageBlockItemRichTextValue,
8
+ PageBlockItemUntypedValue,
9
9
  PageBlockItemV2,
10
10
  PageBlockText,
11
11
  } from "@supernova-studio/model";
@@ -18,23 +18,23 @@ export const BlockParsingUtils = {
18
18
  return block.data.items[0]!;
19
19
  },
20
20
 
21
- richTextPropertyValue(item: PageBlockItemV2, propertyKey: string): PageBlockItemRichTextPropertyValue {
21
+ richTextPropertyValue(item: PageBlockItemV2, propertyKey: string): PageBlockItemRichTextValue {
22
22
  const objectValue = this.objectPropertyValue(item, propertyKey);
23
- const richText = PageBlockItemRichTextPropertyValue.parse(objectValue);
23
+ const richText = PageBlockItemRichTextValue.parse(objectValue);
24
24
 
25
25
  return richText;
26
26
  },
27
27
 
28
- multiRichTextPropertyValue(item: PageBlockItemV2, propertyKey: string): PageBlockItemMultiRichTextPropertyValue {
28
+ multiRichTextPropertyValue(item: PageBlockItemV2, propertyKey: string): PageBlockItemMultiRichTextValue {
29
29
  const objectValue = this.objectPropertyValue(item, propertyKey);
30
- const richText = PageBlockItemMultiRichTextPropertyValue.parse(objectValue);
30
+ const richText = PageBlockItemMultiRichTextValue.parse(objectValue);
31
31
 
32
32
  return richText;
33
33
  },
34
34
 
35
- embedPropertyValue(item: PageBlockItemV2, propertyKey: string): PageBlockItemEmbedPropertyValue {
35
+ embedPropertyValue(item: PageBlockItemV2, propertyKey: string): PageBlockItemEmbedValue {
36
36
  const objectValue = this.objectPropertyValue(item, propertyKey);
37
- const embed = PageBlockItemEmbedPropertyValue.parse(objectValue);
37
+ const embed = PageBlockItemEmbedValue.parse(objectValue);
38
38
 
39
39
  return embed;
40
40
  },
@@ -49,7 +49,7 @@ export const BlockParsingUtils = {
49
49
  return value;
50
50
  },
51
51
 
52
- propertyValueOrThrow(item: PageBlockItemV2, propertyKey: string): PageBlockItemUntypedPropertyValue {
52
+ propertyValueOrThrow(item: PageBlockItemV2, propertyKey: string): PageBlockItemUntypedValue {
53
53
  const value = item.props[propertyKey];
54
54
  if (!value) throw new Error(`Property ${propertyKey} is not defined on block item`);
55
55