Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/* eslint-disable no-case-declarations */
import pick from "lodash-es/pick.js";
import NOTION_ICONS from "../../common/notion-icons.mjs";
import utils from "../../common/utils.mjs";
import notion from "../../notion.app.mjs";
import base from "../common/base-page-builder.mjs";
import pick from "lodash-es/pick.js";

export default {
...base,
key: "notion-create-page-from-database",
name: "Create Page from Data Source",
description: "Create a page from a data source. [See the documentation](https://developers.notion.com/reference/post-page)",
version: "1.1.0",
version: "2.0.0",
annotations: {
destructiveHint: false,
openWorldHint: true,
Expand All @@ -25,13 +26,26 @@ export default {
],
label: "Parent Data Source ID",
description: "Select a parent data source or provide a data source ID",
reloadProps: true,
},
Name: {
templateType: {
type: "string",
label: "Name",
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.",
optional: true,
label: "Template Type",
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.",
options: [
{
label: "No template. Provided children and properties are immediately applied.",
value: "none",
},
{
label: "Applies the data source's default template to the newly created page. `children` cannot be specified in the create page request.",
value: "default",
},
{
label: "Indicates which exact template to apply to the newly created page. children cannot be specified in the create page request.",
value: "template_id",
},
],
reloadProps: true,
},
propertyTypes: {
propDefinition: [
Expand Down Expand Up @@ -64,11 +78,6 @@ export default {
description: "Cover [External URL](https://developers.notion.com/reference/file-object#external-file-objects)",
optional: true,
},
alert: {
type: "alert",
alertType: "info",
content: "This action will create an empty page by default. To add content, use the `Page Content` prop below.",
},
pageContent: {
propDefinition: [
notion,
Expand All @@ -77,11 +86,61 @@ export default {
},
},
async additionalProps() {
const { properties } = await this.notion.retrieveDataSource(this.parentDataSource);
const selectedProperties = pick(properties, this.propertyTypes);
return this.buildAdditionalProps({
properties: selectedProperties,
});
switch (this.templateType) {
case "none":
const { properties } = await this.notion.retrieveDataSource(this.parentDataSource);
const selectedProperties = pick(properties, this.propertyTypes);
return {
alert: {
type: "alert",
alertType: "info",
content: "This action will create an empty page by default. To add content, use the `Page Content` prop below.",
},
...this.buildAdditionalProps({
properties: selectedProperties,
}),
};
case "default":
return {
alert: {
type: "alert",
alertType: "info",
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.",
},
};
case "template_id":
return {
templateId: {
type: "string",
label: "Template ID",
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.",
options: async({ prevContext }) => {
const {
templates, next_cursor: nCursor,
} = await this.notion.listTemplates({
data_source_id: this.parentDataSource,
start_cursor: prevContext?.nCursor,
});
return {
options: templates.map(({
name: label, id: value,
}) => ({
label,
value,
})),
context: {
nCursor,
},
};
},
},
alert: {
type: "alert",
alertType: "info",
content: "This action will create a page using the selected template. Using the `Page Content` prop below will `not` apply to the page.",
},
};
}
},
methods: {
...base.methods,
Expand All @@ -91,33 +150,45 @@ export default {
* @returns the constructed page in Notion format
*/
buildPage(parentDataSource) {
const meta = this.buildDataSourceMeta(parentDataSource);
this.properties = utils.parseObject(this.properties);
const properties = this.buildPageProperties(parentDataSource.properties);

const propertiesArray = [];
for (const property of Object.values(parentDataSource.properties)) {
if (properties[property.id]) {
propertiesArray.push({
label: property.name,
type: property.type,
value: this[property.name] || this.properties[property.name],
});
}
}

return propertiesArray;
const children = this.createBlocks(this.pageContent);
return {
...meta,
properties,
children,
};
},
},
async run({ $ }) {
const MAX_BLOCKS = 100;
const parentPage = await this.notion.retrieveDataSource(this.parentDataSource);
const properties = await this.buildPage(parentPage);
const response = await this.buildPageFromDataSource({
pageContent: this.pageContent,
parentDataSourceId: this.parentDataSource,
properties,
icon: this.icon,
cover: this.cover,
const {
children, ...page
} = this.buildPage(parentPage);
const data = this.templateId
? {
template: {
type: this.templateType,
template_id: this.templateId,
},
}
: {
children: children.slice(0, MAX_BLOCKS),
};
const response = await this.notion.createPage({
...data,
...page,
parent: {
data_source_id: this.parentDataSource,
},
});
let remainingBlocks = children.slice(MAX_BLOCKS);
while (remainingBlocks.length > 0) {
await this.notion.appendBlock(response.id, remainingBlocks.slice(0, MAX_BLOCKS));
remainingBlocks = remainingBlocks.slice(MAX_BLOCKS);
}
$.export("$summary", "Created page successfully");
return response;
},
Expand Down
3 changes: 3 additions & 0 deletions components/notion/notion.app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ export default {
...params,
});
},
async listTemplates(params = {}) {
return this._getNotionClient().dataSources.listTemplates(params);
},
async retrieveDataSource(dataSourceId) {
return this._getNotionClient().dataSources.retrieve({
data_source_id: dataSourceId,
Expand Down
4 changes: 2 additions & 2 deletions components/notion/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/notion",
"version": "1.0.5",
"version": "1.0.6",
"description": "Pipedream Notion Components",
"main": "notion.app.mjs",
"keywords": [
Expand All @@ -10,7 +10,7 @@
"homepage": "https://pipedream.com/apps/notion",
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"dependencies": {
"@notionhq/client": "^5.0.0",
"@notionhq/client": "^5.3.0",
"@pipedream/platform": "^3.1.0",
"@tryfabric/martian": "^1.2.4",
"lodash-es": "^4.17.21",
Expand Down
Loading
Loading