Skip to content

Commit 02df2e9

Browse files
committed
collection fixes from Splitbee repo
1 parent 22998ae commit 02df2e9

File tree

8 files changed

+50
-17
lines changed

8 files changed

+50
-17
lines changed

.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.5.3

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"name": "notion-api-worker",
3-
"version": "0.1.0",
3+
"version": "0.1.",
44
"main": "dist/index.js",
55
"license": "MIT",
66
"scripts": {
77
"build": "webpack",
88
"dev": "wrangler dev",
99
"preview": "wrangler preview",
10-
"deploy": "wrangler publish -e production"
10+
"deploy": "wrangler publish"
1111
},
1212
"dependencies": {
1313
"tiny-request-router": "^1.2.2"

src/api/notion.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,24 @@ export const fetchPageById = async (pageId: string, notionToken?: string) => {
5656
};
5757

5858
const queryCollectionBody = {
59-
query: { aggregations: [{ property: "title", aggregator: "count" }] },
6059
loader: {
61-
type: "table",
62-
limit: 999,
60+
type: "reducer",
61+
reducers: {
62+
collection_group_results: {
63+
type: "results",
64+
limit: 999,
65+
loadContentCover: true,
66+
},
67+
"table:uncategorized:title:count": {
68+
type: "aggregation",
69+
aggregation: {
70+
property: "title",
71+
aggregator: "count",
72+
},
73+
},
74+
},
6375
searchQuery: "",
6476
userTimeZone: "Europe/Vienna",
65-
userLocale: "en",
66-
loadContentCover: true,
6777
},
6878
};
6979

@@ -75,8 +85,12 @@ export const fetchTableData = async (
7585
const table = await fetchNotionData<CollectionData>({
7686
resource: "queryCollection",
7787
body: {
78-
collectionId,
79-
collectionViewId,
88+
collection: {
89+
id: collectionId,
90+
},
91+
collectionView: {
92+
id: collectionViewId,
93+
},
8094
...queryCollectionBody,
8195
},
8296
notionToken,

src/api/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ export interface CollectionData {
165165
};
166166
};
167167
result: {
168-
blockIds: string[];
168+
reducerResults: {
169+
collection_group_results: { blockIds: string[] };
170+
};
169171
};
170172
}
171173

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const corsHeaders = {
2020
"Access-Control-Allow-Origin": "*",
2121
"Access-Control-Allow-Headers": "*",
2222
"Access-Control-Allow-Methods": "GET, HEAD, POST, OPTIONS",
23+
// "Cache-Control":`public, s-maxage=${30}, max-age=${60*60*0.1}, stale-while-revalidate=${60*4}`,
24+
"Cache-Control":`public, s-maxage=${10}, max-age=${10}, stale-while-revalidate=${10}`,
2325
};
2426

2527
const router = new Router<Handler>();
@@ -86,6 +88,7 @@ const handleRequest = async (fetchEvent: FetchEvent): Promise<Response> => {
8688
return res;
8789
};
8890

91+
// console.log('responding ...')
8992
if (response) {
9093
fetchEvent.waitUntil(getResponseAndPersist());
9194
return response;

src/response.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export const createResponse = (
1111
"Access-Control-Allow-Origin": "*",
1212
"Access-Control-Allow-Methods": "GET, OPTIONS",
1313
"Content-Type": "application/json",
14+
// "Cache-Control":`public, s-maxage=${30}, max-age=${60*60*0.1}, stale-while-revalidate=${60*4}`,
15+
"Cache-Control":`public, s-maxage=${10}, max-age=${10}, stale-while-revalidate=${10}`,
1416
...headers,
1517
},
1618
});

src/routes/page.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ export async function pageRoute(req: HandlerRequest) {
2222
const block = allBlocks[blockId];
2323
const content = block.value && block.value.content;
2424

25-
return content && block.value.type !== "page"
26-
? content.filter((id: string) => !allBlocks[id])
27-
: [];
25+
if (!content || (block.value.type === "page" && blockId !== pageId!)) {
26+
// skips pages other than the requested page
27+
return [];
28+
}
29+
30+
return content.filter((id: string) => !allBlocks[id]);
2831
});
2932

3033
if (!pendingBlocks.length) {
@@ -68,6 +71,8 @@ export async function pageRoute(req: HandlerRequest) {
6871
(k) => collPage.recordMap.collection_view[k]
6972
)[0];
7073

74+
// console.log('>>>>> page getting table data')
75+
7176
const { rows, schema } = await getTableData(
7277
coll,
7378
collView.value.id,

src/routes/table.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,16 @@ export const getTableData = async (
2323
const collectionRows = collection.value.schema;
2424
const collectionColKeys = Object.keys(collectionRows);
2525

26-
const tableArr: RowType[] = table.result.blockIds.map(
26+
// console.log('>>>>> table block ids?!', table.result)
27+
28+
type Row = { id: string; [key: string]: RowContentType };
29+
30+
const rows: Row[] = [];
31+
32+
if(!table.result) // no tables
33+
return { rows, schema: collectionRows };
34+
35+
const tableArr: RowType[] = table.result.reducerResults.collection_group_results.blockIds.map(
2736
(id: string) => table.recordMap.block[id]
2837
);
2938

@@ -32,9 +41,6 @@ export const getTableData = async (
3241
b.value && b.value.properties && b.value.parent_id === collection.value.id
3342
);
3443

35-
type Row = { id: string; [key: string]: RowContentType };
36-
37-
const rows: Row[] = [];
3844

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

0 commit comments

Comments
 (0)