@@ -6,7 +6,7 @@ export interface ContentstackEndpoints {
66const 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 ( / ^ h t t p s ? : \/ \/ / , '' ) : 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 ( / ^ h t t p s ? : \/ \/ / , '' ) ;
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