Skip to content

Commit 9d6ff90

Browse files
committed
feat: add get method to credentials
1 parent 5b82bcc commit 9d6ff90

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

adonis-typings/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
declare module '@ioc:Adonis/Addons/Credentials' {
22
export interface CredentialsContract {
3+
get(key?: string): string | Record<string, string>
34
initialize(): void
45
}
56

src/Credentials/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ import { CredentialsContract } from '@ioc:Adonis/Addons/Credentials'
1616
import { Vault } from '../Vault'
1717

1818
export 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
}

test/credentials.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)