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:
- Extract
operation
andpayload
from the event using keys defined in HandlerOptions - Lookup property
this[operation]
in handler and validate it as an Endpoint - Create a new InvocationContext with the current handler as the prototype
- Invoke the Endpoint, binding
this
to the InvocationContext - Call
context.succeed()
orcontext.fail()
if an AWSLambdaContext is present - Resolve or reject the results (or error) in the promise returned to the caller
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 |
getEndpointMetadata(endpoint: Endpoint): EndpointMetadata Gets the EndpointMetadata from an Endpoint. |
since 2.0.0 |
Constructor Summary
Public Constructor | ||
public |
constructor(options: HandlerOptions) Creates a new instance of the base handler class. |
since 2.0.0 |
Method Summary
Public Methods | ||
public |
createInvocationContext(thisArgs: Object): InvocationContext 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
|
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 |
resolveEndpoint(operation: string): Array 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 static getEndpointMetadata(endpoint: Endpoint): EndpointMetadata since 2.0.0 source
Gets the EndpointMetadata from an Endpoint.
Params:
Name | Type | Attribute | Description |
endpoint | Endpoint | the endpoint to check |
Throw:
if the metadata is of the wrong type (not an object), including |
Test:
- Handler .getEndpointMetadata should throw an exception for a null endpoint
- Handler .getEndpointMetadata should throw an exception for an undefined endpoint
- Handler .getEndpointMetadata should throw an exception for a non-object endpoint
- Handler .getEndpointMetadata should return undefined for an endpoint with no metadata
- Handler .getEndpointMetadata should throw an exception for an endpoint with non-object metadata
- Handler .getEndpointMetadata should return the metadata for an endpoint without error
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:
Name | Type | Attribute | Description |
options | HandlerOptions |
|
hash of options to adjust the behavior of the handler |
Public Methods
public createInvocationContext(thisArgs: Object): InvocationContext since 2.0.0 source
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:
Name | Type | Attribute | Description |
thisArgs | Object |
|
object containing assignable values |
Throw:
if |
Test:
- Handler #createInvocationContext() should throw a TypeError exception for a non-object
- Handler #createInvocationContext() options.deepCopy = falsey should create a new "this" context additional properties
- Handler #createInvocationContext() options.deepCopy = truthy should create a new "this" context with additional properties that are deeply read-only
public handle(event: Object, context: AWSLambdaContext, args: ...args): Promise since 2.0.0 source
Handler method that is exported to AWS Lambda.
Params:
Name | Type | Attribute | Description |
event | Object | the AWS Lambda event to be processed |
|
context | AWSLambdaContext |
|
the AWS Lambda context, optional if testing |
args | ...args |
|
additional arguments that will get passed to the endpoint method when it is invoked. |
Test:
- Handler #handle() should reject the promise for a null event
- Handler #handle() should reject the promise for an undefined event
- Handler #handle() should not invoke an endpoint without the @operation decorator
- Handler #handle() should invoke the endpoint with correct "this" value
- Handler #handle() should invoke the endpoint, succeed and call context#succeed() if present
- Handler #handle() should invoke the endpoint, fail and call context#fail() if present
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:
Name | Type | Attribute | Description |
operation | string | the name of the operation to resolve |
Return:
Array | an array with two elements: the endpoint function and the metadata |
Return Properties:
Name | Type | Attribute | Description |
0 | Endpoint | the endpoint function |
|
1 | EndpointMetadata | the endpoint metadata |
Throw:
if |
|
if an endpoint cannot be found |
Test:
- Handler #resolveEndpoint() should throw an exception for a undefined operation
- Handler #resolveEndpoint() should throw an exception for a null operation
- Handler #resolveEndpoint() should throw an exception for a non-string operation
- Handler #resolveEndpoint() should throw an exception for an operation that cannot be found
- Handler #resolveEndpoint() should throw an exception for an operation that cannot be found (missing metadata)
- Handler #resolveEndpoint() should throw an exception for an operation that cannot be found (invalid metadata)
- Handler #resolveEndpoint() should return [endpoint, metadata] for valid operation