@@ -36,13 +36,14 @@ export type {
3636
3737
3838export default class Phase {
39- token : string ;
40- host : string ;
41- tokenType : string | null = null ;
42- version : string | null = null ;
43- bearerToken : string | null = null ;
44- keypair : PhaseKeyPair = { } as PhaseKeyPair ;
45- apps : App [ ] = [ ] ;
39+ private token : string ;
40+ private host : string ;
41+ private _tokenType : string | null = null ;
42+ private _version : string | null = null ;
43+ private _bearerToken : string | null = null ;
44+ private _keypair : PhaseKeyPair = { } as PhaseKeyPair ;
45+ private apps : App [ ] = [ ] ;
46+ _isInitialized : boolean = false
4647
4748 constructor ( token : string , host ?: string ) {
4849 this . host = host || DEFAULT_HOST ;
@@ -64,7 +65,7 @@ export default class Phase {
6465 const data : SessionResponse = response . data ;
6566
6667 // Set the keypair for the class instance
67- this . keypair = {
68+ this . _keypair = {
6869 publicKey : this . token . split ( ":" ) [ 3 ] ,
6970 privateKey : await reconstructPrivateKey (
7071 data . wrapped_key_share ,
@@ -80,7 +81,7 @@ export default class Phase {
8081 const { publicKey, privateKey, salt } = await unwrapEnvKeys (
8182 envData . wrapped_seed ,
8283 envData . wrapped_salt ,
83- this . keypair
84+ this . _keypair
8485 ) ;
8586
8687 const { id, name } = envData . environment ;
@@ -106,6 +107,7 @@ export default class Phase {
106107
107108 const apps : App [ ] = await Promise . all ( appPromises ) ;
108109 this . apps = apps ;
110+ this . _isInitialized = true
109111 } catch ( error ) {
110112 if ( axios . isAxiosError ( error ) && error . response ) {
111113 throw `Error: ${ error . response . status } : ${
@@ -139,18 +141,18 @@ export default class Phase {
139141 const [ tokenType , version , bearerToken ] = token . split ( ":" ) ;
140142
141143 // Assign the parsed values to the instance properties
142- this . tokenType = tokenType . includes ( "user" )
144+ this . _tokenType = tokenType . includes ( "user" )
143145 ? "User"
144146 : version === "v1"
145147 ? "Service"
146148 : "ServiceAccount" ;
147- this . version = version ;
148- this . bearerToken = bearerToken ;
149+ this . _version = version ;
150+ this . _bearerToken = bearerToken ;
149151 }
150152
151153 private getAuthHeaders ( ) {
152154 return {
153- Authorization : `Bearer ${ this . tokenType } ${ this . bearerToken } ` ,
155+ Authorization : `Bearer ${ this . _tokenType } ${ this . _bearerToken } ` ,
154156 Accept : "application/json" ,
155157 "X-Use-Camel-Case" : true ,
156158 "User-agent" : `phase-node-sdk/${ LIB_VERSION } ` ,
@@ -159,6 +161,9 @@ export default class Phase {
159161
160162 async get ( options : GetSecretOptions ) : Promise < Secret [ ] > {
161163 return new Promise < Secret [ ] > ( async ( resolve , reject ) => {
164+
165+ if ( ! this . _isInitialized ) await this . init ( )
166+
162167 const cache = new Map < string , string > ( ) ;
163168
164169 const app = this . apps . find ( ( app ) => app . id === options . appId ) ;
@@ -170,7 +175,7 @@ export default class Phase {
170175 ( e ) => e . name . toLowerCase ( ) === options . envName . toLowerCase ( )
171176 ) ;
172177 if ( ! env ) {
173- return reject ( `Invalid environment name: ${ options . envName } ` ) ;
178+ return reject ( `Invalid environment name: ' ${ options . envName } ' ` ) ;
174179 }
175180
176181 try {
@@ -313,14 +318,16 @@ export default class Phase {
313318 return new Promise < void > ( async ( resolve , reject ) => {
314319 const { appId, envName } = options ;
315320
321+ if ( ! this . _isInitialized ) await this . init ( )
322+
316323 const app = this . apps . find ( ( app ) => app . id === appId ) ;
317324 if ( ! app ) {
318325 throw "Invalid app id" ;
319326 }
320327
321- const env = app ?. environments . find ( ( env ) => env . name === envName ) ;
328+ const env = app ?. environments . find ( ( env ) => env . name . toLowerCase ( ) === envName . toLowerCase ( ) ) ;
322329 if ( ! env ) {
323- throw " Invalid environment name" ;
330+ throw ` Invalid environment name: ' ${ envName } '` ;
324331 }
325332
326333 try {
@@ -379,14 +386,16 @@ export default class Phase {
379386 return new Promise < void > ( async ( resolve , reject ) => {
380387 const { appId, envName } = options ;
381388
389+ if ( ! this . _isInitialized ) await this . init ( )
390+
382391 const app = this . apps . find ( ( app ) => app . id === appId ) ;
383392 if ( ! app ) {
384393 throw "Invalid app id" ;
385394 }
386395
387- const env = app ?. environments . find ( ( env ) => env . name === envName ) ;
396+ const env = app ?. environments . find ( ( env ) => env . name . toLowerCase ( ) === envName . toLowerCase ( ) ) ;
388397 if ( ! env ) {
389- throw " Invalid environment name" ;
398+ throw ` Invalid environment name: ' ${ envName } '` ;
390399 }
391400
392401 try {
@@ -440,14 +449,16 @@ export default class Phase {
440449 try {
441450 const { appId, envName } = options ;
442451
452+ if ( ! this . _isInitialized ) await this . init ( )
453+
443454 const app = this . apps . find ( ( app ) => app . id === appId ) ;
444455 if ( ! app ) {
445456 throw "Invalid app id" ;
446457 }
447458
448- const env = app ?. environments . find ( ( env ) => env . name === envName ) ;
459+ const env = app ?. environments . find ( ( env ) => env . name . toLowerCase ( ) === envName . toLowerCase ( ) ) ;
449460 if ( ! env ) {
450- throw " Invalid environment name" ;
461+ throw ` Invalid environment name: ' ${ envName } '` ;
451462 }
452463
453464 const requestHeaders = { environment : env . id } ;
0 commit comments