From 5056ed1af133957acd2c8ffa1b6a9654b0d7e50d Mon Sep 17 00:00:00 2001 From: Trevor Healy Date: Wed, 8 Oct 2025 09:53:55 -0700 Subject: [PATCH] Query Collection Requires Specific Base URL --- src/api/notion.ts | 45 +++++++++++++++++++++++++++++++++------------ src/api/types.ts | 4 ++++ src/routes/page.ts | 2 ++ src/routes/table.ts | 11 +++++++++-- 4 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/api/notion.ts b/src/api/notion.ts index f3da329..d107eba 100644 --- a/src/api/notion.ts +++ b/src/api/notion.ts @@ -5,6 +5,7 @@ import { CollectionData, NotionSearchParamsType, NotionSearchResultsType, + BlockType, } from "./types"; const NOTION_API = "https://www.notion.so/api/v3"; @@ -13,6 +14,7 @@ interface INotionParams { resource: string; body: JSONData; notionToken?: string; + baseUrl?: string; } const loadPageChunkBody = { @@ -26,8 +28,9 @@ const fetchNotionData = async ({ resource, body, notionToken, + baseUrl = NOTION_API, }: INotionParams): Promise => { - const res = await fetch(`${NOTION_API}/${resource}`, { + const res = await fetch(`${baseUrl}/${resource}`, { method: "POST", headers: { "content-type": "application/json", @@ -77,23 +80,41 @@ const queryCollectionBody = { export const fetchTableData = async ( collectionId: string, collectionViewId: string, - notionToken?: string + notionToken?: string, + blockData?: BlockType ) => { - const table = await fetchNotionData({ - resource: "queryCollection", - body: { - collection: { + const spaceId = blockData?.value?.space_id; + const siteId = blockData?.value?.format?.site_id; + + const apiBaseUrl = siteId + ? `https://${siteId}.notion.site/api/v3` + : NOTION_API; + + + const requestBody: JSONData = { + ...(spaceId ? { + source: { + type: "collection", id: collectionId, + spaceId: spaceId, }, - collectionView: { - id: collectionViewId, - }, - ...queryCollectionBody, + } : {}), + collection: { + id: collectionId, + }, + collectionView: { + id: collectionViewId, + ...(spaceId && { spaceId: spaceId }), }, + ...queryCollectionBody, + } + + return fetchNotionData({ + resource: "queryCollection", + body: requestBody, notionToken, + baseUrl: apiBaseUrl, }); - - return table; }; export const fetchNotionUsers = async ( diff --git a/src/api/types.ts b/src/api/types.ts index bb8b472..7d413ff 100644 --- a/src/api/types.ts +++ b/src/api/types.ts @@ -74,6 +74,10 @@ export interface BaseValueType { last_edited_by_table: string; last_edited_by_id: string; content?: string[]; + space_id?: string; + format?: { + site_id?: string; + }; } export interface CollectionType { diff --git a/src/routes/page.ts b/src/routes/page.ts index d40c844..432abe5 100644 --- a/src/routes/page.ts +++ b/src/routes/page.ts @@ -5,6 +5,7 @@ import { getTableData } from "./table"; import { BlockType, CollectionType, HandlerRequest } from "../api/types"; export async function pageRoute(req: HandlerRequest) { + const pageId = parsePageId(req.params.pageId); const page = await fetchPageById(pageId!, req.notionToken); @@ -75,6 +76,7 @@ export async function pageRoute(req: HandlerRequest) { coll, collView.value.id, req.notionToken, + undefined, true ); diff --git a/src/routes/table.ts b/src/routes/table.ts index 13ef423..3cca0f2 100644 --- a/src/routes/table.ts +++ b/src/routes/table.ts @@ -5,6 +5,7 @@ import { CollectionType, RowType, HandlerRequest, + BlockType, } from "../api/types"; import { createResponse } from "../response"; @@ -12,12 +13,14 @@ export const getTableData = async ( collection: CollectionType, collectionViewId: string, notionToken?: string, + blockData?: BlockType, raw?: boolean ) => { const table = await fetchTableData( collection.value.id, collectionViewId, - notionToken + notionToken, + blockData ); const collectionRows = collection.value.schema; @@ -77,10 +80,14 @@ export async function tableRoute(req: HandlerRequest) { (k) => page.recordMap.collection_view[k] )[0]; + const blockData = page.recordMap.block[pageId!]; + const { rows } = await getTableData( collection, collectionView.value.id, - req.notionToken + req.notionToken, + blockData + ); return createResponse(rows);