Skip to main content

ServerlessAdapter

@h4ad/serverless-adapter > ServerlessAdapter

(class) ServerlessAdapter

The class used to build the serverless handler.

Signature:

export declare class ServerlessAdapter<TApp, TEvent, TContext, TCallback, TResponse, TReturn> 

Example

const app = express();
export const handler = ServerlessAdapter.new(app)
.setFramework(new ExpressFramework())
.setHandler(new DefaultHandler())
.setResolver(new PromiseResolver())
.setRespondWithErrors(true)
.addAdapter(new AlbAdapter())
.addAdapter(new SQSAdapter())
.addAdapter(new SNSAdapter())
.build();

(property) adapters

The list of adapters used to handle an event's request and response

Signature:

protected adapters: AdapterContract<TEvent, TContext, TResponse>[];

(method) addAdapter

Add an adapter to the adapters list to handle the event coming from any serverless event source

Signature:

addAdapter(adapter: AdapterContract<TEvent, TContext, TResponse>): Pick<this, 'addAdapter' | 'build'>;

Parameters

Parameter

Type

Description

adapter

AdapterContract <TEvent, TContext, TResponse>

The implementation of the adapter contract

Returns:

Pick<this, 'addAdapter' | 'build'>

(property) app

The instance of the app (express, hapi, koa, etc...)

Signature:

protected app: TApp;

(property) binarySettings

Settings for whether the response should be treated as binary or not

Signature:

protected binarySettings: BinarySettings;

(method) build

The builder method that returns the handler function to be exported for serverless consumption

Signature:

build(): ServerlessHandler<TReturn>;

Returns:

ServerlessHandler <TReturn>

(property) framework

The framework that will process requests

Signature:

protected framework?: FrameworkContract<TApp>;

(property) handler

The handler that will get the event, context and callback and pass it to the adapter and framework

Signature:

protected handler?: HandlerContract<TApp, TEvent, TContext, TCallback, TResponse, TReturn>;

(property) log

The instance of the logger service

Signature:

protected log: ILogger;

(method) new

Creates a new instance of the builder with app (express, hapi, koa, etc...)

Signature:

static new<TApp, TEvent, TContext = any, TCallback = any, TResponse = any, TReturn = any>(app: TApp): ServerlessAdapter<TApp, TEvent, TContext, TCallback, TResponse, TReturn>;

Parameters

Parameter

Type

Description

app

TApp

The instance of the app

Returns:

ServerlessAdapter <TApp, TEvent, TContext, TCallback, TResponse, TReturn>

(property) resolver

The resolver that aims to resolve the response to serverless and stop its execution when the request ends

Signature:

protected resolver?: ResolverContract<TEvent, TContext, TCallback, TResponse, TReturn>;

(property) respondWithErrors

Indicates whether the error stack should be included in the response or not

Signature:

protected respondWithErrors: boolean;

Remarks

These errors will only be included when an error occurs while forwarding the event to the framework

(method) setBinarySettings

Defines the binary settings for whether the response should be treated as binary or not

Signature:

setBinarySettings(binarySettings: BinarySettings): Omit<this, 'setBinarySettings'>;

Parameters

Parameter

Type

Description

binarySettings

BinarySettings

The binary settings

Returns:

Omit<this, 'setBinarySettings'>

(method) setFramework

Defines the framework that will process requests

Signature:

setFramework(framework: FrameworkContract<TApp>): Omit<this, 'setFramework'>;

Parameters

Parameter

Type

Description

framework

FrameworkContract <TApp>

The implementation of the framework contract

Returns:

Omit<this, 'setFramework'>

(method) setHandler

Defines the handler that will get the event, context and callback and pass it to the adapter and framework

Signature:

setHandler(handler: HandlerContract<TApp, TEvent, TContext, TCallback, TResponse, TReturn>): Omit<this, 'setHandler'>;

Parameters

Parameter

Type

Description

handler

HandlerContract <TApp, TEvent, TContext, TCallback, TResponse, TReturn>

The implementation of the handler contract

Returns:

Omit<this, 'setHandler'>

(method) setLogger

Defines the logger service used during the execution of the handler

Signature:

setLogger(logger: ILogger): Omit<this, 'setLogger'>;

Parameters

Parameter

Type

Description

logger

ILogger

The implementation of the logger

Returns:

Omit<this, 'setLogger'>

(method) setResolver

Defines the resolver that aims to resolve the response to serverless and stop its execution when the request ends

Signature:

setResolver(resolver: ResolverContract<TEvent, TContext, TCallback, TResponse, TReturn>): Omit<this, 'setResolver'>;

Parameters

Parameter

Type

Description

resolver

ResolverContract <TEvent, TContext, TCallback, TResponse, TReturn>

The implementation of the resolver contract

Returns:

Omit<this, 'setResolver'>

(method) setRespondWithErrors

Defines the responseWithErrors, a property that indicates whether the error stack should be included in the response or not

Signature:

setRespondWithErrors(respondWithErrors: boolean): Omit<this, 'setRespondWithErrors'>;

Parameters

Parameter

Type

Description

respondWithErrors

boolean

Should include or not the errors in response

Returns:

Omit<this, 'setRespondWithErrors'>