-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathnotion-page.ts
106 lines (84 loc) · 2.92 KB
/
notion-page.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { fetchPageById, fetchBlocks } from "../notion-api/notion";
import { parsePageId } from "../notion-api/utils";
import { BlockType, CollectionType, HandlerRequest } from "../notion-api/types";
import { getTableData } from "./table";
import { createResponse } from "../utils/response";
import { getNotionToken } from "../utils";
export async function pageRoute(c: HandlerRequest) {
const pageId = parsePageId(c.req.param("pageId"));
const notionToken = getNotionToken(c);
const page = await fetchPageById(pageId!, notionToken);
const baseBlocks = page.recordMap.block;
let allBlocks: { [id: string]: BlockType & { collection?: any } } = {
...baseBlocks,
};
let allBlockKeys;
while (true) {
allBlockKeys = Object.keys(allBlocks);
const pendingBlocks = allBlockKeys.flatMap((blockId) => {
const block = allBlocks[blockId];
const content = block.value && block.value.content;
if (!content || (block.value.type === "page" && blockId !== pageId!)) {
// skips pages other than the requested page
return [];
}
return content.filter((id: string) => !allBlocks[id]);
});
if (!pendingBlocks.length) {
break;
}
const newBlocks = await fetchBlocks(pendingBlocks, notionToken).then(
(res) => res.recordMap.block
);
allBlocks = { ...allBlocks, ...newBlocks };
}
const collection = page.recordMap.collection
? page.recordMap.collection[Object.keys(page.recordMap.collection)[0]]
: null;
const collectionView = page.recordMap.collection_view
? page.recordMap.collection_view[
Object.keys(page.recordMap.collection_view)[0]
]
: null;
if (collection && collectionView) {
const pendingCollections = allBlockKeys.flatMap((blockId) => {
const block = allBlocks[blockId];
return block.value && block.value.type === "collection_view"
? [block.value.id]
: [];
});
for (let b of pendingCollections) {
const collPage = await fetchPageById(b!, notionToken);
const coll = Object.keys(collPage.recordMap.collection).map(
(k) => collPage.recordMap.collection[k]
)[0];
const collView: {
value: { id: CollectionType["value"]["id"] };
} = Object.keys(collPage.recordMap.collection_view).map(
(k) => collPage.recordMap.collection_view[k]
)[0];
const { rows, schema } = await getTableData(
coll,
collView.value.id,
notionToken,
true
);
const viewIds = (allBlocks[b] as any).value.view_ids as string[];
allBlocks[b] = {
...allBlocks[b],
collection: {
title: coll.value.name,
schema,
types: viewIds.map((id) => {
const col = collPage.recordMap.collection_view[id];
return col ? col.value : undefined;
}),
data: rows,
},
};
}
}
return createResponse(allBlocks, {
request: c,
});
}