@firecms/schema_inference 3.0.0-canary.231 → 3.0.0-canary.233

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,798 @@
1
+ export type CMSType =
2
+ | string
3
+ | number
4
+ | boolean
5
+ | Date
6
+ | GeoPoint
7
+ | EntityReference
8
+ | Record<string, any>
9
+ | CMSType[];
10
+
11
+ /**
12
+ * @group Entity properties
13
+ */
14
+ export type DataType<T extends CMSType = CMSType> =
15
+ T extends string ? "string" :
16
+ T extends number ? "number" :
17
+ T extends boolean ? "boolean" :
18
+ T extends Date ? "date" :
19
+ T extends GeoPoint ? "geopoint" :
20
+ T extends Vector ? "vector" :
21
+ T extends EntityReference ? "reference" :
22
+ T extends Array<any> ? "array" :
23
+ T extends Record<string, any> ? "map" : never;
24
+
25
+ /**
26
+ * New or existing status
27
+ * @group Models
28
+ */
29
+ export type EntityStatus = "new" | "existing" | "copy";
30
+
31
+ /**
32
+ * Representation of an entity fetched from the datasource
33
+ * @group Models
34
+ */
35
+ export interface Entity<M extends object = any> {
36
+
37
+ /**
38
+ * ID of the entity
39
+ */
40
+ id: string;
41
+
42
+ /**
43
+ * A string representing the path of the referenced document (relative
44
+ * to the root of the database).
45
+ */
46
+ path: string;
47
+
48
+ /**
49
+ * Current values
50
+ */
51
+ values: EntityValues<M>;
52
+
53
+ databaseId?: string;
54
+ }
55
+
56
+ /**
57
+ * This type represents a record of key value pairs as described in an
58
+ * entity collection.
59
+ * @group Models
60
+ */
61
+ export type EntityValues<M extends object> = M;
62
+
63
+ /**
64
+ * Class used to create a reference to an entity in a different path
65
+ */
66
+ export class EntityReference {
67
+ /**
68
+ * ID of the entity
69
+ */
70
+ readonly id: string;
71
+ /**
72
+ * A string representing the path of the referenced document (relative
73
+ * to the root of the database).
74
+ */
75
+ readonly path: string;
76
+
77
+ constructor(id: string, path: string) {
78
+ this.id = id;
79
+ this.path = path;
80
+ }
81
+
82
+ get pathWithId() {
83
+ return `${this.path}/${this.id}`;
84
+ }
85
+
86
+ isEntityReference() {
87
+ return true;
88
+ }
89
+ }
90
+
91
+ export class GeoPoint {
92
+
93
+ /**
94
+ * The latitude of this GeoPoint instance.
95
+ */
96
+ readonly latitude: number;
97
+ /**
98
+ * The longitude of this GeoPoint instance.
99
+ */
100
+ readonly longitude: number;
101
+
102
+ constructor(latitude: number, longitude: number) {
103
+ this.latitude = latitude;
104
+ this.longitude = longitude;
105
+ }
106
+ }
107
+
108
+ export class Vector {
109
+ readonly value: number[];
110
+
111
+ constructor(value: number[]) {
112
+ this.value = value;
113
+ }
114
+ }
115
+
116
+ /**
117
+ * @group Entity properties
118
+ */
119
+ export type Property<T extends CMSType = any> =
120
+ T extends string ? StringProperty :
121
+ T extends number ? NumberProperty :
122
+ T extends boolean ? BooleanProperty :
123
+ T extends Date ? DateProperty :
124
+ T extends GeoPoint ? GeopointProperty :
125
+ T extends EntityReference ? ReferenceProperty :
126
+ T extends Array<CMSType> ? ArrayProperty<T> :
127
+ T extends Record<string, any> ? MapProperty<T> : any;
128
+
129
+ /**
130
+ * Interface including all common properties of a CMS property
131
+ * @group Entity properties
132
+ */
133
+ export interface BaseProperty<T extends CMSType, CustomProps = any> {
134
+
135
+ /**
136
+ * Datatype of the property
137
+ */
138
+ dataType: DataType;
139
+
140
+ /**
141
+ * Property name (e.g. Product)
142
+ */
143
+ name?: string;
144
+
145
+ /**
146
+ * Property description, always displayed under the field
147
+ */
148
+ description?: string;
149
+
150
+ /**
151
+ * You can use this prop to reuse a property that has been defined
152
+ * in the top level of the CMS in the prop `fields`.
153
+ * All the configuration will be taken from the inherited config, and
154
+ * overwritten by the current property config.
155
+ */
156
+ propertyConfig?: string;
157
+
158
+ /**
159
+ * Longer description of a field, displayed under a popover
160
+ */
161
+ longDescription?: string;
162
+
163
+ /**
164
+ * Width in pixels of this column in the collection view. If not set
165
+ * the width is inferred based on the other configurations
166
+ */
167
+ columnWidth?: number;
168
+
169
+ /**
170
+ * Do not show this property in the collection view
171
+ */
172
+ hideFromCollection?: boolean;
173
+
174
+ /**
175
+ * Is this a read only property. When set to true, it gets rendered as a
176
+ * preview.
177
+ */
178
+ readOnly?: boolean;
179
+
180
+ /**
181
+ * Is this field disabled.
182
+ * When set to true, it gets rendered as a
183
+ * disabled field. You can also specify a configuration for defining the
184
+ * behaviour of disabled properties (including custom messages, clear value on
185
+ * disabled or hide the field completely)
186
+ */
187
+ disabled?: boolean | PropertyDisabledConfig;
188
+
189
+ /**
190
+ * Rules for validating this property
191
+ */
192
+ validation?: PropertyValidationSchema;
193
+
194
+ /**
195
+ * Additional props that are passed to the components defined in `field`
196
+ * or in `preview`.
197
+ */
198
+ customProps?: CustomProps;
199
+
200
+ /**
201
+ * This value will be set by default for new entities.
202
+ */
203
+ defaultValue?: T | null;
204
+
205
+ /**
206
+ * Should this property be editable. If set to true, the user will be able to modify the property and
207
+ * save the new config. The saved config will then become the source of truth.
208
+ */
209
+ editable?: boolean;
210
+
211
+ /**
212
+ * A number between 0 and 100 that indicates the width of the field in the form view.
213
+ * It defaults to 100, but you can set it to 50 to have two fields in the same row.
214
+ */
215
+ widthPercentage?: number;
216
+ }
217
+
218
+ /**
219
+ * @group Entity properties
220
+ */
221
+ export interface PropertyDisabledConfig {
222
+
223
+ /**
224
+ * Enable this flag if you would like to clear the value of the field
225
+ * when the corresponding property gets disabled.
226
+ *
227
+ * This is useful for keeping data consistency when you have conditional
228
+ * properties.
229
+ */
230
+ clearOnDisabled?: boolean;
231
+
232
+ /**
233
+ * Explanation of why this property is disabled (e.g. a different field
234
+ * needs to be enabled)
235
+ */
236
+ disabledMessage?: string;
237
+
238
+ /**
239
+ * Set this flag to true if you want to hide this field when disabled
240
+ */
241
+ hidden?: boolean;
242
+ }
243
+
244
+ /**
245
+ * We use this type to define mapping between string or number values in
246
+ * the data source to a label (such in a select dropdown).
247
+ * The key in this Record is the value saved in the datasource, and the value in
248
+ * this record is the label displayed in the UI.
249
+ * You can add additional customization by assigning a {@link EnumValueConfig} for the
250
+ * label instead of a simple string (for enabling or disabling options and
251
+ * choosing colors).
252
+ * If you need to ensure the order of the elements use an array of {@link EnumValueConfig}
253
+ * @group Entity properties
254
+ */
255
+ export type EnumValues = EnumValueConfig[]
256
+ | Record<string | number, string | EnumValueConfig>;
257
+
258
+ /**
259
+ * Configuration for a particular entry in an `EnumValues`
260
+ * @group Entity properties
261
+ */
262
+ export type EnumValueConfig = {
263
+ /**
264
+ * Value stored in the data source.
265
+ */
266
+ id: string | number;
267
+ /**
268
+ * Displayed label
269
+ */
270
+ label: string;
271
+ /**
272
+ * This value will not be selectable
273
+ */
274
+ disabled?: boolean;
275
+ }
276
+
277
+ /**
278
+ * Record of properties of an entity or a map property
279
+ * @group Entity properties
280
+ */
281
+ export type Properties<M extends Record<string, any> = any> = {
282
+ [k in keyof M]: Property<M[keyof M]>;
283
+ };
284
+
285
+ /**
286
+ * @group Entity properties
287
+ */
288
+ export type PropertyOrBuilder<T extends CMSType = CMSType, M extends Record<string, any> = any> =
289
+ Property<T>;
290
+
291
+ /**
292
+ * @group Entity properties
293
+ */
294
+ export type PropertiesOrBuilders<M extends Record<string, any> = any> =
295
+ {
296
+ [k in keyof M]: PropertyOrBuilder<M[k], M>;
297
+ };
298
+
299
+ /**
300
+ * @group Entity properties
301
+ */
302
+ export interface NumberProperty extends BaseProperty<number> {
303
+
304
+ dataType: "number";
305
+
306
+ /**
307
+ * You can use the enum values providing a map of possible
308
+ * exclusive values the property can take, mapped to the label that it is
309
+ * displayed in the dropdown.
310
+ */
311
+ enumValues?: EnumValues;
312
+
313
+ /**
314
+ * Rules for validating this property
315
+ */
316
+ validation?: NumberPropertyValidationSchema,
317
+
318
+ /**
319
+ * Add an icon to clear the value and set it to `null`. Defaults to `false`
320
+ */
321
+ clearable?: boolean;
322
+ }
323
+
324
+ /**
325
+ * @group Entity properties
326
+ */
327
+ export interface BooleanProperty extends BaseProperty<boolean> {
328
+
329
+ dataType: "boolean";
330
+
331
+ /**
332
+ * Rules for validating this property
333
+ */
334
+ validation?: PropertyValidationSchema,
335
+
336
+ }
337
+
338
+ /**
339
+ * @group Entity properties
340
+ */
341
+ export interface StringProperty extends BaseProperty<string> {
342
+
343
+ dataType: "string";
344
+
345
+ /**
346
+ * Is this string property long enough so it should be displayed in
347
+ * a multiple line field. Defaults to false. If set to true,
348
+ * the number of lines adapts to the content
349
+ */
350
+ multiline?: boolean;
351
+
352
+ /**
353
+ * Should this string property be displayed as a markdown field. If true,
354
+ * the field is rendered as a text editors that supports markdown highlight
355
+ * syntax. It also includes a preview of the result.
356
+ */
357
+ markdown?: boolean;
358
+
359
+ /**
360
+ * You can use the enum values providing a map of possible
361
+ * exclusive values the property can take, mapped to the label that it is
362
+ * displayed in the dropdown. You can use a simple object with the format
363
+ * `value` => `label`, or with the format `value` => `EnumValueConfig` if you
364
+ * need extra customization, (like disabling specific options or assigning
365
+ * colors). If you need to ensure the order of the elements, you can pass
366
+ * a `Map` instead of a plain object.
367
+ *
368
+ */
369
+ enumValues?: EnumValues;
370
+
371
+ /**
372
+ * You can specify a `Storage` configuration. It is used to
373
+ * indicate that this string refers to a path in your storage provider.
374
+ */
375
+ storage?: StorageConfig;
376
+
377
+ /**
378
+ * If the value of this property is a URL, you can set this flag to true
379
+ * to add a link, or one of the supported media types to render a preview
380
+ */
381
+ url?: boolean | PreviewType;
382
+
383
+ /**
384
+ * Does this field include an email
385
+ */
386
+ email?: boolean;
387
+
388
+ /**
389
+ * Should this string be rendered as a tag instead of just text.
390
+ */
391
+ previewAsTag?: boolean;
392
+
393
+ /**
394
+ * Rules for validating this property
395
+ */
396
+ validation?: StringPropertyValidationSchema;
397
+
398
+ /**
399
+ * Add an icon to clear the value and set it to `null`. Defaults to `false`
400
+ */
401
+ clearable?: boolean;
402
+ }
403
+
404
+ /**
405
+ * @group Entity properties
406
+ */
407
+ export interface ArrayProperty<T extends ArrayT[] = any[], ArrayT extends CMSType = any> extends BaseProperty<T> {
408
+
409
+ dataType: "array";
410
+
411
+ /**
412
+ * The property of this array.
413
+ * You can specify any property (except another Array property)
414
+ * You can leave this field empty only if you are providing a custom field,
415
+ * or using the `oneOf` prop, otherwise an error will be thrown.
416
+ */
417
+ of?: PropertyOrBuilder<ArrayT> | Property<ArrayT>[];
418
+
419
+ /**
420
+ * Use this field if you would like to have an array of typed objects.
421
+ * It is useful if you need to have values of different types in the same
422
+ * array.
423
+ * Each entry of the array is an object with the shape:
424
+ * ```
425
+ * { type: "YOUR_TYPE", value: "YOUR_VALUE"}
426
+ * ```
427
+ * Note that you can use any property so `value` can take any value (strings,
428
+ * numbers, array, objects...)
429
+ * You can customise the `type` and `value` fields to suit your needs.
430
+ *
431
+ * An example use case for this feature may be a blog entry, where you have
432
+ * images and text blocks using markdown.
433
+ */
434
+ oneOf?: {
435
+ /**
436
+ * Record of properties, where the key is the `type` and the value
437
+ * is the corresponding property
438
+ */
439
+ properties: Properties;
440
+
441
+ /**
442
+ * Order in which the properties are displayed.
443
+ * If you are specifying your collection as code, the order is the same as the
444
+ * one you define in `properties`, and you don't need to specify this prop.
445
+ */
446
+ propertiesOrder?: string[];
447
+
448
+ /**
449
+ * Name of the field to use as the discriminator for type
450
+ * Defaults to `type`
451
+ */
452
+ typeField?: string;
453
+
454
+ /**
455
+ * Name of the field to use as the value
456
+ * Defaults to `value`
457
+ */
458
+ valueField?: string;
459
+ };
460
+
461
+ /**
462
+ * Rules for validating this property
463
+ */
464
+ validation?: ArrayPropertyValidationSchema;
465
+
466
+ /**
467
+ * Should the field be initially expanded. Defaults to `true`
468
+ */
469
+ expanded?: boolean;
470
+
471
+ /**
472
+ * Display the child properties directly, without being wrapped in an
473
+ * extendable panel.
474
+ */
475
+ minimalistView?: boolean;
476
+
477
+ /**
478
+ * Can the elements in this array be reordered. Defaults to `true`.
479
+ * This prop has no effect if `disabled` is set to true.
480
+ */
481
+ sortable?: boolean;
482
+
483
+ /**
484
+ * Can the elements in this array be added. Defaults to `true`
485
+ * This prop has no effect if `disabled` is set to true.
486
+ */
487
+ canAddElements?: boolean;
488
+
489
+ }
490
+
491
+ /**
492
+ * @group Entity properties
493
+ */
494
+ export interface MapProperty<T extends Record<string, CMSType> = Record<string, CMSType>> extends BaseProperty<T> {
495
+
496
+ dataType: "map";
497
+
498
+ /**
499
+ * Record of properties included in this map.
500
+ */
501
+ properties?: PropertiesOrBuilders<T>;
502
+
503
+ /**
504
+ * Order in which the properties are displayed.
505
+ * If you are specifying your collection as code, the order is the same as the
506
+ * one you define in `properties`, and you don't need to specify this prop.
507
+ */
508
+ propertiesOrder?: Extract<keyof T, string>[];
509
+
510
+ /**
511
+ * Rules for validating this property.
512
+ * NOTE: If you don't set `required` in the map property, an empty object
513
+ * will be considered valid, even if you set `required` in the properties.
514
+ */
515
+ validation?: PropertyValidationSchema,
516
+
517
+ /**
518
+ * Properties that are displayed when rendered as a preview
519
+ */
520
+ previewProperties?: Partial<Extract<keyof T, string>>[];
521
+
522
+ /**
523
+ * Allow the user to add only some keys in this map.
524
+ * By default, all properties of the map have the corresponding field in
525
+ * the form view. Setting this flag to true allows to pick only some.
526
+ * Useful for map that can have a lot of sub-properties that may not be
527
+ * needed
528
+ */
529
+ pickOnlySomeKeys?: boolean;
530
+
531
+ /**
532
+ * Display the child properties as independent columns in the collection
533
+ * view
534
+ */
535
+ spreadChildren?: boolean;
536
+
537
+ /**
538
+ * Display the child properties directly, without being wrapped in an
539
+ * extendable panel. Note that this will also hide the title of this property.
540
+ */
541
+ minimalistView?: boolean;
542
+
543
+ /**
544
+ * Should the field be initially expanded. Defaults to `true`
545
+ */
546
+ expanded?: boolean;
547
+
548
+ /**
549
+ * Render this map as a key-value table that allows to use
550
+ * arbitrary keys. You don't need to define the properties in this case.
551
+ */
552
+ keyValue?: boolean;
553
+
554
+ }
555
+
556
+ /**
557
+ * @group Entity properties
558
+ */
559
+ export interface DateProperty extends BaseProperty<Date> {
560
+
561
+ dataType: "date";
562
+
563
+ /**
564
+ * Set the granularity of the field to a date or date + time.
565
+ * Defaults to `date_time`.
566
+ *
567
+ */
568
+ mode?: "date" | "date_time";
569
+
570
+ /**
571
+ * Rules for validating this property
572
+ */
573
+ validation?: DatePropertyValidationSchema;
574
+
575
+ /**
576
+ * If this flag is set to `on_create` or `on_update` this timestamp is
577
+ * updated automatically on creation of the entity only or on every
578
+ * update (including creation). Useful for creating `created_on` or
579
+ * `updated_on` fields
580
+ */
581
+ autoValue?: "on_create" | "on_update"
582
+
583
+ /**
584
+ * Add an icon to clear the value and set it to `null`. Defaults to `false`
585
+ */
586
+ clearable?: boolean;
587
+ }
588
+
589
+ /**
590
+ * @group Entity properties
591
+ */
592
+ // TODO: currently this is the only unsupported field
593
+ export interface GeopointProperty extends BaseProperty<GeoPoint> {
594
+
595
+ dataType: "geopoint";
596
+
597
+ /**
598
+ * Rules for validating this property
599
+ */
600
+ validation?: PropertyValidationSchema,
601
+
602
+ }
603
+
604
+ /**
605
+ * @group Entity properties
606
+ */
607
+ export interface ReferenceProperty extends BaseProperty<EntityReference> {
608
+
609
+ dataType: "reference";
610
+
611
+ /**
612
+ * Absolute collection path of the collection this reference points to.
613
+ * The collection of the entity is inferred based on the root navigation, so
614
+ * the filters and search delegate existing there are applied to this view
615
+ * as well.
616
+ * You can leave this prop undefined if the path is not yet know, e.g.
617
+ * you are using a property builder and the path depends on a different
618
+ * property.
619
+ * Note that you can also use a collection alias.
620
+ */
621
+ path?: string;
622
+
623
+ /**
624
+ * Properties that need to be rendered when displaying a preview of this
625
+ * reference. If not specified the first 3 are used. Only the first 3
626
+ * specified values are considered.
627
+ */
628
+ previewProperties?: string[];
629
+
630
+ /**
631
+ * Should the reference include the ID of the entity. Defaults to `true`
632
+ */
633
+ includeId?: boolean;
634
+
635
+ /**
636
+ * Should the reference include a link to the entity (open the entity details). Defaults to `true`
637
+ */
638
+ includeEntityLink?: boolean;
639
+
640
+ }
641
+
642
+ export interface PropertyValidationSchema {
643
+ /**
644
+ * Is this field required
645
+ */
646
+ required?: boolean;
647
+
648
+ /**
649
+ * Customize the required message when the property is not set
650
+ */
651
+ requiredMessage?: string;
652
+
653
+ /**
654
+ * If the unique flag is set to `true`, you can only have one entity in the
655
+ * collection with this value.
656
+ */
657
+ unique?: boolean;
658
+
659
+ /**
660
+ * If the uniqueInArray flag is set to `true`, you can only have this value
661
+ * once per entry in the parent `ArrayProperty`. It has no effect if this
662
+ * property is not a child of an `ArrayProperty`. It works on direct
663
+ * children of an `ArrayProperty` or first level children of `MapProperty`
664
+ */
665
+ uniqueInArray?: boolean;
666
+ }
667
+
668
+ /**
669
+ * Validation rules for numbers
670
+ * @group Entity properties
671
+ */
672
+ export interface NumberPropertyValidationSchema extends PropertyValidationSchema {
673
+ min?: number;
674
+ max?: number;
675
+ lessThan?: number;
676
+ moreThan?: number;
677
+ positive?: boolean;
678
+ negative?: boolean;
679
+ integer?: boolean;
680
+ }
681
+
682
+ /**
683
+ * Validation rules for strings
684
+ * @group Entity properties
685
+ */
686
+ export interface StringPropertyValidationSchema extends PropertyValidationSchema {
687
+ length?: number;
688
+ min?: number;
689
+ max?: number;
690
+ matches?: string | RegExp;
691
+ /**
692
+ * Message displayed when the input does not satisfy the regex in `matches`
693
+ */
694
+ matchesMessage?: string;
695
+ trim?: boolean;
696
+ lowercase?: boolean;
697
+ uppercase?: boolean;
698
+ }
699
+
700
+ /**
701
+ * Validation rules for dates
702
+ * @group Entity properties
703
+ */
704
+ export interface DatePropertyValidationSchema extends PropertyValidationSchema {
705
+ min?: Date;
706
+ max?: Date;
707
+ }
708
+
709
+ /**
710
+ * Validation rules for arrays
711
+ * @group Entity properties
712
+ */
713
+ export interface ArrayPropertyValidationSchema extends PropertyValidationSchema {
714
+ min?: number;
715
+ max?: number;
716
+ }
717
+
718
+ /**
719
+ * Additional configuration related to Storage related fields
720
+ * @group Entity properties
721
+ */
722
+ export type StorageConfig = {
723
+
724
+ /**
725
+ * File MIME types that can be uploaded to this reference. Don't specify for
726
+ * all.
727
+ * Note that you can also use the asterisk notation, so `image/*`
728
+ * accepts any image file, and so on.
729
+ */
730
+ acceptedFiles?: FileType[];
731
+
732
+ /**
733
+ * Specific metadata set in your uploaded file.
734
+ * For the default Firebase implementation, the values passed here are of type
735
+ * `firebase.storage.UploadMetadata`
736
+ */
737
+ metadata?: Record<string, unknown>,
738
+
739
+ /**
740
+ * You can use this prop to customize the uploaded filename.
741
+ * You can use a function as a callback or a string where you
742
+ * specify some placeholders that get replaced with the corresponding values.
743
+ * - `{file}` - Full file name
744
+ * - `{file.name}` - Name of the file without extension
745
+ * - `{file.ext}` - Extension of the file
746
+ * - `{rand}` - Random value used to avoid name collisions
747
+ * - `{entityId}` - ID of the entity
748
+ * - `{propertyKey}` - ID of this property
749
+ * - `{path}` - Path of this entity
750
+ *
751
+ * @param context
752
+ */
753
+ fileName?: string;
754
+
755
+ /**
756
+ * Absolute path in your bucket.
757
+ *
758
+ * You can use a function as a callback or a string where you
759
+ * specify some placeholders that get replaced with the corresponding values.
760
+ * - `{file}` - Full file name
761
+ * - `{file.name}` - Name of the file without extension
762
+ * - `{file.ext}` - Extension of the file
763
+ * - `{rand}` - Random value used to avoid name collisions
764
+ * - `{entityId}` - ID of the entity
765
+ * - `{propertyKey}` - ID of this property
766
+ * - `{path}` - Path of this entity
767
+ */
768
+ storagePath: string;
769
+
770
+ /**
771
+ * When set to true, this flag indicates that the download URL of the file
772
+ * will be saved in the datasource, instead of the storage path.
773
+ *
774
+ * Note that the generated URL may use a token that, if disabled, may
775
+ * make the URL unusable and lose the original reference to Cloud Storage,
776
+ * so it is not encouraged to use this flag.
777
+ *
778
+ * Defaults to false.
779
+ */
780
+ storeUrl?: boolean,
781
+
782
+ /**
783
+ * Define maximal file size in bytes
784
+ */
785
+ maxSize?: number,
786
+
787
+ }
788
+
789
+ export type FileType =
790
+ "image/*"
791
+ | "video/*"
792
+ | "audio/*"
793
+ | "application/*"
794
+ | "text/*"
795
+ | "font/*"
796
+ | string;
797
+
798
+ export type PreviewType = "image" | "video" | "audio" | "file";