Skip to content

Commit

Permalink
feat(shape): provideMany optionally supply a function to shape the re…
Browse files Browse the repository at this point in the history
…turned object

Supply a function that alters the shape of the final object when calling provideMany. Supply a
string, a function or array of functions to modify property names.
  • Loading branch information
jfrazx committed Mar 26, 2020
1 parent 86541dc commit a162fe5
Show file tree
Hide file tree
Showing 15 changed files with 783 additions and 628 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mocha.opts

typings/
node_modules/
src/
spec/
test/
.vscode/
Expand Down
2 changes: 2 additions & 0 deletions .nycrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"functions": 99,
"branches": 95,
"reporter": ["lcov", "text-summary", "html"],
"include": ["src/**/*.ts"],
"exclude": ["test/**/*.spec.ts"],
"all": true,
"cache": true,
"report-dir": "./coverage",
Expand Down
19 changes: 1 addition & 18 deletions .releaserc
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
{
"branch": "master",
"plugins": [
[
"@semantic-release/commit-analyzer",
{
"preset": "angular",
"releaseRules": [
{ "type": "docs", "scope": "README", "release": "patch" },
{ "type": "refactor", "release": "patch" },
{ "type": "style", "release": "patch" }
],
"parserOpts": {
"noteKeywords": ["BREAKING CHANGE", "BREAKING CHANGES"]
}
}
],
"@semantic-release/release-notes-generator"
]
"branch": "master"
}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ script:
- npm run coverage:test
- npm run coverage:check
- npm run coverage:report
- npm run compile
- npm run build
- npm run coverage:post
deploy:
provider: script
Expand Down
75 changes: 58 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ Use `Envirator` to provide and manipulate environment variables.
```typescript
import { Envirator } from '@status/envirator';

const envirator = new Envirator();
const env = new Envirator();

const port = envirator.provide('PORT');
const port = env.provide('PORT');
```

If `PORT` exists it will be of type `string`. Otherwise, a message will print to the console and immediately exit.
Expand All @@ -48,7 +48,7 @@ const envOpts: EnvInitOptions = {
keyToJsProp: true,
};

const envirator = new Envirator(envOpts);
const env = new Envirator(envOpts);
```

### Initialization Options
Expand All @@ -67,13 +67,13 @@ Load a config by specifying a path or based on the current environment.
```typescript
import { Envirator } from '@status/envirator';

const envirator = new Envirator();
const env = new Envirator();

envirator.load();
env.load();

// or

envirator.load('./path/to/config');
env.load('./path/to/config');
```

Environment based loading expects a file named `.env.environment` in the root of your project. For example, a development based environment would attempt to load `.env.development`.
Expand All @@ -87,7 +87,7 @@ Providing environment variables is what Envirator does best! There are a few opt
import { Envirator, EnvOptions } from '@status/envirator';
import * as winston from 'winston';

const envirator = new Envirator();
const env = new Envirator();

const envOpts: EnvOptions = {
warnOnly: true,
Expand All @@ -97,7 +97,7 @@ const envOpts: EnvOptions = {
mutators: parseInt,
};

const port = envirator.provide<number>('PORT', envOpts);
const port = env.provide<number>('PORT', envOpts);
```

In addition to the three options previously discussed, you may provide a default value for use in the event an environment variable does not exist.
Expand All @@ -108,7 +108,7 @@ A single function or an array of functions may be passed to modify the extracted
Often you may need many environment variables.

```typescript
import { EnvManyOptions } from '@status/codes';
import { EnvManyOptions } from '@status/envirator';

const envVar: EnvManyOptions = {
key: 'SOME_VAR',
Expand All @@ -119,31 +119,72 @@ const envVar: EnvManyOptions = {
keyToJsProp: true,
};

const { NODE_ENV, someVar, CONTENT: content } = envirator.provideMany([
const { NODE_ENV, someVar, CONTENT: content } = env.provideMany([
'NODE_ENV',
envVar,
{ key: 'CONTENT' },
]);
```

Additionally you may wish to change the property or the entire shape of the returned object.

```typescript
const env = new Env({ keyToJsProp: true });

interface JwtOptions {
secret: string;
signOptions: {
iss: string;
algorithm: string;
};
}

function toJwtOptions({ secret, iss, algorithm }: EnvManyResult): JwtOptions {
return {
secret,
signOptions: {
algorithm,
iss,
},
};
}

const jwtOptions: JwtOptions = env.provideMany(
[
{ key: 'JWT_SECRET', keyTo: [() => 'secret'], defaultValue: 'token' },
{
key: 'JWT_ALGORITHM',
keyTo: key => 'algorithm',
defaultValue: 'RSA',
},
{
key: 'JWT_ISSUER',
keyTo: 'iss',
defaultValue: 'something',
},
],
toJwtOptions
);
```

### Set Values

You may set environment variables by passing an object or a single key value pair.

```typescript
import { Envirator } from '@status/envirator';

const envirator = new Envirator();
const env = new Envirator();

envirator.setEnv('NODE_ENV', 'development');
env.setEnv('NODE_ENV', 'development');

const envVars = {
PORT: 5200,
SESSION: 'session-key',
COOKIE: 'cookie-monster',
};

envirator.set(envVars);
env.set(envVars);
```

All values are set as strings. No checks are made to ensure the key currently does not exist.
Expand All @@ -153,18 +194,18 @@ All values are set as strings. No checks are made to ensure the key currently do
Envirator also has a handy property that indicates if the current environment is production.

```typescript
if (envirator.isProduction) {
if (env.isProduction) {
// do stuff
}
```

You can retrieve or set the current environment:

```typescript
envirator.currentEnv;
env.currentEnv;
// => development or whatever the current environment may be (always lowercase)

envirator.currentEnv = 'test';
env.currentEnv = 'test';
// equivalent to 'envirator.setEnv('NODE_ENV', 'test');' NODE_ENV is whatever was set at initialization
```

Expand Down Expand Up @@ -205,8 +246,8 @@ Create a config that includes envirator to provide in other files.

```typescript
export const config = {
environment: env.currentEnv,
port: env.provide<number>('PORT', { mutators: parseInt }),
environment: env.currentEnv,
env,
};

Expand Down
1 change: 0 additions & 1 deletion index.ts

This file was deleted.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"name": "@status/envirator",
"version": "0.0.0-development",
"description": "Ensure environment variable availability during program initialization",
"main": "index.js",
"main": "dist/index.js",
"scripts": {
"commit": "git-cz",
"test": "mocha --require ts-node/register test/*.spec.ts",
"tsc": "tsc",
"compile": "tsc -d",
"prebuild": "rimraf dist",
"build": "tsc -d",
"coverage:check": "nyc check-coverage",
"coverage:report": "nyc report --reporter=text-lcov > coverage.lcov",
"coverage:post": "codecov",
Expand Down Expand Up @@ -58,6 +59,7 @@
"husky": "^4.2.3",
"mocha": "^7.0.1",
"nyc": "^15.0.0",
"rimraf": "^3.0.2",
"semantic-release": "^17.0.3",
"semantic-release-cli": "^5.1.2",
"sinon": "^9.0.0",
Expand Down
Loading

0 comments on commit a162fe5

Please sign in to comment.