Skip to content

A Fastify plugin for creating an API Gateway with route configuration, middlewares, rate limit and URL prefix rewriting.

License

Notifications You must be signed in to change notification settings

flaviodelgrosso/fastify-api-gateway

Repository files navigation

Fastify API Gateway

NPM version NPM downloads CI codecov

This library is a Fastify plugin that can be used as API Gateway that proxies requests to different backend services. It supports route-level and global middlewares, rate limits, and allows for URL prefix rewriting.

Install

npm install fastify-api-gateway

Compatibility

Plugin version Fastify version
^1.x ^5.x

Configuration

When registering the plugin it will register under the hood the following Fastify plugins:

Optionally, it can also register the following plugins:

  • fastify-rate-limit plugin, which is used to rate limit requests. It is enabled only if rateLimitOptions is set.
  • fastify-middie plugin, which is used to apply middlewares on routes. It is enabled only if middlewares are found in the configuration.

The plugin accepts the following options:

  • routes: The routes to register.
  • middlewares: Middlewares to apply to all routes.
  • rateLimitOptions: The rate limit configuration.
  • replyFromOptions: The reply.from options.
  • middieOptions: The middie options.

Routes

Routes can be configured with the following properties:

  • prefix: The URL prefix for the route.
  • target: The target backend service URL.
  • bodyLimit: (Optional) The maximum body size allowed for the route. Default is 1048576 (1MB).
  • method: (Optional) The HTTP method to match for the route. Default is *.
  • pathRegex: (Optional) A regular expression to match the path. Default is /*.
  • prefixRewrite: (Optional) The URL prefix to rewrite to.
  • middlewares: (Optional) An array of middlewares to apply to the route.
  • hooks: (Optional) Additional hooks to pass to the reply.from method.
  • config: (Optional) The Fastify route configuration object.

Rate limit can be configured at the route level or globally. If you want to disable the global rate limit, pass false to the rateLimitOptions.global property. Check the docs for all the configurations.

Example

const options = {
  rateLimitOptions: {
    global: false // Pass false if you want to disable the global rate limit and add configurations to the routes
  },
  replyFromOptions: {
    // reply.from options
  },
  middieOptions: {
    // middie options
  },
  middlewares: [
    // Global middlewares
  ],
  routes: [
    {
      method: 'GET',
      pathRegex: '/*',
      prefix: '/api',
      target: 'http://backend-service',
      config: {
        rateLimit: {
          max: 10,
          timeWindow: '1 minute'
        }
      }
      prefixRewrite: '/backend',
      middlewares: [
        // Route-level middlewares
      ],
      hooks: {
        // Additional hooks
      }
    }
  ]
}

app.register(gatewayPlugin, options)

License

This project is licensed under the ISC License - see the LICENSE file for details.

About

A Fastify plugin for creating an API Gateway with route configuration, middlewares, rate limit and URL prefix rewriting.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published