Socket
Book a DemoInstallSign in
Socket

@evokegroup/aws-config

Package Overview
Dependencies
Maintainers
3
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@evokegroup/aws-config

Configuration base class for AWS Lambda functions

1.0.2
latest
Source
npmnpm
Version published
Maintainers
3
Created
Source

@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
args[0]object, Array<string>, stringA string, Array<string> or object specifying the property/properties to return
args[1]anyIf 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.

FAQs

Package last updated on 17 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.