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 🙏

© 2024 – Pkg Stats / Ryan Hefner

deco-brick

v0.3.8

Published

A decorator style http server with koa

Downloads

36

Readme

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;