Skip to content

Commit a7a8caa

Browse files
committed
refactor: update getContentstackEndpoint function to improve region normalization and error handling
1 parent 892855c commit a7a8caa

File tree

3 files changed

+33
-39
lines changed

3 files changed

+33
-39
lines changed

.commitlintrc.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
{
22
"extends": ["@commitlint/config-conventional"],
33
"rules": {
4-
"subject-case": [
5-
2,
6-
"always",
7-
["sentence-case", "start-case", "pascal-case", "upper-case", "lower-case"]
8-
],
4+
"subject-case": [0],
95
"subject-empty": [2, "never"],
106
"subject-full-stop": [2, "never", "."],
117
"type-enum": [

.talismanrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ fileignoreconfig:
99
- filename: .husky/pre-commit
1010
checksum: 5baabd7d2c391648163f9371f0e5e9484f8fb90fa2284cfc378732ec3192c193
1111
- filename: src/endpoints.ts
12-
checksum: 061295893d0ef7f3be959b65b857c543a4ad8439c07a1ecea2ebb5864eb99f18
12+
checksum: 721a1df93b02d04c1c19a76c171fe2748e4abb1fc3e43452e76fecfd8f384751

src/endpoints.ts

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export interface ContentstackEndpoints {
66
const DEFAULT_ENDPOINTS_URL = 'https://raw.githubusercontent.com/nadeem-cs/cs-endpoints/refs/heads/main/endpoints.json';
77

88

9-
export async function getContentstackEndpoint(region: string = 'us', service: string = 'CDA', omitHttps: boolean = false): Promise<string | ContentstackEndpoints> {
9+
export async function getContentstackEndpoint(region: string = 'us', service: string = '', omitHttps: boolean = false): Promise<string | ContentstackEndpoints> {
1010
try {
1111
const response = await fetch(DEFAULT_ENDPOINTS_URL);
1212

@@ -22,59 +22,57 @@ export async function getContentstackEndpoint(region: string = 'us', service: st
2222
throw new Error('Invalid JSON response from endpoints service');
2323
}
2424

25-
// Normalize region name
26-
let normalizedRegion = region.toLowerCase();
27-
28-
// Convert 'us' to 'aws_na' and handle existing cloud_us patterns
29-
if (normalizedRegion === 'us') {
30-
normalizedRegion = 'aws_na';
31-
} else if (normalizedRegion.includes('_') || normalizedRegion.includes('-')) {
32-
// Handle case where cloud provider is already included (e.g., 'aws_us' -> 'aws_na' or 'aws-us' -> 'aws_na')
25+
let normalizedRegion = region.toUpperCase();
26+
27+
// Convert 'US' to 'aws_na' and handle existing patterns
28+
if (normalizedRegion === 'US') {
29+
normalizedRegion = 'AWS-NA';
30+
} else if (normalizedRegion.includes('_') || normalizedRegion.includes('-')) { // (e.g., 'aws_us' -> 'aws_na' or 'aws-us' -> 'aws-na')
3331
const separator = normalizedRegion.includes('_') ? '_' : '-';
3432
const parts = normalizedRegion.split(separator);
35-
if (parts.length === 2 && parts[1] === 'us') {
36-
normalizedRegion = `${parts[0]}_na`;
33+
if (parts.length === 2 && parts[1] === 'US') {
34+
normalizedRegion = `${parts[0]}-NA`;
3735
} else if (parts.length === 2) {
38-
// Convert hyphen to underscore for consistency
39-
normalizedRegion = `${parts[0]}_${parts[1]}`;
36+
normalizedRegion = `${parts[0]}-${parts[1]}`;
4037
}
4138
} else if (!normalizedRegion.includes('_') && !normalizedRegion.includes('-') && normalizedRegion) {
42-
// If region doesn't contain a cloud provider separator, append 'aws'
43-
normalizedRegion = `aws_${normalizedRegion}`;
39+
normalizedRegion = `AWS-${normalizedRegion}`;
4440
}
4541

4642
if (normalizedRegion) {
47-
const parts = normalizedRegion.toUpperCase().split('_');
43+
const parts = normalizedRegion.toUpperCase().split('-');
4844
if (parts.length === 2) {
4945
const [cloud, region] = parts;
5046

5147
try {
52-
const endpoint = endpointsData[cloud][region][service];
48+
const endpoint = service ? endpointsData[cloud][region][service] : endpointsData[cloud][region];
49+
endpoint['Region'] = normalizedRegion;
5350

54-
return omitHttps ? endpoint.replace(/^https?:\/\//, '') : endpoint;
51+
return omitHttps ? stripHttps(endpoint) : endpoint;
5552
} catch (error) {
56-
console.warn(`Invalid region combination: ${cloud}_${region} - ${service}`);
57-
throw Error('Unable to set the host. Please put valid host');
53+
throw Error(`Invalid region combination: ${cloud}-${region} - ${service || 'all'}`);
5854
}
5955
} else {
60-
// Handle invalid region format (not cloud_region pattern)
61-
console.warn(`Invalid region format: ${normalizedRegion}`);
62-
throw Error('Unable to set the host. Please put valid host');
56+
throw Error(`Invalid region format: ${normalizedRegion}`);
6357
}
6458
} else {
6559
// Handle empty or falsy region
66-
console.warn('Invalid region: empty or invalid region provided');
67-
throw Error('Unable to set the host. Please put valid host');
60+
throw Error('Invalid region: empty or invalid region provided');
6861
}
6962
}
7063
} catch (error) {
71-
// Re-throw errors that are explicitly thrown by our logic
72-
if (error instanceof Error && error.message === 'Unable to set the host. Please put valid host') {
73-
throw error;
74-
}
75-
// If fetch fails or any other error occurs, return default host
76-
console.warn('Failed to fetch endpoints:', error);
64+
throw error;
7765
}
78-
79-
return 'cdn.contentstack.io';
8066
}
67+
68+
function stripHttps(endpoint: string | ContentstackEndpoints): string | ContentstackEndpoints {
69+
if (typeof endpoint === 'string') {
70+
return endpoint.replace(/^https?:\/\//, '');
71+
} else {
72+
const result: ContentstackEndpoints = {};
73+
for (const key in endpoint) {
74+
result[key] = stripHttps(endpoint[key]);
75+
}
76+
return result;
77+
}
78+
}

0 commit comments

Comments
 (0)