1.0.2 • Published 5 years ago

@evokegroup/aws-config v1.0.2

Weekly downloads
-
License
ISC
Repository
bitbucket
Last release
5 years ago

@evokegroup/aws-config

Configuration base class for AWS Lambda functions

Class: EvokeAwsConfig

constructor(event)

ParametersTypeDefaultDescription
eventEvokeAws.Event, objectThe 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

PropertyTypeDescription
eventEvokeAws.EventThe 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.

ParametersTypeDefaultDescription
args0object, Array<string>, stringA string, Array<string> or object specifying the property/properties to return
args1anyIf 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.

ParametersTypeDefaultDescription
configEvokeAwsConfigthisThe 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.

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago