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 🙏

© 2025 – Pkg Stats / Ryan Hefner

fastify-organizer

v1.1.3

Published

Fastify file structure organizer

Readme

Why is this library need

This library will help you to organize the file structure in your project. Just specify the path to the folder, and it will automatically register all files in this folder as components of the specified type. It can register routes, decorators, plugins, middlewares, hooks and content type parsers.

Requirements

  • fastify = 3.x.x

Getting started

Installation

Install fastify-organizer package via NPM:

npm install --save fastify-organizer

Or via Yarn:

yarn add fastify-organizer

Usage

Import fastify-organizer into your project and connect it to your fastify instance as plugin:

using javascript:

const path = require('path');
const fastify = require('fastify');
const Organizer = require('fastify-organizer');

const server = fastify();

server.register(Organizer, {
    type: 'routes',
    dir: path.join(__dirname, 'routes'),
    ignorePattern: /.*\.test\.(ts | js)$/
});

using typescript:

import path from 'path';
import * as fastify from 'fastify'
import * as fastifyOrganizer from 'fastify-organizer';

const server = fastify();

server.register(Organizer, {
    type: 'routes',
    dir: path.join(__dirname, 'src/routes'),
    prodDir: path.join(__dirname, 'dist/routes'),
    ignorePattern: /.*\.test\.(ts | js)$/
});

Plugin options

These options are used when registering the plugin.

| Name | Required | Type | Description | |------|----------|------|-------------| | type | + | string ( may be one of routes, decorators, plugins, middlewares, hooks or parsers) | Type of folder structure | | dir | + | string | Path to folder | | prodDir | - | string | Path to folder in production mode. This path will be used in production mode. If your files in production mode are transpiled to another folder, you need to specify it here. | | ignorePattern | - | Regexp | If the file name matches the specified pattern, this file will be ignored.

Types of structures

All files have several required parameters. These parameters are what you need to export from the file.

| Name | Required | Type | Description | |------|----------|------|-------------| | default | + | any | The main entity to be connected. Each file type has its own specifics. Read about the types below. | autoload | - | boolean | If the value is false, the file will not be connected. If no value is specified, the file will be connected anyway.

Routes

Creating files

Routing files are created according to the specification of the full declaration.

using javascript:

exports.default = {
  url: '/articles',
  method: 'GET'
  schema: {...},
  async handler(request, reply) {
    ...
  }
}

using typescript:

import { RouteOptions } from 'fastify';

export default {
  url: '/articles',
  method: 'GET',
  schema: {...},
  async handler(request, reply) {
    ...
  }
} as RouteOptions;

Decorators

Additional options

These parameters are passed along with the export of the decorator. See example below.

| Name | Required? | Type | Description | |------|-----------|------|-------------| | name | + | string | Property name to be added | | target | - | string (may be one of request or response) | Defines which entity should be decorated. If the target property is not passed, the global fastify object will be decorated. |

Creating files

For example, let's connect the configuration object from the config library to the global object fastify

using javascript:

const config = require('config');

exports.name = 'config';
exports.target = undefined; // Or just do not define this variable.
exports.default = config;

using typescript:

import * as config from 'config';
import {FastifyInstance} from 'fastify';

declare module 'fastify' {
  export interface FastifyInstance {
    config: any;
  }
}

export const name = 'config';
export const target = undefined; // Or just do not define this variable.
export default config;

Hooks

Additional options

These parameters are passed along with the export of the hook. See example below.

| Name | Required? | Type | Description | |------|-----------|------|-------------| | event | + | string | Lifecycle event to which the hook will be connected |

Creating files

using javascript:

exports.event = 'onRequest';
exports.default = function (request, reply, next) {
  ...
  next();
};

using typescript:

import * as fastify from 'fastify';
import { Server, IncomingMessage, ServerResponse } from 'http';

export const event = 'onRequest';
const hook: fastify.FastifyMiddleware<Server, IncomingMessage, ServerResponse> = function (request, reply, next) {
  ...
  next();
};

export default hook;

Plugins

Additional options

| Name | Required? | Type | Description | |------|-----------|------|-------------| | opts | - | object | Default options of fastify plugin |

Creating files

using javascript:

exports.opts = {
  prefix: '/articles'
};

exports.default = function (request, reply, next) {
  ...
  next();
};

using typescript:

import {Plugin}  from "fastify";
import { Server, IncomingMessage, ServerResponse } from "http";

export const opts = {
  prefix: '/articles'
};

const plugin: Plugin<Server, IncomingMessage, ServerResponse, {}> = (fastify, opts, next) => {
  ...
  next();
};

export default plugin;

Content type parsers

Additional options

| Name | Required? | Type | Description | |------|-----------|------|-------------| | type | + | string or array | Type or array of types of the added parser |

Creating files

using javascript:

exports.type = 'application/jsoff';

exports.default = function (reqest, done) {
  jsoffParser(request, function (err, body) {
    done(err, body)
  });
};

using typescript:

import {ContentTypeParser, FastifyRequest} from "fastify";
import {IncomingMessage} from "http";

export const type = 'application/jsoff';

const parser: ContentTypeParser<FastifyRequest<IncomingMessage>> = function (request, done) {
  done(null, request.body)
};

export default parser;

Middlewares

Additional options

| Name | Required? | Type | Description | |------|-----------|------|-------------| | url | - | string or array of strings | Define this property if you want the middleware above to work only under certain path(s). |

Creating files

using javascript:

exports.default = function (reqest, reply, next) {
  ...
  next();
};

using typescript:

import { FastifyMiddleware } from "fastify";
import { Server, IncomingMessage, ServerResponse } from "http";

const mware: FastifyMiddleware<Server, IncomingMessage, ServerResponse> = function (request, reply, next) {
  ...
  next()
}

export default mware;