Since 0.1.0
Collection, Tree, Item
The return types of defineCollection, defineTree, and defineItem.
On this page
Use these to type a function that accepts a primitive, or to constrain a generic.
import type { Collection, Item, Tree } from "@qino/cms";
Collection
type Collection<
Schema extends StandardSchemaV1<unknown, Record<string, unknown>>,
Ext extends ".md" | ".mdx" | ".markdown" | ".json",
Rels extends Relations<Schema> = object,
Dir extends `/${string}` = `/${string}`,
Views extends object = object,
> = {
getAllSlugs(): Promise<Array<SlugFor<Dir>>>;
getEntries(options?: { view?: keyof Views }): Promise<Array<Entry>>;
getEntry(
slug: SlugFor<Dir>,
options?: { view?: keyof Views },
): Promise<Entry>;
getPageMarkdown(): Promise<string>;
getPageMarkdown(
slug: string,
options?: { view?: keyof Views },
): Promise<string | null>;
getMarkdownUrl(): string;
getMarkdownUrl(slug: SlugFor<Dir>): string;
};
Tree
type Tree<
Schema,
Ext,
Title extends StringKeys<Schema>,
Rels = object,
Dir extends `/${string}` = `/${string}`,
Views extends object = object,
> = {
getTree(): Promise<Array<TreeNode>>;
getTree(slug: SlugFor<Dir>): Promise<TreeNode>;
getFlatTree(): Promise<Array<TreeNode>>;
getEntries(options?: { view?: keyof Views }): Promise<Array<Entry>>;
getEntry(
slug: SlugFor<Dir>,
options?: { view?: keyof Views },
): Promise<Entry>;
getAncestorNodes(slug: SlugFor<Dir>): Promise<Array<TreeNode>>;
getNextNode(slug: SlugFor<Dir>): Promise<TreeNode | null>;
getPreviousNode(slug: SlugFor<Dir>): Promise<TreeNode | null>;
getPageMarkdown(): Promise<string>;
getPageMarkdown(slug: string): Promise<string | null>;
getMarkdownUrl(): string;
getMarkdownUrl(slug: SlugFor<Dir>): string;
};
Item
type Item<Schema, Ext, Rels = object, Views extends object = object> = {
getEntry(options?: { view?: keyof Views }): Promise<Entry>;
getPageMarkdown(): Promise<string>;
getMarkdownUrl(): string;
};
Generics
Each type also carries a hidden marker with the definition metadata and the inference data that Infer reads. In practice you rarely write these generics by hand; typeof postCollection is the usual spelling.
Example
import type { Collection } from "@qino/cms";
async function countEntries<C extends Collection<any, any, any, any, any>>(
collection: C,
) {
return (await collection.getAllSlugs()).length;
}