Customizing
With the instance of adapter, we have some options:
setHandler
Calling this method more than once throws an error.
Defines the handler that will get the event, context and callback and pass it to the adapter and framework.
See supported handlers in Handlers Section)
setResolver
Calling this method more than once throws an error.
Defines the resolver that aims to resolve the response to serverless and stop its execution when the request ends.
See supported resolvers in Resolvers Section)
setFramework
Calling this method more than once throws an error.
Defines the framework that will process requests.
See supported frameworks in Frameworks Section)
setLogger
Defines the logger service used during the execution of the handler
To know how to implement, see the Logger Interface in API section.
setNetwork
Defines the network implementation used by handlers to create the IncomingMessage and ServerResponse sent to your framework, then read the response body and headers before the adapter builds the provider response.
Most applications should not call this method. By default, DEFAULT_NETWORK is a DefaultNetwork instance that creates ServerlessRequest and ServerlessResponse objects and extracts their buffered body and headers.
Use setNetwork when you need to tune DefaultNetwork, provide custom Node.js request/response classes, add framework-specific shims, or customize response extraction. Call it before the first addAdapter, because the selected network response type becomes part of the adapter type.
Handlers that already receive a real HTTP request/response from the platform may use only part of the contract. For example, Firebase, GCP, and AWS response streaming use createRequest, while Huawei keeps the real Node.js HTTP server request and response objects untouched.
import { DefaultNetwork, ServerlessAdapter } from '@h4ad/serverless-adapter';
export const handler = ServerlessAdapter.new(app)
.setFramework(new ExpressFramework())
.setHandler(new DefaultHandler())
.setResolver(new PromiseResolver())
.setNetwork(new DefaultNetwork({ setRequestIp: false }))
.addAdapter(new ApiGatewayV2Adapter())
.build();
A custom network must implement NetworkContract:
createRequestmaps adapter request values to the request object forwarded to the framework.createResponsecreates the response object passed to the framework.getResponseBodyreturns the collected response body as aBuffer.getResponseHeadersreturns the collected response headers.
If your adapter needs access to a custom response class in getResponse, provide it as the fourth AdapterContract generic.
setBinarySettings
Defines the binary settings for whether the response should be treated as binary or not.
When you call this method, you need to pass the interface BinarySettings, and you have two options:
- Send an function called
isBinaryto verify looking inside the headers if the response should be treated as binary. - Or send the specific
contentTypesandcontentEncodingsthat should be treated as binary.
After that, your adapter needs to handle the binary data, it will deal correctly because they know which responses are encoded in binary.
Not every adapter deals with binary but if you used adapters like ApiGatewayV1 or ApiGatewayV2, you should be aware of this configuration.
By default, the library already handles some common cases like DEFAULT_BINARY_CONTENT_TYPES for contentTypes
and DEFAULT_BINARY_ENCODINGS for contentEncodings.
setRespondWithErrors
Defines the responseWithErrors, a property that indicates whether the error stack should be included in the response or not
The errors this option will usually handle are errors during forwarding to your framework, eg: when your framework throws error 500 from a middleware error.
By default, this option will be true only when the NODE_ENV is equal to development.
addAdapter
Add an adapter to the adapters list to handle the event coming from any serverless event source.
You can call this method multiple times to add support for whatever event source you want.
To know which adapters you can use, see the Adapters) section.