2020-09-06 12:31:55 -07:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
|
|
|
|
2020-02-10 12:55:28 -08:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
2020-09-06 12:31:55 -07:00
|
|
|
|
2020-02-10 12:55:28 -08:00
|
|
|
import {
|
|
|
|
IDataObject
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2020-09-06 12:31:55 -07:00
|
|
|
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
|
2020-02-10 12:55:28 -08:00
|
|
|
const credentials = this.getCredentials('salesforceOAuth2Api');
|
2020-09-01 00:22:41 -07:00
|
|
|
const subdomain = ((credentials!.accessTokenUrl as string).match(/https:\/\/(.+).salesforce\.com/) || [])[1];
|
2020-02-10 12:55:28 -08:00
|
|
|
const options: OptionsWithUri = {
|
|
|
|
method,
|
2020-08-26 23:23:02 -07:00
|
|
|
body: method === "GET" ? undefined : body,
|
2020-02-10 12:55:28 -08:00
|
|
|
qs,
|
2020-09-06 12:31:55 -07:00
|
|
|
uri: `https://${subdomain}.salesforce.com/services/data/v39.0${uri || endpoint}`,
|
2020-02-10 12:55:28 -08:00
|
|
|
json: true
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
//@ts-ignore
|
2020-06-01 17:42:38 -07:00
|
|
|
return await this.helpers.requestOAuth2.call(this, 'salesforceOAuth2Api', options);
|
2020-02-10 12:55:28 -08:00
|
|
|
} catch (error) {
|
2020-02-12 22:35:56 -08:00
|
|
|
if (error.response && error.response.body && error.response.body[0] && error.response.body[0].message) {
|
2020-02-10 12:55:28 -08:00
|
|
|
// Try to return the error prettier
|
|
|
|
throw new Error(`Salesforce error response [${error.statusCode}]: ${error.response.body[0].message}`);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 11:51:15 -07:00
|
|
|
export async function salesforceApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
2020-02-10 12:55:28 -08:00
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
let uri: string | undefined;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await salesforceApiRequest.call(this, method, endpoint, body, query, uri);
|
2020-09-06 12:31:55 -07:00
|
|
|
uri = `${endpoint}/${responseData.nextRecordsUrl?.split('/')?.pop()}`;
|
2020-02-10 12:55:28 -08:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
|
|
} while (
|
|
|
|
responseData.nextRecordsUrl !== undefined &&
|
|
|
|
responseData.nextRecordsUrl !== null
|
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|