npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@autotelic/fastify-openapi-autoload

v0.2.0

Published

Plugin for fastify

Downloads

488

Readme

Fastify OpenAPI Autoload

The fastify-openapi-autoload plugin is a tool for building API servers with Fastify, leveraging OpenAPI specifications. It integrates fastify-openapi-glue for handling OpenAPI specs and @fastify/autoload for auto-loading route handlers.

Features

  • OpenAPI Integration: Utilizes fastify-openapi-glue to automatically handle routes as defined in your OpenAPI spec.
  • Automatic Route Handlers Loading: Loads route handlers from a specified directory, significantly reducing route setup code.

Installation

To install the plugin, run:

npm i @autotelic/fastify-openapi-autoload

Prerequisites

  • Node.js
  • Fastify
  • OpenAPI specification file
  • Directory with Fastify OpenAPI route handlers

Example

import fastify from 'fastify'
import openapiAutoload from '@autotelic/fastify-openapi-autoload'

export default async function app (fastify, options) {
  fastify.register(openapiAutoload, {
    handlersDir: '/path/to/handlers',
    openapiOpts: {
      specification: '/path/to/openapi/spec.yaml'
    }
  })
}

To run an example app, see this guide

API Reference - Options

handlersDir (required)

Path to the route handlers directory.

// example:
export default async function app (fastify, options) {
 fastify.register(openapiAutoload, {
   handlersDir: '/path/to/handlers',
   // Other configuration options...
 })
}

openapiOpts (required)

OpenAPI-related options. Refer to fastify-openapi-glue documentation for more details. At minimum, specification must be defined. This can be a JSON object, or the path to a JSON or YAML file containing a valid OpenApi(v2/v3) file. If specification is a path to a yaml file, fastify-openapi-autoload supports multi-file resolving. See this test directory for example.

// example
export default async function app (fastify, options) {
 fastify.register(openapiAutoload, {
   openapiOpts: {
     specification: '/path/to/spec/openapi.yaml'
   },
   // Other configuration options...
 })
}

makeOperationResolver (optional)

By default, the fastify-openapi-autoload provides a standard resolver that locates a handler based on the operation ID, looking for a matching decorator method in the Fastify instance. However, if your application requires a different mapping strategy or additional logic for resolving operations, you can provide a custom resolver function.

The custom resolver should be a factory function that receives the Fastify instance as an argument and returns an operation resolver function. This resolver function, when invoked with an operationId, should return the corresponding handler function for that specific operation.

For more information on the operation resolver, refer to the fastify-openapi-glue operation resolver documentation.

// example
export default async function app (fastify, options) {
 fastify.register(openapiAutoload, {
   makeOperationResolver: (fastify) => (operationId) => {
     // Custom logic to determine the handler function for the given operationId
     // For example, returning a fixed response for demonstration:
     return async (_req, reply) => {
       reply.code(200).send(`Custom response for operation ${operationId}`)
     }
   },
   // Other configuration options...
 })
}

makeSecurityHandlers (optional)

If your application requires custom security handlers for your OpenAPI handlers, you can provide a factory function similar to the makeOperationResolver option.

This factory function should take the Fastify instance as an argument and return an object containing the security handlers. Each handler within this object should implement the logic for handling security aspects as defined in your OpenAPI specification.

For guidance on implementing security handlers, see the fastify-openapi-glue security handlers documentation.

Example usage:

// example
export default async function app (fastify, options) {
  fastify.register(openapiAutoload, {
    makeSecurityHandlers: (fastify) => {
      // Custom logic for security handlers
      return {
        someSecurityHandler: (notOk) => {
          if (notOk) {
            throw new Error('not ok')
          }
        }
      }
    },
    // Other configuration options...
  })
}

JSON Web Token Security Handler

The jwtJwksHandler function, exported with the fastify-openapi-autoload plugin, allows you to integrate JWT/JWKS authentication as security handlers.

To use this function, you need to install the following dependencies:

npm i @autotelic/fastify-openapi-autoload @fastify/jwt get-jwks

Options

When configuring jwtJwksHandler, you can customize its behavior with the following options:

  • jwksOpts (optional): See get-jwks documentation for details.
  • issuer (*required): The issuer URL of the JWT tokens. This is typically the base URL of the token provider. Required option if jwksOpts.issuersWhitelist & jwksOpts.checkIssuer options are not provided.
  • authRequestDecorator (optional - default provided): A function to decorate the Fastify request with custom JWT authentication logic.
  • securityHandlers (optional - default provided): An object containing Fastify security handlers.

Example Usage

import fastify from 'fastify'
import openapiAutoload from '@autotelic/fastify-openapi-autoload'
import { jwtJwksHandler } from '@autotelic/fastify-openapi-autoload/jwtJwks'

export default async function app (fastify, options) {
  const makeSecurityHandlers = jwtJwksHandler({
    issuer: 'https://your-issuer-url.com',
    jwksOpts: {
      max: 100,
      ttl: 60000,
      timeout: 5000
      // ...additional JWKS options
    },
    // Custom authentication request decorator (optional)
    authRequestDecorator: async (request) => {
      try {
        const decodedToken = await request.jwtVerify(request)
        const { userId } = decodedToken
        return userId
      } catch (err) {
        return null
      }
    }
  })

  fastify.register(openapiAutoload, {
    handlersDir: '/path/to/handlers',
    openapiOpts: {
      specification: '/path/to/openapi/spec.yaml'
    },
    makeSecurityHandlers
  })
}

Plugin Development: Triggering a Release

To trigger a new release:

git checkout main && git pull
npm version { minor | major | patch }
git push --follow-tags