@lamware/fastify
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published
NPM Discord Apache-2.0

Lamware - Fastify

This Lamware Middleware utilizes the official AWS Lambda Fastify Client to provide a convenient, and performant, method of intializing and memoizing Fastify in AWS Lambda:

  • Enforces memoization of Fastify Client outside of function handler
  • Allow for top-level ready state initialization, perfect for Provisioned Concurrency

Installation

This package is available via NPM:

yarn add @lamware/fastify

# or

npm install @lamware/fastify

Usage

import type { APIGatewayProxyHandlerV2 } from 'aws-lambda';
import type { LamwareState } from '@lamware/core';
import { fastify } from '@lamware/fastify';
import { lamware } from '@lamware/core';
// import createFastify from 'fastify';

const { instance, handler } = lamware<APIGatewayProxyHandlerV2<any>>()
  .use(fastify(app => {
    // You can provide a closure that exposes the `fastify` instance.
    app.get('/', (request, reply) => {
      reply.send({ hello: 'world' }));
    };

    return app;
  }, {
    // You can optionally provide an existing Fastify client.
    // client: createFastify(),

    // And also enforce Fastify to ready-up before running.
    enforceReady: true,
  }))
  .execute(async (payload) => {
    return payload.state.fastifyHandler(payload);
  });

/**
 * Fastify doesn't have _great_ flexibility when it comes to typing.
 * As a result, you'll have to manually type the state request object.
 * For example:
 */

declare module 'fastify' {
  interface FastifyRequest {
    state: LamwareState<typeof instance>;
  }
}

export { handler };

Package Sidebar

Install

npm i @lamware/fastify

Weekly Downloads

2

Version

2.0.1

License

GPL-3.0-only

Unpacked Size

42 kB

Total Files

8

Last publish

Collaborators

  • oyed