mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 21:19:43 -08:00
0448feec56
* ⚡ Initial setup
* 👕 Update `.eslintignore`
* 👕 Autofix node-param-default-missing (#3173)
* 🔥 Remove duplicate key
* 👕 Add exceptions
* 📦 Update package-lock.json
* 👕 Apply `node-class-description-inputs-wrong-trigger-node` (#3176)
* 👕 Apply `node-class-description-inputs-wrong-regular-node` (#3177)
* 👕 Apply `node-class-description-outputs-wrong` (#3178)
* 👕 Apply `node-execute-block-double-assertion-for-items` (#3179)
* 👕 Apply `node-param-default-wrong-for-collection` (#3180)
* 👕 Apply node-param-default-wrong-for-boolean (#3181)
* Autofixed default missing
* Autofixed booleans, worked well
* ⚡ Fix params
* ⏪ Undo exempted autofixes
* 📦 Update package-lock.json
* 👕 Apply node-class-description-missing-subtitle (#3182)
* ⚡ Fix missing comma
* 👕 Apply `node-param-default-wrong-for-fixed-collection` (#3184)
* 👕 Add exception for `node-class-description-missing-subtitle`
* 👕 Apply `node-param-default-wrong-for-multi-options` (#3185)
* 👕 Apply `node-param-collection-type-unsorted-items` (#3186)
* Missing coma
* 👕 Apply `node-param-default-wrong-for-simplify` (#3187)
* 👕 Apply `node-param-description-comma-separated-hyphen` (#3190)
* 👕 Apply `node-param-description-empty-string` (#3189)
* 👕 Apply `node-param-description-excess-inner-whitespace` (#3191)
* Rule looks good
* Add whitespace rule in eslint config
* :zao: fix
* 👕 Apply `node-param-description-identical-to-display-name` (#3193)
* 👕 Apply `node-param-description-missing-for-ignore-ssl-issues` (#3195)
* ⏪ Revert ":zao: fix"
This reverts commit ef8a76f3df
.
* 👕 Apply `node-param-description-missing-for-simplify` (#3196)
* 👕 Apply `node-param-description-missing-final-period` (#3194)
* Rule working as intended
* Add rule to eslint
* 👕 Apply node-param-description-missing-for-return-all (#3197)
* ⚡ Restore `lintfix` command
Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: agobrech <ael.gobrecht@gmail.com>
Co-authored-by: Michael Kret <michael.k@radency.com>
447 lines
17 KiB
TypeScript
447 lines
17 KiB
TypeScript
import {
|
|
IExecuteFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject,
|
|
ILoadOptionsFunctions,
|
|
INodeExecutionData,
|
|
INodePropertyOptions,
|
|
INodeType,
|
|
INodeTypeBaseDescription,
|
|
INodeTypeDescription,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
extractDatabaseId,
|
|
extractPageId,
|
|
formatBlocks,
|
|
formatTitle,
|
|
getBlockTypes,
|
|
mapFilters,
|
|
mapProperties,
|
|
mapSorting,
|
|
notionApiRequest,
|
|
notionApiRequestAllItems,
|
|
simplifyObjects,
|
|
} from '../GenericFunctions';
|
|
|
|
import moment from 'moment-timezone';
|
|
|
|
import {
|
|
versionDescription
|
|
} from './VersionDescription';
|
|
|
|
export class NotionV1 implements INodeType {
|
|
|
|
description: INodeTypeDescription;
|
|
|
|
constructor(baseDescription: INodeTypeBaseDescription) {
|
|
this.description = {
|
|
...baseDescription,
|
|
...versionDescription,
|
|
};
|
|
}
|
|
|
|
methods = {
|
|
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 { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
|
|
for (const key of Object.keys(properties)) {
|
|
//remove parameters that cannot be set from the API.
|
|
if (!['created_time', 'last_edited_time', 'created_by', 'last_edited_by', 'formula', 'files', 'rollup'].includes(properties[key].type)) {
|
|
returnData.push({
|
|
name: `${key} - (${properties[key].type})`,
|
|
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;
|
|
},
|
|
async getFilterProperties(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const databaseId = this.getCurrentNodeParameter('databaseId') as string;
|
|
const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
|
|
for (const key of Object.keys(properties)) {
|
|
returnData.push({
|
|
name: `${key} - (${properties[key].type})`,
|
|
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;
|
|
},
|
|
async getBlockTypes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
return getBlockTypes();
|
|
},
|
|
async getPropertySelectValues(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const [name, type] = (this.getCurrentNodeParameter('&key') as string).split('|');
|
|
const databaseId = this.getCurrentNodeParameter('databaseId') 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') {
|
|
return (properties[name][type].options)
|
|
.map((option: IDataObject) => ({ name: option.name, value: option.name }));
|
|
} else if (['multi_select'].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 }));
|
|
},
|
|
async 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;
|
|
},
|
|
async getDatabaseIdFromPage(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const pageId = this.getCurrentNodeParameter('pageId') 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)) {
|
|
//remove parameters that cannot be set from the API.
|
|
if (!['created_time', 'last_edited_time', 'created_by', 'last_edited_by', 'formula', 'files'].includes(properties[key].type)) {
|
|
returnData.push({
|
|
name: `${key} - (${properties[key].type})`,
|
|
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;
|
|
},
|
|
|
|
async getDatabaseOptionsFromPage(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const pageId = this.getCurrentNodeParameter('pageId') 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.id }));
|
|
},
|
|
|
|
// Get all the timezones to display them to user so that he can
|
|
// select them easily
|
|
async 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;
|
|
},
|
|
},
|
|
};
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
const items = this.getInputData();
|
|
const returnData: IDataObject[] = [];
|
|
const length = items.length;
|
|
let responseData;
|
|
const qs: IDataObject = {};
|
|
const timezone = this.getTimezone();
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
if (resource === 'block') {
|
|
|
|
if (operation === 'append') {
|
|
for (let i = 0; i < length; i++) {
|
|
const blockId = extractPageId(this.getNodeParameter('blockId', i) as string);
|
|
const body: IDataObject = {
|
|
children: formatBlocks(this.getNodeParameter('blockUi.blockValues', i, []) as IDataObject[]),
|
|
};
|
|
const block = await notionApiRequest.call(this, 'PATCH', `/blocks/${blockId}/children`, body);
|
|
returnData.push(block);
|
|
}
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
|
for (let i = 0; i < length; i++) {
|
|
const blockId = extractPageId(this.getNodeParameter('blockId', i) as string);
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
if (returnAll) {
|
|
responseData = await notionApiRequestAllItems.call(this, 'results', 'GET', `/blocks/${blockId}/children`, {});
|
|
} else {
|
|
qs.page_size = this.getNodeParameter('limit', i) as number;
|
|
responseData = await notionApiRequest.call(this, 'GET', `/blocks/${blockId}/children`, {}, qs);
|
|
responseData = responseData.results;
|
|
}
|
|
returnData.push.apply(returnData, responseData);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (resource === 'database') {
|
|
|
|
if (operation === 'get') {
|
|
for (let i = 0; i < length; i++) {
|
|
const databaseId = extractDatabaseId(this.getNodeParameter('databaseId', i) as string);
|
|
responseData = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
|
|
returnData.push(responseData);
|
|
}
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
|
for (let i = 0; i < length; i++) {
|
|
const body: IDataObject = {
|
|
filter: { property: 'object', value: 'database' },
|
|
};
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
if (returnAll) {
|
|
responseData = await notionApiRequestAllItems.call(this, 'results', 'POST', `/search`, body);
|
|
} else {
|
|
body['page_size'] = this.getNodeParameter('limit', i) as number;
|
|
responseData = await notionApiRequest.call(this, 'POST', `/search`, body);
|
|
responseData = responseData.results;
|
|
}
|
|
returnData.push.apply(returnData, responseData);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (resource === 'databasePage') {
|
|
|
|
if (operation === 'create') {
|
|
for (let i = 0; i < length; i++) {
|
|
const simple = this.getNodeParameter('simple', i) as boolean;
|
|
// tslint:disable-next-line: no-any
|
|
const body: { [key: string]: any } = {
|
|
parent: {},
|
|
properties: {},
|
|
};
|
|
body.parent['database_id'] = this.getNodeParameter('databaseId', i) as string;
|
|
const properties = this.getNodeParameter('propertiesUi.propertyValues', i, []) as IDataObject[];
|
|
if (properties.length !== 0) {
|
|
body.properties = mapProperties(properties, timezone) as IDataObject;
|
|
}
|
|
body.children = formatBlocks(this.getNodeParameter('blockUi.blockValues', i, []) as IDataObject[]);
|
|
responseData = await notionApiRequest.call(this, 'POST', '/pages', body);
|
|
if (simple === true) {
|
|
responseData = simplifyObjects(responseData, false, 1);
|
|
}
|
|
returnData.push.apply(returnData, Array.isArray(responseData) ? responseData : [responseData]);
|
|
}
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
|
for (let i = 0; i < length; i++) {
|
|
const simple = this.getNodeParameter('simple', 0) as boolean;
|
|
const databaseId = this.getNodeParameter('databaseId', i) as string;
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
const filters = this.getNodeParameter('options.filter', i, {}) as IDataObject;
|
|
const sort = this.getNodeParameter('options.sort.sortValue', i, []) as IDataObject[];
|
|
const body: IDataObject = {
|
|
filter: {},
|
|
};
|
|
if (filters.singleCondition) {
|
|
body['filter'] = mapFilters([filters.singleCondition] as IDataObject[], timezone);
|
|
}
|
|
if (filters.multipleCondition) {
|
|
const { or, and } = (filters.multipleCondition as IDataObject).condition as IDataObject;
|
|
if (Array.isArray(or) && or.length !== 0) {
|
|
Object.assign(body.filter, { or: (or as IDataObject[]).map((data) => mapFilters([data], timezone)) });
|
|
}
|
|
if (Array.isArray(and) && and.length !== 0) {
|
|
Object.assign(body.filter, { and: (and as IDataObject[]).map((data) => mapFilters([data], timezone)) });
|
|
}
|
|
}
|
|
if (!Object.keys(body.filter as IDataObject).length) {
|
|
delete body.filter;
|
|
}
|
|
if (sort) {
|
|
//@ts-expect-error
|
|
body['sorts'] = mapSorting(sort);
|
|
}
|
|
if (returnAll) {
|
|
responseData = await notionApiRequestAllItems.call(this, 'results', 'POST', `/databases/${databaseId}/query`, body, {});
|
|
} else {
|
|
body.page_size = this.getNodeParameter('limit', i) as number;
|
|
responseData = await notionApiRequest.call(this, 'POST', `/databases/${databaseId}/query`, body, qs);
|
|
responseData = responseData.results;
|
|
}
|
|
if (simple === true) {
|
|
responseData = simplifyObjects(responseData, false, 1);
|
|
}
|
|
returnData.push.apply(returnData, responseData);
|
|
}
|
|
}
|
|
|
|
if (operation === 'update') {
|
|
for (let i = 0; i < length; i++) {
|
|
const pageId = extractPageId(this.getNodeParameter('pageId', i) as string);
|
|
const simple = this.getNodeParameter('simple', i) as boolean;
|
|
const properties = this.getNodeParameter('propertiesUi.propertyValues', i, []) as IDataObject[];
|
|
// tslint:disable-next-line: no-any
|
|
const body: { [key: string]: any } = {
|
|
properties: {},
|
|
};
|
|
if (properties.length !== 0) {
|
|
body.properties = mapProperties(properties, timezone) as IDataObject;
|
|
}
|
|
responseData = await notionApiRequest.call(this, 'PATCH', `/pages/${pageId}`, body);
|
|
if (simple === true) {
|
|
responseData = simplifyObjects(responseData, false, 1);
|
|
}
|
|
returnData.push.apply(returnData, Array.isArray(responseData) ? responseData : [responseData]);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (resource === 'user') {
|
|
|
|
if (operation === 'get') {
|
|
for (let i = 0; i < length; i++) {
|
|
const userId = this.getNodeParameter('userId', i) as string;
|
|
responseData = await notionApiRequest.call(this, 'GET', `/users/${userId}`);
|
|
returnData.push(responseData);
|
|
}
|
|
}
|
|
if (operation === 'getAll') {
|
|
for (let i = 0; i < length; i++) {
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
if (returnAll) {
|
|
responseData = await notionApiRequestAllItems.call(this, 'results', 'GET', '/users');
|
|
} else {
|
|
qs.limit = this.getNodeParameter('limit', i) as number;
|
|
responseData = await notionApiRequestAllItems.call(this, 'results', 'GET', '/users');
|
|
responseData = responseData.splice(0, qs.limit);
|
|
}
|
|
returnData.push.apply(returnData, responseData);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (resource === 'page') {
|
|
|
|
if (operation === 'create') {
|
|
for (let i = 0; i < length; i++) {
|
|
const simple = this.getNodeParameter('simple', i) as boolean;
|
|
// tslint:disable-next-line: no-any
|
|
const body: { [key: string]: any } = {
|
|
parent: {},
|
|
properties: {},
|
|
};
|
|
body.parent['page_id'] = extractPageId(this.getNodeParameter('pageId', i) as string);
|
|
body.properties = formatTitle(this.getNodeParameter('title', i) as string);
|
|
body.children = formatBlocks(this.getNodeParameter('blockUi.blockValues', i, []) as IDataObject[]);
|
|
responseData = await notionApiRequest.call(this, 'POST', '/pages', body);
|
|
if (simple === true) {
|
|
responseData = simplifyObjects(responseData, false, 1);
|
|
}
|
|
returnData.push.apply(returnData, Array.isArray(responseData) ? responseData : [responseData]);
|
|
}
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
for (let i = 0; i < length; i++) {
|
|
const pageId = extractPageId(this.getNodeParameter('pageId', i) as string);
|
|
const simple = this.getNodeParameter('simple', i) as boolean;
|
|
responseData = await notionApiRequest.call(this, 'GET', `/pages/${pageId}`);
|
|
if (simple === true) {
|
|
responseData = simplifyObjects(responseData, false, 1);
|
|
}
|
|
returnData.push.apply(returnData, Array.isArray(responseData) ? responseData : [responseData]);
|
|
}
|
|
}
|
|
|
|
if (operation === 'search') {
|
|
for (let i = 0; i < length; i++) {
|
|
const text = this.getNodeParameter('text', i) as string;
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
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) as number;
|
|
responseData = await notionApiRequestAllItems.call(this, 'results', 'POST', '/search', body);
|
|
responseData = responseData.splice(0, qs.limit);
|
|
}
|
|
|
|
if (simple === true) {
|
|
responseData = simplifyObjects(responseData, false, 1);
|
|
}
|
|
|
|
returnData.push.apply(returnData, responseData);
|
|
}
|
|
}
|
|
}
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
}
|
|
}
|