UNPKG

@stackbit/types

Version:

Types for Stackbit config and Content Source Interface

124 lines 4.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.defineStackbitConfig = exports.isDocumentFieldOneOfFieldTypes = exports.isModelFieldSpecificPropsOneOfFieldTypes = exports.isModelFieldOneOfFieldTypes = exports.isOneOfFieldTypes = exports.getLocalizedFieldForLocale = exports.isLocalizedField = exports.getVersion = exports.getInterfaceVersion = exports.loadJSON = void 0; async function loadJSON(filePath) { // The `@stackbit/pacakge` can be imported in browser environment. To use // the types in client-side's code, or when using the // `getLocalizedFieldForLocale` utility method. // Therefore, don't use `require('fs/promises')`, as bundlers will try to // bundle the inline `fs/promises` and browser will not be able to load it. const module = 'fs/promises'; const fs = require(module); return JSON.parse(await fs.readFile(filePath, { encoding: `utf8` })); } exports.loadJSON = loadJSON; async function getInterfaceVersion() { const path = require('path'); const packageJson = await loadJSON(path.join(__dirname, '../package.json')); return packageJson.version; } exports.getInterfaceVersion = getInterfaceVersion; async function getVersion(options = {}) { const interfaceVersion = await getInterfaceVersion(); if (options.contentSourceVersion) { return { interfaceVersion: interfaceVersion, contentSourceVersion: options.contentSourceVersion }; } else if (options.packageJsonPath) { const packageJson = await loadJSON(options.packageJsonPath); return { interfaceVersion: interfaceVersion, contentSourceVersion: packageJson.version }; } else { return { interfaceVersion: interfaceVersion, contentSourceVersion: '' }; } } exports.getVersion = getVersion; function isLocalizedField(field) { return !!field.localized; } exports.isLocalizedField = isLocalizedField; /** * Returns non-localized version of a DocumentField for the provided `locale`. * If the `field` is localized, and the provided locale is not present, this * function returns `undefined`. If the `field` is not localized, this function * returns the `field` as-is ignoring the `locale`. * * @example Passing localized field * getLocalizedFieldForLocale({ * type: 'string', * localized: true, * locales: { * en-US: { value: 'Hello' }, * es-ES: { value: 'Hola' }, * fr-FR: { value: 'Bonjour' } * } * }, 'es-ES'); * returns: * { * type: 'string', * value: 'Hola' * } * * @example Passing non-localized field * getLocalizedFieldForLocale({ * type: 'string', * value: 'Hello' * }); * returns: * { * type: 'string', * value: 'Hello' * } * * @param field * @param locale */ function getLocalizedFieldForLocale(field, locale) { if (field && field.localized) { if (!locale) { return undefined; } const { localized, locales, ...base } = field; const localizedField = locales?.[locale]; if (!localizedField) { return undefined; } return { ...base, ...localizedField }; } return field; } exports.getLocalizedFieldForLocale = getLocalizedFieldForLocale; function isOneOfFieldTypes(fieldType, fieldTypes) { return fieldTypes.includes(fieldType); } exports.isOneOfFieldTypes = isOneOfFieldTypes; function isModelFieldOneOfFieldTypes(modelField, fieldTypes) { return fieldTypes.includes(modelField.type); } exports.isModelFieldOneOfFieldTypes = isModelFieldOneOfFieldTypes; function isModelFieldSpecificPropsOneOfFieldTypes(modelField, fieldTypes) { return fieldTypes.includes(modelField.type); } exports.isModelFieldSpecificPropsOneOfFieldTypes = isModelFieldSpecificPropsOneOfFieldTypes; function isDocumentFieldOneOfFieldTypes(documentField, fieldTypes) { return fieldTypes.includes(documentField.type); } exports.isDocumentFieldOneOfFieldTypes = isDocumentFieldOneOfFieldTypes; function defineStackbitConfig(stackbitConfig) { return stackbitConfig; } exports.defineStackbitConfig = defineStackbitConfig; //# sourceMappingURL=utils.js.map