Skip to content
qino

Since 0.1.0

Collection, Tree, Item

The return types of defineCollection, defineTree, and defineItem.

View Markdown
On this page

Use these to type a function that accepts a primitive, or to constrain a generic.

@qino/cms
import type { Collection, Item, Tree } from "@qino/cms";

Collection

@qino/cms
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

@qino/cms
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

@qino/cms
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

src/lib/count-entries.ts
import type { Collection } from "@qino/cms";

async function countEntries<C extends Collection<any, any, any, any, any>>(
  collection: C,
) {
  return (await collection.getAllSlugs()).length;
}