@stackbit/cms-core 0.1.13-presets.0 → 0.1.14-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/dist/content-store-types.d.ts +27 -3
  2. package/dist/content-store-types.d.ts.map +1 -1
  3. package/dist/content-store-utils.d.ts +12 -1
  4. package/dist/content-store-utils.d.ts.map +1 -1
  5. package/dist/content-store-utils.js +44 -1
  6. package/dist/content-store-utils.js.map +1 -1
  7. package/dist/content-store.d.ts +6 -16
  8. package/dist/content-store.d.ts.map +1 -1
  9. package/dist/content-store.js +71 -126
  10. package/dist/content-store.js.map +1 -1
  11. package/dist/utils/csi-to-store-docs-converter.d.ts.map +1 -1
  12. package/dist/utils/csi-to-store-docs-converter.js +13 -6
  13. package/dist/utils/csi-to-store-docs-converter.js.map +1 -1
  14. package/dist/utils/store-to-api-docs-converter.js +1 -1
  15. package/dist/utils/store-to-api-docs-converter.js.map +1 -1
  16. package/dist/utils/store-to-csi-docs-converter.d.ts +4 -0
  17. package/dist/utils/store-to-csi-docs-converter.d.ts.map +1 -0
  18. package/dist/utils/store-to-csi-docs-converter.js +211 -0
  19. package/dist/utils/store-to-csi-docs-converter.js.map +1 -0
  20. package/package.json +5 -5
  21. package/src/content-store-types.ts +41 -4
  22. package/src/content-store-utils.ts +61 -3
  23. package/src/content-store.ts +99 -183
  24. package/src/utils/csi-to-store-docs-converter.ts +21 -12
  25. package/src/utils/store-to-api-docs-converter.ts +3 -3
  26. package/src/utils/store-to-csi-docs-converter.ts +222 -0
  27. package/dist/utils/preset-utils.d.ts +0 -4
  28. package/dist/utils/preset-utils.d.ts.map +0 -1
  29. package/dist/utils/preset-utils.js +0 -37
  30. package/dist/utils/preset-utils.js.map +0 -1
  31. package/src/utils/preset-utils.ts +0 -33
@@ -0,0 +1,222 @@
1
+ import _ from 'lodash';
2
+ import * as CSITypes from '@stackbit/types';
3
+ import * as ContentStoreTypes from '../content-store-types';
4
+
5
+ export function mapStoreDocumentsToCSIDocumentsWithSource(documents: ContentStoreTypes.Document[]): CSITypes.DocumentWithSource[] {
6
+ return documents.map((document) => mapStoreDocumentToCSIDocumentWithSource(document));
7
+ }
8
+
9
+ function mapStoreDocumentToCSIDocumentWithSource(document: ContentStoreTypes.Document): CSITypes.DocumentWithSource {
10
+ return {
11
+ type: document.type,
12
+ id: document.srcObjectId,
13
+ srcType: document.srcType,
14
+ srcProjectId: document.srcProjectId,
15
+ manageUrl: document.srcObjectUrl,
16
+ modelName: document.srcModelName,
17
+ status: document.status,
18
+ createdAt: document.createdAt,
19
+ createdBy: document.createdBy,
20
+ updatedAt: document.updatedAt,
21
+ updatedBy: document.updatedBy,
22
+ locale: document.locale,
23
+ context: null,
24
+ fields: mapStoreFieldsToCSIFields(document.fields)
25
+ };
26
+ }
27
+
28
+ function mapStoreFieldsToCSIFields(documentFields: Record<string, ContentStoreTypes.DocumentField>): Record<string, CSITypes.DocumentField> {
29
+ return _.reduce(
30
+ documentFields,
31
+ (accum: Record<string, CSITypes.DocumentField>, documentField, fieldName) => {
32
+ const csiField = mapStoreFieldToCSIField(documentField);
33
+ if (csiField) {
34
+ accum[fieldName] = csiField;
35
+ }
36
+ return accum;
37
+ },
38
+ {}
39
+ );
40
+ }
41
+
42
+ function mapStoreFieldToCSIField<Type extends CSITypes.FieldType>(
43
+ documentField: ContentStoreTypes.DocumentFieldLocalizedForType<Type>
44
+ ): CSITypes.DocumentFieldLocalizedForType<Type> | undefined;
45
+ function mapStoreFieldToCSIField<Type extends CSITypes.FieldType>(
46
+ documentField: ContentStoreTypes.DocumentFieldNonLocalizedForType<Type>
47
+ ): CSITypes.DocumentFieldNonLocalizedForType<Type> | undefined;
48
+ function mapStoreFieldToCSIField<Type extends CSITypes.FieldType>(
49
+ documentField: ContentStoreTypes.DocumentFieldForType<Type>
50
+ ): CSITypes.DocumentFieldForType<Type> | undefined;
51
+ function mapStoreFieldToCSIField(documentField: ContentStoreTypes.DocumentField): CSITypes.DocumentField | undefined {
52
+ switch (documentField.type) {
53
+ case 'string':
54
+ case 'text':
55
+ case 'html':
56
+ case 'slug':
57
+ case 'url':
58
+ case 'color':
59
+ case 'boolean':
60
+ case 'number':
61
+ case 'date':
62
+ case 'datetime':
63
+ case 'enum':
64
+ case 'file':
65
+ case 'json':
66
+ case 'style':
67
+ case 'markdown':
68
+ case 'richText': {
69
+ if (documentField.localized) {
70
+ if (_.isEmpty(documentField.locales)) {
71
+ return;
72
+ }
73
+ return {
74
+ type: documentField.type,
75
+ localized: true,
76
+ locales: _.mapValues(documentField.locales, (locale) => ({
77
+ locale: locale.locale,
78
+ value: locale.value
79
+ }))
80
+ };
81
+ }
82
+ if (typeof documentField.value === 'undefined' || ('isUnset' in documentField && documentField.isUnset)) {
83
+ return;
84
+ }
85
+ return {
86
+ type: documentField.type,
87
+ value: documentField.value
88
+ } as CSITypes.DocumentField;
89
+ }
90
+ case 'image': {
91
+ if (documentField.localized) {
92
+ if (_.isEmpty(documentField.locales)) {
93
+ return;
94
+ }
95
+ return {
96
+ type: 'image',
97
+ localized: true,
98
+ locales: _.mapValues(documentField.locales, (locale) => ({
99
+ locale: locale.locale,
100
+ fields: mapStoreFieldsToCSIFields(locale.fields)
101
+ }))
102
+ } as CSITypes.DocumentImageFieldLocalized;
103
+ }
104
+ if (documentField.isUnset) {
105
+ return;
106
+ }
107
+ return {
108
+ type: 'image',
109
+ fields: mapStoreFieldsToCSIFields(documentField.fields)
110
+ } as CSITypes.DocumentImageFieldNonLocalized;
111
+ }
112
+ case 'object': {
113
+ if (documentField.localized) {
114
+ if (_.isEmpty(documentField.locales)) {
115
+ return;
116
+ }
117
+ return {
118
+ type: 'object',
119
+ localized: true,
120
+ locales: _.mapValues(documentField.locales, (locale) => ({
121
+ locale: locale.locale,
122
+ fields: mapStoreFieldsToCSIFields(locale.fields)
123
+ }))
124
+ };
125
+ }
126
+ if (documentField.isUnset) {
127
+ return;
128
+ }
129
+ return {
130
+ type: 'object',
131
+ fields: mapStoreFieldsToCSIFields(documentField.fields)
132
+ };
133
+ }
134
+ case 'model': {
135
+ if (documentField.localized) {
136
+ if (_.isEmpty(documentField.locales)) {
137
+ return;
138
+ }
139
+ return {
140
+ type: 'model',
141
+ localized: true,
142
+ locales: _.mapValues(documentField.locales, (locale) => ({
143
+ locale: locale.locale,
144
+ modelName: locale.srcModelName,
145
+ fields: mapStoreFieldsToCSIFields(locale.fields)
146
+ }))
147
+ };
148
+ }
149
+ if (documentField.isUnset) {
150
+ return;
151
+ }
152
+ return {
153
+ type: 'model',
154
+ modelName: documentField.srcModelName,
155
+ fields: mapStoreFieldsToCSIFields(documentField.fields)
156
+ };
157
+ }
158
+ case 'reference': {
159
+ if (documentField.localized) {
160
+ if (_.isEmpty(documentField.locales)) {
161
+ return;
162
+ }
163
+ return {
164
+ type: 'reference',
165
+ localized: true,
166
+ refType: documentField.refType,
167
+ locales: _.mapValues(documentField.locales, (locale) => ({
168
+ locale: locale.locale,
169
+ refId: locale.refId
170
+ }))
171
+ };
172
+ }
173
+ if (documentField.isUnset) {
174
+ return;
175
+ }
176
+ return {
177
+ type: 'reference',
178
+ refType: documentField.refType,
179
+ refId: documentField.refId
180
+ };
181
+ }
182
+ case 'list': {
183
+ if (documentField.localized) {
184
+ if (_.isEmpty(documentField.locales)) {
185
+ return;
186
+ }
187
+ return {
188
+ type: 'list',
189
+ localized: true,
190
+ locales: _.mapValues(documentField.locales, (locale) => ({
191
+ locale: locale.locale,
192
+ items: locale.items.reduce((accum: CSITypes.DocumentListFieldItems[], item) => {
193
+ const csiItem = mapStoreFieldToCSIField(item);
194
+ // shouldn't happen because all list item fields are non-localized by definition,
195
+ // but just in case and for the sake of typescript
196
+ if (!csiItem) {
197
+ return accum;
198
+ }
199
+ return accum.concat(csiItem);
200
+ }, [])
201
+ }))
202
+ };
203
+ }
204
+ return {
205
+ type: 'list',
206
+ items: documentField.items.reduce((accum: CSITypes.DocumentListFieldItems[], item) => {
207
+ const csiItem = mapStoreFieldToCSIField(item);
208
+ // shouldn't happen because all list item fields are non-localized by definition,
209
+ // but just in case and for the sake of typescript
210
+ if (!csiItem) {
211
+ return accum;
212
+ }
213
+ return accum.concat(csiItem);
214
+ }, [])
215
+ };
216
+ }
217
+ default: {
218
+ const _exhaustiveCheck: never = documentField;
219
+ return _exhaustiveCheck;
220
+ }
221
+ }
222
+ }
@@ -1,4 +0,0 @@
1
- import { Preset } from '@stackbit/sdk';
2
- import * as CSITypes from '@stackbit/types';
3
- export declare function getPresetFromDocument(document: CSITypes.Document, assetMap: Record<string, CSITypes.Asset>): Preset | null;
4
- //# sourceMappingURL=preset-utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"preset-utils.d.ts","sourceRoot":"","sources":["../../src/utils/preset-utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AACvC,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAC;AAG5C,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,QAAQ,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,GAAG,MAAM,GAAG,IAAI,CA2B1H"}
@@ -1,37 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPresetFromDocument = void 0;
4
- const types_1 = require("@stackbit/types");
5
- function getPresetFromDocument(document, assetMap) {
6
- var _a, _b, _c, _d, _e, _f, _g;
7
- const dataField = ((_a = document.fields['data']) === null || _a === void 0 ? void 0 : _a.type) === 'json' ? (0, types_1.getLocalizedFieldForLocale)(document.fields['data']) : null;
8
- if (!dataField) {
9
- return null;
10
- }
11
- let thumbnail;
12
- if (((_b = document.fields['thumbnail']) === null || _b === void 0 ? void 0 : _b.type) === 'image') {
13
- thumbnail = (_c = (0, types_1.getLocalizedFieldForLocale)(document.fields['thumbnail'])) === null || _c === void 0 ? void 0 : _c.fields.url;
14
- }
15
- else if (((_d = document.fields['thumbnail']) === null || _d === void 0 ? void 0 : _d.type) === 'reference' && document.fields['thumbnail'].refType === 'asset') {
16
- const refId = (_e = (0, types_1.getLocalizedFieldForLocale)(document.fields['thumbnail'])) === null || _e === void 0 ? void 0 : _e.refId;
17
- if (refId) {
18
- //TODO
19
- const fileField = (_f = assetMap[refId]) === null || _f === void 0 ? void 0 : _f.fields.file;
20
- if (fileField === null || fileField === void 0 ? void 0 : fileField.localized) {
21
- const vals = Object.values(fileField.locales);
22
- if (vals === null || vals === void 0 ? void 0 : vals.length) {
23
- thumbnail = (_g = vals[0]) === null || _g === void 0 ? void 0 : _g.url;
24
- }
25
- }
26
- else {
27
- thumbnail = fileField === null || fileField === void 0 ? void 0 : fileField.url;
28
- }
29
- }
30
- }
31
- return {
32
- ...dataField === null || dataField === void 0 ? void 0 : dataField.value,
33
- thumbnail
34
- };
35
- }
36
- exports.getPresetFromDocument = getPresetFromDocument;
37
- //# sourceMappingURL=preset-utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"preset-utils.js","sourceRoot":"","sources":["../../src/utils/preset-utils.ts"],"names":[],"mappings":";;;AAGA,2CAA6D;AAE7D,SAAgB,qBAAqB,CAAC,QAA2B,EAAE,QAAwC;;IACvG,MAAM,SAAS,GAAG,CAAA,MAAA,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,0CAAE,IAAI,MAAK,MAAM,CAAC,CAAC,CAAC,IAAA,kCAA0B,EAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACxH,IAAI,CAAC,SAAS,EAAE;QACZ,OAAO,IAAI,CAAC;KACf;IACD,IAAI,SAAc,CAAC;IACnB,IAAI,CAAA,MAAA,QAAQ,CAAC,MAAM,CAAC,WAAW,CAAC,0CAAE,IAAI,MAAK,OAAO,EAAE;QAChD,SAAS,GAAG,MAAA,IAAA,kCAA0B,EAAC,QAAQ,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,0CAAE,MAAM,CAAC,GAAG,CAAC;KACpF;SAAM,IAAI,CAAA,MAAA,QAAQ,CAAC,MAAM,CAAC,WAAW,CAAC,0CAAE,IAAI,MAAK,WAAW,IAAI,QAAQ,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,OAAO,KAAK,OAAO,EAAE;QAC/G,MAAM,KAAK,GAAG,MAAA,IAAA,kCAA0B,EAAC,QAAQ,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,0CAAE,KAAK,CAAC;QAC9E,IAAI,KAAK,EAAE;YACP,MAAM;YACN,MAAM,SAAS,GAAG,MAAA,QAAQ,CAAC,KAAK,CAAC,0CAAE,MAAM,CAAC,IAAI,CAAC;YAC/C,IAAI,SAAS,aAAT,SAAS,uBAAT,SAAS,CAAE,SAAS,EAAE;gBACtB,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;gBAC9C,IAAI,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,MAAM,EAAE;oBACd,SAAS,GAAG,MAAA,IAAI,CAAC,CAAC,CAAC,0CAAE,GAAG,CAAC;iBAC5B;aACJ;iBAAM;gBACH,SAAS,GAAG,SAAS,aAAT,SAAS,uBAAT,SAAS,CAAE,GAAG,CAAC;aAC9B;SACJ;KACJ;IACD,OAAO;QACH,GAAG,SAAS,aAAT,SAAS,uBAAT,SAAS,CAAE,KAAK;QACnB,SAAS;KACZ,CAAC;AACN,CAAC;AA3BD,sDA2BC"}
@@ -1,33 +0,0 @@
1
- import _ from 'lodash';
2
- import { Preset } from '@stackbit/sdk';
3
- import * as CSITypes from '@stackbit/types';
4
- import { getLocalizedFieldForLocale } from '@stackbit/types';
5
-
6
- export function getPresetFromDocument(document: CSITypes.Document, assetMap: Record<string, CSITypes.Asset>): Preset | null {
7
- const dataField = document.fields['data']?.type === 'json' ? getLocalizedFieldForLocale(document.fields['data']) : null;
8
- if (!dataField) {
9
- return null;
10
- }
11
- let thumbnail: any;
12
- if (document.fields['thumbnail']?.type === 'image') {
13
- thumbnail = getLocalizedFieldForLocale(document.fields['thumbnail'])?.fields.url;
14
- } else if (document.fields['thumbnail']?.type === 'reference' && document.fields['thumbnail'].refType === 'asset') {
15
- const refId = getLocalizedFieldForLocale(document.fields['thumbnail'])?.refId;
16
- if (refId) {
17
- //TODO
18
- const fileField = assetMap[refId]?.fields.file;
19
- if (fileField?.localized) {
20
- const vals = Object.values(fileField.locales);
21
- if (vals?.length) {
22
- thumbnail = vals[0]?.url;
23
- }
24
- } else {
25
- thumbnail = fileField?.url;
26
- }
27
- }
28
- }
29
- return {
30
- ...dataField?.value,
31
- thumbnail
32
- };
33
- }