@quantum-sec/sls-utils
v1.2.0
Published
Utilities for projects using Serverless Framework.
Downloads
14
Readme
sls-utils
Utilities for projects using Serverless Framework.
Declarative Config with TypeScript Decorators
Using meta decorators and the serverless.ts
scaffolding, you can maintain declare Lambda handlers and their
associated triggers in code. To do this, add the appropriate decorators to the handler class. Handler classes
must be suffixed with .handler.ts
.
import { HttpGet, KeepWarm, HttpLambdaBase, LambdaPipeline } from '@quantum-sec/sls-utils';
@KeepWarm()
@HttpGet('users')
export class GetUsersHandler extends HttpLambdaBase {
public async handleRequest(event: any, context: any): Promise<any> {
return this.ok({
users: [{
firstName: 'Obiwan',
lastName: 'Kenobi',
}],
});
}
}
module.exports.handleRequest = LambdaPipeline.create(new GetUsersHandler());
Then in your serverless.ts
file:
import { ServerlessConfig } from '@quantum-sec/sls-utils';
module.exports = new Promise((resolve) => {
new ServerlessConfig({
name: 'example-service',
}).generateConfig().then((config) => {
resolve(config);
});
});
HTTP Events
The following decorators are available to bind HTTP events to handlers:
@HttpGet(path: string)
@HttpPost(path: string)
@HttpPut(path: string)
@HttpPatch(path: string)
@HttpDelete(path: string)
@HttpEvent(method: string, path: string)
Websocket Events
The following decorators are available to bind websocket events to handlers:
@WebsocketEvent(route: string)
@WebsocketEvent(route: string, routeResponseSelectionExpression: string)
Schedule Events
The following decorators are available to bind schedule events to handlers:
@Cron(expression: string)
@Rate(expression: string)
(see rate expression docs)
S3 Events
@S3Created(bucket: string, prefix?: string, suffix?: string)
@S3Removed(bucket: string, prefix?: string, suffix?: string)
@S3Event(bucket: string, event: S3EventKey, prefix?: string, suffix?: string)
SNS Events
@SnsEvent(topic: string)
(topic may be the ARN of an existing topic or the name of a topic that Serverless will create for you)
Utility Decorators
@KeepWarm
(creates a 1 minute heartbeat that theLambdaPipeline
will intercept and no-op but keep the function primed)