Skip to main content

Fetcher

import {
createNotionClient,
fetchBlogPosts,
fetchCollection,
fetchAllCollections,
fetchPostBySlug,
fetchPage,
fetchAllSlugs,
} from "@noxion/core";

These functions are the data layer of Noxion. They use the unofficial Notion API (via notion-client) to fetch page data, then normalize it into typed NoxionPage objects.


createNotionClient()

Creates an authenticated Notion API client.

Signature

function createNotionClient(options?: {
authToken?: string;
}): NotionAPI

Parameters

ParameterTypeDescription
options.authTokenstring?Notion integration token. Required for private pages; omit for public pages.

Example

import { createNotionClient } from "@noxion/core";

const notion = createNotionClient();

const notion = createNotionClient({
authToken: process.env.NOTION_TOKEN,
});

fetchBlogPosts()

Fetches all published posts from a Notion database. Returns BlogPage[] sorted by date descending. This is the v0.1 API — still works in v0.2 for single-database blog sites.

Signature

async function fetchBlogPosts(
client: NotionAPI,
databasePageId: string
): Promise<BlogPage[]>

Internal algorithm

  1. Calls client.getPage(databasePageId) for the full record map
  2. Reads the collection schema to find property keys (matched case-insensitively)
  3. Enumerates all block IDs from all views
  4. Extracts each page: properties, frontmatter, filters unpublished
  5. Sorts by metadata.date descending

Slug resolution order

  1. Frontmatter cleanUrl (leading / stripped)
  2. Frontmatter slug
  3. Notion Slug property
  4. Notion page ID (fallback)

Example

const posts = await fetchBlogPosts(notion, siteConfig.rootNotionPageId);
// posts[0].title → "My Most Recent Post"
// posts[0].metadata.tags → ["react", "typescript"]

fetchCollection()

Fetches all published pages from a Notion database, with page-type-aware schema mapping. This is the v0.2 API for multi-database sites.

Signature

async function fetchCollection(
client: NotionAPI,
collection: NoxionCollection
): Promise<NoxionPage[]>

Parameters

ParameterTypeDescription
clientNotionAPIThe Notion client
collectionNoxionCollectionCollection config with databaseId, pageType, optional schema overrides

Returns

Promise<NoxionPage[]> — pages extracted for the collection's pageType. Blog pages are sorted by date descending.

Schema mapping

The fetcher uses convention-based property mapping per page type. Each page type has default Notion property names:

Page TypeExpected Properties
BlogTitle, Public, Published (date), Tags (multi-select), Category (select), Slug, Description, Author
DocsTitle, Public, Section (select), Order (number), Version (text), Slug, Description
PortfolioTitle, Public, Technologies (multi-select), Project URL (url/text), Year (text), Featured (checkbox), Slug, Description

Override with the schema field:

const collection: NoxionCollection = {
databaseId: "abc123...",
pageType: "docs",
schema: {
section: "Department",
order: "Sort Order",
},
};
const pages = await fetchCollection(notion, collection);

Example

import { fetchCollection } from "@noxion/core";

const blogPages = await fetchCollection(notion, {
databaseId: process.env.NOTION_PAGE_ID!,
pageType: "blog",
});

const docsPages = await fetchCollection(notion, {
databaseId: process.env.DOCS_NOTION_ID!,
pageType: "docs",
pathPrefix: "docs",
});

fetchAllCollections()

Fetches pages from all configured collections and flattens them into one array.

Signature

async function fetchAllCollections(
client: NotionAPI,
config: NoxionConfig
): Promise<NoxionPage[]>

Returns

Promise<NoxionPage[]> — all pages from all collections, flattened into a single array.

Example

const allPages = await fetchAllCollections(notion, siteConfig);
const blogPages = allPages.filter(isBlogPage);
const docsPages = allPages.filter(isDocsPage);

fetchPostBySlug()

Fetches all published posts and returns the one matching the given slug.

Signature

async function fetchPostBySlug(
client: NotionAPI,
databasePageId: string,
slug: string
): Promise<BlogPage | undefined>

This function calls fetchBlogPosts() internally — only one API call is made.

Example

const post = await fetchPostBySlug(notion, siteConfig.rootNotionPageId, "my-post");

fetchPage()

Fetches the full block data (ExtendedRecordMap) for a single Notion page. This is what you pass to <NotionPage recordMap={...} />.

Signature

async function fetchPage(
client: NotionAPI,
pageId: string
): Promise<ExtendedRecordMap>

Example

const recordMap = await fetchPage(notion, post.id);
return <NotionPage recordMap={recordMap} rootPageId={post.id} />;

fetchAllSlugs()

Fetches all published post slugs. Used for generateStaticParams().

Signature

async function fetchAllSlugs(
client: NotionAPI,
databasePageId: string
): Promise<string[]>

Example

export async function generateStaticParams() {
const slugs = await fetchAllSlugs(notion, siteConfig.rootNotionPageId);
return slugs.map((slug) => ({ slug }));
}