Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chrisandrews7/middy-env
Environment variable middleware for the middy framework
https://github.com/chrisandrews7/middy-env
environment-variables middleware middy serverless
Last synced: about 15 hours ago
JSON representation
Environment variable middleware for the middy framework
- Host: GitHub
- URL: https://github.com/chrisandrews7/middy-env
- Owner: chrisandrews7
- License: mit
- Created: 2019-05-04T09:56:57.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-09-09T21:09:46.000Z (about 3 years ago)
- Last Synced: 2024-10-05T00:35:01.279Z (about 1 month ago)
- Topics: environment-variables, middleware, middy, serverless
- Language: JavaScript
- Homepage:
- Size: 325 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Middy Env
Environment variable middleware for the [middy](https://github.com/middyjs/middy) framework
[![npm version](https://img.shields.io/npm/v/middy-env.svg?style=flat)](https://www.npmjs.com/package/middy-env)
## Install
```bash
npm install middy-env
```The specified environment variables will be parsed and passed into the handler.
## Options
- `cache` (boolean) (optional): Set it to `true` to skip further lookups of environment variables. Defaults to `false`.
- `cacheExpiryInMillis` (int) (optional): Time in milliseconds for values to remain cached. Defaults to `undefined`.
- `setToContext` (boolean) (optional): This will assign the parsed values to the `context` object
of the function handler rather than to `process.env`. Defaults to `true`.
- `names` (object) (required): Map of environment variables to parse, where the key is the destination.
Either provide just the environment variable key. Or provide the key, type and fallback value e.g. `['KEY', 'string', 'fallbackValue']`.By default parameters are assigned to the function handler's `context` object. They can instead be assigned to the Node.js `process.env` object by setting the `setToContext` flag to `false`.
If no fallback value is provided a [ReferenceError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError) will be thrown if an environment variable is undefined.## Supported Types
- `string`
- `int`
- `float`
- `bool`## Usage
```js
const middy = require('middy');
const env = require('middy-env');const handler = (event, context, callback) => {
callback(null, `Hello ${context.firstName} ${context.lastName}`);
};module.exports = middy(handler)
.use(env({
names: {
firstName: ['FIRST_NAME', 'string', 'World'],
lastName: 'LAST_NAME'
},
cache: true,
cacheExpiryInMillis: 3600000
}));
```