@stackbit/utils
Version:
Stackbit utilities
93 lines • 3.12 kB
TypeScript
import type * as StackbitTypes from '@stackbit/types';
export type FlatDocument = {
__metadata: Omit<StackbitTypes.Document, 'fields'>;
[key: string]: any;
};
export type GetCSIDocumentsOptions = {
stackbitApiKey: string;
srcType?: string;
srcProjectId?: string;
srcDocumentIds?: string[];
documentSpecs?: StackbitTypes.DocumentSpecifier[];
limit?: number;
offset?: number;
};
export declare function getCSIDocuments(options: GetCSIDocumentsOptions & {
flatDocuments?: never;
flatLocale?: never;
}): Promise<{
total: number;
offset: number;
documents: StackbitTypes.Document[];
}>;
export declare function getCSIDocuments(options: GetCSIDocumentsOptions & {
flatDocuments?: boolean;
flatLocale?: string;
}): Promise<{
total: number;
offset: number;
documents: FlatDocument[];
}>;
/**
* Flattens {@link StackbitTypes.Document} into a simplified object.
* All document properties except `fields` are moved into the `__metadata`
* property. All document `fields` are placed at the root of the document and
* recursively simplified by referencing their values directly.
*
* If the `locale` argument is not specified, only the non-localized fields will
* be returned. If the `locale` is specified, only the localized document fields
* for that locale, and also non-localized fields will be returned.
*
* @example
* flattenDocument({
* document: {
* type: 'document',
* id: 'xyz',
* modelName: 'Page',
* ...other,
* fields: {
* title: { type: 'string', value: 'Welcome' },
* seo: { type: 'object', fields: { title: { type: 'string', value: 'SEO Welcome' } } },
* tags: { type: 'list', items: [{ type: 'string', value: 'tech' }] }
* }
* }
* }) => {
* __metadata: {
* type: 'document',
* id: 'xyz',
* modelName: 'Page',
* ...rest
* },
* title: 'Welcome',
* seo: { title: 'SEO Welcome' },
* tags: ['tech']
* }
*
* @param document
* @param locale
*/
export declare function flattenDocument({ document, locale }: {
document: StackbitTypes.Document;
locale?: string;
}): FlatDocument;
/**
* Returns the value of a document field.
*
* If the document field is a primitive (string, url, slug, text, markdown, enum, date, number, boolean, etc.)
* then the value of the `value` property is returned.
*
* If the document is a field of type "object", "model" or "list", the value of the
* nested object is simplified and returned.
*
* If the document is a field of type "reference", the ID of the referenced
* document is returned.
*
* If the document field is localized, then the value for the provided "locale"
* is returned. If "locale" was not provided, or the document field does not
* have any values for the provided "locale", undefined is returned.
*/
export declare function getDocumentFieldValue({ documentField, locale }: {
documentField: StackbitTypes.DocumentField;
locale?: string;
}): any;
//# sourceMappingURL=document-utils.d.ts.map