Skip to content

Commit

Permalink
feat: getAWS creds
Browse files Browse the repository at this point in the history
  • Loading branch information
recursivefunk committed Aug 18, 2024
1 parent 4c20512 commit 2cdef6c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,27 @@ Why would one use `env.getUrl()` if one just wishes to grab the url string value

As of now, `http`, `redis` and `postgresql` are the only supported protocols. Other protocols will return `null`. I'm not against adding new protocol support, but these are the ones that seemed most obvious to me. If you want other protocols supported, I'd recommend making a PR. You may create an issue, but I can't guarantee when I'll get around to implementation.

Fetch AWS Credentials

```javascript
const {
awsKeyId,
awsSecretAccessKey,
awsDefaultRegion,
} = env.getAWS();

// Use defaults
const {
awsKeyId,
awsSecretAccessKey,
awsDefaultRegion,
} = env.getAWS({
keyId: 'keyId',
accessKey: 'accessKey',
region: 'region',
});
```

## Shortcut Methods

```javascript
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "good-env",
"version": "7.0.0",
"version": "7.1.0",
"description": "Better environment variable handling for Twelve-Factor node apps",
"main": "src/index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
declare module "good-env" {
export const getAWS: (defaults?: object) => object;
/**
* @description Finds the URL string in the environment associated with the given key. If
* it's found, the function tries to construct a URL object. If the URL is invalid, return null.
Expand Down
15 changes: 15 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ const validType = item => ['number', 'boolean', 'string'].includes(item);

module.exports = Object
.create({
getAWS ({
keyId,
accessKey,
region
} = {}) {
const awsKeyId = this.get('AWS_ACCESS_KEY_ID', keyId);
const awsSecretAccessKey = this.get('AWS_SECRET_ACCESS_KEY', accessKey);
const awsDefaultRegion = this.get('AWS_DEFAULT_REGION', region);

return {
awsKeyId,
awsSecretAccessKey,
awsDefaultRegion
};
},
/**
* @description Finds the URL string in the environment associated with the given key. If
* it's found, the function tries to construct a URL object. If the URL is invalid, return null.
Expand Down
3 changes: 3 additions & 0 deletions test/test.env
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ ENDPOINT=https://foo.com
REDIS_URL=redis://user:password@ipaddress:6379/0
POSTGRES_URL=postgresql://user:password@localhost/otherdb?connect_timeout=10&application_name=myapp
UNSUPPORTED_URL=beep://baz.bop
AWS_ACCESS_KEY_ID=exampleaccesskeyid
AWS_SECRET_ACCESS_KEY=examplesecretaccesskey
AWS_DEFAULT_REGION=us-east-1
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ require('dotenv').config({ path: 'test/test.env' });
const test = require('tape');
const env = require('../src/index');

test('it gets AWS creds', (t) => {
const {
awsKeyId,
awsSecretAccessKey,
awsDefaultRegion
} = env.getAWS();

t.equals(awsKeyId, 'exampleaccesskeyid');
t.equals(awsSecretAccessKey, 'examplesecretaccesskey');
t.equals(awsDefaultRegion, 'us-east-1');

t.end();
});

test('it parses a valid url', (t) => {
const { httpOk, href } = env.getUrl('ENDPOINT');
t.equals(href, 'https://foo.com/');
Expand Down

0 comments on commit 2cdef6c

Please sign in to comment.