Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions src/api/notion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CollectionData,
NotionSearchParamsType,
NotionSearchResultsType,
BlockType,
} from "./types";

const NOTION_API = "https://www.notion.so/api/v3";
Expand All @@ -13,6 +14,7 @@ interface INotionParams {
resource: string;
body: JSONData;
notionToken?: string;
baseUrl?: string;
}

const loadPageChunkBody = {
Expand All @@ -26,8 +28,9 @@ const fetchNotionData = async <T extends any>({
resource,
body,
notionToken,
baseUrl = NOTION_API,
}: INotionParams): Promise<T> => {
const res = await fetch(`${NOTION_API}/${resource}`, {
const res = await fetch(`${baseUrl}/${resource}`, {
method: "POST",
headers: {
"content-type": "application/json",
Expand Down Expand Up @@ -77,23 +80,41 @@ const queryCollectionBody = {
export const fetchTableData = async (
collectionId: string,
collectionViewId: string,
notionToken?: string
notionToken?: string,
blockData?: BlockType
) => {
const table = await fetchNotionData<CollectionData>({
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<CollectionData>({
resource: "queryCollection",
body: requestBody,
notionToken,
baseUrl: apiBaseUrl,
});

return table;
};

export const fetchNotionUsers = async (
Expand Down
4 changes: 4 additions & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/routes/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -75,6 +76,7 @@ export async function pageRoute(req: HandlerRequest) {
coll,
collView.value.id,
req.notionToken,
undefined,
true
);

Expand Down
11 changes: 9 additions & 2 deletions src/routes/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ import {
CollectionType,
RowType,
HandlerRequest,
BlockType,
} from "../api/types";
import { createResponse } from "../response";

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;
Expand Down Expand Up @@ -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);
Expand Down