Serverless adapter for Koatty framework - deploy to AWS Lambda, Alibaba Cloud FC, Tencent SCF.
npm install koatty_serverlessFor AWS Lambda, also install the optional dependency:
npm install @codegenie/serverless-express// src/lambda.ts
import { createHandler } from 'koatty_serverless';
import { App } from './App';
export const handler = createHandler(App, {
platform: 'aws',
healthCheck: async (app) => {
// Check database connections after Lambda thaw
// const ds = IOC.get('TypeormStore')?.getDataSource();
// if (ds && !ds.isInitialized) await ds.initialize();
},
eventHandlers: {
scheduled: async (event, context, app) => {
// Handle CloudWatch scheduled events
return { statusCode: 200 };
},
},
});// src/fc.ts
import { createHandler } from 'koatty_serverless';
import { App } from './App';
export const handler = createHandler(App, { platform: 'alicloud' });// src/scf.ts
import { createHandler } from 'koatty_serverless';
import { App } from './App';
export const handler = createHandler(App, { platform: 'tencent' });For platforms like Alibaba Cloud FC HTTP trigger where the input is already a standard HTTP object:
// src/handler.ts
import { createApplication } from 'koatty';
import { App } from './App';
let handler: (req: any, res: any) => Promise<any>;
export async function httpHandler(req: any, resp: any, context: any) {
if (!handler) {
const app = await createApplication(App);
handler = app.getRequestHandler();
}
return handler(req, resp);
}Creates a serverless handler function.
Parameters:
AppClass- Your Koatty application classoptions- Configuration options
Options:
| Option | Type | Default | Description |
|---|---|---|---|
platform |
'aws' | 'alicloud' | 'tencent' |
'aws' |
Target cloud platform |
adapter |
ServerlessAdapter |
- | Custom adapter (overrides platform) |
bootFunc |
Function |
- | Custom bootstrap function |
eventHandlers |
Record<string, EventHandler> |
- | Non-HTTP event handlers |
healthCheck |
(app) => Promise<void> |
- | Health check before each invocation |
type EventHandler = (
event: any,
context: any,
app: KoattyApplication,
) => Promise<any>;interface ServerlessAdapter {
readonly name: string;
createHandler(app: KoattyApplication): (...args: any[]) => Promise<any>;
}Implement ServerlessAdapter for other platforms:
import type { ServerlessAdapter } from 'koatty_serverless';
import type { KoattyApplication } from 'koatty_core';
class CloudflareAdapter implements ServerlessAdapter {
readonly name = 'cloudflare-workers';
createHandler(app: KoattyApplication) {
const httpHandler = app.getRequestHandler();
return async (request: Request) => {
// Convert Cloudflare Request to Node.js HTTP format
// ...
};
}
}
// Usage
export const handler = createHandler(App, {
adapter: new CloudflareAdapter(),
});BSD-3-Clause