diff --git a/README.md b/README.md index 163833c..1c51cee 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index 6a6fff2..97f113d 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.d.ts b/src/index.d.ts index b561ca8..ceb77a5 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -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. diff --git a/src/index.js b/src/index.js index c96b529..ee32b48 100644 --- a/src/index.js +++ b/src/index.js @@ -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. diff --git a/test/test.env b/test/test.env index c061b5c..f57573a 100644 --- a/test/test.env +++ b/test/test.env @@ -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 diff --git a/test/test.js b/test/test.js index 528b02d..18c6cfd 100644 --- a/test/test.js +++ b/test/test.js @@ -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/');