Skip to content
qino

Since 0.1.0

Collection, Tree, Item

The return types of defineCollection, defineTree, and defineItem.

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>;
};

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>>;
  getEntry(
    slug: SlugFor<Dir>,
    options?: { view?: keyof Views },
  ): Promise<Entry>;
  getNextNode(slug: SlugFor<Dir>): Promise<TreeNode | null>;
  getPreviousNode(slug: SlugFor<Dir>): Promise<TreeNode | null>;
};

Item

@qino/cms
type Item<Schema, Ext, Rels = object, Views extends object = object> = {
  getEntry(options?: { view?: keyof Views }): Promise<Entry>;
};

Generics

ParameterDescription
SchemaThe Standard Schema validator passed as schema
ExtThe content extension
TitleTree only. The titleField key
RelsThe relations map
DirThe directory literal. Looked up in QinoSlugRegistry for slug types
ViewsThe object returned by the views factory

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;
}