File tree Expand file tree Collapse file tree 3 files changed +35
-1
lines changed
Expand file tree Collapse file tree 3 files changed +35
-1
lines changed Original file line number Diff line number Diff line change 11declare module '@ioc:Adonis/Addons/Credentials' {
22 export interface CredentialsContract {
3+ get ( key ?: string ) : string | Record < string , string >
34 initialize ( ) : void
45 }
56
Original file line number Diff line number Diff line change @@ -16,12 +16,13 @@ import { CredentialsContract } from '@ioc:Adonis/Addons/Credentials'
1616import { Vault } from '../Vault'
1717
1818export class Credentials implements CredentialsContract {
19+ private initialized = false
1920 private env = 'development'
2021 private key : string | null = null
2122 private keyParam = 'APP_CREDENTIALS_KEY'
2223 private credentialsPath = join ( 'resources' , 'credentials' )
2324 private content : string = ''
24- private credentials : Object = { }
25+ private credentials : Record < string , string > = { }
2526
2627 constructor ( args ?: {
2728 env ?: string
@@ -94,11 +95,25 @@ export class Credentials implements CredentialsContract {
9495 }
9596 }
9697
98+ public get ( key ?: string ) : string | Record < string , string > {
99+ if ( ! this . initialized ) {
100+ this . initialize ( )
101+ }
102+
103+ if ( key ) {
104+ return this . credentials [ key ]
105+ } else {
106+ return this . credentials
107+ }
108+ }
109+
97110 public initialize ( ) : void {
98111 this . check ( )
99112 this . read ( )
100113 this . validate ( )
101114 this . parse ( )
102115 this . populate ( )
116+
117+ this . initialized = true
103118 }
104119}
Original file line number Diff line number Diff line change @@ -83,6 +83,24 @@ test.group('Credentials', (group) => {
8383 assert . equal ( process . env . HELLO , 'world' )
8484 } )
8585
86+ test ( 'should pick up key from credentials and return in' , async ( assert ) => {
87+ const credentials = new Credentials ( {
88+ env : 'test' ,
89+ credentialsPath : app . resourcesPath ( '/credentials' ) ,
90+ } )
91+
92+ assert . equal ( credentials . get ( 'HELLO' ) , 'world' )
93+ } )
94+
95+ test ( 'should pick up all keys from credentials and return them' , async ( assert ) => {
96+ const credentials = new Credentials ( {
97+ env : 'test' ,
98+ credentialsPath : app . resourcesPath ( '/credentials' ) ,
99+ } )
100+
101+ assert . deepStrictEqual ( credentials . get ( ) , { HELLO : 'world' } )
102+ } )
103+
86104 test ( 'should throw an error when no credentials key' , async ( assert ) => {
87105 await fs . remove ( 'resources/credentials/test.key' )
88106
You can’t perform that action at this time.
0 commit comments