-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathregion-handler.ts
More file actions
169 lines (149 loc) · 5.22 KB
/
region-handler.ts
File metadata and controls
169 lines (149 loc) · 5.22 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { configHandler } from '@contentstack/cli-utilities';
import { getContentstackEndpoint } from '@contentstack/utils';
import { Region, RegionsMap } from '../interfaces';
function validURL(str) {
const pattern = new RegExp(
'^(https?:\\/\\/)?' + // protocol (http or https)
'([a-zA-Z0-9.-]+|' + // domain name
'((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))' + // IP address
'(:\\d+)?' + // port
'(/[-a-zA-Z0-9_.~+-]*)*' + // path
'(\\?[;&a-zA-Z0-9_.~+=-]*)?' + // query string
'(\\#[-a-zA-Z0-9_]*)?$', // fragment
'i',
);
return pattern.test(str);
}
/**
* Helper function to build region object from @contentstack/utils
* @param {string} regionKey - Region identifier
* @returns {object} Region object with all necessary URLs
*/
function getRegionObject(regionKey: string): Region {
try {
// getContentstackEndpoint handles all aliases defined in regions.json
const endpoints = getContentstackEndpoint(regionKey) as any;
if (typeof endpoints === 'string') {
throw new Error('Invalid endpoint response');
}
return {
name: regionKey,
cma: endpoints.contentManagement,
cda: endpoints.contentDelivery,
uiHost: endpoints.application,
developerHubUrl: endpoints.developerHub,
launchHubUrl: endpoints.launch,
personalizeUrl: endpoints.personalizeManagement,
composableStudioUrl: endpoints.composableStudio,
assetManagementUrl: endpoints.assetManagement,
};
} catch (error) {
return null;
}
}
/**
* Get all available regions dynamically
* This creates a regions object similar to the old hardcoded one but using @contentstack/utils
*/
function getAvailableRegions() {
const regionKeys = ['NA', 'AWS-NA', 'EU', 'AWS-EU', 'AU', 'AWS-AU', 'AZURE-NA', 'AZURE-EU', 'GCP-NA', 'GCP-EU'];
const regions: RegionsMap = {};
for (const key of regionKeys) {
const regionObj = getRegionObject(key);
if (regionObj) {
regions[key] = regionObj;
}
}
return regions;
}
// Available region list - now dynamically generated
const regions = getAvailableRegions();
class UserConfig {
/**
*
* Set region to config store
* @param {string} region It Can be AWS-NA, AWS-EU, AWS-AU, AZURE-NA, AZURE-EU, GCP-NA, GCP-EU
* @returns {object} region object with cma, cda, region property
*/
setRegion(region) {
const selectedRegion = regions[region];
if (selectedRegion) {
configHandler.set('region', selectedRegion);
return selectedRegion;
}
}
/**
*
* Get current host set for CLI
* @returns { object } Object contains url for cma and cda, and region to which it is pointing to
*/
getRegion() {
const regionDetails = configHandler.get('region');
if (regionDetails) return regionDetails;
// returns AWS-NA region if not found in config
return regions['AWS-NA'];
}
/**
*
* Set region to config store
* @param {object} regionObject should contain cma, cda, region property
* @returns {object} region object with cma, cda, region(name of region) property
*/
setCustomRegion(regionObject) {
if (this.validateRegion(regionObject)) {
regionObject = this.sanitizeRegionObject(regionObject);
configHandler.set('region', regionObject);
return regionObject;
}
throw new TypeError(
'Custom region should include valid cma(URL), cda(URL), name(String) (Name for the Region) property.',
);
}
/**
*
* Set rateLimit to config store
* @param {object} rateLimitObject should contain rate limit property
* @returns {object} ratelimit object with limit property
*/
// setCustomRateLimit(rateLimitObject) {
// if(rateLimitObject !== undefined && isNaN(rateLimitObject)) {
// throw new TypeError(rateLimitObject + " is not a number, Please provide number as a rate limit")
// } else {
// config.set('rate-limit', rateLimitObject)
// return rateLimitObject
// }
// }
/**
* Validate given region JSON object
* @param {*} regionObject JSON object needs to be validated
* @returns {boolean} True if contains cma, cda and region property otherwise false
*/
validateRegion(regionObject) {
if (regionObject.cma && regionObject.cda && regionObject.uiHost && regionObject.name) {
if (validURL(regionObject.cma) && validURL(regionObject.cda)) return true;
}
return false;
}
/**
* Sanitize given region JSON object by removing any other properties than cma, cda and region
* @param { object } regionObject JSON object needs to be sanitized
* @returns { object } JSON object with only valid keys for region
*/
sanitizeRegionObject(regionObject) {
const sanitizedRegion = {
cma: regionObject.cma,
cda: regionObject.cda,
uiHost: regionObject.uiHost,
name: regionObject.name,
developerHubUrl: regionObject['developerHubUrl'],
personalizeUrl: regionObject['personalizeUrl'],
launchHubUrl: regionObject['launchHubUrl'],
composableStudioUrl: regionObject['composableStudioUrl'],
assetManagementUrl: regionObject['assetManagementUrl'],
};
return sanitizedRegion;
}
}
// Export the regions object for use in other packages
export { regions };
export default new UserConfig();