@stackbit/sdk
Advanced tools
Comparing version 0.2.35 to 0.2.36-alpha.0
@@ -35,1 +35,2 @@ /** | ||
export declare function isModelListItemsWithUnknown(items: FieldListItemsWithUnknown): items is FieldModelProps; | ||
//# sourceMappingURL=analyze-schema-types.d.ts.map |
@@ -15,1 +15,2 @@ import { FileBrowser } from './file-browser'; | ||
export {}; | ||
//# sourceMappingURL=analyzer-utils.d.ts.map |
@@ -25,1 +25,2 @@ import { GetFileBrowserOptions } from './file-browser'; | ||
export {}; | ||
//# sourceMappingURL=cms-matcher.d.ts.map |
@@ -91,1 +91,2 @@ export declare const EXCLUDED_LIST_FILES: string[]; | ||
export {}; | ||
//# sourceMappingURL=file-browser.d.ts.map |
@@ -13,1 +13,2 @@ import { GetFileBrowserOptions } from './file-browser'; | ||
export declare function generateSchema({ ssgMatchResult, ...fileBrowserOptions }: SchemaGeneratorOptions): Promise<SchemaGeneratorResult>; | ||
//# sourceMappingURL=schema-generator.d.ts.map |
@@ -12,1 +12,2 @@ import { GetFileBrowserOptions } from './file-browser'; | ||
export declare function analyzeSite(options: SiteAnalyzerOptions): Promise<SiteAnalyzerResult>; | ||
//# sourceMappingURL=site-analyzer.d.ts.map |
@@ -24,1 +24,2 @@ import { GetFileBrowserOptions } from './file-browser'; | ||
export {}; | ||
//# sourceMappingURL=ssg-matcher.d.ts.map |
@@ -27,1 +27,2 @@ export declare const SSG_NAMES: readonly ["unibit", "jekyll", "hugo", "gatsby", "nextjs", "custom", "eleventy", "vuepress", "gridsome", "nuxt", "sapper", "hexo"]; | ||
}; | ||
//# sourceMappingURL=config-consts.d.ts.map |
@@ -27,1 +27,2 @@ export declare class ConfigLoadError extends Error { | ||
export declare type ConfigError = ConfigLoadError | ConfigValidationError | ConfigPresetsError; | ||
//# sourceMappingURL=config-errors.d.ts.map |
@@ -20,1 +20,2 @@ import { YamlConfig } from './config-types'; | ||
}): Promise<LoadStackbitConfigResult & StopConfigWatch>; | ||
//# sourceMappingURL=config-loader-esbuild.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import { ConfigError, ConfigLoadError, ConfigValidationError } from './config-errors'; | ||
import { ConfigError, ConfigLoadError, ConfigPresetsError, ConfigValidationError } from './config-errors'; | ||
import { StopConfigWatch } from './config-loader-esbuild'; | ||
@@ -25,2 +25,17 @@ import { Config, Model, ModelsSource } from './config-types'; | ||
export declare function loadConfig({ dirPath, modelsSource, stackbitConfigESBuildOutDir, watchCallback }: ConfigLoaderOptions): Promise<ConfigLoaderResult & StopConfigWatch>; | ||
export declare function extendConfig({ dirPath, config, externalModels }: { | ||
dirPath: string; | ||
config: Record<string, any>; | ||
externalModels?: Model[]; | ||
}): Promise<{ | ||
valid: boolean; | ||
config: Config; | ||
errors: (ConfigValidationError | ConfigPresetsError)[]; | ||
}>; | ||
export declare function validateAndNormalizeConfig(config: Record<string, any>, externalModels?: Model[]): NormalizedValidationResult; | ||
export declare function loadConfigFromDir({ dirPath, stackbitConfigESBuildOutDir, watchCallback }: { | ||
dirPath: string; | ||
stackbitConfigESBuildOutDir?: string; | ||
watchCallback?: (rawConfigResult: RawConfigLoaderResult) => void; | ||
}): Promise<RawConfigLoaderResult & StopConfigWatch>; | ||
//# sourceMappingURL=config-loader.d.ts.map |
@@ -25,3 +25,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateAndNormalizeConfig = exports.loadConfig = void 0; | ||
exports.loadConfigFromDir = exports.validateAndNormalizeConfig = exports.extendConfig = exports.loadConfig = void 0; | ||
const path_1 = __importDefault(require("path")); | ||
@@ -39,3 +39,3 @@ const fs_extra_1 = __importDefault(require("fs-extra")); | ||
async function loadConfig({ dirPath, modelsSource, stackbitConfigESBuildOutDir, watchCallback }) { | ||
const rawConfigResult = await loadStackbitConfigFromDir({ | ||
const rawConfigResult = await loadConfigFromDir({ | ||
dirPath, | ||
@@ -64,2 +64,10 @@ stackbitConfigESBuildOutDir, | ||
const { models: externalModels, errors: externalModelsLoadErrors } = await loadModelsFromExternalSource(config, dirPath, modelsSource); | ||
const extendedConfig = await extendConfig({ dirPath, config, externalModels }); | ||
return { | ||
valid: extendedConfig.valid, | ||
config: extendedConfig.config, | ||
errors: [...configLoadErrors, ...externalModelsLoadErrors, ...extendedConfig.errors] | ||
}; | ||
} | ||
async function extendConfig({ dirPath, config, externalModels }) { | ||
const normalizedResult = validateAndNormalizeConfig(config, externalModels); | ||
@@ -70,5 +78,6 @@ const presetsResult = await presets_loader_1.loadPresets(dirPath, normalizedResult.config); | ||
config: presetsResult.config, | ||
errors: [...configLoadErrors, ...externalModelsLoadErrors, ...normalizedResult.errors, ...presetsResult.errors] | ||
errors: [...normalizedResult.errors, ...presetsResult.errors] | ||
}; | ||
} | ||
exports.extendConfig = extendConfig; | ||
function validateAndNormalizeConfig(config, externalModels) { | ||
@@ -100,3 +109,3 @@ // extend config models having the "extends" property | ||
exports.validateAndNormalizeConfig = validateAndNormalizeConfig; | ||
async function loadStackbitConfigFromDir({ dirPath, stackbitConfigESBuildOutDir, watchCallback }) { | ||
async function loadConfigFromDir({ dirPath, stackbitConfigESBuildOutDir, watchCallback }) { | ||
var _a; | ||
@@ -169,2 +178,3 @@ // try to load stackbit config from YAML files | ||
} | ||
exports.loadConfigFromDir = loadConfigFromDir; | ||
async function loadConfigFromStackbitYaml(stackbitYamlPath) { | ||
@@ -171,0 +181,0 @@ const stackbitYaml = await fs_extra_1.default.readFile(stackbitYamlPath); |
@@ -5,1 +5,2 @@ import Joi from 'joi'; | ||
export declare const stackbitConfigSchema: Joi.ObjectSchema<YamlConfig>; | ||
//# sourceMappingURL=config-schema.d.ts.map |
import Joi from 'joi'; | ||
export declare const styleFieldPartialSchema: Joi.ObjectSchema<any>; | ||
//# sourceMappingURL=style-field-schema.d.ts.map |
@@ -5,3 +5,3 @@ import { StricterUnion } from '../utils'; | ||
models: Model[]; | ||
presets?: Record<string, any>; | ||
presets?: Record<string, Preset>; | ||
} | ||
@@ -92,2 +92,8 @@ export interface YamlConfig extends BaseConfig { | ||
} | ||
export interface Preset { | ||
label: string; | ||
modelName: string; | ||
thumbnail?: string; | ||
data: Record<string, any>; | ||
} | ||
/******************* | ||
@@ -174,2 +180,3 @@ *** Model Types *** | ||
export declare type Field = FieldSimple | FieldEnum | FieldImage | FieldNumber | FieldObject | FieldModel | FieldReference | FieldStyle | FieldList; | ||
export declare type FieldSpecificProps = FieldSimpleProps | FieldEnumProps | FieldImageProps | FieldNumberProps | FieldObjectProps | FieldModelProps | FieldReferenceProps | FieldStyleProps | FieldListProps; | ||
export declare type FieldSimple = FieldCommonProps & FieldSimpleProps; | ||
@@ -204,2 +211,3 @@ export declare type FieldEnum = FieldCommonProps & FieldEnumProps; | ||
readOnly?: boolean; | ||
localized?: boolean; | ||
} | ||
@@ -282,1 +290,2 @@ export declare type FieldType = typeof FIELD_TYPES[number]; | ||
export {}; | ||
//# sourceMappingURL=config-types.d.ts.map |
@@ -9,1 +9,2 @@ import { ConfigValidationError } from './config-errors'; | ||
export declare function validateContentModels(contentModels: any, models: any): ConfigValidationResult; | ||
//# sourceMappingURL=config-validator.d.ts.map |
@@ -10,1 +10,2 @@ import { Config, YamlConfig } from './config-types'; | ||
}): YamlConfig; | ||
//# sourceMappingURL=config-writer.d.ts.map |
@@ -8,1 +8,2 @@ import { Config } from './config-types'; | ||
export declare function loadPresets(dirPath: string, config: Config): Promise<PresetsLoaderResult>; | ||
//# sourceMappingURL=presets-loader.d.ts.map |
@@ -41,7 +41,8 @@ "use strict"; | ||
const presetId = `${presetFile}:presets[${i}]`; | ||
presets[presetId] = preset; | ||
if (preset.thumbnail) { | ||
preset.thumbnail = resolveThumbnailPath(preset.thumbnail, presetsRelDir); | ||
} | ||
lodash_1.default.set(preset, 'modelName', presetData.model); | ||
const { thumbnail, ...rest } = preset; | ||
presets[presetId] = { | ||
...rest, | ||
...(thumbnail ? { thumbnail: resolveThumbnailPath(thumbnail, presetsRelDir) } : null), | ||
modelName: presetData.model | ||
}; | ||
utils_1.append(presetsIdsByModel, presetData.model, presetId); | ||
@@ -48,0 +49,0 @@ }); |
@@ -7,1 +7,2 @@ export declare const MARKDOWN_FILE_EXTENSIONS: string[]; | ||
export declare const EXCLUDED_COMMON_FILES: string[]; | ||
//# sourceMappingURL=consts.d.ts.map |
@@ -76,1 +76,2 @@ declare type FieldPath = (string | number)[]; | ||
export {}; | ||
//# sourceMappingURL=content-errors.d.ts.map |
@@ -28,1 +28,2 @@ import { Config } from '../config/config-types'; | ||
export {}; | ||
//# sourceMappingURL=content-loader.d.ts.map |
@@ -7,1 +7,2 @@ import Joi from 'joi'; | ||
export {}; | ||
//# sourceMappingURL=content-schema.d.ts.map |
@@ -15,1 +15,2 @@ import { ContentItem } from './content-loader'; | ||
export {}; | ||
//# sourceMappingURL=content-validator.d.ts.map |
@@ -8,3 +8,3 @@ export * from './config/config-schema'; | ||
export * from './utils'; | ||
export { loadConfig, ConfigLoaderOptions, ConfigLoaderResult } from './config/config-loader'; | ||
export { loadConfig, loadConfigFromDir, extendConfig, ConfigLoaderOptions, ConfigLoaderResult } from './config/config-loader'; | ||
export { writeConfig, WriteConfigOptions, convertToYamlConfig } from './config/config-writer'; | ||
@@ -15,1 +15,2 @@ export { loadContent, ContentItem, ContentLoaderOptions, ContentLoaderResult } from './content/content-loader'; | ||
export { analyzeSite, SiteAnalyzerOptions, SiteAnalyzerResult } from './analyzer/site-analyzer'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -13,3 +13,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.analyzeSite = exports.matchCMS = exports.matchSSG = exports.loadContent = exports.convertToYamlConfig = exports.writeConfig = exports.loadConfig = void 0; | ||
exports.analyzeSite = exports.matchCMS = exports.matchSSG = exports.loadContent = exports.convertToYamlConfig = exports.writeConfig = exports.extendConfig = exports.loadConfigFromDir = exports.loadConfig = void 0; | ||
__exportStar(require("./config/config-schema"), exports); | ||
@@ -24,2 +24,4 @@ __exportStar(require("./config/config-types"), exports); | ||
Object.defineProperty(exports, "loadConfig", { enumerable: true, get: function () { return config_loader_1.loadConfig; } }); | ||
Object.defineProperty(exports, "loadConfigFromDir", { enumerable: true, get: function () { return config_loader_1.loadConfigFromDir; } }); | ||
Object.defineProperty(exports, "extendConfig", { enumerable: true, get: function () { return config_loader_1.extendConfig; } }); | ||
var config_writer_1 = require("./config/config-writer"); | ||
@@ -26,0 +28,0 @@ Object.defineProperty(exports, "writeConfig", { enumerable: true, get: function () { return config_writer_1.writeConfig; } }); |
@@ -8,3 +8,3 @@ /// <reference types="node" /> | ||
export declare type StricterUnion<Union, Union2 = Union> = Union2 extends any ? StricterUnionMember<Union2, Union> : never; | ||
export declare type Logger = Pick<Console, 'log' | 'info' | 'warn' | 'error' | 'group' | 'groupEnd'>; | ||
export declare type Logger = Pick<Console, 'log' | 'debug' | 'info' | 'warn' | 'error' | 'group' | 'groupEnd'>; | ||
export * from './model-utils'; | ||
@@ -14,1 +14,2 @@ export * from './model-matcher'; | ||
export * from './model-iterators'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -11,1 +11,2 @@ import { Model, ModelMap } from '../config/config-types'; | ||
}; | ||
//# sourceMappingURL=model-extender.d.ts.map |
@@ -132,2 +132,21 @@ import { Field, FieldListItems, FieldModelProps, Model } from '../config/config-types'; | ||
}): any; | ||
export declare function asyncMapObjectFieldsWithModelRecursively({ value, model, modelsByName, iteratee, pageLayoutKey, objectTypeKey, valueId }: { | ||
value: any; | ||
model: Model; | ||
modelsByName: Record<string, Model>; | ||
iteratee: (options: { | ||
value: any; | ||
model: Model | null; | ||
field: Field | null; | ||
fieldListItem: FieldListItems | null; | ||
error: string | null; | ||
valueKeyPath: (string | number)[]; | ||
modelKeyPath: string[]; | ||
objectStack: any[]; | ||
skipNested: () => void; | ||
}) => Promise<any>; | ||
pageLayoutKey?: string; | ||
objectTypeKey?: string; | ||
valueId?: string; | ||
}): Promise<any>; | ||
export declare function getModelOfObject({ object, field, modelsByName, pageLayoutKey, objectTypeKey, valueKeyPath, modelKeyPath }: { | ||
@@ -147,1 +166,2 @@ object: any; | ||
}; | ||
//# sourceMappingURL=model-iterators.d.ts.map |
@@ -6,4 +6,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getModelOfObject = exports.mapObjectFieldsWithModelRecursively = exports.iterateObjectFieldsWithModelRecursively = exports.mapModelFieldsRecursively = exports.iterateModelFieldsRecursively = void 0; | ||
exports.getModelOfObject = exports.asyncMapObjectFieldsWithModelRecursively = exports.mapObjectFieldsWithModelRecursively = exports.iterateObjectFieldsWithModelRecursively = exports.mapModelFieldsRecursively = exports.iterateModelFieldsRecursively = void 0; | ||
const lodash_1 = __importDefault(require("lodash")); | ||
const utils_1 = require("@stackbit/utils"); | ||
const model_utils_1 = require("./model-utils"); | ||
@@ -310,2 +311,99 @@ /** | ||
exports.mapObjectFieldsWithModelRecursively = mapObjectFieldsWithModelRecursively; | ||
async function asyncMapObjectFieldsWithModelRecursively({ value, model, modelsByName, iteratee, pageLayoutKey = 'layout', objectTypeKey = 'type', valueId }) { | ||
async function _mapDeep({ value, model, field, fieldListItem, valueKeyPath, modelKeyPath, objectStack }) { | ||
let error = null; | ||
let modelField = null; | ||
if (!model && !field && !fieldListItem) { | ||
error = `could not match model/field ${modelKeyPath.join('.')} to content at ${valueKeyPath.join('.')}`; | ||
} | ||
if (field && model_utils_1.isModelField(field)) { | ||
modelField = field; | ||
} | ||
else if (fieldListItem && model_utils_1.isModelListItems(fieldListItem)) { | ||
modelField = fieldListItem; | ||
} | ||
if (modelField) { | ||
const modelResult = getModelOfObject({ | ||
object: value, | ||
field: modelField, | ||
modelsByName, | ||
pageLayoutKey, | ||
objectTypeKey, | ||
valueKeyPath, | ||
modelKeyPath | ||
}); | ||
if ('error' in modelResult) { | ||
error = modelResult.error; | ||
} | ||
else { | ||
model = modelResult.model; | ||
} | ||
field = null; | ||
fieldListItem = null; | ||
modelKeyPath = model ? [model.name] : []; | ||
} | ||
let shouldSkipNested = false; | ||
const skipNested = () => { | ||
shouldSkipNested = true; | ||
}; | ||
const res = await iteratee({ value, model, field, fieldListItem, error, valueKeyPath, modelKeyPath, objectStack, skipNested }); | ||
if (!lodash_1.default.isUndefined(res)) { | ||
value = res; | ||
} | ||
if (shouldSkipNested) { | ||
return value; | ||
} | ||
if (lodash_1.default.isPlainObject(value)) { | ||
// if fields will not be resolved or the object will have a key that | ||
// doesn't exist among fields, the nested calls to _iterateDeep will | ||
// include an error. | ||
const fields = getFieldsOfModelOrField(model, field, fieldListItem); | ||
const fieldsByName = lodash_1.default.keyBy(fields, 'name'); | ||
modelKeyPath = lodash_1.default.concat(modelKeyPath, 'fields'); | ||
value = await utils_1.mapValuesPromise(value, (val, key) => { | ||
const field = lodash_1.default.get(fieldsByName, key, null); | ||
return _mapDeep({ | ||
value: val, | ||
model: null, | ||
field: field, | ||
fieldListItem: null, | ||
valueKeyPath: lodash_1.default.concat(valueKeyPath, key), | ||
modelKeyPath: lodash_1.default.concat(modelKeyPath, key), | ||
objectStack: lodash_1.default.concat(objectStack, value) | ||
}); | ||
}); | ||
} | ||
else if (lodash_1.default.isArray(value)) { | ||
let fieldListItems = null; | ||
if (field && model_utils_1.isListField(field)) { | ||
fieldListItems = model_utils_1.getListFieldItems(field); | ||
} | ||
else if (model && model_utils_1.isListDataModel(model)) { | ||
fieldListItems = model.items; | ||
} | ||
value = await utils_1.mapPromise(value, (val, idx) => { | ||
return _mapDeep({ | ||
value: val, | ||
model: null, | ||
field: null, | ||
fieldListItem: fieldListItems, | ||
valueKeyPath: lodash_1.default.concat(valueKeyPath, idx), | ||
modelKeyPath: lodash_1.default.concat(modelKeyPath, 'items'), | ||
objectStack: lodash_1.default.concat(objectStack, value) | ||
}); | ||
}); | ||
} | ||
return value; | ||
} | ||
return _mapDeep({ | ||
value: value, | ||
model: model, | ||
field: null, | ||
fieldListItem: null, | ||
valueKeyPath: valueId ? [valueId] : [], | ||
modelKeyPath: [model.name], | ||
objectStack: [] | ||
}); | ||
} | ||
exports.asyncMapObjectFieldsWithModelRecursively = asyncMapObjectFieldsWithModelRecursively; | ||
function getModelOfObject({ object, field, modelsByName, pageLayoutKey, objectTypeKey, valueKeyPath, modelKeyPath }) { | ||
@@ -312,0 +410,0 @@ var _a; |
@@ -49,1 +49,2 @@ import { Model } from '../config/config-types'; | ||
export {}; | ||
//# sourceMappingURL=model-matcher.d.ts.map |
@@ -97,1 +97,2 @@ import { Model, ObjectModel, DataModel, PageModel, ConfigModel, Field, FieldEnum, FieldList, FieldListItems, FieldListModel, FieldListObject, FieldListReference, FieldModel, FieldModelProps, FieldObject, FieldObjectProps, FieldReference, FieldReferenceProps } from '../config/config-types'; | ||
}) | import("../config/config-types").FieldSimpleProps | import("../config/config-types").FieldEnumDropdownProps | import("../config/config-types").FieldEnumThumbnailsProps | import("../config/config-types").FieldEnumPaletteProps | import("../config/config-types").FieldImageProps | import("../config/config-types").FieldNumberProps | FieldObjectProps | FieldModelProps | FieldReferenceProps | import("../config/config-types").FieldStyle | FieldList | null; | ||
//# sourceMappingURL=model-utils.d.ts.map |
{ | ||
"name": "@stackbit/sdk", | ||
"version": "0.2.35", | ||
"version": "0.2.36-alpha.0", | ||
"description": "Stackbit SDK", | ||
@@ -40,3 +40,3 @@ "main": "dist/index.js", | ||
"@octokit/rest": "^18.3.5", | ||
"@stackbit/utils": "^0.2.3", | ||
"@stackbit/utils": "^0.2.4-alpha.0", | ||
"acorn": "^8.2.4", | ||
@@ -69,3 +69,3 @@ "esbuild": "^0.14.42", | ||
}, | ||
"gitHead": "ff31bc161f97408414a28557e7f1ed9a75afec5b" | ||
"gitHead": "4f0d10545fffc1b66b9774786949362ecfd06fbe" | ||
} |
@@ -8,3 +8,8 @@ import path from 'path'; | ||
import { ConfigValidationResult, validateConfig, validateContentModels } from './config-validator'; | ||
import { ConfigError, ConfigLoadError, ConfigValidationError } from './config-errors'; | ||
import { | ||
ConfigError, | ||
ConfigLoadError, | ||
ConfigPresetsError, | ||
ConfigValidationError | ||
} from './config-errors'; | ||
import { loadStackbitConfigFromJs, LoadStackbitConfigResult, StopConfigWatch } from './config-loader-esbuild'; | ||
@@ -64,3 +69,3 @@ import { | ||
}: ConfigLoaderOptions): Promise<ConfigLoaderResult & StopConfigWatch> { | ||
const rawConfigResult = await loadStackbitConfigFromDir({ | ||
const rawConfigResult = await loadConfigFromDir({ | ||
dirPath, | ||
@@ -101,10 +106,30 @@ stackbitConfigESBuildOutDir, | ||
const extendedConfig = await extendConfig({ dirPath, config, externalModels }); | ||
return { | ||
valid: extendedConfig.valid, | ||
config: extendedConfig.config, | ||
errors: [...configLoadErrors, ...externalModelsLoadErrors, ...extendedConfig.errors] | ||
}; | ||
} | ||
export async function extendConfig({ | ||
dirPath, | ||
config, | ||
externalModels | ||
}: { | ||
dirPath: string; | ||
config: Record<string, any>; | ||
externalModels?: Model[]; | ||
}): Promise<{ | ||
valid: boolean; | ||
config: Config; | ||
errors: (ConfigValidationError | ConfigPresetsError)[]; | ||
}> { | ||
const normalizedResult = validateAndNormalizeConfig(config, externalModels); | ||
const presetsResult = await loadPresets(dirPath, normalizedResult.config); | ||
return { | ||
valid: normalizedResult.valid, | ||
config: presetsResult.config, | ||
errors: [...configLoadErrors, ...externalModelsLoadErrors, ...normalizedResult.errors, ...presetsResult.errors] | ||
errors: [...normalizedResult.errors, ...presetsResult.errors] | ||
}; | ||
@@ -145,3 +170,3 @@ } | ||
async function loadStackbitConfigFromDir({ | ||
export async function loadConfigFromDir({ | ||
dirPath, | ||
@@ -148,0 +173,0 @@ stackbitConfigESBuildOutDir, |
@@ -6,3 +6,3 @@ import { StricterUnion } from '../utils'; | ||
models: Model[]; | ||
presets?: Record<string, any>; | ||
presets?: Record<string, Preset>; | ||
} | ||
@@ -109,2 +109,9 @@ | ||
export interface Preset { | ||
label: string; | ||
modelName: string; | ||
thumbnail?: string; | ||
data: Record<string, any>; | ||
} | ||
/******************* | ||
@@ -210,2 +217,13 @@ *** Model Types *** | ||
export type FieldSpecificProps = | ||
| FieldSimpleProps | ||
| FieldEnumProps | ||
| FieldImageProps | ||
| FieldNumberProps | ||
| FieldObjectProps | ||
| FieldModelProps | ||
| FieldReferenceProps | ||
| FieldStyleProps | ||
| FieldListProps; | ||
export type FieldSimple = FieldCommonProps & FieldSimpleProps; | ||
@@ -236,2 +254,3 @@ export type FieldEnum = FieldCommonProps & FieldEnumProps; | ||
readOnly?: boolean; | ||
localized?: boolean; | ||
} | ||
@@ -238,0 +257,0 @@ |
@@ -6,3 +6,3 @@ import _ from 'lodash'; | ||
import { Config } from './config-types'; | ||
import { Config, Preset } from './config-types'; | ||
import { ConfigPresetsError } from './config-errors'; | ||
@@ -15,2 +15,13 @@ | ||
interface RawPresetData { | ||
model: string; | ||
presets: RawPreset[]; | ||
} | ||
interface RawPreset { | ||
label: string; | ||
thumbnail?: string; | ||
data: Record<string, any>; | ||
} | ||
export async function loadPresets(dirPath: string, config: Config): Promise<PresetsLoaderResult> { | ||
@@ -31,4 +42,4 @@ const presetFiles = []; | ||
const presets: Record<string, any> = {}; | ||
const presetsIdsByModel: Record<string, any> = {}; | ||
const presets: Record<string, Preset> = {}; | ||
const presetsIdsByModel: Record<string, string[]> = {}; | ||
const errors: ConfigPresetsError[] = []; | ||
@@ -39,3 +50,3 @@ | ||
const presetPath = path.join(dirPath, presetFile); | ||
let presetData: any; | ||
let presetData: RawPresetData; | ||
try { | ||
@@ -49,7 +60,8 @@ presetData = await parseFile(presetPath); | ||
const presetId = `${presetFile}:presets[${i}]`; | ||
presets[presetId] = preset; | ||
if (preset.thumbnail) { | ||
preset.thumbnail = resolveThumbnailPath(preset.thumbnail, presetsRelDir); | ||
} | ||
_.set(preset, 'modelName', presetData.model); | ||
const { thumbnail, ...rest } = preset; | ||
presets[presetId] = { | ||
...rest, | ||
...(thumbnail ? { thumbnail: resolveThumbnailPath(thumbnail, presetsRelDir) } : null), | ||
modelName: presetData.model | ||
}; | ||
append(presetsIdsByModel, presetData.model, presetId); | ||
@@ -56,0 +68,0 @@ }); |
@@ -8,3 +8,3 @@ export * from './config/config-schema'; | ||
export * from './utils'; | ||
export { loadConfig, ConfigLoaderOptions, ConfigLoaderResult } from './config/config-loader'; | ||
export { loadConfig, loadConfigFromDir, extendConfig, ConfigLoaderOptions, ConfigLoaderResult } from './config/config-loader'; | ||
export { writeConfig, WriteConfigOptions, convertToYamlConfig } from './config/config-writer'; | ||
@@ -11,0 +11,0 @@ export { loadContent, ContentItem, ContentLoaderOptions, ContentLoaderResult } from './content/content-loader'; |
@@ -12,3 +12,3 @@ type UnionKeys<U> = U extends any ? keyof U : never; | ||
export type Logger = Pick<Console, 'log' | 'info' | 'warn' | 'error' | 'group' | 'groupEnd'>; | ||
export type Logger = Pick<Console, 'log' | 'debug' | 'info' | 'warn' | 'error' | 'group' | 'groupEnd'>; | ||
@@ -15,0 +15,0 @@ export * from './model-utils'; |
import _ from 'lodash'; | ||
import { mapPromise, mapValuesPromise } from '@stackbit/utils'; | ||
import { getListFieldItems, isListDataModel, isListField, isObjectListItems, isModelField, isObjectField, isModelListItems } from './model-utils'; | ||
@@ -369,2 +370,146 @@ import { DataModel, Field, FieldList, FieldListItems, FieldModelProps, FieldObjectProps, Model } from '../config/config-types'; | ||
export async function asyncMapObjectFieldsWithModelRecursively({ | ||
value, | ||
model, | ||
modelsByName, | ||
iteratee, | ||
pageLayoutKey = 'layout', | ||
objectTypeKey = 'type', | ||
valueId | ||
}: { | ||
value: any; | ||
model: Model; | ||
modelsByName: Record<string, Model>; | ||
iteratee: (options: { | ||
value: any; | ||
model: Model | null; | ||
field: Field | null; | ||
fieldListItem: FieldListItems | null; | ||
error: string | null; | ||
valueKeyPath: (string | number)[]; | ||
modelKeyPath: string[]; | ||
objectStack: any[]; | ||
skipNested: () => void; | ||
}) => Promise<any>; | ||
pageLayoutKey?: string; | ||
objectTypeKey?: string; | ||
valueId?: string; | ||
}): Promise<any> { | ||
async function _mapDeep({ | ||
value, | ||
model, | ||
field, | ||
fieldListItem, | ||
valueKeyPath, | ||
modelKeyPath, | ||
objectStack | ||
}: { | ||
value: any; | ||
model: Model | null; | ||
field: Field | null; | ||
fieldListItem: FieldListItems | null; | ||
valueKeyPath: (string | number)[]; | ||
modelKeyPath: string[]; | ||
objectStack: any[]; | ||
}) { | ||
let error: string | null = null; | ||
let modelField: FieldModelProps | null = null; | ||
if (!model && !field && !fieldListItem) { | ||
error = `could not match model/field ${modelKeyPath.join('.')} to content at ${valueKeyPath.join('.')}`; | ||
} | ||
if (field && isModelField(field)) { | ||
modelField = field; | ||
} else if (fieldListItem && isModelListItems(fieldListItem)) { | ||
modelField = fieldListItem; | ||
} | ||
if (modelField) { | ||
const modelResult = getModelOfObject({ | ||
object: value, | ||
field: modelField, | ||
modelsByName, | ||
pageLayoutKey, | ||
objectTypeKey, | ||
valueKeyPath, | ||
modelKeyPath | ||
}); | ||
if ('error' in modelResult) { | ||
error = modelResult.error; | ||
} else { | ||
model = modelResult.model; | ||
} | ||
field = null; | ||
fieldListItem = null; | ||
modelKeyPath = model ? [model.name] : []; | ||
} | ||
let shouldSkipNested = false; | ||
const skipNested = () => { | ||
shouldSkipNested = true; | ||
}; | ||
const res = await iteratee({ value, model, field, fieldListItem, error, valueKeyPath, modelKeyPath, objectStack, skipNested }); | ||
if (!_.isUndefined(res)) { | ||
value = res; | ||
} | ||
if (shouldSkipNested) { | ||
return value; | ||
} | ||
if (_.isPlainObject(value)) { | ||
// if fields will not be resolved or the object will have a key that | ||
// doesn't exist among fields, the nested calls to _iterateDeep will | ||
// include an error. | ||
const fields = getFieldsOfModelOrField(model, field, fieldListItem); | ||
const fieldsByName = _.keyBy(fields, 'name'); | ||
modelKeyPath = _.concat(modelKeyPath, 'fields'); | ||
value = await mapValuesPromise(value, (val, key) => { | ||
const field = _.get(fieldsByName, key, null); | ||
return _mapDeep({ | ||
value: val, | ||
model: null, | ||
field: field, | ||
fieldListItem: null, | ||
valueKeyPath: _.concat(valueKeyPath, key), | ||
modelKeyPath: _.concat(modelKeyPath, key), | ||
objectStack: _.concat(objectStack, value) | ||
}); | ||
}); | ||
} else if (_.isArray(value)) { | ||
let fieldListItems: FieldListItems | null = null; | ||
if (field && isListField(field)) { | ||
fieldListItems = getListFieldItems(field); | ||
} else if (model && isListDataModel(model)) { | ||
fieldListItems = model.items; | ||
} | ||
value = await mapPromise(value, (val, idx) => { | ||
return _mapDeep({ | ||
value: val, | ||
model: null, | ||
field: null, | ||
fieldListItem: fieldListItems, | ||
valueKeyPath: _.concat(valueKeyPath, idx), | ||
modelKeyPath: _.concat(modelKeyPath, 'items'), | ||
objectStack: _.concat(objectStack, value) | ||
}); | ||
}); | ||
} | ||
return value; | ||
} | ||
return _mapDeep({ | ||
value: value, | ||
model: model, | ||
field: null, | ||
fieldListItem: null, | ||
valueKeyPath: valueId ? [valueId] : [], | ||
modelKeyPath: [model.name], | ||
objectStack: [] | ||
}); | ||
} | ||
export function getModelOfObject({ | ||
@@ -371,0 +516,0 @@ object, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
842992
142
14558