forked from coda/packs-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.ts
1548 lines (1450 loc) · 55.1 KB
/
schema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type {$Values} from './type_utils';
import {assertCondition} from './helpers/ensure';
import {deepCopy} from './helpers/object_utils';
import {ensureExists} from './helpers/ensure';
import {ensureNonEmptyString} from './helpers/ensure';
import {ensureUnreachable} from './helpers/ensure';
import {objectSchemaHelper} from './helpers/migration';
import pascalcase from 'pascalcase';
// Defines a subset of the JSON Object schema for use in annotating API results.
// http://json-schema.org/latest/json-schema-core.html#rfc.section.8.2
/**
* The set of primitive value types that can be used as return values for formulas
* or in object schemas.
*/
export enum ValueType {
/**
* Indicates a JavaScript boolean (true/false) should be returned.
*/
Boolean = 'boolean',
/**
* Indicates a JavaScript number should be returned.
*/
Number = 'number',
/**
* Indicates a JavaScript string should be returned.
*/
String = 'string',
/**
* Indicates a JavaScript array should be returned. The schema of the array items must also be specified.
*/
Array = 'array',
/**
* Indicates a JavaScript object should be returned. The schema of each object property must also be specified.
*/
Object = 'object',
}
/**
* Synthetic types that instruct Coda how to coerce values from primitives at ingestion time.
*/
export enum ValueHintType {
/**
* Indicates to interpret the value as a date (e.g. March 3, 2021).
*/
Date = 'date',
/**
* Indicates to interpret the value as a time (e.g. 5:24pm).
*/
Time = 'time',
/**
* Indicates to interpret the value as a datetime (e.g. March 3, 2021 at 5:24pm).
*/
DateTime = 'datetime',
/**
* Indicates to interpret the value as a duration (e.g. 3 hours).
*/
Duration = 'duration',
/**
* Indicates to interpret the value as an email address (e.g. joe@foo.com).
*/
Email = 'email',
/**
* Indicates to interpret and render the value as a Coda person reference. The provided value should be
* an object whose `id` property is an email address, which Coda will try to resolve to a user
* and render an @-reference to the user.
*
* @example
* ```
* makeObjectSchema({
* type: ValueType.Object,
* codaType: ValueHintType.Person,
* id: 'email',
* primary: 'name',
* properties: {
* email: {type: ValueType.String, required: true},
* name: {type: ValueType.String, required: true},
* },
* });
* ```
*/
Person = 'person',
/**
* Indicates to interpret and render the value as a percentage.
*/
Percent = 'percent',
/**
* Indicates to interpret and render the value as a currency value.
*/
Currency = 'currency',
/**
* Indicates to interpret and render the value as an image. The provided value should be a URL that
* points to an image. Coda will hotlink to the image when rendering it a doc.
*
* Using {@link ImageAttachment} is recommended instead, so that the image is always accessible
* and won't appear as broken if the source image is later deleted.
*/
ImageReference = 'image',
/**
* Indicates to interpret and render the value as an image. The provided value should be a URL that
* points to an image. Coda will ingest the image and host it from Coda infrastructure.
*/
ImageAttachment = 'imageAttachment',
/**
* Indicates to interpret and render the value as a URL link.
*/
Url = 'url',
/**
* Indicates to interpret a text value as Markdown, which will be converted and rendered as Coda rich text.
*/
Markdown = 'markdown',
/**
* Indicates to interpret a text value as HTML, which will be converted and rendered as Coda rich text.
*/
Html = 'html',
/**
* Indicates to interpret and render a value as an embed. The provided value should be a URL pointing
* to an embeddable web page.
*/
Embed = 'embed',
/**
* Indicates to interpret and render the value as a Coda @-reference to a table row. The provided value should
* be an object whose `id` value matches the id of some row in a sync table. The schema where this hint type is
* used must specify an identity that specifies the desired sync table.
*
* Normally a reference schema is constructed from the schema object being referenced using the helper
* {@link makeReferenceSchemaFromObjectSchema}.
*
* @example
* ```
* makeObjectSchema({
* type: ValueType.Object,
* codaType: ValueHintType.Reference,
* identity: {
* name: "SomeSyncTableIdentity"
* },
* id: 'identifier',
* primary: 'name',
* properties: {
* identifier: {type: ValueType.Number, required: true},
* name: {type: ValueType.String, required: true},
* },
* });
* ```
*/
Reference = 'reference',
/**
* Indicates to interpret and render a value as a file attachment. The provided value should be a URL
* pointing to a file of a Coda-supported type. Coda will ingest the file and host it from Coda infrastructure.
*/
Attachment = 'attachment',
/**
* Indicates to render a numeric value as a slider UI component.
*/
Slider = 'slider',
/**
* Indicates to render a numeric value as a scale UI component (e.g. a star rating).
*/
Scale = 'scale',
/**
* Indicates to render a numeric value as a progress bar UI component.
*/
ProgressBar = 'progressBar',
/**
* Indicates to render a boolean value as a toggle.
*/
Toggle = 'toggle',
}
export const StringHintValueTypes = [
ValueHintType.Attachment,
ValueHintType.Date,
ValueHintType.Time,
ValueHintType.DateTime,
ValueHintType.Duration,
ValueHintType.Email,
ValueHintType.Embed,
ValueHintType.Html,
ValueHintType.ImageReference,
ValueHintType.ImageAttachment,
ValueHintType.Markdown,
ValueHintType.Url,
] as const;
export const NumberHintValueTypes = [
ValueHintType.Date,
ValueHintType.Time,
ValueHintType.DateTime,
ValueHintType.Duration,
ValueHintType.Percent,
ValueHintType.Currency,
ValueHintType.Slider,
ValueHintType.ProgressBar,
ValueHintType.Scale,
] as const;
export const BooleanHintValueTypes = [ValueHintType.Toggle] as const;
export const ObjectHintValueTypes = [ValueHintType.Person, ValueHintType.Reference] as const;
/** The subset of {@link ValueHintType} that can be used with a string value. */
export type StringHintTypes = typeof StringHintValueTypes[number];
/** The subset of {@link ValueHintType} that can be used with a number value. */
export type NumberHintTypes = typeof NumberHintValueTypes[number];
/** The subset of {@link ValueHintType} that can be used with a boolean value. */
export type BooleanHintTypes = typeof BooleanHintValueTypes[number];
/** The subset of {@link ValueHintType} that can be used with an object value. */
export type ObjectHintTypes = typeof ObjectHintValueTypes[number];
interface BaseSchema {
/**
* A explanation of this object schema property shown to the user in the UI.
*
* If your pack has an object schema with many properties, it may be useful to
* explain the purpose or contents of any property that is not self-evident.
*/
description?: string;
/**
* Whether this object schema property is editable by the user in the UI.
*/
/** @hidden */
mutable?: boolean;
}
/**
* A schema representing a return value or object property that is a boolean.
*/
export interface BooleanSchema extends BaseSchema {
/** Identifies this schema as relating to a boolean value. */
type: ValueType.Boolean;
/** Indicates how to render values in a table. If not specified, renders a checkbox. */
codaType?: BooleanHintTypes;
}
/**
* The union of all schemas that can represent number values.
*/
export type NumberSchema =
| CurrencySchema
| SliderSchema
| ProgressBarSchema
| ScaleSchema
| NumericSchema
| NumericDateSchema
| NumericTimeSchema
| NumericDateTimeSchema
| NumericDurationSchema;
export interface BaseNumberSchema<T extends NumberHintTypes = NumberHintTypes> extends BaseSchema {
/** Identifies this schema as relating to a number value. */
type: ValueType.Number;
/** An optional type hint instructing Coda about how to interpret or render this value. */
codaType?: T;
}
/**
* A schema representing a return value or object property that is a numeric value,
* i.e. a raw number with an optional decimal precision.
*/
export interface NumericSchema extends BaseNumberSchema {
/** If specified, instructs Coda to render this value as a percentage. */
codaType?: ValueHintType.Percent; // Can also be undefined if it's a vanilla number
/** The decimal precision. The number will be rounded to this precision when rendered. */
precision?: number;
/** If specified, will render thousands separators for large numbers, e.g. `1,234,567.89`. */
useThousandsSeparator?: boolean;
}
/**
* A schema representing a return value or object property that is provided as a number,
* which Coda should interpret as a date. The given number should be in seconds since the Unix epoch.
*/
export interface NumericDateSchema extends BaseNumberSchema<ValueHintType.Date> {
/** Instructs Coda to render this value as a date. */
codaType: ValueHintType.Date;
/**
* A Moment date format string, such as 'MMM D, YYYY', that corresponds to a supported Coda date column format,
* used when rendering the value.
*
* Only applies when this is used as a sync table property.
*/
format?: string;
}
/**
* A schema representing a return value or object property that is provided as a number,
* which Coda should interpret as a time. The given number should be in seconds since the Unix epoch.
* While this is a full datetime, only the time component will be rendered, so the date used is irrelevant.
*/
export interface NumericTimeSchema extends BaseNumberSchema<ValueHintType.Time> {
/** Instructs Coda to render this value as a time. */
codaType: ValueHintType.Time;
/**
* A Moment time format string, such as 'HH:mm:ss', that corresponds to a supported Coda time column format,
* used when rendering the value.
*
* Only applies when this is used as a sync table property.
*/
format?: string;
}
/**
* A schema representing a return value or object property that is provided as a number,
* which Coda should interpret as a datetime. The given number should be in seconds since the Unix epoch.
*/
export interface NumericDateTimeSchema extends BaseNumberSchema<ValueHintType.DateTime> {
/** Instructs Coda to render this value as a datetime. */
codaType: ValueHintType.DateTime;
/**
* A Moment date format string, such as 'MMM D, YYYY', that corresponds to a supported Coda date column format.
*
* Only applies when this is used as a sync table property.
*/
dateFormat?: string;
/**
* A Moment time format string, such as 'HH:mm:ss', that corresponds to a supported Coda time column format,
* used when rendering the value.
*
* Only applies when this is used as a sync table property.
*/
timeFormat?: string;
}
/**
* A schema representing a return value or object property that is provided as a number,
* which Coda should interpret as a duration. The given number should be an amount of days
* (fractions allowed).
*/
export interface NumericDurationSchema extends BaseNumberSchema<ValueHintType.Duration> {
/** Instructs Coda to render this value as a duration. */
codaType: ValueHintType.Duration;
/**
* A refinement of {@link DurationSchema.maxUnit} to use for rounding the duration when rendering.
* Currently only `1` is supported, which is the same as omitting a value.
*/
precision?: number;
/**
* The unit to use for rounding the duration when rendering. For example, if using `DurationUnit.Days`,
* and a value of 273600 is provided (3 days 4 hours) is provided, it will be rendered as "3 days".
*/
maxUnit?: DurationUnit;
}
/**
* Enumeration of formats supported by schemas that use {@link ValueHintType.Currency}.
*
* These affect how a numeric value is rendered in docs.
*/
export enum CurrencyFormat {
/**
* Indicates the value should be rendered as a number with a currency symbol as a prefix, e.g. `$2.50`.
*/
Currency = 'currency',
/**
* Indicates the value should be rendered as a number with a currency symbol as a prefix, but padded
* to allow the numeric values to line up vertically, e.g.
*
* ```
* $ 2.50
* $ 29.99
* ```
*/
Accounting = 'accounting',
/**
* Indicates the value should be rendered as a number without a currency symbol, e.g. `2.50`.
*/
Financial = 'financial',
}
/**
* A schema representing a return value or object property that is an amount of currency.
*/
export interface CurrencySchema extends BaseNumberSchema<ValueHintType.Currency> {
/** Instructs Coda to render this value as a currency amount. */
codaType: ValueHintType.Currency;
/** The decimal precision. The value is rounded to this precision when rendered. */
precision?: number;
/**
* A three-letter ISO 4217 currency code, e.g. USD or EUR.
* If the currency code is not supported by Coda, the value will be rendered using USD.
*/
currencyCode?: string;
/** A render format for further refining how the value is rendered. */
format?: CurrencyFormat;
}
/**
* A schema representing a return value or object property that is a number that should
* be rendered as a slider.
*/
export interface SliderSchema extends BaseNumberSchema<ValueHintType.Slider> {
/** Instructs Coda to render this value as a slider. */
codaType: ValueHintType.Slider;
/** The minimum value selectable by this slider. */
minimum?: number | string;
/** The maximum value selectable by this slider. */
maximum?: number | string;
/** The minimum amount the slider can be moved when dragged. */
step?: number | string;
/** Whether to display the underlying numeric value in addition to the slider. */
showValue?: boolean;
}
/**
* A schema representing a return value or object property that is a number that should
* be rendered as a progress bar.
*/
export interface ProgressBarSchema extends BaseNumberSchema<ValueHintType.ProgressBar> {
/** Instructs Coda to render this value as a progress bar. */
codaType: ValueHintType.ProgressBar;
/** The minimum value selectable by this progress bar. */
minimum?: number | string;
/** The maximum value selectable by this progress bar. */
maximum?: number | string;
/** The minimum amount the progress bar can be moved when dragged. */
step?: number | string;
/** Whether to display the underlying numeric value in addition to the progress bar. */
showValue?: boolean;
}
/**
* Icons that can be used with a {@link ScaleSchema}.
*
* For example, to render a star rating, use a {@link ScaleSchema} with `icon: coda.ScaleIconSet.Star`.
*/
export enum ScaleIconSet {
Star = 'star',
Circle = 'circle',
Fire = 'fire',
Bug = 'bug',
Diamond = 'diamond',
Bell = 'bell',
ThumbsUp = 'thumbsup',
Heart = 'heart',
Chili = 'chili',
Smiley = 'smiley',
Lightning = 'lightning',
Currency = 'currency',
Coffee = 'coffee',
Person = 'person',
Battery = 'battery',
Cocktail = 'cocktail',
Cloud = 'cloud',
Sun = 'sun',
Checkmark = 'checkmark',
LightBulb = 'lightbulb',
}
/**
* A schema representing a return value or object property that is a number that should
* be rendered as a scale.
*
* A scale is a widget with a repeated set of icons, where the number of shaded represents
* a numeric value. The canonical example of a scale is a star rating, which might show
* 5 star icons, with 3 of them shaded, indicating a value of 3.
*/
export interface ScaleSchema extends BaseNumberSchema<ValueHintType.Scale> {
/** Instructs Coda to render this value as a scale. */
codaType: ValueHintType.Scale;
/** The number of icons to render. */
maximum?: number;
/** The icon to render. */
icon?: ScaleIconSet;
}
/**
* Display types that can be used with an {@link EmailSchema}.
*/
export enum EmailDisplayType {
/**
* Display both icon and email (default).
*/
IconAndEmail = 'iconAndEmail',
/**
* Display icon only.
*/
IconOnly = 'iconOnly',
/**
* Display email address only.
*/
EmailOnly = 'emailOnly',
}
/**
* A schema representing a return value or object property that is an email address.
*
* An email address can be represented visually as an icon, an icon plus the email address, or
* the just the email address. Autocomplete against previously typed domain names is
* also an option in the user interface.
*/
export interface EmailSchema extends BaseStringSchema<ValueHintType.Email> {
/** Instructs Coda to render this value as an email address. */
codaType: ValueHintType.Email;
/** How the email should be displayed in the UI. */
display?: EmailDisplayType;
/** Whether to auto-complete the email domain during user input. */
autocomplete?: boolean;
}
/**
* Display types that can be used with a {@link LinkSchema}.
*/
export enum LinkDisplayType {
/**
* Display icon only.
*/
IconOnly = 'iconOnly',
/**
* Display URL.
*/
Url = 'url',
/**
* Display web page title.
*/
Title = 'title',
/**
* Display the referenced web page as a card.
*/
Card = 'card',
/**
* Display the referenced web page as an embed.
*/
Embed = 'embed',
}
/**
* A schema representing a return value or object property that is a hyperlink.
*
* The link can be displayed in the UI in multiple ways, as per the above enumeration.
*/
export interface LinkSchema extends BaseStringSchema<ValueHintType.Url> {
/** Instructs Coda to render this value as a hyperlink. */
codaType: ValueHintType.Url;
/** How the URL should be displayed in the UI. */
display?: LinkDisplayType;
/** Whether to force client embedding (only for LinkDisplayType.Embed) - for example, if user login required. */
force?: boolean;
}
/**
* A schema representing a return value or object property that is provided as a string,
* which Coda should interpret as a date. Coda is able to flexibly parse a number of formal
* and informal string representations of dates. For maximum accuracy, consider using an
* ISO 8601 date string (e.g. 2021-10-29): https://en.wikipedia.org/wiki/ISO_8601.
*/
export interface StringDateSchema extends BaseStringSchema<ValueHintType.Date> {
/** Instructs Coda to render this value as a date. */
codaType: ValueHintType.Date;
/**
* A Moment date format string, such as 'MMM D, YYYY', that corresponds to a supported Coda date column format,
* used when rendering the value.
*
* Only applies when this is used as a sync table property.
*/
format?: string;
}
/**
* A schema representing a return value or object property that is provided as a string,
* which Coda should interpret as an embed value (e.g. a URL). Coda uses an external provider (iframely)
* to handle all embeds by default. If there is no support for a given embed that you want to use,
* you will need to use the `force` option which falls back to a generic iframe.
*/
export interface StringEmbedSchema extends BaseStringSchema<ValueHintType.Embed> {
/** Instructs Coda to render this value as an embed. */
codaType: ValueHintType.Embed;
/**
* Toggle whether to try to force embed the content in Coda. Should be kept to false for most cases.
*
* By default, we use an external provider (iframely) that supports and normalizes embeds for different sites.
* If you are trying to embed an uncommon site or one that is not supported by them,
* you can set this to `true` to tell Coda to force render the embed. This renders a sandboxed iframe for the embed
* but requires user consent per-domain to actually display the embed.
*/
force?: boolean;
}
/**
* A schema representing a return value or object property that is provided as a string,
* which Coda should interpret as a time.
*/
export interface StringTimeSchema extends BaseStringSchema<ValueHintType.Time> {
/** Instructs Coda to render this value as a date. */
codaType: ValueHintType.Time;
/**
* A Moment time format string, such as 'HH:mm:ss', that corresponds to a supported Coda time column format,
* used when rendering the value.
*
* Only applies when this is used as a sync table property.
*/
format?: string;
}
/**
* A schema representing a return value or object property that is provided as a string,
* which Coda should interpret as a datetime. Coda is able to flexibly a parse number of formal
* and informal string representations of dates. For maximum accuracy, consider using an
* ISO 8601 datetime string (e.g. 2021-11-03T19:43:58): https://en.wikipedia.org/wiki/ISO_8601.
*/
export interface StringDateTimeSchema extends BaseStringSchema<ValueHintType.DateTime> {
/** Instructs Coda to render this value as a date. */
codaType: ValueHintType.DateTime;
/**
* A Moment date format string, such as 'MMM D, YYYY', that corresponds to a supported Coda date column format,
* used when rendering the value.
*
* Only applies when this is used as a sync table property.
*/
dateFormat?: string;
/**
* A Moment time format string, such as 'HH:mm:ss', that corresponds to a supported Coda time column format,
* used when rendering the value.
*
* Only applies when this is used as a sync table property.
*/
timeFormat?: string;
}
/**
* State of outline on images that can be used with a {@link ImageSchema}.
*/
export enum ImageOutline {
/** Image is rendered without outline. */
Disabled = 'disabled',
/** Image is rendered with outline. */
Solid = 'solid',
}
/**
* State of corners on images that can be used with a {@link ImageSchema}.
*/
export enum ImageCornerStyle {
/** Image is rendered with rounded corners. */
Rounded = 'rounded',
/** Image is rendered with square corners. */
Square = 'square',
}
/**
* A schema representing a return value or object property that is provided as a string,
* which Coda should interpret as an image.
*/
export interface ImageSchema extends BaseStringSchema<ValueHintType.ImageReference | ValueHintType.ImageAttachment> {
/** Instructs Coda to render this value as an Image. */
codaType: ValueHintType.ImageReference | ValueHintType.ImageAttachment;
/** ImageOutline type specifying style of outline on images. If unspecified, default is Solid. */
imageOutline?: ImageOutline;
/** ImageCornerStyle type specifying style of corners on images. If unspecified, default is Rounded. */
imageCornerStyle?: ImageCornerStyle;
}
/**
* Enumeration of units supported by duration schemas. See {@link DurationSchema.maxUnit}.
*/
export enum DurationUnit {
/**
* Indications a duration as a number of days.
*/
Days = 'days',
/**
* Indications a duration as a number of hours.
*/
Hours = 'hours',
/**
* Indications a duration as a number of minutes.
*/
Minutes = 'minutes',
/**
* Indications a duration as a number of seconds.
*/
Seconds = 'seconds',
}
/**
* A schema representing a return value or object property that represents a duration. The value
* should be provided as a string like "3 days" or "40 minutes 30 seconds".
*/
export interface DurationSchema extends BaseStringSchema<ValueHintType.Duration> {
/**
* A refinement of {@link DurationSchema.maxUnit} to use for rounding the duration when rendering.
* Currently only `1` is supported, which is the same as omitting a value.
*/
precision?: number;
/**
* The unit to use for rounding the duration when rendering. For example, if using `DurationUnit.Days`,
* and a value of "3 days 4 hours" is provided, it will be rendered as "3 days".
*/
maxUnit?: DurationUnit;
}
export interface BaseStringSchema<T extends StringHintTypes = StringHintTypes> extends BaseSchema {
/** Identifies this schema as a string. */
type: ValueType.String;
/** An optional type hint instructing Coda about how to interpret or render this value. */
codaType?: T;
}
/**
* The subset of StringHintTypes that don't have specific schema attributes.
*/
export const SimpleStringHintValueTypes = [
ValueHintType.Attachment,
ValueHintType.Html,
ValueHintType.Markdown,
ValueHintType.Url,
ValueHintType.Email,
] as const;
export type SimpleStringHintTypes = typeof SimpleStringHintValueTypes[number];
/**
* A schema whose underlying value is a string, along with an optional hint about how Coda
* should interpret that string.
*/
export interface SimpleStringSchema<T extends SimpleStringHintTypes = SimpleStringHintTypes>
extends BaseStringSchema<T> {}
/**
* The union of schema definition types whose underlying value is a string.
*/
export type StringSchema =
| StringDateSchema
| StringTimeSchema
| StringDateTimeSchema
| DurationSchema
| EmailSchema
| ImageSchema
| LinkSchema
| StringEmbedSchema
| SimpleStringSchema;
/**
* A schema representing a return value or object property that is an array (list) of items.
* The items are themselves schema definitions, which may refer to scalars or other objects.
*/
export interface ArraySchema<T extends Schema = Schema> extends BaseSchema {
/** Identifies this schema as an array. */
type: ValueType.Array;
/** A schema for the items of this array. */
items: T;
}
/**
* Fields that may be set on a schema property in the {@link ObjectSchemaDefinition.properties} definition
* of an object schema.
*/
export interface ObjectSchemaProperty {
/**
* The name of a field in a return value object that should be re-mapped to this property.
* This provides a way to rename fields from API responses without writing code.
*
* Suppose that you're fetching an object from an API that has a property called "duration".
* But in your pack, you'd like the value to be called "durationSeconds" to be more precise.
* You could write code in your `execute` function to relabel the field, but you could
* also use `fromKey` and Coda will do it for you.
*
* Suppose your `execute` function looked like this:
* ```
* execute: async function(context) {
* const response = await context.fetcher.fetch({method: "GET", url: "/api/some-entity"});
* // Suppose the body of the response looks like {duration: 123, name: "foo"}.
* return response.body;
* }
* ```
*
* You can define your schema like this:
* ```
* coda.makeObjectSchema({
* properties: {
* name: {type: coda.ValueType.String},
* durationSeconds: {type: coda.ValueType.Number, fromKey: "duration"},
* },
* });
* ```
*
* This tells Coda to transform your formula's return value, creating a field "durationSeconds"
* whose value comes another field called "duration".
*/
fromKey?: string;
/**
* When true, indicates that an object return value for a formula that has this schema must
* include a non-empty value for this property.
*/
required?: boolean;
}
/**
* The type of the {@link ObjectSchemaDefinition.properties} in the definition of an object schema.
* This is essentially a dictionary mapping the name of a property to a schema
* definition for that property.
*/
export type ObjectSchemaProperties<K extends string = never> = {
[K2 in K | string]: Schema & ObjectSchemaProperty;
};
/** @hidden */
export type GenericObjectSchema = ObjectSchema<string, string>;
// TODO(jonathan, ekoleda): Link to a guide about identities and references.
/**
* An identifier for a schema, allowing other schemas to reference it.
*
* You may optionally specify an {@link ObjectSchemaDefinition.identity} when defining an object schema.
* This signals that this schema represents an important named entity in the context of your pack.
* Schemas with identities may be referenced by other schemas, in which case Coda
* will render such values as @-references in the doc, allowing you to create relationships
* between entities.
*
* Every sync table's top-level schema is required to have an identity. However, an identity
* will be created on your behalf using the {@link SyncTableOptions.identityName} that you provide in the sync
* table definition, so you needn't explicitly create on unless desired.
*/
export interface IdentityDefinition {
/**
* The name of this entity. This is an arbitrary name but should be unique within your pack.
* For example, if you are defining a schema that represents a user object, "User" would be a good identity name.
*/
name: string;
// TODO(jonathan): Inject the dynamicUrl at runtime like we do the packId so that pack makers
// don't have to it themselves.
/**
* The dynamic URL, if this is a schema for a dynamic sync table. When returning a schema from the
* {@link DynamicSyncTableOptions.getSchema} formula of a dynamic sync table, you must include
* the dynamic URL of that table, so that rows
* in this table may be distinguished from rows in another dynamic instance of the same table.
*
* When creating a reference to a dynamic sync table, you must include the dynamic URL of the table
* you wish to reference, again to distinguish which table instance you are trying to reference.
*/
dynamicUrl?: string;
/** The ID of another pack, if you are trying to reference a value from different pack. */
packId?: number;
/** @deprecated See {@link ObjectSchemaDefinition.attribution} */
attribution?: AttributionNode[];
}
/**
* The runtime version of {@link IdentityDefinition} with the current Pack ID injected if a different
* one isn't set by the maker.
*/
export interface Identity extends IdentityDefinition {
packId: number;
}
/**
* An identifier for a schema property for specifying labels along with the reference to the property.
* This is useful for specifying a label for a property reference that uses a json path, where the
* label of the underlying property might not be descriptive enough at the top-level object.
*/
export interface PropertyIdentifierDetails {
/**
* An optional label for the property. This will be used in locations where the label appears with the property.
*
* If set to '', the label will be omitted.
*/
label?: string;
/**
* The value of the property to reference. Can be either an exact property name or a json path.
*/
property: string;
}
/**
* An identifier for the value of a property for use in the {@link PropertyIdentifierDetails.label} field.
* When used, this will be substituted with the value of the property for the final output of the label.
*
* If not present, the label will be used as-is in the default label format of '\{label\}: \{VALUE\}'.
*/
export const PropertyLabelValueTemplate = '{VALUE}';
/**
* An identifier for an object schema property that is comprised of either an exact property match with the top-level
* `properties or a json path (https://github.com/json-path/JsonPath) to a nested property.
*/
export type PropertyIdentifier<K extends string = string> = K | string | PropertyIdentifierDetails;
/**
* The {@link ObjectSchemaDefinition} properties that reference keys in the `properties` object. These should all be
* {@link PropertyIdentifier} types.
*/
export type ObjectSchemaPathProperties = Pick<
GenericObjectSchema,
'titleProperty' | 'linkProperty' | 'imageProperty' | 'snippetProperty' | 'subtitleProperties'
>;
/**
* A schema definition for an object value (a value with key-value pairs).
*/
// TODO(spencer): follow-up with converting idProperty and other existing properties to support
// PropertyIdentifier.
export interface ObjectSchemaDefinition<K extends string, L extends string> extends BaseSchema {
/** Identifies this schema as an object schema. */
type: ValueType.Object;
/** Definition of the key-value pairs in this object. */
properties: ObjectSchemaProperties<K | L>;
/** @deprecated Use {@link ObjectSchemaDefinition.idProperty} */
id?: K;
/**
* The name of a property within {@link ObjectSchemaDefinition.properties} that represents a unique id for this
* object. Sync table schemas must specify an id property, which uniquely identify each synced row.
*/
idProperty?: K;
/** @deprecated Use {@link ObjectSchemaDefinition.displayProperty} */
primary?: K;
/**
* The name of a property within {@link ObjectSchemaDefinition.properties} that be used to label this object in the
* UI.
* Object values can contain many properties and the Coda UI will display them as a "chip"
* with only the value of the "displayProperty" property used as the chip's display label.
* The other properties can be seen when hovering over the chip.
*/
displayProperty?: K;
/**
* A hint for how Coda should interpret and render this object value.
*
* For example, an object can represent a person (user) in a Coda doc, with properties for the
* email address of the person and their name. Using `ValueHintType.Person` tells Coda to
* render such a value as an @-reference to that person, rather than a basic object chip.
*/
codaType?: ObjectHintTypes;
/** @deprecated Use {@link ObjectSchemaDefinition.featuredProperties} */
featured?: L[];
/**
* A list of property names from within {@link ObjectSchemaDefinition.properties} for the "featured" properties
* of this object, used in sync tables. When a sync table is first added to a document,
* columns are created for each of the featured properties. The user can easily add additional
* columns for any other properties, as desired. All featured properties need to be top-level.
* If you can't or don't want to change the received data format, consider changing the
* received object after fetching and before returning and assigning it to the schema.
*
* This distinction exists for cases where a sync table may include dozens of properties,
* which would create a very wide table that is difficult to use. Featuring properties
* allows a sync table to be created with the most useful columns created by default,
* and the user can add additional columns as they find them useful.
*
* Non-featured properties can always be referenced in formulas regardless of whether column
* projections have been created for them.
*/
featuredProperties?: L[];
/**
* An identity for this schema, if this schema is important enough to be named and referenced.
* See {@link IdentityDefinition}.
*/
identity?: IdentityDefinition;
/**
* Attribution text, images, and/or links that should be rendered along with this value.
*
* See {@link makeAttributionNode}.
*/
attribution?: AttributionNode[];
/**
* Specifies that object instances with this schema can contain additional properties not defined
* in the schema, and that the packs infrastructure should retain these unknown properties
* rather than stripping them.
*
* Properties not declared in the schema will not work properly in Coda: they cannot be
* used natively in the formula language and will not have correct types in Coda. But, in certain
* scenarios they can be useful.
*/
includeUnknownProperties?: boolean;
/**
* The name of a property within {@link properties} that will be used as a title of a rich card preview
* for formulas that return this object.
* Defaults to the value of {@link ObjectSchemaDefinition.displayProperty} if not specified
*
* Must be a {@link ValueType.String} property
*/
titleProperty?: PropertyIdentifier<K>;
/**
* The name of a property within {@link ObjectSchemaDefinition.properties} that will
* navigate users to more details about this object
*
* Must be a {@link ValueType.String} property with a {@link ValueHintType.Url}
* {@link ObjectSchemaDefinition.codaType}.
*/
linkProperty?: PropertyIdentifier<K>;
/**
* A list of property names from within {@link ObjectSchemaDefinition.properties} for the properties of the object
* to be shown in the subtitle of a rich card preview for formulas that return this object.
* Defaults to the value of {@link ObjectSchemaDefinition.featuredProperties} if not specified.
*/
subtitleProperties?: Array<PropertyIdentifier<K>>;
/**
* The name of a property within {@link ObjectSchemaDefinition.properties} that be used as a textual summary
* of the object.
*
* Must be a {@link ValueType.String} property or {@link ValueType.Array} of {@link ValueType.String}s.
*/
snippetProperty?: PropertyIdentifier<K>;
/**
* The name of a property within {@link ObjectSchemaDefinition.properties} that can be used as a rich image preview of
* the object.
*
* Must be a {@link ValueType.String} property with the
* {@link ValueHintType.ImageAttachment} or {@link ValueHintType.ImageReference} hints
*/