knative-lambda
v0.0.14
Published
Lambda interfaces for Knative serving and eventing
Downloads
9
Readme
Async handlers
- Define a schema:
// schema.ts
export const event = {
type: 'object',
properties: {
hello: { type: 'string' }
},
required: ['hello']
};
export type Event = { hello: string };
- Write a handler:
// handler.ts
import { type ServeAsyncHandler } from 'knative-lambda';
import type * as schema from './schema';
export const handler: ServeAsyncHandler<schema.Event> = (event) => {
console.log('hello', event.hello);
};
- Serve:
// index.ts
import { serveAsync } from 'knative-lambda';
import * as schema from './schema';
import { handler } from './handler';
export default serveAsync(handler, schema.event);
Sync handlers
- Define a schema:
// schema.ts
const body = {
type: 'object',
properties: {
hello: { type: 'string' }
},
required: ['hello']
};
type Body = { hello: string };
export const event = { body };
export type Event = { Body: Body };
- Write a handler:
// handler.ts
import { type ServeSyncHandler } from 'knative-lambda';
import type * as schema from './schema';
export const handler: ServeSyncHandler<schema.Event> = (event) => {
console.log('hello', event.body.hello);
};
- Serve:
// index.ts
import { serveSync } from 'knative-lambda';
import * as schema from './schema';
import { handler } from './handler';
export default serveSync(handler, schema.event, { method: 'POST', url: '/' });