sf-composite-call
Version:
Support for making Salesforce composite call requests with integration for JSforce.
111 lines (110 loc) • 5.28 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompositeSubrequestSObject = void 0;
/* eslint-disable @typescript-eslint/no-dynamic-delete */
const Helpers_1 = require("./Helpers");
const CompositeSubrequest_1 = require("./CompositeSubrequest");
/**
* @description Class for SObject Composite Subrequests.
* @augments CompositeSubrequest
* @param {string} sobject - A valid built-in or custom SObject name.
* @param {string} [referenceId] - The reference ID of the query subrequest.
* @param {string} [version] - The version of the Salesforce API to use.
*/
class CompositeSubrequestSObject extends CompositeSubrequest_1.CompositeSubrequest {
constructor(sobject, referenceId, version) {
super(referenceId, version);
this.sobject = sobject;
}
url() {
return super.url() + `/sobjects/${this.sobject}`;
}
delete(body, operation, httpHeaders) {
this.obj = this.makeRequest('DELETE', !Helpers_1.isNullOrUndefined(operation) ? this.url() + '/' + operation : this.url(), body, httpHeaders);
return this.obj;
}
/**
* @description Method to delete an SObject record.
* @param {string} id - The ID of the SObject resource to destory.
* @param {object} [httpHeaders] - **Optional.** Additional HTTP headers to include in the request.
* @returns {CompositeSubrequestBody} - A subrequest object.
*/
destroy(id, httpHeaders) {
return this.delete(undefined, id, httpHeaders);
}
get(body, operation, httpHeaders) {
this.obj = this.makeRequest(null, !Helpers_1.isNullOrUndefined(operation) ? this.url() + '/' + operation : this.url(), body, httpHeaders);
return this.obj;
}
/**
* @description Method to describe an SObject type.
* @param {object} [httpHeaders] - **Optional.** Additional HTTP headers to include in the request.
* @returns {CompositeSubrequestBody} - A subrequest object.
*/
describe(httpHeaders) {
return this.get(undefined, 'describe', httpHeaders);
}
/**
* @description Method to retrieve an SObject record.
* @param {string} id - The ID of the SObject resource to retrieve.
* @param {object} [httpHeaders] - **Optional.** Additional HTTP headers to include in the request.
* @returns {CompositeSubrequestBody} - A subrequest object.
*/
retrieve(id, httpHeaders) {
return this.get(undefined, id, httpHeaders);
}
patch(body, operation, httpHeaders) {
this.obj = this.makeRequest('PATCH', Helpers_1.isNullOrUndefined(operation) ? this.url() : this.url() + '/' + operation, body, httpHeaders);
return this.obj;
}
/**
* @description Method to update an SObject record.
* @param {object} record - An object with valid fields for the SObject record.
* @param {string} [record.Id] - The ID of the SObject resource to update.
* @param {string} [externalId='Id'] - The field name to use as the Id of the object.
* @param {object} [httpHeaders] - **Optional.** Additional HTTP headers to include in the request.
* @returns {CompositeSubrequestBody} - A subrequest object.
*/
update(record, externalId, httpHeaders) {
if (Helpers_1.isNullOrUndefined(externalId)) {
externalId = 'Id';
}
if (typeof externalId === 'object' && !Helpers_1.isNullOrUndefined(httpHeaders)) {
httpHeaders = externalId;
externalId = 'Id';
}
record = Object.assign({}, record);
const id = (externalId === 'Id' ? '' : externalId + '/') +
encodeURIComponent(record[externalId]);
delete record.Id;
delete record[externalId];
return this.patch(record, id, httpHeaders);
}
post(body, operation, httpHeaders) {
this.obj = this.makeRequest('POST', !Helpers_1.isNullOrUndefined(operation) ? this.url() + '/' + operation : this.url(), body, httpHeaders);
return this.obj;
}
/**
* @description Method to create an SObject record.
* @param {object} record - An object with valid fields for the SObject record; do not include an Id field.
* @param {object} [httpHeaders] - **Optional.** Additional HTTP headers to include in the request.
* @returns {CompositeSubrequestBody} - A subrequest object.
*/
create(record, httpHeaders) {
return this.post(record, null, httpHeaders);
}
/**
* @description Synonym of `create()`.
* @param {object} record - An object with valid fields for the SObject record; do not include an Id field.
* @param {object} [httpHeaders] - **Optional.** Additional HTTP headers to include in the request.
* @returns {CompositeSubrequestBody} - A subrequest object.
*/
insert(record, httpHeaders) {
return this.create(record, httpHeaders);
}
put(body, operation, httpHeaders) {
this.obj = this.makeRequest('PUT', Helpers_1.isNullOrUndefined(operation) ? this.url() : `${this.url()}/${operation}`, body, httpHeaders);
return this.obj;
}
}
exports.CompositeSubrequestSObject = CompositeSubrequestSObject;