@yubl/yubl-api-powertools
Tools for creating APIs with Lambda functions
Last updated a year ago by theburningmonk .
ISC · Repository · Original npm · Tarball
$ cnpm install @yubl/yubl-api-powertools 
SYNC missed versions from official npm registry.

This package provides the basic tools & boilerplate that we use to build Lambda-backed APIs.

Getting Started

npm install @yubl/yubl-api-powertools

Helpers

There's a number of helper modules/functions available to you when you require this package, eg.

const powertools    = require('@yubl/yubl-api-powertools');
const log           = powertools.log;
const kms           = powertools.kms;
const mongo         = powertools.mongo;
const HttpError     = powertools.HttpError;
const createHandler = powertools.createHandler;

log

This is the standard log module we use in our functions that logs to console (which then goes into CloudWatch Logs).

debug log messages are ignored if the IS_DEBUG_LOG_ENABLED environment variable is missing or set to false.

const log = require('@yubl/yubl-api-powertools').log;

log.debug('GOT is off-air, #sadface');
log.info('the name is bond, james bond');
log.warn('all work and no play makes Jonny a dull boy');
log.error(err, err.stack);

kms

This is the standard kms module we use in functions.

This module has two Promise-yielding functions:

  • encrypt takes the unencrypted payload, and an optional KMS key-id and return the encrypted blob
  • decrypt takes the encrypted blob and returns the decrypted text

If the KMS key-id is not provided, then the encrypt function will look for a KMS_KEY_ID environment variable. If the environment variable is not found, then the call errors with an error message along the lines of:

KMS key-id not found. Either pass in as second argument or provide a KMS_KEY_ID environment var.

const kms = require('@yubl/yubl-api-powertools').kms;

kms.encrypt('payload', 'key-id').then(..).catch(..);
kms.decrypt('encrypted-blob').then(..).catch(..);

mongo

This module provides basic capability for connecting to MongoDB.

Like the mongo module you might have seen in some projects, this module depends on a MONGODB environemnt variable. The environment variable is expected to be the encrypted MongoDB connection string.

mongo.connectToMongoDB() return a Promise of a mongodb object.

const mongo = require('@yubl/yubl-api-powertools').mongo;

mongo.connectToMongoDB().then(..).catch(..);

HttpError

const HttpError = require('@yubl/yubl-api-powertools').HttpError;

throw new HttpError(404, 'Yubl not found');

createHandler

This function takes a function (what you want to run) and an options array and returns a handler function for your Lambda.

i.e. createHandler(f, options) where f(event) => result | Promise<result> and options : { timeout, onSuccess, onError }

Unless specified, the timeout option defaults to 5s.

The optional onSuccess function is called (with the result from f) after f has returned a result and before the result is returned to API Gateway.

The optional onError function is called (with the error object) before the error is returned to API Gateway.

The handler function f is called with the event object the Lambda function is invoked with.

const createHandler = require('@yubl/yubl-api-powertools').createHandler;
const loadAll = require('./lib').loadAll;
const options = {
  timeout: 9000
};

module.exports.handler = createHandler(loadAll, options);
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 0
Last Month 3
Dependencies (3)
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |