Code Monkey home page Code Monkey logo

fastify-routes's Introduction

@fastify/routes

CI NPM version js-standard-style

This plugin decorates a Fastify instance with routes, which is a Map of registered routes. Note that you have to await the registration of this plugin before registering any routes so that @fastify/routes can collect them.

Data Structure

The fastify.routes Map has a key for each path any route has been registered, which points to an array of routes registered on that path. There can be more than one route for a given path if there are multiple routes added with different methods or different constraints.

  {
    '/hello': [
      {
        method: 'GET',
        url: '/hello',
        schema: { ... },
        handler: Function,
        prefix: String,
        logLevel: String,
        bodyLimit: Number,
        constraints: undefined,
      },
      {
        method: 'POST',
        url: '/hello',
        schema: { ... },
        handler: Function,
        prefix: String,
        logLevel: String,
        bodyLimit: Number,
        constraints: { ... },
      }
    ]
  }

Example

const fastify = require("fastify")();

(async () => {
  await fastify.register(require("@fastify/routes"));
  fastify.get("/hello", {}, (request, reply) => {
    reply.send({ hello: "world" });
  });

  fastify.listen({ port: 3000 }, (err, address) => {
    if (err) {
      console.error(err);
      return;
    }
    console.log(fastify.routes);
    /* will output a Map with entries:
    {
      '/hello': [
        {
          method: 'GET',
          url: '/hello',
          schema: Object,
          handler: <Function>,
          prefix: <String>,
          logLevel: <String>,
          bodyLimit: <Number>
        }
      ]
    }
    */
  });
})();

License

MIT License

fastify-routes's People

Contributors

airhorns avatar cemremengu avatar delvedor avatar dependabot-preview[bot] avatar dependabot[bot] avatar eomm avatar fdawgs avatar filoozom avatar frikille avatar greenkeeper[bot] avatar jsumners avatar mcollina avatar miquelfire avatar mrsabs avatar rafaelgss avatar salmanm avatar uzlopak avatar zekth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fastify-routes's Issues

The map provided by .routes is empty

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.17.0

Plugin version

5.1.0

Node.js version

18.16.0

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

11

Description

I'm looking for a way to get information about fastify routes so that I can parse and output some of that to a file. I've setup a barebones npm project so I can play around with plugins.

Steps to Reproduce

const fastify = require("fastify")({
  logger: true,
});
fastify.register(require("@fastify/routes"));

fastify.get("/hello", async (request, reply) => {
  return { hello: "world" };
});

fastify.listen({ port: 3000 }, function (err, address) {
  if (err) {
    fastify.log.error(err);
    process.exit(1);
  }
  console.log(fastify.routes);
});

Expected Behavior

I would expect the Map to not be empty.

{"level":30,"time":1685619932741,"pid":19812,"hostname":"vasilache","msg":"Server listening at http://[::1]:3000"}
{"level":30,"time":1685619932743,"pid":19812,"hostname":"vasilache","msg":"Server listening at http://127.0.0.1:3000"}
Map(0) {}

Add Typescript definitions

๐Ÿš€ Feature Proposal

Add missing Typescript definitions.

Motivation

I would like to use fastify-routes in a Typescript project, especially for testing registered routes. I did not found any type definitions in this project or some definitions from https://www.npmjs.com/~types.

Would be happy if you can support Typescript in the future. I'm not so deep into TS definitions but with some practice I can support.

Example

No example needed.

Package renamed without updating README

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

The package is now called @fastify/routes but the readme refers to it as fastify-routes

Errant "oracle" tag in package.json file

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

While looking for other Oracle plugins for Fastify, I happened to see this package appear with a tag of oracle.

Error when registering array of methds

const key = method.toLowerCase()

From the Fastify documentation (https://www.fastify.io/docs/latest/Routes/):

method: currently it supports 'DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT' and 'OPTIONS'. It could also be an array of methods.

This plugin is assuming that routes are only defined with one method. Thus, it bombs with "TypeError: method.toLowerCase is not a function" when confronted with an array of methods.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.