Skip to content

Commit 3fb8853

Browse files
[ACTION] Improve Notion actions to loop block-creation, comply with request limits (#13889)
* Notion: Improved on addiong chunkArray function to get chunks of 1000 blocks each * Flatten results --------- Co-authored-by: Leo Vu <[email protected]>
1 parent fec33ab commit 3fb8853

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

components/notion/actions/append-block/append-block.mjs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "notion-append-block",
77
name: "Append Block to Parent",
88
description: "Creates and appends blocks to the specified parent. [See the documentation](https://developers.notion.com/reference/patch-block-children)",
9-
version: "0.2.13",
9+
version: "0.2.14",
1010
type: "action",
1111
props: {
1212
notion,
@@ -47,6 +47,15 @@ export default {
4747
optional: true,
4848
},
4949
},
50+
methods: {
51+
chunkArray(array, chunkSize = 100) {
52+
const chunks = [];
53+
for (let i = 0; i < array.length; i += chunkSize) {
54+
chunks.push(array.slice(i, i + chunkSize));
55+
}
56+
return chunks;
57+
},
58+
},
5059
async run({ $ }) {
5160
const children = [];
5261
// add blocks from blockObjects
@@ -97,8 +106,17 @@ export default {
97106
return;
98107
}
99108

100-
const { results } = await this.notion.appendBlock(this.pageId, children);
101-
$.export("$summary", `Appended ${results.length} block(s) successfully`);
102-
return results;
109+
const results = [];
110+
const chunks = this.chunkArray(children);
111+
112+
for (const chunk of chunks) {
113+
const { results: payload } = await this.notion.appendBlock(this.pageId, chunk);
114+
results.push(payload);
115+
}
116+
117+
const totalAppended = results.reduce((sum, res) => sum + res.length, 0);
118+
119+
$.export("$summary", `Appended ${totalAppended} block(s) successfully`);
120+
return results.flat();
103121
},
104122
};

components/notion/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/notion",
3-
"version": "0.1.19",
3+
"version": "0.1.20",
44
"description": "Pipedream Notion Components",
55
"main": "notion.app.mjs",
66
"keywords": [

0 commit comments

Comments
 (0)