pull:初次提交
This commit is contained in:
776
n8n-n8n-1.109.2/packages/nodes-base/nodes/Notion/v2/NotionV2.node.ts
Executable file
776
n8n-n8n-1.109.2/packages/nodes-base/nodes/Notion/v2/NotionV2.node.ts
Executable file
@@ -0,0 +1,776 @@
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
IDataObject,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeBaseDescription,
|
||||
INodeTypeDescription,
|
||||
} from 'n8n-workflow';
|
||||
import { jsonParse, NodeApiError } from 'n8n-workflow';
|
||||
|
||||
import { loadOptions } from './methods';
|
||||
import { versionDescription } from './VersionDescription';
|
||||
import type { SortData, FileRecord } from '../shared/GenericFunctions';
|
||||
import {
|
||||
downloadFiles,
|
||||
extractBlockId,
|
||||
extractDatabaseId,
|
||||
extractDatabaseMentionRLC,
|
||||
getPageId,
|
||||
formatBlocks,
|
||||
formatTitle,
|
||||
mapFilters,
|
||||
mapProperties,
|
||||
mapSorting,
|
||||
notionApiRequest,
|
||||
notionApiRequestAllItems,
|
||||
notionApiRequestGetBlockChildrens,
|
||||
prepareNotionError,
|
||||
simplifyBlocksOutput,
|
||||
simplifyObjects,
|
||||
validateJSON,
|
||||
} from '../shared/GenericFunctions';
|
||||
import { listSearch } from '../shared/methods';
|
||||
|
||||
export class NotionV2 implements INodeType {
|
||||
description: INodeTypeDescription;
|
||||
|
||||
constructor(baseDescription: INodeTypeBaseDescription) {
|
||||
this.description = {
|
||||
...baseDescription,
|
||||
...versionDescription,
|
||||
};
|
||||
}
|
||||
|
||||
methods = { listSearch, loadOptions };
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
const nodeVersion = this.getNode().typeVersion;
|
||||
const resource = this.getNodeParameter('resource', 0);
|
||||
const operation = this.getNodeParameter('operation', 0);
|
||||
|
||||
const itemsLength = items.length;
|
||||
const timezone = this.getTimezone();
|
||||
const qs: IDataObject = {};
|
||||
|
||||
let returnData: INodeExecutionData[] = [];
|
||||
let responseData;
|
||||
let download = false;
|
||||
|
||||
if (resource === 'block') {
|
||||
if (operation === 'append') {
|
||||
for (let i = 0; i < itemsLength; i++) {
|
||||
try {
|
||||
const blockId = extractBlockId.call(this, nodeVersion, i);
|
||||
const blockValues = this.getNodeParameter(
|
||||
'blockUi.blockValues',
|
||||
i,
|
||||
[],
|
||||
) as IDataObject[];
|
||||
extractDatabaseMentionRLC(blockValues);
|
||||
const body: IDataObject = {
|
||||
children: formatBlocks(blockValues),
|
||||
};
|
||||
const block = await notionApiRequest.call(
|
||||
this,
|
||||
'PATCH',
|
||||
`/blocks/${blockId}/children`,
|
||||
body,
|
||||
);
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(block as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
returnData = returnData.concat(executionData);
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({
|
||||
json: { error: error.message },
|
||||
pairedItem: { item: i },
|
||||
});
|
||||
} else {
|
||||
throw prepareNotionError(this.getNode(), error, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (operation === 'getAll') {
|
||||
for (let i = 0; i < itemsLength; i++) {
|
||||
try {
|
||||
const blockId = extractBlockId.call(this, nodeVersion, i);
|
||||
const returnAll = this.getNodeParameter('returnAll', i);
|
||||
const fetchNestedBlocks = this.getNodeParameter('fetchNestedBlocks', i) as boolean;
|
||||
|
||||
if (returnAll) {
|
||||
responseData = await notionApiRequestAllItems.call(
|
||||
this,
|
||||
'results',
|
||||
'GET',
|
||||
`/blocks/${blockId}/children`,
|
||||
{},
|
||||
);
|
||||
|
||||
if (fetchNestedBlocks) {
|
||||
responseData = await notionApiRequestGetBlockChildrens.call(this, responseData);
|
||||
}
|
||||
} else {
|
||||
const limit = this.getNodeParameter('limit', i);
|
||||
qs.page_size = limit;
|
||||
responseData = await notionApiRequest.call(
|
||||
this,
|
||||
'GET',
|
||||
`/blocks/${blockId}/children`,
|
||||
{},
|
||||
qs,
|
||||
);
|
||||
const results = responseData.results;
|
||||
|
||||
if (fetchNestedBlocks) {
|
||||
responseData = await notionApiRequestGetBlockChildrens.call(
|
||||
this,
|
||||
results,
|
||||
[],
|
||||
limit,
|
||||
);
|
||||
} else {
|
||||
responseData = results;
|
||||
}
|
||||
}
|
||||
|
||||
responseData = responseData.map((_data: IDataObject) => ({
|
||||
object: _data.object,
|
||||
parent_id: blockId,
|
||||
..._data,
|
||||
}));
|
||||
|
||||
if (nodeVersion > 2) {
|
||||
const simplifyOutput = this.getNodeParameter('simplifyOutput', i) as boolean;
|
||||
|
||||
if (simplifyOutput) {
|
||||
responseData = simplifyBlocksOutput(responseData, blockId);
|
||||
}
|
||||
}
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(responseData as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
returnData = returnData.concat(executionData);
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({
|
||||
json: { error: error.message },
|
||||
pairedItem: { item: i },
|
||||
});
|
||||
} else {
|
||||
throw prepareNotionError(this.getNode(), error, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (resource === 'database') {
|
||||
if (operation === 'get') {
|
||||
const simple = this.getNodeParameter('simple', 0) as boolean;
|
||||
for (let i = 0; i < itemsLength; i++) {
|
||||
try {
|
||||
const databaseId = extractDatabaseId(
|
||||
this.getNodeParameter('databaseId', i, '', { extractValue: true }) as string,
|
||||
);
|
||||
responseData = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
|
||||
if (simple) {
|
||||
responseData = simplifyObjects(responseData, download)[0];
|
||||
}
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(responseData as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
returnData = returnData.concat(executionData);
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({
|
||||
json: { error: error.message },
|
||||
pairedItem: { item: i },
|
||||
});
|
||||
} else {
|
||||
throw prepareNotionError(this.getNode(), error, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (operation === 'getAll') {
|
||||
const simple = this.getNodeParameter('simple', 0) as boolean;
|
||||
for (let i = 0; i < itemsLength; i++) {
|
||||
try {
|
||||
const body: IDataObject = {
|
||||
filter: { property: 'object', value: 'database' },
|
||||
};
|
||||
const returnAll = this.getNodeParameter('returnAll', i);
|
||||
if (returnAll) {
|
||||
responseData = await notionApiRequestAllItems.call(
|
||||
this,
|
||||
'results',
|
||||
'POST',
|
||||
'/search',
|
||||
body,
|
||||
);
|
||||
} else {
|
||||
body.page_size = this.getNodeParameter('limit', i);
|
||||
responseData = await notionApiRequest.call(this, 'POST', '/search', body);
|
||||
responseData = responseData.results;
|
||||
}
|
||||
if (simple) {
|
||||
responseData = simplifyObjects(responseData, download);
|
||||
}
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(responseData as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
returnData = returnData.concat(executionData);
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({
|
||||
json: { error: error.message },
|
||||
pairedItem: { item: i },
|
||||
});
|
||||
} else {
|
||||
throw prepareNotionError(this.getNode(), error, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (operation === 'search') {
|
||||
for (let i = 0; i < itemsLength; i++) {
|
||||
try {
|
||||
const text = this.getNodeParameter('text', i) as string;
|
||||
const options = this.getNodeParameter('options', i);
|
||||
const returnAll = this.getNodeParameter('returnAll', i);
|
||||
const simple = this.getNodeParameter('simple', i) as boolean;
|
||||
const body: IDataObject = {
|
||||
filter: {
|
||||
property: 'object',
|
||||
value: 'database',
|
||||
},
|
||||
};
|
||||
|
||||
if (text) {
|
||||
body.query = text;
|
||||
}
|
||||
if (options.sort) {
|
||||
const sort = ((options.sort as IDataObject)?.sortValue as IDataObject) || {};
|
||||
body.sort = sort;
|
||||
}
|
||||
if (returnAll) {
|
||||
responseData = await notionApiRequestAllItems.call(
|
||||
this,
|
||||
'results',
|
||||
'POST',
|
||||
'/search',
|
||||
body,
|
||||
);
|
||||
} else {
|
||||
qs.limit = this.getNodeParameter('limit', i);
|
||||
responseData = await notionApiRequestAllItems.call(
|
||||
this,
|
||||
'results',
|
||||
'POST',
|
||||
'/search',
|
||||
body,
|
||||
);
|
||||
responseData = responseData.splice(0, qs.limit);
|
||||
}
|
||||
|
||||
if (simple) {
|
||||
responseData = simplifyObjects(responseData, download);
|
||||
}
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(responseData as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
returnData = returnData.concat(executionData);
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({
|
||||
json: { error: error.message },
|
||||
pairedItem: { item: i },
|
||||
});
|
||||
} else {
|
||||
throw prepareNotionError(this.getNode(), error, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (resource === 'databasePage') {
|
||||
if (operation === 'create') {
|
||||
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 as IDataObject)) {
|
||||
if (properties[key].type === 'title') {
|
||||
titleKey = key;
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < itemsLength; i++) {
|
||||
try {
|
||||
const title = this.getNodeParameter('title', i) as string;
|
||||
const simple = this.getNodeParameter('simple', i) as boolean;
|
||||
|
||||
const body: { [key: string]: any } = {
|
||||
parent: {},
|
||||
properties: {},
|
||||
};
|
||||
if (title !== '') {
|
||||
body.properties[titleKey] = {
|
||||
title: [
|
||||
{
|
||||
text: {
|
||||
content: title,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
body.parent.database_id = this.getNodeParameter('databaseId', i, '', {
|
||||
extractValue: true,
|
||||
}) as string;
|
||||
const propertiesValues = this.getNodeParameter(
|
||||
'propertiesUi.propertyValues',
|
||||
i,
|
||||
[],
|
||||
) as IDataObject[];
|
||||
if (propertiesValues.length !== 0) {
|
||||
body.properties = Object.assign(
|
||||
body.properties,
|
||||
mapProperties.call(this, propertiesValues, timezone, 2) as IDataObject,
|
||||
);
|
||||
}
|
||||
const blockValues = this.getNodeParameter(
|
||||
'blockUi.blockValues',
|
||||
i,
|
||||
[],
|
||||
) as IDataObject[];
|
||||
extractDatabaseMentionRLC(blockValues);
|
||||
body.children = formatBlocks(blockValues);
|
||||
|
||||
const options = this.getNodeParameter('options', i);
|
||||
if (options.icon) {
|
||||
if (options.iconType && options.iconType === 'file') {
|
||||
body.icon = { external: { url: options.icon } };
|
||||
} else {
|
||||
body.icon = { emoji: options.icon };
|
||||
}
|
||||
}
|
||||
|
||||
responseData = await notionApiRequest.call(this, 'POST', '/pages', body);
|
||||
if (simple) {
|
||||
responseData = simplifyObjects(responseData);
|
||||
}
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(responseData as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
returnData = returnData.concat(executionData);
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({
|
||||
json: { error: error.message },
|
||||
pairedItem: { item: i },
|
||||
});
|
||||
} else {
|
||||
throw prepareNotionError(this.getNode(), error, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (operation === 'get') {
|
||||
for (let i = 0; i < itemsLength; i++) {
|
||||
try {
|
||||
const pageId = getPageId.call(this, i);
|
||||
|
||||
const simple = this.getNodeParameter('simple', i) as boolean;
|
||||
responseData = await notionApiRequest.call(this, 'GET', `/pages/${pageId}`);
|
||||
if (simple) {
|
||||
responseData = simplifyObjects(responseData, download);
|
||||
}
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(responseData as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
returnData = returnData.concat(executionData);
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({
|
||||
json: { error: error.message },
|
||||
pairedItem: { item: i },
|
||||
});
|
||||
} else {
|
||||
throw prepareNotionError(this.getNode(), error, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (operation === 'getAll') {
|
||||
for (let i = 0; i < itemsLength; i++) {
|
||||
try {
|
||||
download = this.getNodeParameter('options.downloadFiles', 0, false) as boolean;
|
||||
const simple = this.getNodeParameter('simple', 0) as boolean;
|
||||
const databaseId = this.getNodeParameter('databaseId', i, '', {
|
||||
extractValue: true,
|
||||
}) as string;
|
||||
const returnAll = this.getNodeParameter('returnAll', i);
|
||||
const filterType = this.getNodeParameter('filterType', 0) as string;
|
||||
const conditions = this.getNodeParameter('filters.conditions', i, []) as IDataObject[];
|
||||
const sort = this.getNodeParameter('options.sort.sortValue', i, []) as IDataObject[];
|
||||
const body: IDataObject = {
|
||||
filter: {},
|
||||
};
|
||||
|
||||
if (filterType === 'manual') {
|
||||
const matchType = this.getNodeParameter('matchType', 0) as string;
|
||||
if (matchType === 'anyFilter') {
|
||||
Object.assign(body.filter!, {
|
||||
or: conditions.map((data) => mapFilters([data], timezone)),
|
||||
});
|
||||
} else if (matchType === 'allFilters') {
|
||||
Object.assign(body.filter!, {
|
||||
and: conditions.map((data) => mapFilters([data], timezone)),
|
||||
});
|
||||
}
|
||||
} else if (filterType === 'json') {
|
||||
const filterJson = this.getNodeParameter('filterJson', i) as string;
|
||||
if (validateJSON(filterJson) !== undefined) {
|
||||
body.filter = jsonParse(filterJson);
|
||||
} else {
|
||||
throw new NodeApiError(
|
||||
this.getNode(),
|
||||
{
|
||||
message: 'Filters (JSON) must be a valid json',
|
||||
},
|
||||
{ itemIndex: i },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!Object.keys(body.filter as IDataObject).length) {
|
||||
delete body.filter;
|
||||
}
|
||||
if (sort) {
|
||||
body.sorts = mapSorting(sort as SortData[]);
|
||||
}
|
||||
if (returnAll) {
|
||||
responseData = await notionApiRequestAllItems.call(
|
||||
this,
|
||||
'results',
|
||||
'POST',
|
||||
`/databases/${databaseId}/query`,
|
||||
body,
|
||||
{},
|
||||
);
|
||||
} else {
|
||||
body.page_size = this.getNodeParameter('limit', i);
|
||||
responseData = await notionApiRequest.call(
|
||||
this,
|
||||
'POST',
|
||||
`/databases/${databaseId}/query`,
|
||||
body,
|
||||
qs,
|
||||
);
|
||||
responseData = responseData.results;
|
||||
}
|
||||
if (download) {
|
||||
responseData = await downloadFiles.call(this, responseData as FileRecord[], [
|
||||
{ item: i },
|
||||
]);
|
||||
}
|
||||
if (simple) {
|
||||
responseData = simplifyObjects(responseData, download);
|
||||
}
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(responseData as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
returnData = returnData.concat(executionData);
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({
|
||||
json: { error: error.message },
|
||||
pairedItem: { item: i },
|
||||
});
|
||||
} else {
|
||||
throw prepareNotionError(this.getNode(), error, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (operation === 'update') {
|
||||
for (let i = 0; i < itemsLength; i++) {
|
||||
try {
|
||||
const pageId = getPageId.call(this, i);
|
||||
const simple = this.getNodeParameter('simple', i) as boolean;
|
||||
const properties = this.getNodeParameter(
|
||||
'propertiesUi.propertyValues',
|
||||
i,
|
||||
[],
|
||||
) as IDataObject[];
|
||||
|
||||
const body: { [key: string]: any } = {
|
||||
properties: {},
|
||||
};
|
||||
if (properties.length !== 0) {
|
||||
body.properties = mapProperties.call(this, properties, timezone, 2) as IDataObject;
|
||||
}
|
||||
|
||||
const options = this.getNodeParameter('options', i);
|
||||
if (options.icon) {
|
||||
if (options.iconType && options.iconType === 'file') {
|
||||
body.icon = { type: 'external', external: { url: options.icon } };
|
||||
} else {
|
||||
body.icon = { type: 'emoji', emoji: options.icon };
|
||||
}
|
||||
}
|
||||
|
||||
responseData = await notionApiRequest.call(this, 'PATCH', `/pages/${pageId}`, body);
|
||||
if (simple) {
|
||||
responseData = simplifyObjects(responseData, false);
|
||||
}
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(responseData as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
returnData = returnData.concat(executionData);
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({
|
||||
json: { error: error.message },
|
||||
pairedItem: { item: i },
|
||||
});
|
||||
} else {
|
||||
throw prepareNotionError(this.getNode(), error, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (resource === 'user') {
|
||||
if (operation === 'get') {
|
||||
for (let i = 0; i < itemsLength; i++) {
|
||||
try {
|
||||
const userId = this.getNodeParameter('userId', i) as string;
|
||||
responseData = await notionApiRequest.call(this, 'GET', `/users/${userId}`);
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(responseData as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
returnData = returnData.concat(executionData);
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({
|
||||
json: { error: error.message },
|
||||
pairedItem: { item: i },
|
||||
});
|
||||
} else {
|
||||
throw prepareNotionError(this.getNode(), error, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (operation === 'getAll') {
|
||||
for (let i = 0; i < itemsLength; i++) {
|
||||
try {
|
||||
const returnAll = this.getNodeParameter('returnAll', i);
|
||||
if (returnAll) {
|
||||
responseData = await notionApiRequestAllItems.call(this, 'results', 'GET', '/users');
|
||||
} else {
|
||||
qs.limit = this.getNodeParameter('limit', i);
|
||||
responseData = await notionApiRequestAllItems.call(this, 'results', 'GET', '/users');
|
||||
responseData = responseData.splice(0, qs.limit);
|
||||
}
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(responseData as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
returnData = returnData.concat(executionData);
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({
|
||||
json: { error: error.message },
|
||||
pairedItem: { item: i },
|
||||
});
|
||||
} else {
|
||||
throw prepareNotionError(this.getNode(), error, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (resource === 'page') {
|
||||
if (operation === 'archive') {
|
||||
for (let i = 0; i < itemsLength; i++) {
|
||||
try {
|
||||
const pageId = getPageId.call(this, i);
|
||||
const simple = this.getNodeParameter('simple', i) as boolean;
|
||||
responseData = await notionApiRequest.call(this, 'PATCH', `/pages/${pageId}`, {
|
||||
archived: true,
|
||||
});
|
||||
if (simple) {
|
||||
responseData = simplifyObjects(responseData, download);
|
||||
}
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(responseData as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
returnData = returnData.concat(executionData);
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({
|
||||
json: { error: error.message },
|
||||
pairedItem: { item: i },
|
||||
});
|
||||
} else {
|
||||
throw prepareNotionError(this.getNode(), error, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (operation === 'create') {
|
||||
for (let i = 0; i < itemsLength; i++) {
|
||||
try {
|
||||
const simple = this.getNodeParameter('simple', i) as boolean;
|
||||
const body: { [key: string]: any } = {
|
||||
parent: {},
|
||||
properties: {},
|
||||
};
|
||||
body.parent.page_id = getPageId.call(this, i);
|
||||
body.properties = formatTitle(this.getNodeParameter('title', i) as string);
|
||||
const blockValues = this.getNodeParameter(
|
||||
'blockUi.blockValues',
|
||||
i,
|
||||
[],
|
||||
) as IDataObject[];
|
||||
extractDatabaseMentionRLC(blockValues);
|
||||
body.children = formatBlocks(blockValues);
|
||||
|
||||
const options = this.getNodeParameter('options', i);
|
||||
if (options.icon) {
|
||||
if (options.iconType && options.iconType === 'file') {
|
||||
body.icon = { external: { url: options.icon } };
|
||||
} else {
|
||||
body.icon = { emoji: options.icon };
|
||||
}
|
||||
}
|
||||
|
||||
responseData = await notionApiRequest.call(this, 'POST', '/pages', body);
|
||||
if (simple) {
|
||||
responseData = simplifyObjects(responseData, download);
|
||||
}
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(responseData as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
returnData = returnData.concat(executionData);
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({
|
||||
json: { error: error.message },
|
||||
pairedItem: { item: i },
|
||||
});
|
||||
} else {
|
||||
throw prepareNotionError(this.getNode(), error, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (operation === 'search') {
|
||||
for (let i = 0; i < itemsLength; i++) {
|
||||
try {
|
||||
const text = this.getNodeParameter('text', i) as string;
|
||||
const options = this.getNodeParameter('options', i);
|
||||
const returnAll = this.getNodeParameter('returnAll', i);
|
||||
const simple = this.getNodeParameter('simple', i) as boolean;
|
||||
const body: IDataObject = {};
|
||||
|
||||
if (text) {
|
||||
body.query = text;
|
||||
}
|
||||
if (options.filter) {
|
||||
const filter = ((options.filter as IDataObject)?.filters as IDataObject[]) || [];
|
||||
body.filter = filter;
|
||||
}
|
||||
if (options.sort) {
|
||||
const sort = ((options.sort as IDataObject)?.sortValue as IDataObject) || {};
|
||||
body.sort = sort;
|
||||
}
|
||||
if (returnAll) {
|
||||
responseData = await notionApiRequestAllItems.call(
|
||||
this,
|
||||
'results',
|
||||
'POST',
|
||||
'/search',
|
||||
body,
|
||||
);
|
||||
} else {
|
||||
qs.limit = this.getNodeParameter('limit', i);
|
||||
responseData = await notionApiRequestAllItems.call(
|
||||
this,
|
||||
'results',
|
||||
'POST',
|
||||
'/search',
|
||||
body,
|
||||
);
|
||||
responseData = responseData.splice(0, qs.limit);
|
||||
}
|
||||
|
||||
if (simple) {
|
||||
responseData = simplifyObjects(responseData, download);
|
||||
}
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(responseData as IDataObject),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
returnData = returnData.concat(executionData);
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({
|
||||
json: { error: error.message },
|
||||
pairedItem: { item: i },
|
||||
});
|
||||
} else {
|
||||
throw prepareNotionError(this.getNode(), error, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [returnData];
|
||||
}
|
||||
}
|
||||
122
n8n-n8n-1.109.2/packages/nodes-base/nodes/Notion/v2/VersionDescription.ts
Executable file
122
n8n-n8n-1.109.2/packages/nodes-base/nodes/Notion/v2/VersionDescription.ts
Executable file
@@ -0,0 +1,122 @@
|
||||
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
|
||||
import { NodeConnectionTypes, type INodeTypeDescription } from 'n8n-workflow';
|
||||
|
||||
import { blockFields, blockOperations } from '../shared/descriptions/BlockDescription';
|
||||
import { databaseFields, databaseOperations } from '../shared/descriptions/DatabaseDescription';
|
||||
import {
|
||||
databasePageFields,
|
||||
databasePageOperations,
|
||||
} from '../shared/descriptions/DatabasePageDescription';
|
||||
import { pageFields, pageOperations } from '../shared/descriptions/PageDescription';
|
||||
import { userFields, userOperations } from '../shared/descriptions/UserDescription';
|
||||
|
||||
export const versionDescription: INodeTypeDescription = {
|
||||
displayName: 'Notion',
|
||||
name: 'notion',
|
||||
icon: { light: 'file:notion.svg', dark: 'file:notion.dark.svg' },
|
||||
group: ['output'],
|
||||
version: [2, 2.1, 2.2],
|
||||
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
||||
description: 'Consume Notion API',
|
||||
defaults: {
|
||||
name: 'Notion',
|
||||
},
|
||||
inputs: [NodeConnectionTypes.Main],
|
||||
outputs: [NodeConnectionTypes.Main],
|
||||
usableAsTool: true,
|
||||
credentials: [
|
||||
{
|
||||
name: 'notionApi',
|
||||
required: true,
|
||||
// displayOptions: {
|
||||
// show: {
|
||||
// authentication: [
|
||||
// 'apiKey',
|
||||
// ],
|
||||
// },
|
||||
// },
|
||||
},
|
||||
// {
|
||||
// name: 'notionOAuth2Api',
|
||||
// required: true,
|
||||
// displayOptions: {
|
||||
// show: {
|
||||
// authentication: [
|
||||
// 'oAuth2',
|
||||
// ],
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
],
|
||||
properties: [
|
||||
// {
|
||||
// displayName: 'Authentication',
|
||||
// name: 'authentication',
|
||||
// type: 'options',
|
||||
// options: [
|
||||
// {
|
||||
// name: 'API Key',
|
||||
// value: 'apiKey',
|
||||
// },
|
||||
// {
|
||||
// name: 'OAuth2',
|
||||
// value: 'oAuth2',
|
||||
// },
|
||||
// ],
|
||||
// default: 'apiKey',
|
||||
// description: 'The resource to operate on.',
|
||||
// },
|
||||
{
|
||||
displayName:
|
||||
'In Notion, make sure to <a href="https://www.notion.so/help/add-and-manage-connections-with-the-api" target="_blank">add your connection</a> to the pages you want to access.',
|
||||
name: 'notionNotice',
|
||||
type: 'notice',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: '',
|
||||
name: 'Credentials',
|
||||
type: 'credentials',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Resource',
|
||||
name: 'resource',
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
options: [
|
||||
{
|
||||
name: 'Block',
|
||||
value: 'block',
|
||||
},
|
||||
{
|
||||
name: 'Database',
|
||||
value: 'database',
|
||||
},
|
||||
{
|
||||
name: 'Database Page',
|
||||
value: 'databasePage',
|
||||
},
|
||||
{
|
||||
name: 'Page',
|
||||
value: 'page',
|
||||
},
|
||||
{
|
||||
name: 'User',
|
||||
value: 'user',
|
||||
},
|
||||
],
|
||||
default: 'page',
|
||||
},
|
||||
...blockOperations,
|
||||
...blockFields,
|
||||
...databaseOperations,
|
||||
...databaseFields,
|
||||
...databasePageOperations,
|
||||
...databasePageFields,
|
||||
...pageOperations,
|
||||
...pageFields,
|
||||
...userOperations,
|
||||
...userFields,
|
||||
],
|
||||
};
|
||||
1
n8n-n8n-1.109.2/packages/nodes-base/nodes/Notion/v2/methods/index.ts
Executable file
1
n8n-n8n-1.109.2/packages/nodes-base/nodes/Notion/v2/methods/index.ts
Executable file
@@ -0,0 +1 @@
|
||||
export * as loadOptions from './loadOptions';
|
||||
201
n8n-n8n-1.109.2/packages/nodes-base/nodes/Notion/v2/methods/loadOptions.ts
Executable file
201
n8n-n8n-1.109.2/packages/nodes-base/nodes/Notion/v2/methods/loadOptions.ts
Executable file
@@ -0,0 +1,201 @@
|
||||
import moment from 'moment-timezone';
|
||||
import type { IDataObject, ILoadOptionsFunctions, INodePropertyOptions } from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
extractPageId,
|
||||
getBlockTypesOptions,
|
||||
notionApiRequest,
|
||||
notionApiRequestAllItems,
|
||||
} from '../../shared/GenericFunctions';
|
||||
|
||||
export async function getDatabaseProperties(
|
||||
this: ILoadOptionsFunctions,
|
||||
): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
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 as IDataObject)) {
|
||||
//remove parameters that cannot be set from the API.
|
||||
if (
|
||||
![
|
||||
'created_time',
|
||||
'last_edited_time',
|
||||
'created_by',
|
||||
'last_edited_by',
|
||||
'formula',
|
||||
'rollup',
|
||||
].includes(properties[key].type as string)
|
||||
) {
|
||||
returnData.push({
|
||||
name: `${key}`,
|
||||
value: `${key}|${properties[key].type}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
export async function getFilterProperties(
|
||||
this: ILoadOptionsFunctions,
|
||||
): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
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 as IDataObject)) {
|
||||
returnData.push({
|
||||
name: `${key}`,
|
||||
value: `${key}|${properties[key].type}`,
|
||||
});
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
export async function getBlockTypes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
return getBlockTypesOptions();
|
||||
}
|
||||
|
||||
export async function getPropertySelectValues(
|
||||
this: ILoadOptionsFunctions,
|
||||
): Promise<INodePropertyOptions[]> {
|
||||
const [name, type] = (this.getCurrentNodeParameter('&key') as string).split('|');
|
||||
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', 'status'].includes(type) && operation === 'getAll') {
|
||||
return properties[name][type].options.map((option: IDataObject) => ({
|
||||
name: option.name,
|
||||
value: option.name,
|
||||
}));
|
||||
} else if (
|
||||
['multi_select', 'select', 'status'].includes(type) &&
|
||||
['create', 'update'].includes(operation)
|
||||
) {
|
||||
return properties[name][type].options.map((option: IDataObject) => ({
|
||||
name: option.name,
|
||||
value: option.name,
|
||||
}));
|
||||
}
|
||||
}
|
||||
return properties[name][type].options.map((option: IDataObject) => ({
|
||||
name: option.name,
|
||||
value: option.id,
|
||||
}));
|
||||
}
|
||||
|
||||
export async function getUsers(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const users = await notionApiRequestAllItems.call(this, 'results', 'GET', '/users');
|
||||
for (const user of users) {
|
||||
if (user.type === 'person') {
|
||||
returnData.push({
|
||||
name: user.name,
|
||||
value: user.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
return returnData;
|
||||
}
|
||||
|
||||
export async function getDatabaseIdFromPage(
|
||||
this: ILoadOptionsFunctions,
|
||||
): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const pageId = extractPageId(
|
||||
this.getCurrentNodeParameter('pageId', { extractValue: true }) as string,
|
||||
);
|
||||
const {
|
||||
parent: { database_id: databaseId },
|
||||
} = await notionApiRequest.call(this, 'GET', `/pages/${pageId}`);
|
||||
const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
|
||||
for (const key of Object.keys(properties as IDataObject)) {
|
||||
//remove parameters that cannot be set from the API.
|
||||
if (
|
||||
![
|
||||
'created_time',
|
||||
'last_edited_time',
|
||||
'created_by',
|
||||
'last_edited_by',
|
||||
'formula',
|
||||
'rollup',
|
||||
].includes(properties[key].type as string)
|
||||
) {
|
||||
returnData.push({
|
||||
name: `${key}`,
|
||||
value: `${key}|${properties[key].type}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
export async function getDatabaseOptionsFromPage(
|
||||
this: ILoadOptionsFunctions,
|
||||
): Promise<INodePropertyOptions[]> {
|
||||
const pageId = extractPageId(
|
||||
this.getCurrentNodeParameter('pageId', { extractValue: true }) as string,
|
||||
);
|
||||
const [name, type] = (this.getCurrentNodeParameter('&key') as string).split('|');
|
||||
const {
|
||||
parent: { database_id: databaseId },
|
||||
} = await notionApiRequest.call(this, 'GET', `/pages/${pageId}`);
|
||||
const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
|
||||
return properties[name][type].options.map((option: IDataObject) => ({
|
||||
name: option.name,
|
||||
value: option.name,
|
||||
}));
|
||||
}
|
||||
|
||||
// Get all the timezones to display them to user so that they can
|
||||
// select them easily
|
||||
export async function getTimezones(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
for (const timezone of moment.tz.names()) {
|
||||
const timezoneName = timezone;
|
||||
const timezoneId = timezone;
|
||||
returnData.push({
|
||||
name: timezoneName,
|
||||
value: timezoneId,
|
||||
});
|
||||
}
|
||||
returnData.unshift({
|
||||
name: 'Default',
|
||||
value: 'default',
|
||||
description: 'Timezone set in n8n',
|
||||
});
|
||||
return returnData;
|
||||
}
|
||||
Reference in New Issue
Block a user