Home Reference Source Test Repository
import {Handler} from 'lambda6/src/index.js'
public class | version 2.0.0 | since 1.0.0 | source

Handler

Base class for AWS Lambda handlers. This class should be extended and new operations should be added to the class as prototype methods. A method added to the subclass needs to be registered with the operation decorator in order to be visible as an operation Endpoint. Handler will handle an event in the following way:

  1. Extract operation and payload from the event using keys defined in HandlerOptions
  2. Lookup property this[operation] in handler and validate it as an Endpoint
  3. Create a new InvocationContext with the current handler as the prototype
  4. Invoke the Endpoint, binding this to the InvocationContext
  5. Call context.succeed() or context.fail() if an AWSLambdaContext is present
  6. Resolve or reject the results (or error) in the promise returned to the caller

See:

Example:


import { Handler, operation } from 'lambda6';

class MyHandler extends Handler {

  @operation
  echoOperationName() {
    return { operationName: this.operation };
  }

  @operation
  echoValuesFromPayload({ value1, value2 }) {
    return { oldValue1: value1, oldValue2: value2 };
  }

}

Static Member Summary

Static Public Members
public static get

Gets the default options for a new Handler instance.

since 2.0.0
public static get

Gets the key used to access the EndpointMetadata for an Endpoint.

since 2.0.0

Static Method Summary

Static Public Methods
public static

Gets the EndpointMetadata from an Endpoint.

since 2.0.0

Constructor Summary

Public Constructor
public

Creates a new instance of the base handler class.

since 2.0.0

Method Summary

Public Methods
public
this method is experimental. Deep copying is disabled by default. To enable, set options.deepCopy to a truthy value. This feature requires further testing to make sure it operates as expected within the AWS environment.

Creates an InvocationContext by creating a new object with the current Handler instance as the prototype, assigning the properties from thisArgs as read-only, enumerable own properties of the object and optionally performing a deep copy of the values in thisArgs to make them deeply immutable.

since 2.0.0
public

handle(event: Object, context: AWSLambdaContext, args: ...args): Promise

Handler method that is exported to AWS Lambda.

since 2.0.0
public

Resolves an operation name to a prototype method of a derived class.

since 2.0.0

Static Public Members

public static get defaultOptions: HandlerOptions since 2.0.0 source

Gets the default options for a new Handler instance. The defaults are "operation" and "payloadKey" for

public static get metadataKey: string since 2.0.0 source

Gets the key used to access the EndpointMetadata for an Endpoint. The current value is "_λ6_metadata" and most likely won't conflict with any existing properties of the function. AWS Lambda does not yet support Symbol, so the metadata key is currently a string.

Static Public Methods

Public Constructors

public constructor(options: HandlerOptions) since 2.0.0 source

Creates a new instance of the base handler class. Subclasses don't need to override this if they wish to store custom data. Simply pass in an options value and the data will be available as this.options[key] during method invocation.

Params:

NameTypeAttributeDescription
options HandlerOptions
  • optional

hash of options to adjust the behavior of the handler

Test:

Public Methods

public createInvocationContext(thisArgs: Object): InvocationContext since 2.0.0 source

this method is experimental. Deep copying is disabled by default. To enable, set options.deepCopy to a truthy value. This feature requires further testing to make sure it operates as expected within the AWS environment.

Creates an InvocationContext by creating a new object with the current Handler instance as the prototype, assigning the properties from thisArgs as read-only, enumerable own properties of the object and optionally performing a deep copy of the values in thisArgs to make them deeply immutable. The resulting object is then used as the this value during invocation of the Endpoint.

Params:

NameTypeAttributeDescription
thisArgs Object
  • optional

object containing assignable values

Return:

InvocationContext

that can be used to invoke an endpoint

Throw:

TypeError

if thisArgs is not undefined or null and isn't an object

Test:

public handle(event: Object, context: AWSLambdaContext, args: ...args): Promise since 2.0.0 source

Handler method that is exported to AWS Lambda.

Params:

NameTypeAttributeDescription
event Object

the AWS Lambda event to be processed

context AWSLambdaContext
  • optional

the AWS Lambda context, optional if testing

args ...args
  • optional

additional arguments that will get passed to the endpoint method when it is invoked.

Return:

Promise

that resolves to the return value of the invoked endpoint or rejects with an error

Test:

public resolveEndpoint(operation: string): Array since 2.0.0 source

Resolves an operation name to a prototype method of a derived class. This method is a wrapper for Handler.getEndpointMetadata that type checks the operation name to make sure it isn't null or undefined, then attempts to retrieve the endpoint and metadata.

Params:

NameTypeAttributeDescription
operation string

the name of the operation to resolve

Return:

Array

an array with two elements: the endpoint function and the metadata

Return Properties:

NameTypeAttributeDescription
0 Endpoint

the endpoint function

1 EndpointMetadata

the endpoint metadata

Throw:

TypeError

if operation is not a string, or if the endpoint has invalid metadata.

Error

if an endpoint cannot be found

Test: