@stackbit/types
Version:
Types for Stackbit config and Content Source Interface
150 lines (139 loc) • 4.62 kB
text/typescript
import type { Version } from './content-source';
import type { FieldType, Field, FieldForType, FieldSpecificProps, FieldSpecificPropsForType } from './model-fields';
import type { StackbitConfig } from './config';
import type { AssetFileField, AssetFileFieldLocalized } from './content-source-document';
import type {
DocumentField,
DocumentFieldForType,
DocumentFieldLocalized,
DocumentFieldNonLocalizedForType
} from './content-source-document-fields';
export async function loadJSON(filePath: string) {
// 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`
})
);
}
export async function getInterfaceVersion() {
const path = require('path');
const packageJson = await loadJSON(path.join(__dirname, '../package.json'));
return packageJson.version;
}
export async function getVersion(
options: { packageJsonPath?: string; contentSourceVersion?: string } = {}
): Promise<Version> {
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: ''
};
}
}
export function isLocalizedField(
field: DocumentField | AssetFileField
): field is DocumentFieldLocalized | AssetFileFieldLocalized {
return !!field.localized;
}
/**
* 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
*/
export function getLocalizedFieldForLocale<Type extends FieldType>(
field?: DocumentFieldForType<Type>,
locale?: string
): DocumentFieldNonLocalizedForType<Type> | undefined {
if (field && field.localized) {
if (!locale) {
return undefined;
}
const { localized, locales, ...base } = field;
const localizedField = locales?.[locale];
if (!localizedField) {
return undefined;
}
return {
...base,
...localizedField
} as unknown as DocumentFieldNonLocalizedForType<Type>;
}
return field;
}
export function isOneOfFieldTypes<T extends FieldType>(
fieldType: FieldType,
fieldTypes: ReadonlyArray<T>
): fieldType is T {
return fieldTypes.includes(fieldType as T);
}
export function isModelFieldOneOfFieldTypes<T extends FieldType>(
modelField: Field,
fieldTypes: ReadonlyArray<T>
): modelField is FieldForType<T> {
return fieldTypes.includes(modelField.type as T);
}
export function isModelFieldSpecificPropsOneOfFieldTypes<T extends FieldType>(
modelField: FieldSpecificProps,
fieldTypes: ReadonlyArray<T>
): modelField is FieldSpecificPropsForType<T> {
return fieldTypes.includes(modelField.type as T);
}
export function isDocumentFieldOneOfFieldTypes<T extends FieldType>(
documentField: DocumentField,
fieldTypes: ReadonlyArray<T>
): documentField is DocumentFieldForType<T> {
return fieldTypes.includes(documentField.type as T);
}
export function defineStackbitConfig(stackbitConfig: StackbitConfig) {
return stackbitConfig;
}