1.1.3 • Published 3 years ago

fastify-organizer v1.1.3

Weekly downloads
1
License
ISC
Repository
github
Last release
3 years ago

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.

NameRequiredTypeDescription
type+string ( may be one of routes, decorators, plugins, middlewares, hooks or parsers)Type of folder structure
dir+stringPath to folder
prodDir-stringPath 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-RegexpIf 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.

NameRequiredTypeDescription
default+anyThe main entity to be connected. Each file type has its own specifics. Read about the types below.
autoload-booleanIf 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.

NameRequired?TypeDescription
name+stringProperty 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.

NameRequired?TypeDescription
event+stringLifecycle 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

NameRequired?TypeDescription
opts-objectDefault 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

NameRequired?TypeDescription
type+string or arrayType 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

NameRequired?TypeDescription
url-string or array of stringsDefine 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;
1.1.3

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.1.2

3 years ago

1.0.0

4 years ago

0.5.5

5 years ago

0.5.4

5 years ago

0.5.3

5 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago