knative-lambda
TypeScript icon, indicating that this package has built-in type declarations

0.0.14 • Public • Published

Async handlers

  1. Define a schema:
// schema.ts

export const event = {
  type: 'object',
  properties: {
    hello: { type: 'string' }
  },
  required: ['hello']
};

export type Event = { hello: string };
  1. 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);
};
  1. 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

  1. 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 };
  1. 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);
};
  1. 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: '/' });

Readme

Keywords

none

Package Sidebar

Install

npm i knative-lambda

Weekly Downloads

1

Version

0.0.14

License

ISC

Unpacked Size

61.9 kB

Total Files

30

Last publish

Collaborators

  • richardbranson