0.3.8 • Published 4 years ago

deco-brick v0.3.8

Weekly downloads
5
License
ISC
Repository
github
Last release
4 years ago

deco-brick

English | 中文

decoration style koa server with typescript

Quick start

There is a scaffold for deco-brick

npm i -g deco-brick-cli
deco-brick-cli init <project>
cd <project> && yarn
yarn start:dev

Usage

BrickServer

brick server config as blow:

{
	port: 3000,                         // koa server port
	controllerPath: PATH_TO_CONTROLLER, // controller path, string or array
	viewPath: PATH_TO_VIEWS             // optional, html files path
}

example:

import { BrickServer } from 'deco-brick';

const app = new BrickServer({
    port: 3000,
    controllerPath: __dirname + '/controllers',
    viewPath: __dirname + '/../../views'
});
app.start();

custom your own koa server

import log = require('./middlewares/log');
import errorHandler = require('./middlewares/error-handler');
import bodyParser = require('koa-bodyparser');

const session = require('koa-session');

import { BrickServer } from 'deco-brick';

class App extends BrickServer {
	start() {
	// load body parse
	this.koa.use(bodyParser({
		onerror: function (err, ctx) {
			ctx.throw('body parse error', 422);
		}
	}));
	// load cors
	this.useCors();
	// load session
	this.koa.keys = [ 'secret-shhh' ];
	this.koa.use(session(this.koa));
	this.koa.use(log());
	this.koa.use(errorHandler());
	super.start();
  }
}

const app = new App({
  port: 3000,
  controllerPath: __dirname + '/controllers',
  viewPath: __dirname + '/../../views'
});

app.start();

Decorators

GET, POST, PUT, DEL

The closest decorator in controller functions must be one of GET, POST, PUT, DEL.

Using koa-router for routes, you can use koa-router path style with GET, POST, PUT, DEL decorator.

Validate

The Validate decorator use joi to validate params. All ctx.params,ctx.request.query and ctx.request.body data are in ctx.params after validate.

Render

Using koa-views and underscore to render views in viewPath you set.

BeforeMiddleware、AfterMiddleware

Using BeforeMiddleware and AfterMiddleware decorator hook some middleware in route lifecycle like koa.

Controller

A controler demo demo as below:

import { GET, POST, PUT, DEL, Validate, Render, BeforeMiddleware, AfterMiddleware } from '../cores/Decorator';
import Joi = require('joi');
import { m1, m2, m3, m4 } from '../middlewares/middleware';

class Controller {
	@BeforeMiddleware(m1())
	@BeforeMiddleware(m2())
 	@AfterMiddleware(m3())
 	@AfterMiddleware(m4())  
	@GET('/')
	async get (ctx: any) {
		return await Promise.resolve({ message: 'hello world' });
  	}
	
	@Render('index')
 	@GET('/view')
  	async page (ctx: any) {
		return {  name: 'pascal' };
 	}

	@Validate({
		username: Joi.string().required(),
		password: Joi.string().required(),
 	})
 	@POST('/login')
 	async login (ctx: any) {
		return await Promise.resolve({ status: 'success' });
	}
}

export = Controller;
0.3.8

4 years ago

0.3.7

4 years ago

0.3.6

4 years ago

0.3.5

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago