Skip to content

SerayaEryn/revane-fastify

Repository files navigation

revane-fastify

Build Status Coverage Status NPM version

Installation

npm install revane-fastify --save

Example

const RevaneFastify = require('revane-fastify');
const BeanProvider = require('revane-ioc');

const beanProvider = new BeanProvider({
  basePackage: __dirname
});
const options = {
  port: 3000
};
const revaneFastify = new RevaneFastify(options, beanProvider);
return revaneFastify
    .setNotFoundHandler((err, request, reply) => {
      // ...
    })
    .setErrorHandler((request, reply) => {
      // ...
    })
    .register('something')
    .register((instance, opts, next) => {
      // ...
      next()
    })
    .after((err) => {
      // ...
    })
    .ready((err, instance) => {
      // ...
    })
    .listen()
    .then((address) => {
      // ...
    });

API

new RevaneFastify(options, beanProvider)

options

host

The host that will passed to fastify.

port

The port that will passed to fastify.

silent

If set to true no information about the application will be logged. Defaults to false.

use(middleware)

Allows to add a middleware.
Note: This will be executed asynchronous.

register(plugin, [options])

Allows to add a fastify plugin.
Note: This will be executed asynchronous.

register(id)

Selects a bean by id and adds it to the fastify server.
Note: This will be executed asynchronous.

registerControllers()

Registers all beans decorated with @Controller(). Note: This will be executed asynchronous.

listen()

Starts listening on the configured host/port. Returns a Promise.

close()

Closes the server. Returns a Promise.

port()

Returns the port of the server.

ready(handler)

Calls the handler function when all olugins have been loaded.
Note: This will be executed asynchronous.

setErrorHandler(handler)

Adds an error handler.

fastifyRevane.setErrorHandler((error, request, reply) => {
  // ...
})

Note: This will be executed asynchronous.

setNotFoundHandler(handler)

Adds a page not found handler.

fastifyRevane.setNotFoundHandler((error, request, reply) => {
  // ...
})

Note: This will be executed asynchronous.

after(handler)

Executes the handler function after the current plugin has been added.
Note: This will be executed asynchronous.

Controllers

It is possible to create controllers using decorators.

import { Get, Param } from 'revane-fastify'

class UserController {
  @Get('/user/:id')
  getUser (@Param id) {
    return {
      id
    }
  }

  @Get('/users/')
  getUsers () {
    return []
  }
}

There are decorators for the http methods:

  • Get(url, [options])
  • Post(url, [options])
  • Put(url, [options])
  • Delete(url, [options])
  • Patch(url, [options])
  • Head(url, [options])
  • Options(url, [options])
  • All(url, [options])

Furthermore there are decorators that provide information from the request and the reply itself.

  • Query

  • Cookie

  • Param

  • Body

  • Header

  • Reply

  • Log

  • Headers

  • Params

  • QueryParameters

  • RequestBody

  • Cookies