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

fastify-now

v3.1.0

Published

Fastify Now - file based routing for fastify

Downloads

2,402

Readme

Fastify Now - a fastify plugin

npm badge ci badge license badge

File based routing for fastify (v4). If you want to use this library with fastify v3, you should use version 2.X If you want to use this library with fastify v2, you should use version 1.X

Example (see example folder)

For the given folder structure

├── src
    ├── routes -- routes folder (can be changed)
    |   ├── user -- user resource endpoint folder
    |   |   └──index.ts -- has POST method function
    |   |   └──[id].ts -- has GET & PUT method functions
    |   └── index.ts
    └── index.ts -- server file

will result:

└── / (GET)
    └── user (POST)
        └── /
            └── :id (GET)
                :id (PUT)

Path params can also be prefixed with : E.g. :id instead of [id]

The plugin will ignore any .{test|spec|bench}.js files when loading routes

Install

Versions

The latest version is 2.X which supports fastify v3. If you want to use this library with fastify v2, you should use version 1.X

npm install fastify-now

Use

In your main server file (can see in example folder)

import fastify from 'fastify';
import path from 'path';
import fastifyNow from 'fastify-now';

const server = fastify({ logger: true });
server.register(fastifyNow, {
  routesFolder: path.join(__dirname, './routes'),
  /**
   * Can also provide a prefix
   */
  // pathPrefix: '/api'
});
const PORT = Number(process.env.PORT) || 5000;
server.listen({ port: PORT }).then(() => {
  // ...
});

In each route file, you need to export a function with a supported HTTP method as the function name, in uppercase letters

Currently supported HTTP methods: GET, POST, DELETE & PUT

In /routes/user/:id/index.ts:

import { NowRequestHandler } from 'fastify-now';

export const GET: NowRequestHandler<{ Params: { id: string } }> = async (req, rep) => {
  return { userId: req.params.id };
};

export const PUT: NowRequestHandler<{ Params: { id: string } }> = async (req, res) => {
  req.log.info(`updating user with id ${req.params.id}`);
  return { message: 'user updated' };
};

// with the server instance as the context (aka this)
export const POST: NowRequestHandler<{ Body: { name: string } }> = async function(req, res) {
  // this = fastify instance
  const [user] = await this.db.query(`SELECT * FROM users WHERE name = ${req.body.name}`);
  // ... rest of code
}

Dynamic params are specified in the file name, or the folder name.

routes/user/:id/index.ts - will resolve to /user/:id routes/book/:id/chapter/:chapterId.ts - will resolve to /book/:id/chapter/:chapterId

You can also add endpoint fastify opts. For that, I created a new interface called NowRequestHandler.

in /routes/user/index.ts:

import { NowRequestHandler } from 'fastify-now';

type Post = NowRequestHandler<{
  Body: { name: string };
  Params: { id: string };
  Reply: { message: string } | { userId: string };
}>;

export const POST: Post = async (req, rep) => {
  if (req.body.name === 'Jon Doe') {
    /**
     * in async function, you can return undefined if you already sent a response
     * then it won't try to send a response again with the returned value;
     */
    rep.status(400).send({ message: 'Name can not be Jon Doe' });
    return;
  }
  return { userId: req.params.id };
};

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

License

Licensed under MIT