UNPKG

@heroku/salesforce-sdk-nodejs

Version:

Salesforce SDK for Heroku Apps.

45 lines (44 loc) 1.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getConnection = getConnection; const request_1 = require("../utils/request"); const org_1 = require("../sdk/org"); const HTTP_REQUEST = new request_1.HttpRequestUtil(); async function getConnection(name) { if (!name) { throw Error(`Connection name not provided`); } const addonEndpoint = process.env.HEROKU_INTEGRATION_API_URL || process.env.HEROKU_INTEGRATION_STAGING_API_URL; if (!addonEndpoint) { throw Error(`Heroku Integration add-on not provisioned on app or endpoint not provided`); } const addonAuthToken = process.env.HEROKU_INTEGRATION_TOKEN; if (!addonAuthToken) { throw Error(`Heroku Integration add-on not provisioned on app or authorization token not found`); } const authUrl = `${addonEndpoint}/invocations/authorization`; const opts = { method: 'POST', headers: { 'Authorization': `Bearer ${addonAuthToken}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ 'org_name': name }), retry: { limit: 1 } }; let response; try { response = await HTTP_REQUEST.request(authUrl, opts); } catch (err) { throw new Error(`Unable to get connection ${name}: ${err.message}`); } if (response.message) { throw new Error(response.message); } return new org_1.OrgImpl(response.access_token, response.api_version, response.namespace, response.org_id, response.org_domain_url, response.user_id, response.username, response.datacloud_token, response.datacloud_instance_url); }