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
40 changes: 39 additions & 1 deletion src/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,42 @@ export const parsePageId = (id: string) => {
}
};

// Notion has started returning some recordMap entries wrapped with one or
// more `{ value, role }` envelopes. Normalize to the inner record object.
export const normalizeNotionRecord = <T = any>(record: any): T => {
let current = record;

while (
current &&
typeof current === "object" &&
current.value &&
typeof current.value === "object" &&
(Object.prototype.hasOwnProperty.call(current, "role") ||
(Object.keys(current).length === 1 &&
Object.prototype.hasOwnProperty.call(current, "value")))
) {
current = current.value;
}

return current as T;
};

export const getRecordValue = <T = any>(record: any): T | undefined => {
const normalized = normalizeNotionRecord<any>(record);
if (!normalized) return undefined;

if (
typeof normalized === "object" &&
normalized.value &&
typeof normalized.value === "object" &&
Object.prototype.hasOwnProperty.call(normalized, "role")
) {
return normalized.value as T;
}

return normalized as T;
};

export const getNotionValue = (
val: DecorationType[],
type: ColumnType,
Expand Down Expand Up @@ -67,7 +103,9 @@ export const getNotionValue = (
);

url.searchParams.set("table", "block");
url.searchParams.set("id", row.value.id);
const rowId =
(row as any)?.value?.id || (row as any)?.id || "";
url.searchParams.set("id", rowId);
url.searchParams.set("cache", "v2");

return { name: v[0] as string, url: url.toString(), rawUrl };
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
113 changes: 94 additions & 19 deletions src/routes/table.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,98 @@
import { fetchPageById, fetchTableData, fetchNotionUsers } from "../api/notion";
import { parsePageId, getNotionValue } from "../api/utils";
import { parsePageId, getNotionValue, getRecordValue } from "../api/utils";
import {
RowContentType,
CollectionType,
RowType,
HandlerRequest,
BlockType,
} from "../api/types";
import { createResponse } from "../response";

const resolveBlockValue = (block: any) => {
const normalized = getRecordValue<any>(block);
if (!normalized) return undefined;
if (normalized.properties) return normalized;
if (normalized.value && normalized.value.properties) return normalized.value;
if (
normalized.value &&
normalized.value.value &&
normalized.value.value.properties
) {
return normalized.value.value;
}
return normalized;
};

export const getTableData = async (
collection: CollectionType,
collectionViewId: string,
notionToken?: string,
blockData?: BlockType,
raw?: boolean
) => {
const collectionValue = getRecordValue<any>(collection);

const table = await fetchTableData(
collection.value.id,
collectionValue.id,
collectionViewId,
notionToken
notionToken,
blockData
);

const collectionRows = collection.value.schema;
const collectionRows = collectionValue.schema;
const collectionColKeys = Object.keys(collectionRows);

const tableArr: RowType[] = table.result.reducerResults.collection_group_results.blockIds.map(
(id: string) => table.recordMap.block[id]
);
const blockIds =
table.result.reducerResults.collection_group_results.blockIds || [];

const firstBlockId = blockIds[0];
if (firstBlockId) {
const firstRaw = table.recordMap.block[firstBlockId];
const firstResolved = resolveBlockValue(firstRaw);
console.log(
`[table-debug] firstId=${firstBlockId} rawKeys=${Object.keys(
firstRaw || {}
).join(",")} resolvedKeys=${Object.keys(firstResolved || {}).join(
","
)} hasProps=${Boolean(
firstResolved && firstResolved.properties
)}`
);
}

const tableArr: any[] = blockIds
.map((id: string) => resolveBlockValue(table.recordMap.block[id]))
.filter(Boolean);

const tableData = tableArr.filter(
(b) =>
b.value && b.value.properties && b.value.parent_id === collection.value.id
(b) => b && b.properties && b.parent_id === collectionValue.id
);

const parentIds = tableArr
.map((b) => (b ? b.parent_id : undefined))
.filter(Boolean)
.slice(0, 5)
.join(",");

console.log(
`[table] ids=${tableArr.length} filtered=${tableData.length} collection=${collectionValue.id} sampleParentIds=${parentIds}`
);

type Row = { id: string; [key: string]: RowContentType };

const rows: Row[] = [];

for (const td of tableData) {
let row: Row = { id: td.value.id };
let row: Row = { id: td.id };

for (const key of collectionColKeys) {
const val = td.value.properties[key];
const val = td.properties[key];
if (val) {
const schema = collectionRows[key];
row[schema.name] = raw ? val : getNotionValue(val, schema.type, td);
row[schema.name] = raw
? val
: getNotionValue(val, schema.type, { value: td } as any);
if (schema.type === "person" && row[schema.name]) {
const users = await fetchNotionUsers(row[schema.name] as string[]);
row[schema.name] = users as any;
Expand All @@ -67,20 +116,46 @@ export async function tableRoute(req: HandlerRequest) {
401
);

const collection = Object.keys(page.recordMap.collection).map(
const collection = getRecordValue<CollectionType>(
Object.keys(page.recordMap.collection).map(
(k) => page.recordMap.collection[k]
)[0];
)[0]
);

const collectionView: {
const collectionView = getRecordValue<{
value: { id: CollectionType["value"]["id"] };
} = Object.keys(page.recordMap.collection_view).map(
}>(
Object.keys(page.recordMap.collection_view).map(
(k) => page.recordMap.collection_view[k]
)[0];
)[0]
);

const blockData = getRecordValue<BlockType>(page.recordMap.block[pageId!]) as any;
const collectionId = (collection as any)?.id || (collection as any)?.value?.id;
const collectionViewId =
(collectionView as any)?.id || (collectionView as any)?.value?.id;

console.log(
`[tableRoute] page=${pageId} collection=${collectionId} view=${collectionViewId}`
);

if (!collection || !collectionView || !collectionId || !collectionViewId) {
return createResponse(
JSON.stringify({
error: "Failed to resolve collection or collection view",
pageId,
}),
{},
500
);
}

const { rows } = await getTableData(
collection,
collectionView.value.id,
req.notionToken
collectionViewId as string,
req.notionToken,
blockData

);

return createResponse(rows);
Expand Down