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

@fgiova/fastify-rest-gateway

v1.1.0

Published

REST gateway plugin for fastify

Downloads

7

Readme

fastify rest-gateway

NPM version CI workflow TypeScript

Description

This plugin for fastify 4.x allows you to expose REST APIs for your REST microservices starting from OpenApi contracts hosted into same microservice.

Routes to be exposed by the gateway must be tagged with the appropriate tags; by default "public-api" for public APIs, "private-api" for hidden APIs.

Note To distribute open-api compliant contracts I recommend using the plugins for fastify @fastify/swagger and @fastify/swagger-ui

Warning The plugin mandatorily requires the @fastify/reply-from plugin.

Install

npm i @fgiova/fastify-rest-gateway @fastify/reply-from

Usage

const app = require("fastify")();
const replyFrom = require("@fastify/reply-from");
const fastifyRestGateway = require("@fgiova/fastify-rest-gateway");
app.register(replyFrom);
app.register(fastifyRestGateway, {
    services: [
        {
            host: "https://petstore.test.com",
            openApiUrl: "/open-api/json",
            remoteBaseUrl: "/v1/test/public-api/",
            gwBaseUrl: "/v1/test/",
            
        }
    ]
});

The plugin can be used in conjunction with @fastify/swagger and @fastify/swagger-ui to produce an open-api contract of the routes exposed by the fastify-rest-gateway plugin.

npm i @fgiova/fastify-rest-gateway @fastify/reply-from @fastify/swagger @fastify/swagger-ui
const app = require("fastify")();
const fastifyRestGateway = require("@fgiova/fastify-rest-gateway");

await app.register(require("@fastify/reply-from"));
await app.register(fastifyRestGateway, {
    services: [
        {
            host: "https://petstore.test.com",
            openApiUrl: "/open-api/json",
            remoteBaseUrl: "/v1/test/public-api/",
            gwBaseUrl: "/v1/test/",
            
        }
    ]
});

await app.register(require("@fastify/swagger"), {
    mode: "dynamic",
    openapi: {
        ...
    }
});
await app.register(require("@fastify/swagge-ui"), {
    routePrefix: "/open-api",
});

The plugin can be used in conjunction with @fastify/rate-limit to restrict upstream accesses to the target microservice.

npm i @fgiova/fastify-rest-gateway @fastify/reply-from @fastify/rate-limit
const app = require("fastify")();
const fastifyRestGateway = require("@fgiova/fastify-rest-gateway");

await app.register(require("@fastify/reply-from"));
await app.register(require("@fastify/rate-limit"));
await app.register(fastifyRestGateway, {
    services: [
        {
            host: "https://petstore.test.com",
            openApiUrl: "/open-api/json",
            remoteBaseUrl: "/v1/test/public-api/",
            gwBaseUrl: "/v1/test/",
            hitLimit: {
                max: (req: FastifyRequest) => {
                    return 10;
                },
                keyGenerator: (req: FastifyRequest) => {
                    return `${req.ip}_test`;
                },
                timeWindow: 5000
            }
        }
    ]
});

await app.register(require("@fastify/swagger"), {
    mode: "dynamic",
    openapi: {
        ...
    }
});
await app.register(require("@fastify/swagge-ui"), {
    routePrefix: "/open-api",
});

This plugin can be used in conjunction with @fastify/restartable to reload the routes when open-api contracts of microservices are changed.

npm i @fgiova/fastify-rest-gateway @fastify/reply-from @fastify/rate-limit @fastify/restartable
const app = require("fastify")();
const fastifyRestGateway = require("@fgiova/fastify-rest-gateway");
const restartable = require("@fastify/restartable");


async function createApp (fastify, opts) {
    const app = fastify(opts)

    await app.register(require("@fastify/reply-from"));
    await app.register(require("@fastify/rate-limit"));
    await app.register(fastifyRestGateway, {
        services: [
            {
                host: "https://petstore.test.com",
                openApiUrl: "/open-api/json",
                remoteBaseUrl: "/v1/test/public-api/",
                gwBaseUrl: "/v1/test/",
                refreshTimeout: 60000, // each 60s reload open-api contract from host if are changed, restart fastify
                hitLimit: {
                    max: (req: FastifyRequest) => {
                        return 10;
                    },
                    keyGenerator: (req: FastifyRequest) => {
                        return `${req.ip}_test`;
                    },
                    timeWindow: 5000
                }
            }
        ]
    });

    await app.register(require("@fastify/swagger"), {
        mode: "dynamic",
        openapi: {
            ...
        }
    });
    await app.register(require("@fastify/swagge-ui"), {
        routePrefix: "/open-api",
    });

    return app;
}

const app = await restartable(createApp, { logger: true });
const host = await app.listen({ port: 3000 });

Options

| Option | Type | Description | |--------------------------|----------------|-------------------------------------------------------------------------------------------------------------| | services | array | The list of services to expose. | | services[].host | string | The host of the service. | | services[].openApiUrl | string | The URL of the OpenApi JSON contract (default: /open-api/json). | | services[].remoteBaseUrl | string | The baseUrl of the remote service. | | services[].gwBaseUrl | string | The baseUrl where the service will be connected on the gateway | | services[].tag | string | Optional tag for selecting target routes to expose (default: public-api) | | services[].hiddenTag | string | Optional tag for selecting target routes to expose, but hidden on fastify-swagger (default: private-api) | | services[].preHandler | FastifyHandler | Optional Fastify Pre Handler function to add to each service route | | services[].hooks | object | Optional hooks for each route exposed | | services[].hitLimit | object | Optional limit configuration for each route exposed | | defaultLimit | object | Optional default limit configuration | | gwTag | string | Optional default tag for select target routes to expose (default: public-api) | | gwHiddenTag | string | Optional default tag for select target routes to expose, but hidden on fastify-swagger (default: X-HIDDEN) | | ignoreHidden | boolean | Optional flag to ignore hidden routes | | refreshTimeout | number | Optional interval in ms for watching open-api contracts and reload through Fastify restartable |

Service Hooks

| Option | Type | Description | |-----------------------------|----------------|------------------------------------------------------------------------------| | onRequest | FastifyHandler | Optional Fastify OnRoute Handler function to add to each service route | | onResponse | FastifyHandler | Optional Fastify OnResponse Handler function to add to each service route | | onError | FastifyHandler | Optional Fastify OnError Handler function to add to each service route |

Service limit options

| Option | Type | Description | |--------------|--------------------|----------------------------------------------------------------------------------------------------------| | max | number | function | Optional sync/async Function or number maximum hit per timeWindow (default: 1000) | | keyGenerator | function | Optional sync/async function to generate a unique identifier for each incoming request (default: req.ip) | | timeWindow | number | Optional the duration of the time window in ms (default: 60000) |

License

Licensed under MIT.

Acknowledgements

This project is kindly sponsored by: isendu Srl www.isendu.com