mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
n8n-3867-progressively-apply-prettier-to-all (#3873)
* 🔨 formatting nodes with prettier
This commit is contained in:
@@ -1,28 +1,26 @@
|
||||
import {
|
||||
OptionsWithUri,
|
||||
} from 'request';
|
||||
import { OptionsWithUri } from 'request';
|
||||
|
||||
import {
|
||||
IExecuteFunctions,
|
||||
IExecuteSingleFunctions,
|
||||
ILoadOptionsFunctions,
|
||||
} from 'n8n-core';
|
||||
import { IExecuteFunctions, IExecuteSingleFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
||||
|
||||
import {
|
||||
IDataObject,
|
||||
INodePropertyOptions,
|
||||
NodeApiError,
|
||||
} from 'n8n-workflow';
|
||||
import { IDataObject, INodePropertyOptions, NodeApiError } from 'n8n-workflow';
|
||||
|
||||
import moment from 'moment-timezone';
|
||||
|
||||
import jwt from 'jsonwebtoken';
|
||||
|
||||
import {
|
||||
LoggerProxy as Logger
|
||||
} from 'n8n-workflow';
|
||||
import { LoggerProxy as Logger } from 'n8n-workflow';
|
||||
|
||||
export async function salesforceApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
||||
export async function salesforceApiRequest(
|
||||
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
||||
method: string,
|
||||
endpoint: string,
|
||||
// tslint:disable-next-line:no-any
|
||||
body: any = {},
|
||||
qs: IDataObject = {},
|
||||
uri?: string,
|
||||
option: IDataObject = {},
|
||||
// tslint:disable-next-line:no-any
|
||||
): Promise<any> {
|
||||
const authenticationMethod = this.getNodeParameter('authentication', 0, 'oAuth2') as string;
|
||||
try {
|
||||
if (authenticationMethod === 'jwt') {
|
||||
@@ -31,8 +29,17 @@ export async function salesforceApiRequest(this: IExecuteFunctions | IExecuteSin
|
||||
const credentials = await this.getCredentials(credentialsType);
|
||||
const response = await getAccessToken.call(this, credentials);
|
||||
const { instance_url, access_token } = response;
|
||||
const options = getOptions.call(this, method, (uri || endpoint), body, qs, instance_url as string);
|
||||
Logger.debug(`Authentication for "Salesforce" node is using "jwt". Invoking URI ${options.uri}`);
|
||||
const options = getOptions.call(
|
||||
this,
|
||||
method,
|
||||
uri || endpoint,
|
||||
body,
|
||||
qs,
|
||||
instance_url as string,
|
||||
);
|
||||
Logger.debug(
|
||||
`Authentication for "Salesforce" node is using "jwt". Invoking URI ${options.uri}`,
|
||||
);
|
||||
options.headers!.Authorization = `Bearer ${access_token}`;
|
||||
Object.assign(options, option);
|
||||
//@ts-ignore
|
||||
@@ -40,9 +47,20 @@ export async function salesforceApiRequest(this: IExecuteFunctions | IExecuteSin
|
||||
} else {
|
||||
// https://help.salesforce.com/articleView?id=remoteaccess_oauth_web_server_flow.htm&type=5
|
||||
const credentialsType = 'salesforceOAuth2Api';
|
||||
const credentials = await this.getCredentials(credentialsType) as { oauthTokenData: { instance_url: string } };
|
||||
const options = getOptions.call(this, method, (uri || endpoint), body, qs, credentials.oauthTokenData.instance_url);
|
||||
Logger.debug(`Authentication for "Salesforce" node is using "OAuth2". Invoking URI ${options.uri}`);
|
||||
const credentials = (await this.getCredentials(credentialsType)) as {
|
||||
oauthTokenData: { instance_url: string };
|
||||
};
|
||||
const options = getOptions.call(
|
||||
this,
|
||||
method,
|
||||
uri || endpoint,
|
||||
body,
|
||||
qs,
|
||||
credentials.oauthTokenData.instance_url,
|
||||
);
|
||||
Logger.debug(
|
||||
`Authentication for "Salesforce" node is using "OAuth2". Invoking URI ${options.uri}`,
|
||||
);
|
||||
Object.assign(options, option);
|
||||
//@ts-ignore
|
||||
return await this.helpers.requestOAuth2.call(this, credentialsType, options);
|
||||
@@ -52,7 +70,16 @@ export async function salesforceApiRequest(this: IExecuteFunctions | IExecuteSin
|
||||
}
|
||||
}
|
||||
|
||||
export async function salesforceApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
||||
export async function salesforceApiRequestAllItems(
|
||||
this: IExecuteFunctions | ILoadOptionsFunctions,
|
||||
propertyName: string,
|
||||
method: string,
|
||||
endpoint: string,
|
||||
// tslint:disable-next-line:no-any
|
||||
body: any = {},
|
||||
query: IDataObject = {},
|
||||
// tslint:disable-next-line:no-any
|
||||
): Promise<any> {
|
||||
const returnData: IDataObject[] = [];
|
||||
|
||||
let responseData;
|
||||
@@ -62,10 +89,7 @@ export async function salesforceApiRequestAllItems(this: IExecuteFunctions | ILo
|
||||
responseData = await salesforceApiRequest.call(this, method, endpoint, body, query, uri);
|
||||
uri = `${endpoint}/${responseData.nextRecordsUrl?.split('/')?.pop()}`;
|
||||
returnData.push.apply(returnData, responseData[propertyName]);
|
||||
} while (
|
||||
responseData.nextRecordsUrl !== undefined &&
|
||||
responseData.nextRecordsUrl !== null
|
||||
);
|
||||
} while (responseData.nextRecordsUrl !== undefined && responseData.nextRecordsUrl !== null);
|
||||
|
||||
return returnData;
|
||||
}
|
||||
@@ -79,13 +103,26 @@ export async function salesforceApiRequestAllItems(this: IExecuteFunctions | ILo
|
||||
*/
|
||||
export function sortOptions(options: INodePropertyOptions[]): void {
|
||||
options.sort((a, b) => {
|
||||
if (a.name < b.name) { return -1; }
|
||||
if (a.name > b.name) { return 1; }
|
||||
if (a.name < b.name) {
|
||||
return -1;
|
||||
}
|
||||
if (a.name > b.name) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
function getOptions(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any, qs: IDataObject, instanceUrl: string): OptionsWithUri { // tslint:disable-line:no-any
|
||||
function getOptions(
|
||||
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
||||
method: string,
|
||||
endpoint: string,
|
||||
// tslint:disable-next-line:no-any
|
||||
body: any,
|
||||
qs: IDataObject,
|
||||
instanceUrl: string,
|
||||
): OptionsWithUri {
|
||||
// tslint:disable-line:no-any
|
||||
const options: OptionsWithUri = {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
@@ -105,22 +142,28 @@ function getOptions(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOpt
|
||||
return options;
|
||||
}
|
||||
|
||||
function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, credentials: IDataObject): Promise<IDataObject> {
|
||||
function getAccessToken(
|
||||
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
||||
credentials: IDataObject,
|
||||
): Promise<IDataObject> {
|
||||
const now = moment().unix();
|
||||
const authUrl = credentials.environment === 'sandbox' ? 'https://test.salesforce.com' : 'https://login.salesforce.com';
|
||||
const authUrl =
|
||||
credentials.environment === 'sandbox'
|
||||
? 'https://test.salesforce.com'
|
||||
: 'https://login.salesforce.com';
|
||||
|
||||
const signature = jwt.sign(
|
||||
{
|
||||
'iss': credentials.clientId as string,
|
||||
'sub': credentials.username as string,
|
||||
'aud': authUrl,
|
||||
'exp': now + 3 * 60,
|
||||
iss: credentials.clientId as string,
|
||||
sub: credentials.username as string,
|
||||
aud: authUrl,
|
||||
exp: now + 3 * 60,
|
||||
},
|
||||
credentials.privateKey as string,
|
||||
{
|
||||
algorithm: 'RS256',
|
||||
header: {
|
||||
'alg': 'RS256',
|
||||
alg: 'RS256',
|
||||
},
|
||||
},
|
||||
);
|
||||
@@ -143,10 +186,15 @@ function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions | ILoa
|
||||
}
|
||||
|
||||
export function getConditions(options: IDataObject) {
|
||||
const conditions = (options.conditionsUi as IDataObject || {}).conditionValues as IDataObject[];
|
||||
const conditions = ((options.conditionsUi as IDataObject) || {}).conditionValues as IDataObject[];
|
||||
let data = undefined;
|
||||
if (Array.isArray(conditions) && conditions.length !== 0) {
|
||||
data = conditions.map((condition: IDataObject) => `${condition.field} ${(condition.operation) === 'equal' ? '=' : condition.operation} ${getValue(condition.value)}`);
|
||||
data = conditions.map(
|
||||
(condition: IDataObject) =>
|
||||
`${condition.field} ${
|
||||
condition.operation === 'equal' ? '=' : condition.operation
|
||||
} ${getValue(condition.value)}`,
|
||||
);
|
||||
data = `WHERE ${data.join(' AND ')}`;
|
||||
}
|
||||
return data;
|
||||
@@ -155,14 +203,14 @@ export function getConditions(options: IDataObject) {
|
||||
export function getDefaultFields(sobject: string) {
|
||||
return (
|
||||
{
|
||||
'Account': 'id,name,type',
|
||||
'Lead': 'id,company,firstname,lastname,street,postalCode,city,email,status',
|
||||
'Contact': 'id,firstname,lastname,email',
|
||||
'Opportunity': 'id,accountId,amount,probability,type',
|
||||
'Case': 'id,accountId,contactId,priority,status,subject,type',
|
||||
'Task': 'id,subject,status,priority',
|
||||
'Attachment': 'id,name',
|
||||
'User': 'id,name,email',
|
||||
Account: 'id,name,type',
|
||||
Lead: 'id,company,firstname,lastname,street,postalCode,city,email,status',
|
||||
Contact: 'id,firstname,lastname,email',
|
||||
Opportunity: 'id,accountId,amount,probability,type',
|
||||
Case: 'id,accountId,contactId,priority,status,subject,type',
|
||||
Task: 'id,subject,status,priority',
|
||||
Attachment: 'id,name',
|
||||
User: 'id,name,email',
|
||||
} as IDataObject
|
||||
)[sobject];
|
||||
}
|
||||
@@ -177,20 +225,23 @@ export function getQuery(options: IDataObject, sobject: string, returnAll: boole
|
||||
fields.push.apply(fields, options.fields as string[]);
|
||||
}
|
||||
} else {
|
||||
fields.push.apply(fields, (getDefaultFields(sobject) as string || 'id').split(','));
|
||||
fields.push.apply(fields, ((getDefaultFields(sobject) as string) || 'id').split(','));
|
||||
}
|
||||
const conditions = getConditions(options);
|
||||
|
||||
let query = `SELECT ${fields.join(',')} FROM ${sobject} ${(conditions ? conditions : '')}`;
|
||||
let query = `SELECT ${fields.join(',')} FROM ${sobject} ${conditions ? conditions : ''}`;
|
||||
|
||||
if (returnAll === false) {
|
||||
query = `SELECT ${fields.join(',')} FROM ${sobject} ${(conditions ? conditions : '')} LIMIT ${limit}`;
|
||||
query = `SELECT ${fields.join(',')} FROM ${sobject} ${
|
||||
conditions ? conditions : ''
|
||||
} LIMIT ${limit}`;
|
||||
}
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
export function getValue(value: any) { // tslint:disable-line:no-any
|
||||
// tslint:disable-next-line:no-any
|
||||
export function getValue(value: any) {
|
||||
if (moment(value).isValid()) {
|
||||
return value;
|
||||
} else if (typeof value === 'string') {
|
||||
|
||||
Reference in New Issue
Block a user