@stackbit/cms-core 0.1.5 → 0.1.7

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.
@@ -1,8 +1,8 @@
1
1
  import _ from 'lodash';
2
- import { loadConfig, isListDataModel, Config } from '@stackbit/sdk';
2
+ import { Config, isListDataModel, loadConfigWithModelsPresetsAndValidate } from '@stackbit/sdk';
3
3
 
4
4
  export function fetchAndConvertSchema(options: { dirPath: string }): Promise<{ schema: Config; errors: Error[] }> {
5
- return loadConfig({ dirPath: options.dirPath }).then(({ config, errors }) => {
5
+ return loadConfigWithModelsPresetsAndValidate({ dirPath: options.dirPath }).then(({ config, errors }) => {
6
6
  if (!config) {
7
7
  return { schema: {} as Config, errors };
8
8
  }
@@ -1,5 +1,5 @@
1
1
  import _ from 'lodash';
2
- import { Field, FieldListProps, FieldModelProps, FieldObjectProps, FieldSpecificProps, Model } from '@stackbit/sdk';
2
+ import { Field, FieldListProps, FieldModelProps, FieldObjectProps, FieldSpecificProps, Model, ImageModel } from '@stackbit/sdk';
3
3
  import { isLocalizedField } from '@stackbit/types';
4
4
  import * as CSITypes from '@stackbit/types';
5
5
 
@@ -341,7 +341,7 @@ function getMetadataFromContentStore({
341
341
 
342
342
  function getObjectLabel(
343
343
  documentFields: Record<string, CSITypes.DocumentField | CSITypes.AssetFileField>,
344
- modelOrObjectField: Model | FieldObjectProps,
344
+ modelOrObjectField: Model | FieldObjectProps | ImageModel,
345
345
  locale?: string
346
346
  ): string {
347
347
  const labelField = modelOrObjectField.labelField;
@@ -0,0 +1,72 @@
1
+ import _ from 'lodash';
2
+
3
+ import { Logger } from '@stackbit/types';
4
+ import { Model, assignLabelFieldIfNeeded, isObjectField, isPageModel, mapModelFieldsRecursively, mapListItemsPropsOrSelfSpecificProps } from '@stackbit/sdk';
5
+ import { validateConfig } from '@stackbit/sdk';
6
+
7
+ export function normalizeModels<T extends Model>({ models, logger }: { models: T[]; logger: Logger }): T[] {
8
+ return models.map((model) => {
9
+ model = { ...model };
10
+
11
+ if (!('name' in model)) {
12
+ logger.warn('model does not have a name');
13
+ }
14
+
15
+ if (!('type' in model)) {
16
+ logger.warn(`model '${model['name']}' does not have a type, using 'object'`);
17
+ _.set(model, 'type', 'object');
18
+ }
19
+
20
+ // add model label if not set
21
+ if (!('label' in model)) {
22
+ model.label = _.startCase(model.name);
23
+ }
24
+
25
+ if (!('fields' in model) || !Array.isArray(model.fields)) {
26
+ model.fields = [];
27
+ }
28
+
29
+ if (isPageModel(model)) {
30
+ // set default urlPath if not set
31
+ if (!model.urlPath) {
32
+ model.urlPath = '/{slug}';
33
+ }
34
+ }
35
+
36
+ assignLabelFieldIfNeeded(model);
37
+
38
+ model = mapModelFieldsRecursively(model, (field) => {
39
+ field = { ...field };
40
+
41
+ if (!('label' in field)) {
42
+ field.label = _.startCase(field.name);
43
+ }
44
+
45
+ mapListItemsPropsOrSelfSpecificProps(field, (listItemsPropsOrField) => {
46
+ if (isObjectField(listItemsPropsOrField)) {
47
+ assignLabelFieldIfNeeded(listItemsPropsOrField);
48
+ }
49
+ return listItemsPropsOrField;
50
+ });
51
+
52
+ return field;
53
+ }) as T;
54
+
55
+ return model;
56
+ });
57
+ }
58
+
59
+ export function validateModels<T extends Model>({ models, logger }: { models: T[], logger: Logger }): T[] {
60
+ const { config, errors } = validateConfig({
61
+ stackbitVersion: '0.5.0',
62
+ models: models,
63
+ dirPath: '',
64
+ filePath: ''
65
+ });
66
+
67
+ for (const error of errors) {
68
+ logger.warn(error.message);
69
+ }
70
+
71
+ return config.models as T[];
72
+ }
@@ -1,5 +1,5 @@
1
1
  import _ from 'lodash';
2
- import { Model } from '@stackbit/sdk';
2
+ import { ImageModel, Model } from '@stackbit/sdk';
3
3
  import * as ContentSourceInterface from '@stackbit/types';
4
4
  import { getLocalizedFieldForLocale } from '@stackbit/types';
5
5
 
@@ -15,7 +15,7 @@ const META_FIELD = {
15
15
  }
16
16
  } as const;
17
17
 
18
- type Schema = Record<string, Record<string, Record<string, Model>>>;
18
+ type Schema = Record<string, Record<string, Record<string, Model | ImageModel>>>;
19
19
 
20
20
  export const searchDocuments = (data: {
21
21
  query?: string;
@@ -156,7 +156,7 @@ const isFieldMatchesFilter = ({
156
156
 
157
157
  case 'list': {
158
158
  const model = schema?.[document.srcType]?.[document.srcProjectId]?.[document.srcModelName];
159
- if (!model) {
159
+ if (!model || model.type === 'image') {
160
160
  throw new Error(`Can't find model for the ${filter.field}`);
161
161
  }
162
162
  return isListFieldMatches({ field, filter, model, locale });