feat(Notion (Beta) Node): Use resource locator component for database and page parameters (#4340)

* use resource locator component for database -> get (Notion V1/V2)

* getDatabases search function for V1/V2 with url

* updated database get list placeholder

* get database RLC by url - regex support optional workspace domain names

* fixed linting error

* listSearch getDatabases support filter query

* support extractValue in getCurrentNodeParameter for RLC

* RLC for database page create/getAll operation

* RLC for database get operation support "By ID" with optional v param.

* use RLC in append blocks operation

* use RLC in NotionTrigger.nodes.ts

* removed unused loadOptions getDatabases

* support database RLC in createPage/createDbPage operation

* page create operation use RLC for parent page param

* page archive operation use RLC for page param

* removed unused imports

* fixed missing extractPageId in NotionV1.node.ts

* database page get operation use RLC for page param

* database page update operation use RLC for page param

* block getAll children operation use RLC for page param

* block append operation use RLC for block param

* support databaseId with optional '-' characters

* support blockId with optional '-' characters

* support pageId with optional '-' characters

* improved RLC descriptions and hints

* NotionTrigger node support databseId with optional '-' characters

* stricter RLC by ID regex rules for uuids

* stricter RLC by URL regex rules for uuids

* stricter RLC by ID regex rules for uuids (support max length)

* RLC regex from URL allow both http and https

* RLC by ID only allow uuid v4 with optional dash

* removed RLC from URL hint "Use Notion's copy link..."

* RLC from URL only allow uuid v4

* DB Status Column: Support Simplify Properties

* Notion Credentials: Support custom Notion-Version header

Use latest Notion-Version 2022-02-22 if not set

* DB Status Column: Support DB Page Create/Update

* DB Status Column: Support DB Page GetMany Filters

* removed unused paginationToken args

* Database Get: RLC by URL improve validation error message
This commit is contained in:
Marcus
2022-11-11 13:37:52 +01:00
committed by GitHub
parent c7133ecd3f
commit 277b6b73c3
13 changed files with 775 additions and 202 deletions

View File

@@ -15,6 +15,7 @@ import {
import {
downloadFiles,
extractDatabaseId,
extractDatabaseMentionRLC,
extractPageId,
formatBlocks,
formatTitle,
@@ -31,6 +32,7 @@ import {
import moment from 'moment-timezone';
import { versionDescription } from './VersionDescription';
import { getDatabases } from '../SearchFunctions';
export class NotionV2 implements INodeType {
description: INodeTypeDescription;
@@ -43,40 +45,13 @@ export class NotionV2 implements INodeType {
}
methods = {
listSearch: {
getDatabases,
},
loadOptions: {
async getDatabases(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const body: IDataObject = {
page_size: 100,
filter: { property: 'object', value: 'database' },
};
const databases = await notionApiRequestAllItems.call(
this,
'results',
'POST',
`/search`,
body,
);
for (const database of databases) {
returnData.push({
name: database.title[0]?.plain_text || database.id,
value: database.id,
});
}
returnData.sort((a, b) => {
if (a.name.toLocaleLowerCase() < b.name.toLocaleLowerCase()) {
return -1;
}
if (a.name.toLocaleLowerCase() > b.name.toLocaleLowerCase()) {
return 1;
}
return 0;
});
return returnData;
},
async getDatabaseProperties(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const databaseId = this.getCurrentNodeParameter('databaseId') as string;
const databaseId = this.getCurrentNodeParameter('databaseId', { extractValue: true }) as string;
const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
for (const key of Object.keys(properties)) {
//remove parameters that cannot be set from the API.
@@ -109,7 +84,7 @@ export class NotionV2 implements INodeType {
},
async getFilterProperties(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const databaseId = this.getCurrentNodeParameter('databaseId') as string;
const databaseId = this.getCurrentNodeParameter('databaseId', { extractValue: true }) as string;
const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
for (const key of Object.keys(properties)) {
returnData.push({
@@ -133,18 +108,18 @@ export class NotionV2 implements INodeType {
},
async getPropertySelectValues(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const [name, type] = (this.getCurrentNodeParameter('&key') as string).split('|');
const databaseId = this.getCurrentNodeParameter('databaseId') as string;
const databaseId = this.getCurrentNodeParameter('databaseId', { extractValue: true }) as string;
const resource = this.getCurrentNodeParameter('resource') as string;
const operation = this.getCurrentNodeParameter('operation') as string;
const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
if (resource === 'databasePage') {
if (['multi_select', 'select'].includes(type) && operation === 'getAll') {
if (['multi_select', 'select', 'status'].includes(type) && operation === 'getAll') {
return properties[name][type].options.map((option: IDataObject) => ({
name: option.name,
value: option.name,
}));
} else if (
['multi_select', 'select'].includes(type) &&
['multi_select', 'select', 'status'].includes(type) &&
['create', 'update'].includes(operation)
) {
return properties[name][type].options.map((option: IDataObject) => ({
@@ -173,7 +148,7 @@ export class NotionV2 implements INodeType {
},
async getDatabaseIdFromPage(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const pageId = extractPageId(this.getCurrentNodeParameter('pageId') as string);
const pageId = extractPageId(this.getCurrentNodeParameter('pageId', { extractValue: true }) as string);
const {
parent: { database_id: databaseId },
} = await notionApiRequest.call(this, 'GET', `/pages/${pageId}`);
@@ -211,7 +186,7 @@ export class NotionV2 implements INodeType {
async getDatabaseOptionsFromPage(
this: ILoadOptionsFunctions,
): Promise<INodePropertyOptions[]> {
const pageId = extractPageId(this.getCurrentNodeParameter('pageId') as string);
const pageId = extractPageId(this.getCurrentNodeParameter('pageId', { extractValue: true }) as string);
const [name, type] = (this.getCurrentNodeParameter('&key') as string).split('|');
const {
parent: { database_id: databaseId },
@@ -260,11 +235,11 @@ export class NotionV2 implements INodeType {
if (resource === 'block') {
if (operation === 'append') {
for (let i = 0; i < length; i++) {
const blockId = extractPageId(this.getNodeParameter('blockId', i) as string);
const blockId = extractPageId(this.getNodeParameter('blockId', i, '', { extractValue: true }) as string);
const blockValues = this.getNodeParameter('blockUi.blockValues', i, []) as IDataObject[];
extractDatabaseMentionRLC(blockValues);
const body: IDataObject = {
children: formatBlocks(
this.getNodeParameter('blockUi.blockValues', i, []) as IDataObject[],
),
children: formatBlocks(blockValues),
};
const block = await notionApiRequest.call(
this,
@@ -283,7 +258,7 @@ export class NotionV2 implements INodeType {
if (operation === 'getAll') {
for (let i = 0; i < length; i++) {
const blockId = extractPageId(this.getNodeParameter('blockId', i) as string);
const blockId = extractPageId(this.getNodeParameter('blockId', i, '', { extractValue: true }) as string);
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll) {
@@ -325,7 +300,7 @@ export class NotionV2 implements INodeType {
if (operation === 'get') {
const simple = this.getNodeParameter('simple', 0) as boolean;
for (let i = 0; i < length; i++) {
const databaseId = extractDatabaseId(this.getNodeParameter('databaseId', i) as string);
const databaseId = extractDatabaseId(this.getNodeParameter('databaseId', i, '', { extractValue: true }) as string);
responseData = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
if (simple === true) {
responseData = simplifyObjects(responseData, download)[0];
@@ -426,7 +401,7 @@ export class NotionV2 implements INodeType {
if (resource === 'databasePage') {
if (operation === 'create') {
const databaseId = this.getNodeParameter('databaseId', 0) as string;
const databaseId = this.getNodeParameter('databaseId', 0, '', { extractValue: true }) as string;
const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
let titleKey = '';
for (const key of Object.keys(properties)) {
@@ -453,7 +428,7 @@ export class NotionV2 implements INodeType {
],
};
}
body.parent['database_id'] = this.getNodeParameter('databaseId', i) as string;
body.parent['database_id'] = this.getNodeParameter('databaseId', i, '', { extractValue: true }) as string;
const properties = this.getNodeParameter(
'propertiesUi.propertyValues',
i,
@@ -465,9 +440,9 @@ export class NotionV2 implements INodeType {
mapProperties(properties, timezone, 2) as IDataObject,
);
}
body.children = formatBlocks(
this.getNodeParameter('blockUi.blockValues', i, []) as IDataObject[],
);
const blockValues = this.getNodeParameter('blockUi.blockValues', i, []) as IDataObject[];
extractDatabaseMentionRLC(blockValues);
body.children = formatBlocks(blockValues);
responseData = await notionApiRequest.call(this, 'POST', '/pages', body);
if (simple === true) {
responseData = simplifyObjects(responseData);
@@ -483,7 +458,7 @@ export class NotionV2 implements INodeType {
if (operation === 'get') {
for (let i = 0; i < length; i++) {
const pageId = extractPageId(this.getNodeParameter('pageId', i) as string);
const pageId = extractPageId(this.getNodeParameter('pageId', i, '', { extractValue: true }) as string);
const simple = this.getNodeParameter('simple', i) as boolean;
responseData = await notionApiRequest.call(this, 'GET', `/pages/${pageId}`);
if (simple === true) {
@@ -502,7 +477,7 @@ export class NotionV2 implements INodeType {
for (let i = 0; i < length; i++) {
download = this.getNodeParameter('options.downloadFiles', 0, false) as boolean;
const simple = this.getNodeParameter('simple', 0) as boolean;
const databaseId = this.getNodeParameter('databaseId', i) as string;
const databaseId = this.getNodeParameter('databaseId', i, '', { extractValue: true }) as string;
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const filterType = this.getNodeParameter('filterType', 0) as string;
const conditions = this.getNodeParameter('filters.conditions', i, []) as IDataObject[];
@@ -577,7 +552,7 @@ export class NotionV2 implements INodeType {
if (operation === 'update') {
for (let i = 0; i < length; i++) {
const pageId = extractPageId(this.getNodeParameter('pageId', i) as string);
const pageId = extractPageId(this.getNodeParameter('pageId', i, '', { extractValue: true }) as string);
const simple = this.getNodeParameter('simple', i) as boolean;
const properties = this.getNodeParameter(
'propertiesUi.propertyValues',
@@ -641,7 +616,7 @@ export class NotionV2 implements INodeType {
if (resource === 'page') {
if (operation === 'archive') {
for (let i = 0; i < length; i++) {
const pageId = extractPageId(this.getNodeParameter('pageId', i) as string);
const pageId = extractPageId(this.getNodeParameter('pageId', i, '', { extractValue: true }) as string);
const simple = this.getNodeParameter('simple', i) as boolean;
responseData = await notionApiRequest.call(this, 'PATCH', `/pages/${pageId}`, {
archived: true,
@@ -666,11 +641,11 @@ export class NotionV2 implements INodeType {
parent: {},
properties: {},
};
body.parent['page_id'] = extractPageId(this.getNodeParameter('pageId', i) as string);
body.parent['page_id'] = extractPageId(this.getNodeParameter('pageId', i, '', { extractValue: true }) as string);
body.properties = formatTitle(this.getNodeParameter('title', i) as string);
body.children = formatBlocks(
this.getNodeParameter('blockUi.blockValues', i, []) as IDataObject[],
);
const blockValues = this.getNodeParameter('blockUi.blockValues', i, []) as IDataObject[];
extractDatabaseMentionRLC(blockValues);
body.children = formatBlocks(blockValues);
responseData = await notionApiRequest.call(this, 'POST', '/pages', body);
if (simple === true) {
responseData = simplifyObjects(responseData, download);