1- import regions from '../regions.json'
1+ /// <reference types="node" />
2+ import * as path from 'path' ;
3+ import * as fs from 'fs' ;
4+
5+ // Type declarations for CommonJS runtime (rollup outputs CommonJS format)
6+ declare const __dirname : string ;
7+
28export interface ContentstackEndpoints {
39 [ key : string ] : string | ContentstackEndpoints ;
410}
@@ -17,17 +23,44 @@ export interface RegionsResponse {
1723 regions : RegionData [ ] ;
1824}
1925
20- export function getContentstackEndpoint ( region : string = 'us' , service : string = '' , omitHttps : boolean = false , localRegionsData ?: RegionsResponse ) : string | ContentstackEndpoints {
26+ // Load regions.json at runtime from the dist/lib directory
27+ function loadRegions ( ) : RegionsResponse {
28+ // The bundled file is at dist/index.es.js, regions.json is at dist/lib/regions.json
29+ // So __dirname will be 'dist/' and we need to go to 'dist/lib/regions.json'
30+ const regionsPath = path . join ( __dirname , 'lib' , 'regions.json' ) ;
31+
32+ if ( fs . existsSync ( regionsPath ) ) {
33+ try {
34+ const regionsData = fs . readFileSync ( regionsPath , 'utf-8' ) ;
35+ return JSON . parse ( regionsData ) ;
36+ } catch ( error ) {
37+ throw new Error ( `Failed to parse regions.json: ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
38+ }
39+ }
40+
41+ // If not found, throw clear error
42+ throw new Error ( 'regions.json file not found at dist/lib/regions.json. Please ensure the package is properly installed and postinstall script has run.' ) ;
43+ }
44+
45+ // Cache the loaded regions data
46+ let cachedRegions : RegionsResponse | null = null ;
47+
48+ function getRegions ( ) : RegionsResponse {
49+ if ( ! cachedRegions ) {
50+ cachedRegions = loadRegions ( ) ;
51+ }
52+ return cachedRegions ;
53+ }
54+
55+ export function getContentstackEndpoint ( region : string = 'us' , service : string = '' , omitHttps : boolean = false ) : string | ContentstackEndpoints {
2156 // Validate empty region before any processing
2257 if ( region === '' ) {
2358 console . warn ( 'Invalid region: empty or invalid region provided' ) ;
2459 throw new Error ( 'Unable to set the host. Please put valid host' ) ;
2560 }
2661
2762 try {
28- let regionsData : RegionsResponse ;
29-
30- regionsData = regions ;
63+ const regionsData : RegionsResponse = getRegions ( ) ;
3164
3265 // Normalize the region input
3366 const normalizedRegion = region . toLowerCase ( ) . trim ( ) || 'us' ;
@@ -64,7 +97,7 @@ export function getContentstackEndpoint(region: string = 'us', service: string =
6497
6598 if ( ! endpoint ) {
6699 // For invalid services, return undefined (as expected by some tests)
67- return undefined as any ;
100+ return undefined as unknown as ContentstackEndpoints ;
68101 }
69102 } else {
70103 return omitHttps ? stripHttps ( regionData . endpoints ) : regionData . endpoints ;
@@ -78,7 +111,8 @@ export function getContentstackEndpoint(region: string = 'us', service: string =
78111}
79112
80113function getDefaultEndpoint ( service : string , omitHttps : boolean ) : string {
81- const defaultEndpoints : ContentstackEndpoints = regions . regions . find ( r => r . isDefault ) ?. endpoints || { } ;
114+ const regions = getRegions ( ) ;
115+ const defaultEndpoints : ContentstackEndpoints = regions . regions . find ( ( r : RegionData ) => r . isDefault ) ?. endpoints || { } ;
82116
83117 const value = defaultEndpoints [ service ] ;
84118 const endpoint = typeof value === 'string' ? value : 'https://cdn.contentstack.io' ;
0 commit comments