@evokegroup/aws-config

1.0.2 • Public • Published

@evokegroup/aws-config

Configuration base class for AWS Lambda functions

Class: EvokeAwsConfig

constructor(event)

Parameters Type Default Description
event EvokeAws.Event, object The event data passed to the Lambda function

Usage

const EvokeAwsConfig = require('@evokegroup/aws-config');

class Config extends EvokeAwsConfig {
  site = null;
  environment = null;

  constructor(event) {
    super(event); 
    this.init();
  }
}

exports.handler = async (event) => {
  return new Promise((resolve, reject) => {
    const config = new Config(event);
    resolve({
      statusCode: 200
    });
  });
};

Properties

Property Type Description
event EvokeAws.Event The event data

Methods

get(...args)

Get the value of a configuration property or a new object containing the specified properties and default values. If the property does not exist it will be added to the object using the default value or null.

Parameters Type Default Description
args[0] object, Array<string>, string A string, Array<string> or object specifying the property/properties to return
args[1] any If args[0] is a string the default value to return. If args[1] is a function the value returned will result of that function.

Usage

const EvokeAwsConfig = require('@evokegroup/aws-config');

// Simulate the setting of environment variables from Lambda
const env = Object.freeze({
  site: 'test.com',
  environment: 'dev',
  severity: 'error'
});
Object.keys(env).forEach((key) => {
  process.env[key] = env[key];
});

function parseSeverity(val) {
  if (val !== undefined && val !== null) {
    switch (val.toString().toLowerCase()) {
      case '0':
      case 'error':
        return 0;
      case '1':
      case 'debug':
        return 1;
      default:
        return -1;
    }
  } else {
    return -1;
  }
}

class Config extends EvokeAwsConfig {
  site = null;
  environment = null;
  severity = parseSeverity;

  constructor(event) {
    super(event);
    this.init();
  }
}

// Simulate the API Gateway sending in stage variables
const event = {
  stageVariables: {
    environment: 'stage'
  }
};

const config = new Config(event);

console.log(config.site); // Expected result (from process.env): 'test.com'
console.log(config.get('environment', 'dev')); // Expected result (overridden by the event.stageVariables): stage 
console.log(config.severity); // Expected result (from parseSeverity(process.env['severity'])): 0
console.log(config.doesNotExist); // Expected result: undefined
console.log(config.get('doesNotExist', 'abc123')); // Expected result: abc123
console.log(config.doesNotExist); // Expected result (doesNotExist was created during the above get call): abc123
console.log(config.get({
  site: null,
  environment: 'dev',
  severity: -1
})); // Expected result: { site: 'test.com', environment: 'stage', severity: 0 }
console.log(config.get(['site','environment'])); // Expected result: { site: 'test.com', environment: 'stage' }

init(config)

Initializes the EvokeAwsConfig instance.

Parameters Type Default Description
config EvokeAwsConfig this The EvokeAwsConfig instance used during initialization.

Usage

const EvokeAwsConfig = require('@evokegroup/aws-config');

// Simulate the setting of environment variables from Lambda
const env = Object.freeze({
  site: 'test.com',
  environment: 'dev'
});
Object.keys(env).forEach((key) => {
  process.env[key] = env[key];
});

const event = {};

// Preferred approach, declare a class
class Config extends EvokeAwsConfig {
  site = null;
  environment = null;

  constructor(event) {
    super(event);
    this.init();
  }
}
const config = new Config(event);

// Quick and dirty, probably fine for just a few properties or creating a config inline
const evokeAwsConfig = new EvokeAwsConfig(event);
evokeAwsConfig.init({
  site: null,
  environment: null
});

serialize()

Serializes the instance.

Readme

Keywords

none

Package Sidebar

Install

npm i @evokegroup/aws-config

Weekly Downloads

1

Version

1.0.2

License

ISC

Unpacked Size

9.7 kB

Total Files

3

Last publish

Collaborators

  • ybevoke
  • jtsuyuki
  • evokejames
  • evoke-cjamodeo