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

@vfshera/fastify-dir-routes

v0.2.3

Published

A Fastify plugin that enables file system-based routing, utilizing directories to specify URL segments matched by the router.

Downloads

9

Readme

Fastify Directory Based Routes

A Fastify plugin that enables file system-based routing, utilizing directories to specify URL segments matched by the router.

Installation

pnpm add @vfshera/fastify-dir-routes
npm i @vfshera/fastify-dir-routes

Setup

Server

import fastify, { type FastifyInstance } from "fastify";
import FastifyDirRoutes from "@vfshera/fastify-dir-routes";

const main = async (): Promise<void> => {
  const app: FastifyInstance = fastify({ logger: { level: "debug" } });

  await app.register(FastifyDirRoutes, {
    routesDir: "./routes",
    prefix: "/api",
  });

  app.printRoutes();
  await app.listen({ port: 3000 });
};
main().catch((error) => { 
  console.log(error);
});

Directory structure

├── src
   ├── server.ts
└──routes
    ├── (common)
    │   ├── booking
    │   │   └── route.ts  -- /api/booking
    │   └── route.ts
    ├── _ignore_me        -- will be ignored
    │   └── route.ts
    ├── profile
    │   └── [...all]
    │       └── route.ts  -- /api/profile/*
    ├── route.ts          -- /api/
    └── user
        └── [id]-[name]
            ├── route.ts  -- /api/user/:id-:name
            └── settings  -- will be ignored because there is no route.ts

Usage

Export a function with HTTP Method name in uppercase as the function name.

In routes/user/[id]-[name] which resolves to /api/user/:id-:name


import type { RouteHandler } from  "@vfshera/fastify-dir-routes";

// handles post request to /api/user/:id-:name
export const POST: RouteHandler<{
  Params: { id: string; name: string };
}> = async (req, res) => {

  res.send({ message: `Hello ${req.params.name} with id ${req.params.id}` });
};

Handler with Options.

routes/(common)/booking resolves to /api/booking


import type { RouteHandler } from "@vfshera/fastify-dir-routes";

// handles post request to /api/booking
export const POST: RouteHandler<{
  Body: { name: string };
  Reply: { message: string };
}> = async (request, reply) => {
  reply.send({ message: `Thank you for booking ${request.body.name}!` });
};

POST.opts = {
  schema: {
    body: {
      type: "object",
      properties: {
        name: {
          type: "string",
        },
      },
      required: ["name"],
    },
  },
};

Catch All route (wildcard route)

routes/profile/[...all] resolves to /api/profile/*

The plugin provides the wildcard value in params with same name used in directory.

[...all] gives adds all property in params. Accessed as req.params.all

[...slug] will provides slug in params. Accessed as req.params.slug


import type { RouteHandler } from "@vfshera/fastify-dir-routes";

// handles get request to /api/profile/*
export const GET: RouteHandler<{
  Params: { all: string };
  Reply: { message: string };
}> = async (req, res) => {

  return res.send({ message: `Catched all: '${req.params.all}'` });
};

Check the examples folder for more

Acknowledgements

This project builds upon Great work from the following packages:

License

MIT