-
Notifications
You must be signed in to change notification settings - Fork 68
/
sfdc.ts
110 lines (98 loc) · 3.37 KB
/
sfdc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { findKey } from '@salesforce/kit';
import { AnyJson, asJsonMap, isJsonMap, JsonMap, Optional } from '@salesforce/ts-types';
import { URL } from 'url';
export const sfdc = {
/**
* Returns `true` if a provided URL contains a Salesforce owned domain.
*
* @param urlString The URL to inspect.
*/
isSalesforceDomain: (urlString: string): boolean => {
let url: URL;
try {
url = new URL(urlString);
} catch (e) {
return false;
}
// Source https://help.salesforce.com/articleView?id=000003652&type=1
const whitelistOfSalesforceDomainPatterns: string[] = [
'.content.force.com',
'.force.com',
'.salesforce.com',
'.salesforceliveagent.com',
'.secure.force.com'
];
const whitelistOfSalesforceHosts: string[] = ['developer.salesforce.com', 'trailhead.salesforce.com'];
return whitelistOfSalesforceDomainPatterns.some(pattern => {
return url.hostname.endsWith(pattern) || whitelistOfSalesforceHosts.includes(url.hostname);
});
},
/**
* Converts an 18 character Salesforce ID to 15 characters.
*
* @param id The id to convert.
*/
trimTo15: (id?: string): Optional<string> => {
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: string): boolean => {
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: string): boolean => {
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: string): boolean => {
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: string): boolean => {
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 sectionBlacklist properties in the object to exclude from the search. e.g. a blacklist of `["a"]` and data of `{ "a": { "B" : "b"}}` would ignore `B` because it is in the object value under `a`.
*/
findUpperCaseKeys: (data?: JsonMap, sectionBlacklist: string[] = []): Optional<string> => {
let key: Optional<string>;
findKey(data, (val: AnyJson, k: string) => {
if (k[0] === k[0].toUpperCase()) {
key = k;
} else if (isJsonMap(val)) {
if (sectionBlacklist.includes(k)) {
return key;
}
key = sfdc.findUpperCaseKeys(asJsonMap(val));
}
return key;
});
return key;
}
};