sf-composite-call
Version:
Support for making Salesforce composite call requests with integration for JSforce.
59 lines (58 loc) • 2.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompositeSubrequest = void 0;
const uuid_1 = require("uuid");
const Helpers_1 = require("./Helpers");
/**
* @description Base class for Composite Subrequests.
* @param {string} [referenceId] - The reference ID of the query subrequest.
* @param {string} [version] - The version of the Salesforce API to use.
*/
class CompositeSubrequest {
constructor(referenceId, version) {
this.version = this.versionRX.test(version) ? version : 'v48.0';
this.referenceId = Helpers_1.isNullOrUndefined(referenceId)
? uuid_1.v4().replace(/-/gu, '')
: referenceId;
}
get versionRX() {
return /v\d\d\.\d/gu;
}
/**
* @property {object} subrequest - The result of constructing the composite call.
* @property {any} [subrequest.body] - **Optional.** The input body for the subrequest.
* @property {object} [subrequest.httpHeaders] - **Optional.** Request headers and their values to include with the subrequest.
* @property {string} subrequest.method - The method to use with the requested resource. Possible values are POST, PUT, PATCH, GET, and DELETE (case-sensitive).
* @property {string} subrequest.referenceId - Reference ID that maps to the subrequest’s response and can be used to reference the response in later subrequests.
* @property {string} subrequest.url - The resource to request.
*/
get subrequest() {
if (Helpers_1.isNullOrUndefined(this.obj)) {
return this.makeRequest();
}
return this.obj;
}
url() {
return `/services/data/${this.version}`;
}
/**
* @description Base method for building the request.
* @param {string} method - The method to use with the requested resource. Possible values are POST, PUT, PATCH, GET, and DELETE (case-sensitive).
* @param {string} url - The resource to request.
* @param {any} body - **Optional.** The input body for the subrequest.
* @param {object} httpHeaders - **Optional.** Request headers and their values to include with the subrequest.
* @returns {CompositeSubrequestBody} - A subrequest object.
*/
makeRequest(method, url, body, httpHeaders) {
method = Helpers_1.isNullOrUndefined(method) ? 'GET' : method;
url = Helpers_1.isNullOrUndefined(url) ? this.url() : url;
return {
method,
url,
body,
httpHeaders,
referenceId: this.referenceId
};
}
}
exports.CompositeSubrequest = CompositeSubrequest;