Skip to content

Commit 7f79f75

Browse files
authored
18869 feature upgrade notion create page in data source action to support templates (#18888)
* Enhance Notion component with template support and version updates - Added `listTemplates` method to the Notion client for retrieving available templates. - Updated `create-page-from-database` action to support template selection, including new properties for template type and ID. - Bumped version to 2.0.0 for the `create-page-from-database` action and updated package version to 1.0.6. - Upgraded `@notionhq/client` dependency to version 5.3.0. * pnpm update * Update Notion component versions to latest releases - Bumped versions for multiple actions and sources, including `append-block`, `complete-file-upload`, `create-comment`, `create-database`, `create-file-upload`, `create-page`, `delete-block`, `duplicate-page`, `get-current-user`, `list-all-users`, `list-file-uploads`, `query-database`, `retrieve-block`, `retrieve-database-content`, `retrieve-database-schema`, `retrieve-file-upload`, `retrieve-page`, `retrieve-page-property-item`, `retrieve-user`, `search`, `send-file-upload`, `update-block`, `update-database`, `update-page`, and various sources. - Ensured all components are aligned with the latest API specifications. * Refactor template retrieval in create-page-from-database action - Updated the `listTemplates` method call to use a parameters object for improved readability and maintainability. - Added support for optional `start_cursor` parameter to handle pagination when retrieving templates.
1 parent fca8d3c commit 7f79f75

File tree

37 files changed

+155
-78
lines changed

37 files changed

+155
-78
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default {
77
key: "notion-append-block",
88
name: "Append Block to Parent",
99
description: "Append new and/or existing blocks to the specified parent. [See the documentation](https://developers.notion.com/reference/patch-block-children)",
10-
version: "0.4.0",
10+
version: "0.4.1",
1111
annotations: {
1212
destructiveHint: false,
1313
openWorldHint: true,

components/notion/actions/complete-file-upload/complete-file-upload.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "notion-complete-file-upload",
77
name: "Complete File Upload",
88
description: "Use this action to finalize a `mode=multi_part` file upload after all of the parts have been sent successfully. [See the documentation](https://developers.notion.com/reference/complete-a-file-upload)",
9-
version: "0.0.7",
9+
version: "0.0.8",
1010
annotations: {
1111
destructiveHint: false,
1212
openWorldHint: true,

components/notion/actions/create-comment/create-comment.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "notion-create-comment",
66
name: "Create Comment",
77
description: "Create a comment in a page or existing discussion thread. [See the documentation](https://developers.notion.com/reference/create-a-comment)",
8-
version: "0.0.9",
8+
version: "0.0.10",
99
annotations: {
1010
destructiveHint: false,
1111
openWorldHint: true,

components/notion/actions/create-database/create-database.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default {
77
key: "notion-create-database",
88
name: "Create Database",
99
description: "Create a database and its initial data source. [See the documentation](https://developers.notion.com/reference/database-create)",
10-
version: "0.1.4",
10+
version: "0.1.5",
1111
annotations: {
1212
destructiveHint: false,
1313
openWorldHint: true,

components/notion/actions/create-file-upload/create-file-upload.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "notion-create-file-upload",
77
name: "Create File Upload",
88
description: "Create a file upload. [See the documentation](https://developers.notion.com/reference/create-a-file-upload)",
9-
version: "0.0.7",
9+
version: "0.0.8",
1010
annotations: {
1111
destructiveHint: false,
1212
openWorldHint: true,

components/notion/actions/create-page-from-database/create-page-from-database.mjs

Lines changed: 111 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
/* eslint-disable no-case-declarations */
2+
import pick from "lodash-es/pick.js";
13
import NOTION_ICONS from "../../common/notion-icons.mjs";
24
import utils from "../../common/utils.mjs";
35
import notion from "../../notion.app.mjs";
46
import base from "../common/base-page-builder.mjs";
5-
import pick from "lodash-es/pick.js";
67

78
export default {
89
...base,
910
key: "notion-create-page-from-database",
1011
name: "Create Page from Data Source",
1112
description: "Create a page from a data source. [See the documentation](https://developers.notion.com/reference/post-page)",
12-
version: "1.1.0",
13+
version: "2.0.0",
1314
annotations: {
1415
destructiveHint: false,
1516
openWorldHint: true,
@@ -25,13 +26,26 @@ export default {
2526
],
2627
label: "Parent Data Source ID",
2728
description: "Select a parent data source or provide a data source ID",
28-
reloadProps: true,
2929
},
30-
Name: {
30+
templateType: {
3131
type: "string",
32-
label: "Name",
33-
description: "The name of the page. Use this only if the data source has a `title` property named `Name`. Otherwise, use the `Properties` prop below to set the title property.",
34-
optional: true,
32+
label: "Template Type",
33+
description: "The type of template to use for the page. [See the documentation](https://developers.notion.com/docs/creating-pages-from-templates) for more information.",
34+
options: [
35+
{
36+
label: "No template. Provided children and properties are immediately applied.",
37+
value: "none",
38+
},
39+
{
40+
label: "Applies the data source's default template to the newly created page. `children` cannot be specified in the create page request.",
41+
value: "default",
42+
},
43+
{
44+
label: "Indicates which exact template to apply to the newly created page. children cannot be specified in the create page request.",
45+
value: "template_id",
46+
},
47+
],
48+
reloadProps: true,
3549
},
3650
propertyTypes: {
3751
propDefinition: [
@@ -64,11 +78,6 @@ export default {
6478
description: "Cover [External URL](https://developers.notion.com/reference/file-object#external-file-objects)",
6579
optional: true,
6680
},
67-
alert: {
68-
type: "alert",
69-
alertType: "info",
70-
content: "This action will create an empty page by default. To add content, use the `Page Content` prop below.",
71-
},
7281
pageContent: {
7382
propDefinition: [
7483
notion,
@@ -77,11 +86,64 @@ export default {
7786
},
7887
},
7988
async additionalProps() {
80-
const { properties } = await this.notion.retrieveDataSource(this.parentDataSource);
81-
const selectedProperties = pick(properties, this.propertyTypes);
82-
return this.buildAdditionalProps({
83-
properties: selectedProperties,
84-
});
89+
switch (this.templateType) {
90+
case "none":
91+
const { properties } = await this.notion.retrieveDataSource(this.parentDataSource);
92+
const selectedProperties = pick(properties, this.propertyTypes);
93+
return {
94+
alert: {
95+
type: "alert",
96+
alertType: "info",
97+
content: "This action will create an empty page by default. To add content, use the `Page Content` prop below.",
98+
},
99+
...this.buildAdditionalProps({
100+
properties: selectedProperties,
101+
}),
102+
};
103+
case "default":
104+
return {
105+
alert: {
106+
type: "alert",
107+
alertType: "info",
108+
content: "This action will create a page using the data source's default template. Using the `Page Content` prop below will `not` apply to the page.",
109+
},
110+
};
111+
case "template_id":
112+
return {
113+
templateId: {
114+
type: "string",
115+
label: "Template ID",
116+
description: "The ID of the template to use for the page. [See the documentation](https://developers.notion.com/docs/creating-pages-from-templates) for more information.",
117+
options: async({ prevContext }) => {
118+
const params = {
119+
data_source_id: this.parentDataSource,
120+
};
121+
if (prevContext?.nCursor) {
122+
params.start_cursor = prevContext.nCursor;
123+
}
124+
const {
125+
templates, next_cursor: nCursor,
126+
} = await this.notion.listTemplates(params);
127+
return {
128+
options: templates.map(({
129+
name: label, id: value,
130+
}) => ({
131+
label,
132+
value,
133+
})),
134+
context: {
135+
nCursor,
136+
},
137+
};
138+
},
139+
},
140+
alert: {
141+
type: "alert",
142+
alertType: "info",
143+
content: "This action will create a page using the selected template. Using the `Page Content` prop below will `not` apply to the page.",
144+
},
145+
};
146+
}
85147
},
86148
methods: {
87149
...base.methods,
@@ -91,33 +153,45 @@ export default {
91153
* @returns the constructed page in Notion format
92154
*/
93155
buildPage(parentDataSource) {
156+
const meta = this.buildDataSourceMeta(parentDataSource);
94157
this.properties = utils.parseObject(this.properties);
95158
const properties = this.buildPageProperties(parentDataSource.properties);
96-
97-
const propertiesArray = [];
98-
for (const property of Object.values(parentDataSource.properties)) {
99-
if (properties[property.id]) {
100-
propertiesArray.push({
101-
label: property.name,
102-
type: property.type,
103-
value: this[property.name] || this.properties[property.name],
104-
});
105-
}
106-
}
107-
108-
return propertiesArray;
159+
const children = this.createBlocks(this.pageContent);
160+
return {
161+
...meta,
162+
properties,
163+
children,
164+
};
109165
},
110166
},
111167
async run({ $ }) {
168+
const MAX_BLOCKS = 100;
112169
const parentPage = await this.notion.retrieveDataSource(this.parentDataSource);
113-
const properties = await this.buildPage(parentPage);
114-
const response = await this.buildPageFromDataSource({
115-
pageContent: this.pageContent,
116-
parentDataSourceId: this.parentDataSource,
117-
properties,
118-
icon: this.icon,
119-
cover: this.cover,
170+
const {
171+
children, ...page
172+
} = this.buildPage(parentPage);
173+
const data = this.templateId
174+
? {
175+
template: {
176+
type: this.templateType,
177+
template_id: this.templateId,
178+
},
179+
}
180+
: {
181+
children: children.slice(0, MAX_BLOCKS),
182+
};
183+
const response = await this.notion.createPage({
184+
...data,
185+
...page,
186+
parent: {
187+
data_source_id: this.parentDataSource,
188+
},
120189
});
190+
let remainingBlocks = children.slice(MAX_BLOCKS);
191+
while (remainingBlocks.length > 0) {
192+
await this.notion.appendBlock(response.id, remainingBlocks.slice(0, MAX_BLOCKS));
193+
remainingBlocks = remainingBlocks.slice(MAX_BLOCKS);
194+
}
121195
$.export("$summary", "Created page successfully");
122196
return response;
123197
},

components/notion/actions/create-page/create-page.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "notion-create-page",
77
name: "Create Page",
88
description: "Create a page from a parent page. [See the documentation](https://developers.notion.com/reference/post-page)",
9-
version: "0.3.0",
9+
version: "0.3.1",
1010
annotations: {
1111
destructiveHint: false,
1212
openWorldHint: true,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "notion-delete-block",
77
name: "Delete Block",
88
description: "Sets a Block object, including page blocks, to archived: true using the ID specified. [See the documentation](https://developers.notion.com/reference/delete-a-block)",
9-
version: "0.0.7",
9+
version: "0.0.8",
1010
annotations: {
1111
destructiveHint: true,
1212
openWorldHint: true,

components/notion/actions/duplicate-page/duplicate-page.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default {
77
key: "notion-duplicate-page",
88
name: "Duplicate Page",
99
description: "Create a new page copied from an existing page block. [See the documentation](https://developers.notion.com/reference/post-page)",
10-
version: "0.0.22",
10+
version: "0.0.23",
1111
annotations: {
1212
destructiveHint: false,
1313
openWorldHint: true,

components/notion/actions/get-current-user/get-current-user.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "notion-get-current-user",
55
name: "Get Current User",
66
description: "Retrieve the Notion identity tied to the current OAuth token, returning the full `users.retrieve` payload for `me` (person or bot). Includes the user ID, name, avatar URL, type (`person` vs `bot`), and workspace ownership metadata—useful for confirming which workspace is connected, adapting downstream queries, or giving an LLM the context it needs about who is operating inside Notion. [See the documentation](https://developers.notion.com/reference/get-user).",
7-
version: "0.0.1",
7+
version: "0.0.2",
88
type: "action",
99
annotations: {
1010
destructiveHint: false,

0 commit comments

Comments
 (0)