@stackbit/cms-core 0.1.3 → 0.1.4-alpha.1

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.
Files changed (57) hide show
  1. package/dist/common/common-types.d.ts +1 -9
  2. package/dist/common/common-types.d.ts.map +1 -1
  3. package/dist/consts.d.ts +1 -24
  4. package/dist/consts.d.ts.map +1 -1
  5. package/dist/consts.js +5 -25
  6. package/dist/consts.js.map +1 -1
  7. package/dist/content-store-types.d.ts +42 -39
  8. package/dist/content-store-types.d.ts.map +1 -1
  9. package/dist/content-store-utils.d.ts +10 -0
  10. package/dist/content-store-utils.d.ts.map +1 -0
  11. package/dist/content-store-utils.js +139 -0
  12. package/dist/content-store-utils.js.map +1 -0
  13. package/dist/content-store.d.ts +18 -5
  14. package/dist/content-store.d.ts.map +1 -1
  15. package/dist/content-store.js +177 -962
  16. package/dist/content-store.js.map +1 -1
  17. package/dist/index.d.ts +2 -1
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +3 -2
  20. package/dist/index.js.map +1 -1
  21. package/dist/types/search-filter.d.ts +42 -0
  22. package/dist/types/search-filter.d.ts.map +1 -0
  23. package/dist/types/search-filter.js +3 -0
  24. package/dist/types/search-filter.js.map +1 -0
  25. package/dist/utils/create-update-csi-docs.d.ts +69 -0
  26. package/dist/utils/create-update-csi-docs.d.ts.map +1 -0
  27. package/dist/utils/create-update-csi-docs.js +386 -0
  28. package/dist/utils/create-update-csi-docs.js.map +1 -0
  29. package/dist/utils/csi-to-store-docs-converter.d.ts +15 -0
  30. package/dist/utils/csi-to-store-docs-converter.d.ts.map +1 -0
  31. package/dist/utils/csi-to-store-docs-converter.js +287 -0
  32. package/dist/utils/csi-to-store-docs-converter.js.map +1 -0
  33. package/dist/utils/search-utils.d.ts +21 -0
  34. package/dist/utils/search-utils.d.ts.map +1 -0
  35. package/dist/utils/search-utils.js +323 -0
  36. package/dist/utils/search-utils.js.map +1 -0
  37. package/dist/utils/store-to-api-docs-converter.d.ts +5 -0
  38. package/dist/utils/store-to-api-docs-converter.d.ts.map +1 -0
  39. package/dist/utils/store-to-api-docs-converter.js +247 -0
  40. package/dist/utils/store-to-api-docs-converter.js.map +1 -0
  41. package/package.json +7 -5
  42. package/src/common/common-types.ts +1 -10
  43. package/src/consts.ts +1 -26
  44. package/src/content-store-types.ts +59 -45
  45. package/src/content-store-utils.ts +150 -0
  46. package/src/content-store.ts +168 -1090
  47. package/src/index.ts +3 -2
  48. package/src/types/search-filter.ts +53 -0
  49. package/src/utils/create-update-csi-docs.ts +457 -0
  50. package/src/utils/csi-to-store-docs-converter.ts +366 -0
  51. package/src/utils/search-utils.ts +437 -0
  52. package/src/utils/store-to-api-docs-converter.ts +246 -0
  53. package/dist/content-source-interface.d.ts +0 -338
  54. package/dist/content-source-interface.d.ts.map +0 -1
  55. package/dist/content-source-interface.js +0 -28
  56. package/dist/content-source-interface.js.map +0 -1
  57. package/src/content-source-interface.ts +0 -495
@@ -1,495 +0,0 @@
1
- import type { Model, Field } from '@stackbit/sdk';
2
- import { UserCommandSpawner } from './common/common-types';
3
-
4
- export type DocumentStatus = 'added' | 'modified' | 'published' | 'deleted';
5
-
6
- export interface LeveledLogMethod {
7
- (message: string): void;
8
- (message: string, ...meta: any[]): void;
9
- (message: any): void;
10
- }
11
-
12
- export interface Logger {
13
- error: LeveledLogMethod;
14
- warn: LeveledLogMethod;
15
- info: LeveledLogMethod;
16
- debug: LeveledLogMethod;
17
- createLogger: ({ label }: { label: string }) => Logger;
18
- }
19
-
20
- export interface Locale {
21
- code: string;
22
- default?: boolean;
23
- }
24
-
25
- export interface Document<DocumentContext = unknown> {
26
- type: 'document';
27
- id: string;
28
- manageUrl: string;
29
- modelName: string;
30
- status: DocumentStatus;
31
- createdAt: string;
32
- createdBy?: string;
33
- updatedAt: string;
34
- updatedBy?: string[];
35
- fields: Record<string, DocumentField>;
36
- context: DocumentContext;
37
- }
38
-
39
- export interface Asset<AssetContext = unknown> {
40
- type: 'asset';
41
- id: string;
42
- manageUrl: string;
43
- status: DocumentStatus;
44
- createdAt: string;
45
- createdBy?: string;
46
- updatedAt: string;
47
- updatedBy?: string[];
48
- fields: AssetFields;
49
- context: AssetContext;
50
- }
51
-
52
- export type AssetFields = {
53
- title: DocumentValueFieldForType<'string'>;
54
- file: AssetFileField;
55
- };
56
-
57
- export type DocumentField =
58
- | DocumentValueField
59
- | DocumentObjectField
60
- | DocumentModelField
61
- | DocumentReferenceField
62
- | DocumentListField
63
- | DocumentRichTextField
64
- | DocumentImageField;
65
-
66
- export type DocumentFieldLocalized =
67
- | DocumentValueFieldLocalized
68
- | DocumentObjectFieldLocalized
69
- | DocumentModelFieldLocalized
70
- | DocumentReferenceFieldLocalized
71
- | DocumentListFieldLocalized
72
- | DocumentRichTextFieldLocalized
73
- | DocumentImageFieldLocalized;
74
-
75
- export type DocumentFieldNonLocalized =
76
- | DocumentValueFieldNonLocalized
77
- | DocumentObjectFieldNonLocalized
78
- | DocumentModelFieldNonLocalized
79
- | DocumentReferenceFieldNonLocalized
80
- | DocumentListFieldNonLocalized
81
- | DocumentRichTextFieldNonLocalized
82
- | DocumentImageFieldNonLocalized;
83
-
84
- export type DocumentFieldBaseProps =
85
- | DocumentValueFieldBaseProps
86
- | DocumentObjectFieldBase
87
- | DocumentModelFieldBase
88
- | DocumentReferenceFieldBase
89
- | DocumentListFieldBase
90
- | DocumentRichTextFieldBase
91
- | DocumentImageFieldBase;
92
-
93
- export type DocumentFieldSpecificProps =
94
- | DocumentValueFieldProps
95
- | DocumentObjectFieldProps
96
- | DocumentModelFieldProps
97
- | DocumentReferenceFieldProps
98
- | DocumentListFieldProps
99
- | DocumentRichTextFieldProps
100
- | DocumentImageFieldProps;
101
-
102
- export function isLocalizedField(field: DocumentField | AssetFileField): field is DocumentFieldLocalized | AssetFileFieldLocalized {
103
- return !!field.localized;
104
- }
105
-
106
- export function getLocalizedFieldForLocale<Type extends FieldType>(
107
- field: DocumentFieldForType<Type>,
108
- locale?: string
109
- ): DocumentFieldNonLocalizedForType<Type> | null {
110
- if (field.localized) {
111
- if (!locale) {
112
- return null;
113
- }
114
- const { localized, locales, ...base } = field;
115
- const localizedField = locales[locale];
116
- if (!localizedField) {
117
- return null;
118
- }
119
- return {
120
- ...base,
121
- ...localizedField
122
- } as unknown as DocumentFieldNonLocalizedForType<Type>;
123
- } else {
124
- return field;
125
- }
126
- }
127
-
128
- export type FieldType =
129
- | 'string'
130
- | 'url'
131
- | 'slug'
132
- | 'text'
133
- | 'markdown'
134
- | 'html'
135
- | 'number'
136
- | 'boolean'
137
- | 'enum'
138
- | 'date'
139
- | 'datetime'
140
- | 'color'
141
- | 'image'
142
- | 'file'
143
- | 'json'
144
- | 'richText'
145
- | 'object'
146
- | 'model'
147
- | 'reference'
148
- | 'style'
149
- | 'list';
150
-
151
- export type Simplify<T> = {
152
- [K in keyof T]: T[K];
153
- };
154
-
155
- export type FindByType<Union extends { type: FieldType }, Type> = Union extends { type: Type } ? Union : never;
156
- export type DocumentFieldForType<Type extends FieldType> = FindByType<DocumentField, Type>;
157
- export type DocumentFieldLocalizedForType<Type extends FieldType> = FindByType<DocumentFieldLocalized, Type>;
158
- export type DocumentFieldNonLocalizedForType<Type extends FieldType> = FindByType<DocumentFieldNonLocalized, Type>;
159
- export type DocumentFieldBasePropsForType<Type extends FieldType> = FindByType<DocumentFieldBaseProps, Type>;
160
- export type DocumentFieldSpecificPropsForType<Type extends FieldType> = Simplify<
161
- Omit<DocumentFieldNonLocalizedForType<Type>, keyof DocumentFieldBasePropsForType<Type> | 'locale' | 'localized'>
162
- >;
163
-
164
- export type DocumentFieldTypeLocalized<BaseFieldProps, LocalizedFieldProps> = Simplify<
165
- BaseFieldProps & {
166
- localized: true;
167
- locales: Record<string, { locale: string } & LocalizedFieldProps>;
168
- }
169
- >;
170
-
171
- export type DocumentFieldTypeNonLocalized<BaseFieldProps, LocalizedFieldProps> = Simplify<
172
- BaseFieldProps &
173
- LocalizedFieldProps & {
174
- localized?: false;
175
- locale?: string;
176
- }
177
- >;
178
-
179
- // any field that is not 'object' | 'model' | 'reference' | 'richText' | 'list'
180
- export type DocumentValueFieldType = Exclude<FieldType, 'object' | 'model' | 'reference' | 'richText' | 'list' | 'image'>;
181
-
182
- export type DocumentValueFieldForType<Type> = Type extends DocumentValueFieldType
183
- ? DocumentValueFieldLocalizedForType<Type> | DocumentValueFieldNonLocalizedForType<Type>
184
- : never;
185
- export type DocumentValueField = DocumentValueFieldForType<DocumentValueFieldType>;
186
-
187
- export type DocumentValueFieldLocalizedForType<Type> = Type extends DocumentValueFieldType
188
- ? DocumentFieldTypeLocalized<DocumentValueFieldBase<Type>, DocumentValueFieldProps>
189
- : never;
190
- export type DocumentValueFieldLocalized = DocumentValueFieldLocalizedForType<DocumentValueFieldType>;
191
-
192
- export type DocumentValueFieldNonLocalizedForType<Type> = Type extends DocumentValueFieldType
193
- ? DocumentFieldTypeNonLocalized<DocumentValueFieldBase<Type>, DocumentValueFieldProps>
194
- : never;
195
- export type DocumentValueFieldNonLocalized = DocumentValueFieldNonLocalizedForType<DocumentValueFieldType>;
196
-
197
- export type DocumentValueFieldBasePropsForType<Type> = Type extends DocumentValueFieldType ? DocumentValueFieldBase<Type> : never;
198
- export type DocumentValueFieldBaseProps = DocumentValueFieldBasePropsForType<DocumentValueFieldType>;
199
- export type DocumentValueFieldBase<Type extends DocumentValueFieldType> = {
200
- type: Type;
201
- };
202
- export type DocumentValueFieldProps = {
203
- value: any;
204
- };
205
-
206
- // object
207
- export type DocumentObjectField = DocumentObjectFieldLocalized | DocumentObjectFieldNonLocalized;
208
- export type DocumentObjectFieldLocalized = DocumentFieldTypeLocalized<DocumentObjectFieldBase, DocumentObjectFieldProps>;
209
- export type DocumentObjectFieldNonLocalized = DocumentFieldTypeNonLocalized<DocumentObjectFieldBase, DocumentObjectFieldProps>;
210
- export type DocumentObjectFieldBase = {
211
- type: 'object';
212
- };
213
- export type DocumentObjectFieldProps = {
214
- fields: Record<string, DocumentField>;
215
- };
216
-
217
- // model
218
- export type DocumentModelField = DocumentModelFieldLocalized | DocumentModelFieldNonLocalized;
219
- export type DocumentModelFieldLocalized = DocumentFieldTypeLocalized<DocumentModelFieldBase, DocumentModelFieldProps>;
220
- export type DocumentModelFieldNonLocalized = DocumentFieldTypeNonLocalized<DocumentModelFieldBase, DocumentModelFieldProps>;
221
- export type DocumentModelFieldBase = {
222
- type: 'model';
223
- };
224
- export type DocumentModelFieldProps = {
225
- modelName: string;
226
- fields: Record<string, DocumentField>;
227
- };
228
-
229
- // reference
230
- export type DocumentReferenceField = DocumentReferenceFieldLocalized | DocumentReferenceFieldNonLocalized;
231
- export type DocumentReferenceFieldLocalized = DocumentFieldTypeLocalized<DocumentReferenceFieldBase, DocumentReferenceFieldProps>;
232
- export type DocumentReferenceFieldNonLocalized = DocumentFieldTypeNonLocalized<DocumentReferenceFieldBase, DocumentReferenceFieldProps>;
233
- export type DocumentReferenceFieldBase = {
234
- type: 'reference';
235
- refType: 'document' | 'asset';
236
- };
237
- export type DocumentReferenceFieldProps = {
238
- refId: string;
239
- };
240
-
241
- // richText
242
- export type DocumentRichTextField = DocumentRichTextFieldLocalized | DocumentRichTextFieldNonLocalized;
243
- export type DocumentRichTextFieldLocalized = DocumentFieldTypeLocalized<DocumentRichTextFieldBase, DocumentRichTextFieldProps>;
244
- export type DocumentRichTextFieldNonLocalized = DocumentFieldTypeNonLocalized<DocumentRichTextFieldBase, DocumentRichTextFieldProps>;
245
- export type DocumentRichTextFieldBase = {
246
- type: 'richText';
247
- };
248
- export type DocumentRichTextFieldProps = {
249
- hint?: string;
250
- value: any;
251
- };
252
-
253
- // list
254
- export type DocumentListField = DocumentListFieldLocalized | DocumentListFieldNonLocalized;
255
- export type DocumentListFieldLocalized = DocumentFieldTypeLocalized<DocumentListFieldBase, DocumentListFieldProps>;
256
- export type DocumentListFieldNonLocalized = DocumentFieldTypeNonLocalized<DocumentListFieldBase, DocumentListFieldProps>;
257
- export type DocumentListFieldBase = {
258
- type: 'list';
259
- };
260
- export type DocumentListFieldProps = {
261
- items: DocumentFieldNonLocalized[];
262
- };
263
-
264
- // image
265
- export type DocumentImageField = DocumentImageFieldLocalized | DocumentImageFieldNonLocalized;
266
- export type DocumentImageFieldLocalized = DocumentFieldTypeLocalized<DocumentImageFieldBase, DocumentImageFieldProps>;
267
- export type DocumentImageFieldNonLocalized = DocumentFieldTypeNonLocalized<DocumentImageFieldBase, DocumentImageFieldProps>;
268
- export type DocumentImageFieldBase = {
269
- type: 'image';
270
- };
271
- export type DocumentImageFieldProps = {
272
- fields: {
273
- title: DocumentValueFieldForType<'string'>;
274
- url: DocumentValueFieldForType<'string'>;
275
- };
276
- };
277
-
278
- // assetFile
279
- export type AssetFileField = AssetFileFieldLocalized | AssetFileFieldNonLocalized;
280
- export type AssetFileFieldLocalized = DocumentFieldTypeLocalized<AssetFileFieldBase, AssetFileFieldProps>;
281
- export type AssetFileFieldNonLocalized = DocumentFieldTypeNonLocalized<AssetFileFieldBase, AssetFileFieldProps>;
282
- export type AssetFileFieldBase = {
283
- type: 'assetFile';
284
- };
285
- export type AssetFileFieldProps = {
286
- url: string;
287
- fileName?: string;
288
- contentType?: string;
289
- size?: number;
290
- dimensions: {
291
- width?: number;
292
- height?: number;
293
- };
294
- };
295
-
296
- export interface InitOptions {
297
- logger: Logger;
298
- userLogger: Logger;
299
- userCommandSpawner?: UserCommandSpawner;
300
- localDev: boolean;
301
- }
302
-
303
- export interface ContentChangeEvent<DocumentContext = unknown, AssetContext = unknown> {
304
- documents: Document<DocumentContext>[];
305
- assets: Asset<AssetContext>[];
306
- deletedDocumentIds: string[];
307
- deletedAssetIds: string[];
308
- }
309
-
310
- export type UpdateOperation = UpdateOperationSet | UpdateOperationUnset | UpdateOperationInsert | UpdateOperationRemove | UpdateOperationReorder;
311
-
312
- export type UpdateOperationBase = {
313
- opType: string;
314
- fieldPath: (string | number)[];
315
- modelField: Field;
316
- locale?: string;
317
- };
318
-
319
- export type UpdateOperationSet = Simplify<
320
- UpdateOperationBase & {
321
- opType: 'set';
322
- field: UpdateOperationField;
323
- }
324
- >;
325
-
326
- export type UpdateOperationUnset = Simplify<
327
- UpdateOperationBase & {
328
- opType: 'unset';
329
- }
330
- >;
331
-
332
- export type UpdateOperationInsert = Simplify<
333
- UpdateOperationBase & {
334
- opType: 'insert';
335
- index?: number;
336
- item: UpdateOperationField;
337
- }
338
- >;
339
-
340
- export type UpdateOperationRemove = Simplify<
341
- UpdateOperationBase & {
342
- opType: 'remove';
343
- index: number;
344
- }
345
- >;
346
-
347
- export type UpdateOperationReorder = Simplify<
348
- UpdateOperationBase & {
349
- opType: 'reorder';
350
- order: number[];
351
- }
352
- >;
353
-
354
- export type UpdateOperationField =
355
- | UpdateOperationValueField
356
- | UpdateOperationObjectField
357
- | UpdateOperationModelField
358
- | UpdateOperationReferenceField
359
- | UpdateOperationListField;
360
-
361
- export type UpdateOperationValueFieldType = Exclude<FieldType, 'object' | 'model' | 'reference' | 'list'>;
362
- export type UpdateOperationValueField<Type = UpdateOperationValueFieldType> = Type extends UpdateOperationValueFieldType
363
- ? UpdateOperationValueFieldForType<Type>
364
- : never;
365
- export type UpdateOperationValueFieldForType<Type extends UpdateOperationValueFieldType> = {
366
- type: Type;
367
- value: any;
368
- };
369
-
370
- export type UpdateOperationObjectField = {
371
- type: 'object';
372
- fields: Record<string, UpdateOperationField>;
373
- };
374
-
375
- export type UpdateOperationModelField = {
376
- type: 'model';
377
- modelName: string;
378
- fields: Record<string, UpdateOperationField>;
379
- };
380
-
381
- export type UpdateOperationReferenceField = {
382
- type: 'reference';
383
- refType: 'document' | 'asset';
384
- refId: string;
385
- };
386
-
387
- export type UpdateOperationListField = {
388
- type: 'list';
389
- items: UpdateOperationField[];
390
- }
391
-
392
- export type ValidationError = {
393
- message: string;
394
- objectType: 'document' | 'asset';
395
- objectId: string;
396
- fieldPath: (string | number)[];
397
- isUniqueValidation?: boolean;
398
- };
399
-
400
- export type ModelMap = Record<string, Model>;
401
-
402
- export interface ContentSourceInterface<UserContext = unknown, DocumentContext = unknown, AssetContext = unknown> {
403
- /**
404
- * This function should return the type of the content source.
405
- * The type must be unique among other content sources within the same project.
406
- */
407
- getContentSourceType(): string;
408
-
409
- /**
410
- * This function should return the project ID of the content source.
411
- * The ID must be unique among other content sources of the same type.
412
- */
413
- getProjectId(): string;
414
-
415
- getProjectEnvironment(): string;
416
-
417
- getProjectManageUrl(): string;
418
-
419
- /**
420
- * This function should initialize the content source by fetching the schema
421
- * and everything else needed to produce the StackbitSchema
422
- */
423
- init(options: InitOptions): Promise<void>;
424
-
425
- reset(): Promise<void>;
426
-
427
- onFilesChange?({
428
- updatedFiles
429
- }: {
430
- updatedFiles: string[];
431
- }): { schemaChanged?: boolean; contentChangeEvent?: ContentChangeEvent<DocumentContext, AssetContext> };
432
-
433
- startWatchingContentUpdates(options: {
434
- getModelMap: () => ModelMap;
435
- getDocument: ({ documentId }: { documentId: string }) => Document<DocumentContext> | undefined;
436
- getAsset: ({ assetId }: { assetId: string }) => Asset<AssetContext> | undefined;
437
- onContentChange: (contentChangeEvent: ContentChangeEvent<DocumentContext, AssetContext>) => void;
438
- onSchemaChange: () => void;
439
- }): void;
440
-
441
- stopWatchingContentUpdates(): void;
442
-
443
- /**
444
- * This function is responsible to fetch and convert content models
445
- * to Stackbit models.
446
- */
447
- getModels(): Promise<Model[]>;
448
-
449
- getLocales(): Promise<Locale[]>;
450
-
451
- /**
452
- * This function should fetch the documents and convert them into Stackbit's
453
- * Document type
454
- */
455
- getDocuments(options: { modelMap: ModelMap }): Promise<Document<DocumentContext>[]>;
456
-
457
- getAssets(): Promise<Asset<AssetContext>[]>;
458
-
459
- hasAccess(options: { userContext?: UserContext }): Promise<boolean>;
460
-
461
- createDocument(options: {
462
- updateOperationFields: Record<string, UpdateOperationField>;
463
- model: Model;
464
- modelMap: ModelMap;
465
- locale?: string;
466
- userContext?: UserContext;
467
- }): Promise<Document<DocumentContext>>;
468
-
469
- updateDocument(options: {
470
- document: Document<DocumentContext>;
471
- operations: UpdateOperation[];
472
- modelMap: ModelMap;
473
- userContext?: UserContext;
474
- }): Promise<Document<DocumentContext>>;
475
-
476
- deleteDocument(options: { document: Document<DocumentContext>; userContext?: UserContext }): Promise<void>;
477
-
478
- uploadAsset(options: {
479
- url?: string;
480
- base64?: string;
481
- fileName: string;
482
- mimeType: string;
483
- locale?: string;
484
- userContext?: UserContext;
485
- }): Promise<Asset<AssetContext>>;
486
-
487
- validateDocuments(options: {
488
- documents: Document<DocumentContext>[];
489
- assets: Asset<AssetContext>[];
490
- locale?: string;
491
- userContext?: UserContext;
492
- }): Promise<{ errors: ValidationError[] }>;
493
-
494
- publishDocuments(options: { documents: Document<DocumentContext>[]; assets: Asset<AssetContext>[]; userContext?: UserContext }): Promise<void>;
495
- }