UNPKG

@salesforce/core

Version:

Core libraries to interact with SFDX projects, orgs, and APIs.

142 lines 4.84 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.sfdc = void 0; const url_1 = require("url"); const kit_1 = require("@salesforce/kit"); const ts_types_1 = require("@salesforce/ts-types"); exports.sfdc = { /** * Returns `true` if a provided URL contains a Salesforce owned domain. * * @param urlString The URL to inspect. */ isSalesforceDomain: (urlString) => { let url; try { url = new url_1.URL(urlString); } catch (e) { return false; } // Source https://help.salesforce.com/articleView?id=000003652&type=1 const allowlistOfSalesforceDomainPatterns = [ '.cloudforce.com', '.content.force.com', '.force.com', '.salesforce.com', '.salesforceliveagent.com', '.secure.force.com', ]; const allowlistOfSalesforceHosts = ['developer.salesforce.com', 'trailhead.salesforce.com']; return allowlistOfSalesforceDomainPatterns.some((pattern) => { return url.hostname.endsWith(pattern) || allowlistOfSalesforceHosts.includes(url.hostname); }); }, /** * Converts an 18 character Salesforce ID to 15 characters. * * @param id The id to convert. */ trimTo15: (id) => { if (id && id.length && id.length > 15) { id = id.substring(0, 15); } return id; }, /** * Tests whether an API version matches the format `i.0`. * * @param value The API version as a string. */ validateApiVersion: (value) => { return value == null || /^[1-9]\d\.0$/.test(value); }, /** * Tests whether an email matches the format `me@my.org` * * @param value The email as a string. */ validateEmail: (value) => { return /^[^.][^@]*@[^.]+(\.[^.\s]+)+$/.test(value); }, /** * Tests whether a Salesforce ID is in the correct format, a 15- or 18-character length string with only letters and numbers * * @param value The ID as a string. */ validateSalesforceId: (value) => { return /[a-zA-Z0-9]{18}|[a-zA-Z0-9]{15}/.test(value) && (value.length === 15 || value.length === 18); }, /** * Tests whether a path is in the correct format; the value doesn't include the characters "[", "]", "?", "<", ">", "?", "|" * * @param value The path as a string. */ validatePathDoesNotContainInvalidChars: (value) => { // eslint-disable-next-line no-useless-escape return !/[\["\?<>\|\]]+/.test(value); }, /** * Returns the first key within the object that has an upper case first letter. * * @param data The object in which to check key casing. * @param sectionBlocklist properties in the object to exclude from the search. e.g. a blocklist of `["a"]` and data of `{ "a": { "B" : "b"}}` would ignore `B` because it is in the object value under `a`. */ findUpperCaseKeys: (data, sectionBlocklist = []) => { let key; kit_1.findKey(data, (val, k) => { if (k.substr(0, 1) === k.substr(0, 1).toUpperCase()) { key = k; } else if (ts_types_1.isJsonMap(val)) { if (sectionBlocklist.includes(k)) { return key; } key = exports.sfdc.findUpperCaseKeys(ts_types_1.asJsonMap(val)); } return key; }); return key; }, /** * Tests whether a given string is an access token * * @param value */ matchesAccessToken: (value) => { return /^(00D\w{12,15})![.\w]*$/.test(value); }, /** * Tests whether a given url is an internal Salesforce domain * * @param url */ isInternalUrl: (url) => { const INTERNAL_URL_PARTS = [ '.vpod.', 'stm.salesforce.com', 'stm.force.com', '.blitz.salesforce.com', '.stm.salesforce.ms', '.pc-rnd.force.com', '.pc-rnd.salesforce.com', ]; return (url.startsWith('https://gs1.') || exports.sfdc.isLocalUrl(url) || INTERNAL_URL_PARTS.some((part) => url.includes(part))); }, /** * Tests whether a given internal url runs on a local machine * * @param url */ isLocalUrl: (url) => { const LOCAL_PARTS = ['localhost.sfdcdev.', '.internal.']; return LOCAL_PARTS.some((part) => url.includes(part)); }, }; //# sourceMappingURL=sfdc.js.map