@firecms/schema_inference 3.0.0-canary.24 → 3.0.0-canary.241

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