My setup
Typescript + webpack, the part of the setup what works.
serverless.yml
service: hello-service
plugins:
- serverless-webpack
- serverless-offline
functions:
HelloWorldLambda:
handler: HelloService.add
-
http:
path: /hello/add
method: get
HelloService.ts
import Service from "..."
import Hello from "..."
class HelloService extens Service<Hello> {
async add(event: APIGatewayEvent, context: Context, cb: Callback): Promise<ApiGatewayResponse> {
const entity = Service.getRequestedEntityProps(event);
try {
const result = await this.repository.addOrUpdate(entity);
return Response.add(result.value());
} catch (e) {
return Response.fail(e);
}
}
/* implementation of update, remove, etc... */
}
function getHandlers(service: Service<Hello>) {
return {
add: service.add,
update: service.update,
remove: service.remove,
};
}
export default getHandlers(new HelloService());
Full stacktrace
Serverless: GET /hello/add (λ: HelloWorldLambda)
Serverless: Error while loading HelloWorldLambda
Error: Serverless-offline: handler for 'HelloWorldLambda' is not a function
at Object.createHandler (./node_modules/serverless-offline/src/functionHelper.js:221:11)
at handler (./node_modules/serverless-offline/src/ApiGateway.js:485:40)
at module.exports.internals.Manager.execute (./node_modules/@hapi/hapi/lib/toolkit.js:41:33)
at Object.internals.handler (./node_modules/@hapi/hapi/lib/handler.js:46:48)
at exports.execute (./node_modules/@hapi/hapi/lib/handler.js:31:36)
at Request._lifecycle (./node_modules/@hapi/hapi/lib/request.js:312:68)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
Steps to reproduce
- Copy the setup.
- Run
serverless offline start
on the command line. - Go to the url http://localhost:3000/hello to run the function.
Expected result
The getHandlers
function should return an object thus the HelloService will do a export default of an object with the keys add, update and remove. The value of a object property should be method of the service, what will work similar as a plain function.