Plumier

Plumier

  • Docs
  • Tutorials
  • GitHub

›Fundamentals

Fundamentals

  • Plumier In Five Minutes
  • Application Startup
  • Controller
  • ActionResult
  • Middleware
  • Facility

References

  • Route Cheat Sheet
  • Parameter Binding
  • Validation
  • Converters
  • Authorization
  • Error Handling
  • Serve Static Files
  • File Upload
  • Mongoose Helper
  • Social Login
  • Controller Invoker
  • Static Analysis
  • Testing Tips

Extending

  • Custom Parameter Binding
  • Custom Validator
  • Custom Authorization

Facility

Facility is prebuilt middlewares and configuration bundled into one component. For example to build an API you will need: a Body parser, CORS, Generic error handler and some configuration etc, all can be bundled into one facility called WebApiFacility.

Signature

Facility is a class that implements Facility, the signature of Facility is like below:

export interface Facility {
    setup(app: Readonly<PlumierApplication>): void
    initialize(app: Readonly<PlumierApplication>, routes:RouteInfo:[]): Promise<void>
}
  • setup called during setup process. This method usually used for registering configurations and middlewares
  • initialize called during initialization process. This method usually used for some preparation required before application run, and possible to call promised functions

Develop Your Own Facility

Develop your own Facility is not required, you can register middleware and set some configuration manually. You develop Facility if you want to make it reusable.

For example the WebApiFacility facility is like below:

import Cors from "@koa/cors"
import BodyParser from "koa-bodyparser"
import { DefaultFacility, PlumierApplication } from "plumier"

export class WebApiFacility extends DefaultFacility {
    setup(app: Readonly<PlumierApplication>) {
        app.use({execute: async next => {
            try{
                return next.proceed()
            }
            catch(e){
                //do something with the error
            }
        }})
        app.use(BodyParser({ /* configuration */ }))
        app.use(Cors({ /* configuration */ }))
    }
}

Above code showing that we setup error handler, body parser and cors with some order. Error handler in the top most, it means it will handle all error caused by the next middleware / action.

Access Koa from Facility

In some case if you want to configure Koa, you can do it in facility.

import Cors from "@koa/cors"
import BodyParser from "koa-bodyparser"
import { DefaultFacility, PlumierApplication } from "plumier"

export class WebApiFacility extends DefaultFacility {
    async setup({ koa }: Readonly<PlumierApplication>) {
        //do something with the Koa instance
        koa.use(<koa middleware>)
    }
}
← MiddlewareRoute Cheat Sheet →
  • Signature
  • Develop Your Own Facility
  • Access Koa from Facility
Copyright © 2019 Plumier