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

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,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
+ }