@@ -4,7 +4,6 @@ import * as fs from 'fs';
44
55// Type declarations for CommonJS runtime (rollup outputs CommonJS format)
66declare const __dirname : string ;
7- declare function require ( id : string ) : unknown ;
87
98export interface ContentstackEndpoints {
109 [ key : string ] : string | ContentstackEndpoints ;
@@ -26,36 +25,21 @@ export interface RegionsResponse {
2625
2726// Load regions.json at runtime from the dist/lib directory
2827function loadRegions ( ) : RegionsResponse {
29- try {
30- // Path to regions.json relative to the bundled file location
31- // The bundled file is at dist/index.es.js, and regions.json is at dist/lib/regions.json
32- // So from dist/index.es.js, the path is ./lib/regions.json
33- const regionsPath = path . join ( __dirname , 'lib/regions.json' ) ;
34-
35- // Try loading from the installed package location first
36- if ( fs . existsSync ( regionsPath ) ) {
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 {
3734 const regionsData = fs . readFileSync ( regionsPath , 'utf-8' ) ;
3835 return JSON . parse ( regionsData ) ;
36+ } catch ( error ) {
37+ throw new Error ( `Failed to parse regions.json: ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
3938 }
40-
41- // Fallback: try loading from package root (for development/testing)
42- const fallbackPath = path . join ( __dirname , '../../regions.json' ) ;
43- if ( fs . existsSync ( fallbackPath ) ) {
44- const regionsData = fs . readFileSync ( fallbackPath , 'utf-8' ) ;
45- return JSON . parse ( regionsData ) ;
46- }
47-
48- // If neither path works, try require as final fallback
49- try {
50- // This might work in some bundler scenarios
51- const regionsData = require ( './lib/regions.json' ) as RegionsResponse ;
52- return regionsData ;
53- } catch {
54- throw new Error ( 'regions.json file not found. Please ensure the package is properly installed and postinstall script has run.' ) ;
55- }
56- } catch ( error ) {
57- throw new Error ( `Failed to load regions.json: ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
5839 }
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.' ) ;
5943}
6044
6145// Cache the loaded regions data
@@ -68,17 +52,15 @@ function getRegions(): RegionsResponse {
6852 return cachedRegions ;
6953}
7054
71- export function getContentstackEndpoint ( region : string = 'us' , service : string = '' , omitHttps : boolean = false , localRegionsData ?: RegionsResponse ) : string | ContentstackEndpoints {
55+ export function getContentstackEndpoint ( region : string = 'us' , service : string = '' , omitHttps : boolean = false ) : string | ContentstackEndpoints {
7256 // Validate empty region before any processing
7357 if ( region === '' ) {
7458 console . warn ( 'Invalid region: empty or invalid region provided' ) ;
7559 throw new Error ( 'Unable to set the host. Please put valid host' ) ;
7660 }
7761
7862 try {
79- let regionsData : RegionsResponse ;
80-
81- regionsData = localRegionsData || getRegions ( ) ;
63+ const regionsData : RegionsResponse = getRegions ( ) ;
8264
8365 // Normalize the region input
8466 const normalizedRegion = region . toLowerCase ( ) . trim ( ) || 'us' ;
0 commit comments