refactor: Format nodes-base package (A-F) (#3800)

* 🔨 prettier formated nodes - A

* 🔨 prettier formated nodes - B

*  prettier formated nodes - C

*  prettier formated nodes - D

*  prettier formated nodes - E-F

* 🎨 Adjust nodes-base formatting command (#3805)

* Format additional files in nodes A-F (#3811)

*  fixes

* 🎨 Add Mindee to ignored dirs

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
This commit is contained in:
Michael Kret
2022-08-01 23:47:55 +03:00
committed by GitHub
parent 2c17e6f3ca
commit 0ecbb4a19d
411 changed files with 12906 additions and 20148 deletions

View File

@@ -1,6 +1,4 @@
import {
INodeProperties,
} from 'n8n-workflow';
import { INodeProperties } from 'n8n-workflow';
export const documentOperations: INodeProperties[] = [
{
@@ -10,9 +8,7 @@ export const documentOperations: INodeProperties[] = [
noDataExpression: true,
displayOptions: {
show: {
resource: [
'document',
],
resource: ['document'],
},
},
options: [
@@ -63,16 +59,13 @@ export const documentFields: INodeProperties[] = [
loadOptionsMethod: 'getDocTypes',
},
default: '',
description: 'DocType whose documents to retrieve. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
description:
'DocType whose documents to retrieve. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
placeholder: 'Customer',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'getAll',
],
resource: ['document'],
operation: ['getAll'],
},
},
},
@@ -84,12 +77,8 @@ export const documentFields: INodeProperties[] = [
description: 'Whether to return all results or only up to a given limit',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'getAll',
],
resource: ['document'],
operation: ['getAll'],
},
},
},
@@ -104,15 +93,9 @@ export const documentFields: INodeProperties[] = [
description: 'Max number of results to return',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'getAll',
],
returnAll: [
false,
],
resource: ['document'],
operation: ['getAll'],
returnAll: [false],
},
},
},
@@ -124,12 +107,8 @@ export const documentFields: INodeProperties[] = [
default: {},
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'getAll',
],
resource: ['document'],
operation: ['getAll'],
},
},
options: [
@@ -139,12 +118,11 @@ export const documentFields: INodeProperties[] = [
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getDocFilters',
loadOptionsDependsOn: [
'docType',
],
loadOptionsDependsOn: ['docType'],
},
default: [],
description: 'Comma-separated list of fields to return. Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
description:
'Comma-separated list of fields to return. Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
placeholder: 'name,country',
},
{
@@ -166,12 +144,11 @@ export const documentFields: INodeProperties[] = [
displayName: 'Field Name or ID',
name: 'field',
type: 'options',
description: 'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
description:
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
typeOptions: {
loadOptionsMethod: 'getDocFields',
loadOptionsDependsOn: [
'docType',
],
loadOptionsDependsOn: ['docType'],
},
default: '',
},
@@ -233,16 +210,13 @@ export const documentFields: INodeProperties[] = [
loadOptionsMethod: 'getDocTypes',
},
required: true,
description: 'DocType you would like to create. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
description:
'DocType you would like to create. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
placeholder: 'Customer',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'create',
],
resource: ['document'],
operation: ['create'],
},
},
},
@@ -258,12 +232,8 @@ export const documentFields: INodeProperties[] = [
},
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'create',
],
resource: ['document'],
operation: ['create'],
},
},
options: [
@@ -276,12 +246,11 @@ export const documentFields: INodeProperties[] = [
displayName: 'Field Name or ID',
name: 'field',
type: 'options',
description: 'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
description:
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
typeOptions: {
loadOptionsMethod: 'getDocFields',
loadOptionsDependsOn: [
'docType',
],
loadOptionsDependsOn: ['docType'],
},
default: [],
},
@@ -307,15 +276,12 @@ export const documentFields: INodeProperties[] = [
loadOptionsMethod: 'getDocTypes',
},
default: '',
description: 'The type of document you would like to get. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
description:
'The type of document you would like to get. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'get',
],
resource: ['document'],
operation: ['get'],
},
},
required: true,
@@ -328,12 +294,8 @@ export const documentFields: INodeProperties[] = [
description: 'The name (ID) of document you would like to get',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'get',
],
resource: ['document'],
operation: ['get'],
},
},
required: true,
@@ -350,15 +312,12 @@ export const documentFields: INodeProperties[] = [
loadOptionsMethod: 'getDocTypes',
},
default: '',
description: 'The type of document you would like to delete. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
description:
'The type of document you would like to delete. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'delete',
],
resource: ['document'],
operation: ['delete'],
},
},
required: true,
@@ -371,12 +330,8 @@ export const documentFields: INodeProperties[] = [
description: 'The name (ID) of document you would like to get',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'delete',
],
resource: ['document'],
operation: ['delete'],
},
},
required: true,
@@ -393,15 +348,12 @@ export const documentFields: INodeProperties[] = [
loadOptionsMethod: 'getDocTypes',
},
default: '',
description: 'The type of document you would like to update. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
description:
'The type of document you would like to update. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'update',
],
resource: ['document'],
operation: ['update'],
},
},
required: true,
@@ -414,12 +366,8 @@ export const documentFields: INodeProperties[] = [
description: 'The name (ID) of document you would like to get',
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'update',
],
resource: ['document'],
operation: ['update'],
},
},
required: true,
@@ -436,12 +384,8 @@ export const documentFields: INodeProperties[] = [
},
displayOptions: {
show: {
resource: [
'document',
],
operation: [
'update',
],
resource: ['document'],
operation: ['update'],
},
},
options: [
@@ -453,12 +397,11 @@ export const documentFields: INodeProperties[] = [
displayName: 'Field Name or ID',
name: 'field',
type: 'options',
description: 'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
description:
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
typeOptions: {
loadOptionsMethod: 'getDocFields',
loadOptionsDependsOn: [
'docType',
],
loadOptionsDependsOn: ['docType'],
},
default: '',
},

View File

@@ -1,7 +1,5 @@
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
import {
IExecuteFunctions,
} from 'n8n-core';
import { IExecuteFunctions } from 'n8n-core';
import {
IDataObject,
@@ -13,21 +11,11 @@ import {
NodeOperationError,
} from 'n8n-workflow';
import {
documentFields,
documentOperations,
} from './DocumentDescription';
import { documentFields, documentOperations } from './DocumentDescription';
import {
erpNextApiRequest,
erpNextApiRequestAllItems
} from './GenericFunctions';
import { erpNextApiRequest, erpNextApiRequestAllItems } from './GenericFunctions';
import {
DocumentProperties,
processNames,
toSQL,
} from './utils';
import { DocumentProperties, processNames, toSQL } from './utils';
export class ERPNext implements INodeType {
description: INodeTypeDescription = {
@@ -71,7 +59,13 @@ export class ERPNext implements INodeType {
methods = {
loadOptions: {
async getDocTypes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const data = await erpNextApiRequestAllItems.call(this, 'data', 'GET', '/api/resource/DocType', {});
const data = await erpNextApiRequestAllItems.call(
this,
'data',
'GET',
'/api/resource/DocType',
{},
);
const docTypes = data.map(({ name }: { name: string }) => {
return { name, value: encodeURI(name) };
});
@@ -80,11 +74,18 @@ export class ERPNext implements INodeType {
},
async getDocFilters(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const docType = this.getCurrentNodeParameter('docType') as string;
const { data } = await erpNextApiRequest.call(this, 'GET', `/api/resource/DocType/${docType}`, {});
const { data } = await erpNextApiRequest.call(
this,
'GET',
`/api/resource/DocType/${docType}`,
{},
);
const docFields = data.fields.map(({ label, fieldname }: { label: string, fieldname: string }) => {
return ({ name: label, value: fieldname });
});
const docFields = data.fields.map(
({ label, fieldname }: { label: string; fieldname: string }) => {
return { name: label, value: fieldname };
},
);
docFields.unshift({ name: '*', value: '*' });
@@ -92,11 +93,18 @@ export class ERPNext implements INodeType {
},
async getDocFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const docType = this.getCurrentNodeParameter('docType') as string;
const { data } = await erpNextApiRequest.call(this, 'GET', `/api/resource/DocType/${docType}`, {});
const { data } = await erpNextApiRequest.call(
this,
'GET',
`/api/resource/DocType/${docType}`,
{},
);
const docFields = data.fields.map(({ label, fieldname }: { label: string, fieldname: string }) => {
return ({ name: label, value: fieldname });
});
const docFields = data.fields.map(
({ label, fieldname }: { label: string; fieldname: string }) => {
return { name: label, value: fieldname };
},
);
return processNames(docFields);
},
@@ -116,18 +124,15 @@ export class ERPNext implements INodeType {
const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < items.length; i++) {
// https://app.swaggerhub.com/apis-docs/alyf.de/ERPNext/11#/Resources/post_api_resource_Webhook
// https://frappeframework.com/docs/user/en/guides/integration/rest_api/manipulating_documents
if (resource === 'document') {
// *********************************************************************
// document
// *********************************************************************
if (operation === 'get') {
// ----------------------------------
// document: get
// ----------------------------------
@@ -137,12 +142,15 @@ export class ERPNext implements INodeType {
const docType = this.getNodeParameter('docType', i) as string;
const documentName = this.getNodeParameter('documentName', i) as string;
responseData = await erpNextApiRequest.call(this, 'GET', `/api/resource/${docType}/${documentName}`);
responseData = await erpNextApiRequest.call(
this,
'GET',
`/api/resource/${docType}/${documentName}`,
);
responseData = responseData.data;
}
if (operation === 'getAll') {
// ----------------------------------
// document: getAll
// ----------------------------------
@@ -152,14 +160,11 @@ export class ERPNext implements INodeType {
const docType = this.getNodeParameter('docType', i) as string;
const endpoint = `/api/resource/${docType}`;
const {
fields,
filters,
} = this.getNodeParameter('options', i) as {
fields: string[],
const { fields, filters } = this.getNodeParameter('options', i) as {
fields: string[];
filters: {
customProperty: Array<{ field: string, operator: string, value: string }>,
},
customProperty: Array<{ field: string; operator: string; value: string }>;
};
};
// fields=["test", "example", "hi"]
@@ -173,14 +178,11 @@ export class ERPNext implements INodeType {
// filters=[["Person","first_name","=","Jane"]]
// TODO: filters not working
if (filters) {
qs.filters = JSON.stringify(filters.customProperty.map((filter) => {
return [
docType,
filter.field,
toSQL(filter.operator),
filter.value,
];
}));
qs.filters = JSON.stringify(
filters.customProperty.map((filter) => {
return [docType, filter.field, toSQL(filter.operator), filter.value];
}),
);
}
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
@@ -191,13 +193,17 @@ export class ERPNext implements INodeType {
qs.limit_start = 0;
responseData = await erpNextApiRequest.call(this, 'GET', endpoint, {}, qs);
responseData = responseData.data;
} else {
responseData = await erpNextApiRequestAllItems.call(this, 'data', 'GET', endpoint, {}, qs);
responseData = await erpNextApiRequestAllItems.call(
this,
'data',
'GET',
endpoint,
{},
qs,
);
}
} else if (operation === 'create') {
// ----------------------------------
// document: create
// ----------------------------------
@@ -207,20 +213,27 @@ export class ERPNext implements INodeType {
const properties = this.getNodeParameter('properties', i) as DocumentProperties;
if (!properties.customProperty.length) {
throw new NodeOperationError(this.getNode(), 'Please enter at least one property for the document to create.', { itemIndex: i },);
throw new NodeOperationError(
this.getNode(),
'Please enter at least one property for the document to create.',
{ itemIndex: i },
);
}
properties.customProperty.forEach(property => {
properties.customProperty.forEach((property) => {
body[property.field] = property.value;
});
const docType = this.getNodeParameter('docType', i) as string;
responseData = await erpNextApiRequest.call(this, 'POST', `/api/resource/${docType}`, body);
responseData = await erpNextApiRequest.call(
this,
'POST',
`/api/resource/${docType}`,
body,
);
responseData = responseData.data;
} else if (operation === 'delete') {
// ----------------------------------
// document: delete
// ----------------------------------
@@ -230,10 +243,12 @@ export class ERPNext implements INodeType {
const docType = this.getNodeParameter('docType', i) as string;
const documentName = this.getNodeParameter('documentName', i) as string;
responseData = await erpNextApiRequest.call(this, 'DELETE', `/api/resource/${docType}/${documentName}`);
responseData = await erpNextApiRequest.call(
this,
'DELETE',
`/api/resource/${docType}/${documentName}`,
);
} else if (operation === 'update') {
// ----------------------------------
// document: update
// ----------------------------------
@@ -243,26 +258,33 @@ export class ERPNext implements INodeType {
const properties = this.getNodeParameter('properties', i) as DocumentProperties;
if (!properties.customProperty.length) {
throw new NodeOperationError(this.getNode(), 'Please enter at least one property for the document to update.', { itemIndex: i });
throw new NodeOperationError(
this.getNode(),
'Please enter at least one property for the document to update.',
{ itemIndex: i },
);
}
properties.customProperty.forEach(property => {
properties.customProperty.forEach((property) => {
body[property.field] = property.value;
});
const docType = this.getNodeParameter('docType', i) as string;
const documentName = this.getNodeParameter('documentName', i) as string;
responseData = await erpNextApiRequest.call(this, 'PUT', `/api/resource/${docType}/${documentName}`, body);
responseData = await erpNextApiRequest.call(
this,
'PUT',
`/api/resource/${docType}/${documentName}`,
body,
);
responseData = responseData.data;
}
}
Array.isArray(responseData)
? returnData.push(...responseData)
: returnData.push(responseData);
}
return [this.helpers.returnJsonArray(returnData)];
}

View File

@@ -1,18 +1,8 @@
import {
OptionsWithUri,
} from 'request';
import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import { IExecuteFunctions, ILoadOptionsFunctions } from 'n8n-core';
import {
IDataObject,
IHookFunctions,
IWebhookFunctions,
NodeApiError,
} from 'n8n-workflow';
import { IDataObject, IHookFunctions, IWebhookFunctions, NodeApiError } from 'n8n-workflow';
export async function erpNextApiRequest(
this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions,
@@ -23,12 +13,12 @@ export async function erpNextApiRequest(
uri?: string,
option: IDataObject = {},
) {
const credentials = await this.getCredentials('erpNextApi') as ERPNextApiCredentials;
const credentials = (await this.getCredentials('erpNextApi')) as ERPNextApiCredentials;
const baseUrl = getBaseUrl(credentials);
let options: OptionsWithUri = {
headers: {
'Accept': 'application/json',
Accept: 'application/json',
'Content-Type': 'application/json',
},
method,
@@ -49,18 +39,19 @@ export async function erpNextApiRequest(
delete options.qs;
}
try {
return await this.helpers.requestWithAuthentication.call(this, 'erpNextApi',options);
return await this.helpers.requestWithAuthentication.call(this, 'erpNextApi', options);
} catch (error) {
if (error.statusCode === 403) {
throw new NodeApiError(this.getNode(), { message: 'DocType unavailable.' });
}
if (error.statusCode === 307) {
throw new NodeApiError(this.getNode(), { message: 'Please ensure the subdomain is correct.' });
throw new NodeApiError(this.getNode(), {
message: 'Please ensure the subdomain is correct.',
});
}
throw new NodeApiError(this.getNode(), error);
}
}
@@ -83,9 +74,7 @@ export async function erpNextApiRequestAllItems(
responseData = await erpNextApiRequest.call(this, method, resource, body, query);
returnData.push.apply(returnData, responseData[propertyName]);
query!.limit_start += query!.limit_page_length - 1;
} while (
responseData.data && responseData.data.length > 0
);
} while (responseData.data && responseData.data.length > 0);
return returnData;
}
@@ -94,9 +83,7 @@ export async function erpNextApiRequestAllItems(
* Return the base API URL based on the user's environment.
*/
const getBaseUrl = ({ environment, domain, subdomain }: ERPNextApiCredentials) =>
environment === 'cloudHosted'
? `https://${subdomain}.erpnext.com`
: domain;
environment === 'cloudHosted' ? `https://${subdomain}.erpnext.com` : domain;
type ERPNextApiCredentials = {
apiKey: string;

View File

@@ -1,29 +1,25 @@
import {
flow,
sortBy,
uniqBy,
} from 'lodash';
import { flow, sortBy, uniqBy } from 'lodash';
export type DocumentProperties = {
customProperty: Array<{ field: string; value: string; }>;
customProperty: Array<{ field: string; value: string }>;
};
type DocFields = Array<{ name: string, value: string }>;
type DocFields = Array<{ name: string; value: string }>;
const ensureName = (docFields: DocFields) => docFields.filter(o => o.name);
const ensureName = (docFields: DocFields) => docFields.filter((o) => o.name);
const sortByName = (docFields: DocFields) => sortBy(docFields, ['name']);
const uniqueByName = (docFields: DocFields) => uniqBy(docFields, o => o.name);
const uniqueByName = (docFields: DocFields) => uniqBy(docFields, (o) => o.name);
export const processNames = flow(ensureName, sortByName, uniqueByName);
export const toSQL = (operator: string) => {
const operators: { [key: string]: string } = {
'is': '=',
'isNot': '!=',
'greater': '>',
'less': '<',
'equalsGreater': '>=',
'equalsLess': '<=',
is: '=',
isNot: '!=',
greater: '>',
less: '<',
equalsGreater: '>=',
equalsLess: '<=',
};
return operators[operator];