0.2.0 • Published 8 months ago

@chrishrb/middy-env v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

middy-env

Simple environment variable middleware for the middy framework. Full typescript support.

Getting started

pnpm install @chrishrb/middy-env

Options

  • 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.
  • variables (object) (required): Map of environment variables to parse, where the key is the destination.

Supported Types

  • string
  • boolean
  • number

Usage

import middy from '@middy/core';
import env, { envVar } from '@chrishrb/middy-env';

const handler = middy(() => {});

// or export it from outside
process.env.MY_EXAMPLE_VARIABLE = 'myValue';

handler
  .use(
    env({
      variables: {
        myExampleVariable: envVar<boolean>('MY_EXAMPLE_VARIABLE', 'boolean'),
      },
      setToContext: true,
    }),
  )
  .before(async (request) => {
    // Full typescript support here
    console.log(request.context.envVariables.myExampleVariable);
  });

await handler(event, context);
0.2.0

8 months ago

0.1.0

8 months ago