Skip to content

Commit 4270373

Browse files
committed
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.
1 parent b3cd11c commit 4270373

File tree

3 files changed

+113
-39
lines changed

3 files changed

+113
-39
lines changed

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

Lines changed: 108 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,61 @@ 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 {
119+
templates, next_cursor: nCursor,
120+
} = await this.notion.listTemplates({
121+
data_source_id: this.parentDataSource,
122+
start_cursor: prevContext?.nCursor,
123+
});
124+
return {
125+
options: templates.map(({
126+
name: label, id: value,
127+
}) => ({
128+
label,
129+
value,
130+
})),
131+
context: {
132+
nCursor,
133+
},
134+
};
135+
},
136+
},
137+
alert: {
138+
type: "alert",
139+
alertType: "info",
140+
content: "This action will create a page using the selected template. Using the `Page Content` prop below will `not` apply to the page.",
141+
},
142+
};
143+
}
85144
},
86145
methods: {
87146
...base.methods,
@@ -91,33 +150,45 @@ export default {
91150
* @returns the constructed page in Notion format
92151
*/
93152
buildPage(parentDataSource) {
153+
const meta = this.buildDataSourceMeta(parentDataSource);
94154
this.properties = utils.parseObject(this.properties);
95155
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;
156+
const children = this.createBlocks(this.pageContent);
157+
return {
158+
...meta,
159+
properties,
160+
children,
161+
};
109162
},
110163
},
111164
async run({ $ }) {
165+
const MAX_BLOCKS = 100;
112166
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,
167+
const {
168+
children, ...page
169+
} = this.buildPage(parentPage);
170+
const data = this.templateId
171+
? {
172+
template: {
173+
type: this.templateType,
174+
template_id: this.templateId,
175+
},
176+
}
177+
: {
178+
children: children.slice(0, MAX_BLOCKS),
179+
};
180+
const response = await this.notion.createPage({
181+
...data,
182+
...page,
183+
parent: {
184+
data_source_id: this.parentDataSource,
185+
},
120186
});
187+
let remainingBlocks = children.slice(MAX_BLOCKS);
188+
while (remainingBlocks.length > 0) {
189+
await this.notion.appendBlock(response.id, remainingBlocks.slice(0, MAX_BLOCKS));
190+
remainingBlocks = remainingBlocks.slice(MAX_BLOCKS);
191+
}
121192
$.export("$summary", "Created page successfully");
122193
return response;
123194
},

components/notion/notion.app.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ export default {
275275
...params,
276276
});
277277
},
278+
async listTemplates(params = {}) {
279+
return this._getNotionClient().dataSources.listTemplates(params);
280+
},
278281
async retrieveDataSource(dataSourceId) {
279282
return this._getNotionClient().dataSources.retrieve({
280283
data_source_id: dataSourceId,

components/notion/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/notion",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Pipedream Notion Components",
55
"main": "notion.app.mjs",
66
"keywords": [
@@ -10,7 +10,7 @@
1010
"homepage": "https://pipedream.com/apps/notion",
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"dependencies": {
13-
"@notionhq/client": "^5.0.0",
13+
"@notionhq/client": "^5.3.0",
1414
"@pipedream/platform": "^3.1.0",
1515
"@tryfabric/martian": "^1.2.4",
1616
"lodash-es": "^4.17.21",

0 commit comments

Comments
 (0)