sf-composite-call
Version:
Support for making Salesforce composite call requests with integration for JSforce.
144 lines (143 loc) • 7.57 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompositeCall = void 0;
const Helpers_1 = require("./Helpers");
const CompositeSubrequestQuery_1 = require("./CompositeSubrequestQuery");
const CompositeSubrequestSObject_1 = require("./CompositeSubrequestSObject");
const CompositeSubrequestSObjectCollection_1 = require("./CompositeSubrequestSObjectCollection");
/**
* @description Main class for constructing a composite call. Tracks and enforces limits on query and request imposed by Salesforce API.
* @param {object} [options] - **Optional.** Options for creating the instance.
* @param {string} [options.version] - **Optional.** The version of Salesforce API to use.
* @param {boolean} [options.allOrNone] - **Optional.** Used in the request to Salesforce.
* See their [documentation](https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/requests_composite.htm).
* @param {boolean} [options.collateSubrequests] - **Optional.** Used in the request to Salesforce.
* See their [documentation](https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/requests_composite.htm).
* @param {object} [options.jsforceConnection] - **Optional.** This connection enables the `execute()` method for convenience.
* Without it, the result of Composite Call will have to be passed to another method to post it to Salesforce.
*/
class CompositeCall {
constructor(options) {
this.version = this.versionRX.test(options.version)
? options.version
: 'v48.0';
this.calls = [];
this.options = {
allOrNone: options.allOrNone,
collateSubrequests: options.collateSubrequests
};
this.connection = options.jsforceConnection;
this.limits = {
query: 5,
total: 25,
queryCount: 0,
totalCount: 0
};
}
get versionRX() {
return /v\d\d\.\d/gu;
}
/**
* @property {string} url - The versioned url of the composite request.
*/
get url() {
return `/services/data/${this.version}/composite`;
}
/**
* @property {object} request - The result of constructing the composite call.
* @property {boolean} [request.allOrNone] - **Optional.** Specifies what to do when an error occurs while processing a subrequest.
* @property {boolean} [request.collateSubrequests] - **Optional.** Specifies what to do when an error occurs while processing a subrequest.
* @property {object[]} request.CompositeSubrequest - Collection of subrequests to execute.
*/
get request() {
const compositeRequest = this.calls.map(val => val.subrequest);
return Object.assign({ compositeRequest }, this.options);
}
get queryLimitMet() {
return this.limits.queryCount === this.limits.query;
}
get totalLimitMet() {
return this.limits.totalCount === this.limits.total;
}
/**
* @description Add a query subrequest instance to the composite request.
* @param {string} query - A SOQL query.
* @param {string} [referenceId] - **Optional.** The reference ID of the query subrequest.
* @param {string} [version] - **Optional.** The version of the Salesforce API to use. Must be less than or equal to the version defined for the Composite Call.
* @returns {CompositeSubrequestQuery} - An instance of `CompositeSubrequestQuery`.
* @throws {Error} Query limit met. No more queries may be added.
* @throws {Error} Total request limit met. No more requests may be added.
*/
addQuery(query, referenceId, version) {
if (this.queryLimitMet) {
throw new Error('Query limit met. No more queries may be added.');
}
if (this.totalLimitMet) {
throw new Error('Total request limit met. No more requests may be added.');
}
version = Helpers_1.isNullOrUndefined(version) ? this.version : version;
const newCall = new CompositeSubrequestQuery_1.CompositeSubrequestQuery(query, referenceId, version);
this.calls.push(newCall);
this.limits.queryCount += 1;
this.limits.totalCount += 1;
return newCall;
}
/**
* @description Add a SObject subrequest instance to the composite request.
* @param {string} sobject - A SObject name; may be built-in or custom.
* @param {string} [referenceId] - **Optional.** The reference ID of the SObject subrequest.
* @param {string} [version] - **Optional.** The version of the Salesforce API to use. Must be less than or equal to the version defined for the Composite Call.
* @returns {CompositeSubrequestSObject} - An instance of `CompositeSubrequestSObject`.
* @throws {Error} Total request limit met. No more requests may be added.
*/
addSObject(sobject, referenceId, version) {
if (this.totalLimitMet) {
throw new Error('Total request limit met. No more requests may be added.');
}
version = Helpers_1.isNullOrUndefined(version) ? this.version : version;
const newCall = new CompositeSubrequestSObject_1.CompositeSubrequestSObject(sobject, referenceId, version);
this.calls.push(newCall);
this.limits.totalCount += 1;
return newCall;
}
/**
* @description Add a SObject Collection subrequest instance to the composite request.
* @param {string} [referenceId] - **Optional.** The reference ID of the SObject subrequest.
* @param {string} [version] - **Optional.** The version of the Salesforce API to use. Must be less than or equal to the version defined for the Composite Call.
* @returns {CompositeSubrequestSObjectCollection} - An instance of `CompositeSubrequestSObjectCollection`.
* @throws {Error} Total request limit met. No more requests may be added.
*/
addSObjectCollection(referenceId, version) {
if (this.totalLimitMet) {
throw new Error('Total request limit met. No more requests may be added.');
}
version = Helpers_1.isNullOrUndefined(version) ? this.version : version;
const newCall = new CompositeSubrequestSObjectCollection_1.CompositeSubrequestSObjectCollection(referenceId, version);
this.calls.push(newCall);
this.limits.totalCount += 1;
return newCall;
}
/**
* @description Convenience method for internally clearing previous calls, limits, etc.
*/
clear() {
this.calls = [];
this.limits.queryCount = 0;
this.limits.totalCount = 0;
}
/**
* @description Convenience method for integrating with JSforce.
* @param {any} [connection] - Optionally pass a JSforce connection instance; used if not defined as part this class instance options.
* @returns {Promise<CompositeCallResponse>} - The result of executing the composite call, or undefined if no JSforce connection was given.
*/
async execute(connection) {
if (!Helpers_1.isNullOrUndefined(connection)) {
return await connection.requestPost(this.url, this.request);
}
else if (!Helpers_1.isNullOrUndefined(this.connection)) {
return await this.connection.requestPost(this.url, this.request);
}
console.warn('No JSForce Connection object provided. Request cannot be executed.');
}
}
exports.CompositeCall = CompositeCall;