UNPKG

@jsforce/jsforce-node

Version:

Salesforce API Library for JavaScript

168 lines (167 loc) 6.01 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SoapApi = void 0; /** * @file Salesforce SOAP API * @author Shinichi Tomita <shinichi.tomita@gmail.com> */ const jsforce_1 = require("../jsforce"); const soap_1 = __importDefault(require("../soap")); const schema_1 = require("./soap/schema"); /** * */ function toSoapRecord(records) { return (Array.isArray(records) ? records : [records]).map((record) => { const { type, attributes, ...rec } = record; const t = type || attributes?.type; if (!t) { throw new Error('Given record is not including sObject type information'); } const fieldsToNull = Object.keys(rec).filter((field) => record[field] === null); for (const field of fieldsToNull) { delete rec[field]; } return fieldsToNull.length > 0 ? { type: t, fieldsToNull, ...rec } : { type: t, ...rec }; }); } /** * API class for Partner SOAP call */ class SoapApi { _conn; constructor(conn) { this._conn = conn; } /** * Call SOAP Api (Partner) endpoint * @private */ async _invoke(method, message, schema) { const soapEndpoint = new soap_1.default(this._conn, { xmlns: 'urn:partner.soap.sforce.com', endpointUrl: `${this._conn.instanceUrl}/services/Soap/u/${this._conn.version}`, }); const res = await soapEndpoint.invoke(method, message, schema ? { result: schema } : undefined, schema_1.ApiSchemas); return res.result; } async convertLead(leadConverts) { const schema = Array.isArray(leadConverts) ? [schema_1.ApiSchemas.LeadConvertResult] : schema_1.ApiSchemas.LeadConvertResult; return this._invoke('convertLead', { leadConverts }, schema); } async merge(mergeRequests) { const schema = Array.isArray(mergeRequests) ? [schema_1.ApiSchemas.MergeResult] : schema_1.ApiSchemas.MergeResult; return this._invoke('merge', { mergeRequests }, schema); } /** * Delete records from the recycle bin immediately */ async emptyRecycleBin(ids) { return this._invoke('emptyRecycleBin', { ids }, [ schema_1.ApiSchemas.EmptyRecycleBinResult, ]); } /** * Returns information about the standard and custom apps available to the logged-in user */ async describeTabs() { return this._invoke('describeTabs', {}, [schema_1.ApiSchemas.DescribeTabSetResult]); } /** * Retrieves the current system timestamp (Coordinated Universal Time (UTC) time zone) from the API */ async getServerTimestamp() { return this._invoke('getServerTimestamp', {}, schema_1.ApiSchemas.GetServerTimestampResult); } /** * Retrieves personal information for the user associated with the current session */ async getUserInfo() { return this._invoke('getUserInfo', {}, schema_1.ApiSchemas.GetUserInfoResult); } /** * Sets the specified user’s password to the specified value */ setPassword(userId, password) { return this._invoke('setPassword', { userId, password }, 'string'); } /** * Resets the specified user’s password */ resetPassword(userId) { return this._invoke('resetPassword', { userId }, schema_1.ApiSchemas.ResetPasswordResult); } create(sObjects) { const schema = Array.isArray(sObjects) ? [schema_1.ApiSchemas.SaveResult] : schema_1.ApiSchemas.SaveResult; const args = { '@xmlns': 'urn:partner.soap.sforce.com', '@xmlns:ns1': 'sobject.partner.soap.sforce.com', 'ns1:sObjects': toSoapRecord(sObjects), }; return this._invoke('create', args, schema); } update(sObjects) { const schema = Array.isArray(sObjects) ? [schema_1.ApiSchemas.SaveResult] : schema_1.ApiSchemas.SaveResult; const args = { '@xmlns': 'urn:partner.soap.sforce.com', '@xmlns:ns1': 'sobject.partner.soap.sforce.com', 'ns1:sObjects': toSoapRecord(sObjects), }; return this._invoke('update', args, schema); } upsert(externalIdFieldName, sObjects) { const schema = Array.isArray(sObjects) ? [schema_1.ApiSchemas.UpsertResult] : schema_1.ApiSchemas.UpsertResult; const args = { '@xmlns': 'urn:partner.soap.sforce.com', '@xmlns:ns1': 'sobject.partner.soap.sforce.com', 'ns1:externalIDFieldName': externalIdFieldName, 'ns1:sObjects': toSoapRecord(sObjects), }; return this._invoke('upsert', args, schema); } delete(ids) { const schema = Array.isArray(ids) ? [schema_1.ApiSchemas.DeleteResult] : schema_1.ApiSchemas.DeleteResult; const args = { '@xmlns': 'urn:partner.soap.sforce.com', '@xmlns:ns1': 'sobject.partner.soap.sforce.com', 'ns1:ids': ids, }; return this._invoke('delete', args, schema); } /** * Undelete records from the recycle bin immediately */ undelete(ids) { const schema = [schema_1.ApiSchemas.UndeleteResult]; const args = { '@xmlns': 'urn:partner.soap.sforce.com', '@xmlns:ns1': 'sobject.partner.soap.sforce.com', 'ns1:ids': ids, }; return this._invoke('undelete', args, schema); } } exports.SoapApi = SoapApi; /*--------------------------------------------*/ /* * Register hook in connection instantiation for dynamically adding this API module features */ (0, jsforce_1.registerModule)('soap', (conn) => new SoapApi(conn)); exports.default = SoapApi;