UNPKG

@salesforce/sobject-metadata

Version:

Retrieve Salesforce object metadata from connected org

150 lines 6.05 kB
"use strict"; /* * Copyright (c) 2020, salesforce.com, inc. * All rights reserved. * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ Object.defineProperty(exports, "__esModule", { value: true }); exports.SObjectDescribeAPI = exports.SObjectCategory = void 0; var SObjectCategory; (function (SObjectCategory) { SObjectCategory["ALL"] = "ALL"; SObjectCategory["STANDARD"] = "STANDARD"; SObjectCategory["CUSTOM"] = "CUSTOM"; })(SObjectCategory = exports.SObjectCategory || (exports.SObjectCategory = {})); class SObjectDescribeAPI { constructor(connection) { // URL constants this.SERVICESPATH = 'services/data'; // the targetVersion should be consistent with the Cli even if only using REST calls this.TARGETVERSION = '49.0'; this.VERSIONPREFIX = 'v'; this.SOBJECTS = 'sobjects'; this.BATCH = 'composite/batch'; this.DESCRIBE = 'describe'; this.CLIENT_ID = 'sfdx-vscode'; this.commonHeaders = { 'User-Agent': 'salesforcedx-extension', 'Sforce-Call-Options': `client=${this.CLIENT_ID}`, }; this.connection = connection; } async describeSObject(sObjectName, lastRefreshDate) { try { let response; let options; options = this.buildSingleXHROptions(sObjectName, lastRefreshDate); response = (await this.connection.requestRaw(options)); const sObject = response.body ? JSON.parse(response.body) : undefined; return Promise.resolve({ sObjectName, result: sObject, timestamp: response.headers['date'], }); } catch (error) { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const errorMsg = // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access 'responseText' in error ? error.responseText : error.message; return Promise.reject(errorMsg); } } async describeSObjectBatch(types, nextToProcess) { try { let response; let options; options = this.buildBatchXHROptions(types, nextToProcess); response = (await this.connection.requestRaw(options)); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment const timestamp = response.headers['date']; const batchResponse = JSON.parse(response.body); const fetchedObjects = []; let i = nextToProcess; for (const sr of batchResponse.results) { if (sr.result instanceof Array) { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access if (sr.result[0].errorCode && sr.result[0].message) { // eslint-disable-next-line console.log(`Error: ${sr.result[0].message} - ${types[i]}`); } } i++; fetchedObjects.push({ sObjectName: sr.result.name, result: sr.result, timestamp, }); } return Promise.resolve(fetchedObjects); } catch (error) { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const errorMsg = // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access 'responseText' in error ? error.responseText : error.message; return Promise.reject(errorMsg); } } buildSObjectDescribeURL(sObjectName, fullUrl) { const urlElements = []; if (fullUrl) { urlElements.push(this.connection.instanceUrl, this.SERVICESPATH); } urlElements.push(this.getVersion(), this.SOBJECTS, sObjectName, this.DESCRIBE); return urlElements.join('/'); } buildBatchRequestURL() { const batchUrlElements = [ this.connection.instanceUrl, this.SERVICESPATH, this.getVersion(), this.BATCH, ]; return batchUrlElements.join('/'); } buildBatchRequestBody(types, nextToProcess) { const batchSize = 25; const batchRequest = { batchRequests: [] }; for (let i = nextToProcess; i < nextToProcess + batchSize && i < types.length; i++) { batchRequest.batchRequests.push({ method: 'GET', url: this.buildSObjectDescribeURL(types[i]), }); } return batchRequest; } buildSingleXHROptions(sObjectName, lastRefreshDate) { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment let additionalHeaders = {}; if (lastRefreshDate) { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment additionalHeaders = { 'If-Modified-Since': lastRefreshDate, }; } return { method: 'GET', url: this.buildSObjectDescribeURL(sObjectName, true), // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment headers: Object.assign(Object.assign({}, this.commonHeaders), additionalHeaders), }; } buildBatchXHROptions(types, nextToProcess) { const batchRequest = this.buildBatchRequestBody(types, nextToProcess); return { method: 'POST', url: this.buildBatchRequestURL(), headers: Object.assign({}, this.commonHeaders), body: JSON.stringify(batchRequest), }; } getVersion() { return `${this.VERSIONPREFIX}${this.TARGETVERSION}`; } } exports.SObjectDescribeAPI = SObjectDescribeAPI; //# sourceMappingURL=sobjectApi.js.map