0.2.5 • Published 5 years ago

koa-decorate v0.2.5

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

koa-decorate

npm downloads Travis License

koa-router 提供装饰器。

安装

NPM

应用配置

Koa-decorate 基于 es7 提供的装饰器, 但是 NodeJs 暂时不支持装饰器语法糖。 所以我们需要使用 TypeScript 开发我们的应用, 我们可以通过 ts-node 直接运行 TypeScript,无需通过线下先编译。

npm install --save-dev ts-node nodemon

配置 nodemon.json (使用详情请查看koa-app)

{
  "restartable": "rs",
  "verbose": true,
  "execMap": {
	"ts": "ts-node",
	"js": "node"
  },
  "watch": [
    "src"
  ],
  "ext": "ts"
}

API 使用说明

Decorator

类别: 工厂类

new Decorator(opts)

创建一个 koa-decorate 实例。

参数类别描述
optsObject
opts.routerObjectkoa-router 实例
opts.controllersObject路由控制器类

Decorator.routes ⇒ function

该方法用来装载控制器,并派发与请求想匹配的路由,返回一个 koa 中间件。

类别Decorator 的实例属性。

举例
基础用法:

// app.ts
import Koa from 'koa';
import Router from 'koa-router';
import Decorator from 'koa-decorate';

import Controller from './controller'; // 路由控制器类

const router = new Decorator({
    router: new Router(),
  	controllers: Controller
});

app.use(router.routes());

http-method ⇒ @Get|@Post|@Put|@Delete|@All

创建 @Verb 方法来匹配 HTTP 方法,Verb 是 HTTP 动词中的一个,像 @Get or @Post 等。

另外, @All 可以与所有的 HTTP 方法相匹配。

举例

// CatController.ts
import { Path, Get, Post } from 'koa-decorate';

@Path('/api/cat')
class CatController {

  	@Get
  	@Path('/info')
  	async getCatInfo () {
    	return await new Promise(resolve => {
			setTimeout(() => resolve({
				id: 1,
				name: 'Lina Weiss',
				type: 'Norwegian Forest Cat',
			}), 1000);
    	})
  }

	@Post
	@Path('/info/')
	async CreateCat () {
		return {
			status: 200,
			data: {
				id: 2
			},
			message: 'Created successfully...',
		};
	}

}

export { CatController };

path ⇒ @Path

将 URL 与回调函数或控制器相匹配使用 @Path,当 authFunc 返回 true ,控制器可以执行逻辑操作,否则拒绝访问。

参数类别描述
pathString
authFuncFunction => Boolean验证回调函数,非必须

举例

// CatController.ts
import { Path, Get } from 'koa-decorate';

@Path('/api/cat')
class CatController {

  	@Get
  	@Path('/info/:id', (ctx) => Number(ctx.params.id) === 1)
  	async getCatInfo () {
    	return {
      		id: 1,
      		name: 'Lina Weiss',
      		type: 'Norwegian Forest Cat'
    	};
	}
}

export { CatController };

parameter ⇒ @Param|@Query|@Body|@Ctx|@Next

@Parameter 类型的装饰器用来修饰 URL 中的参数或 koa 回调路由回调中的参数,Parameter 可以是 @Param@Query@Body@Ctx@Next 中的一个。(函数形参修饰器只有 TypeScript 支持)

参数类别描述
nameString

举例

// CatController.ts
import { Path, Get, Post, Param, Query, Body, Ctx } from 'koa-decorate';

@Path('/api/cat')
class CatController {

  	@Get
  	@Path('/info/:type')
  	async getCatInfo (@Param('type') type: string,
				@Query('info') info: string,
	) {
    	return { type, info };
  	}

  	@Post
  	@Path('/info/:type')
  	async CreateCat (@Param('type') type: string,
				 @Body('requestBody') requestBody: any,
	) {
    	return {
      		status: 200,
      		data: Object.assign(requestBody, { type }),
      		message: 'Created successfully...',
    	};
  	}

}

export { CatController };

hook ⇒ @Before|@After

当 URL 与路由匹配正确后,在执行控制器逻辑前会先执行 @BeforehookFunc ,处理完控制器逻辑后会执行 @AfterhookFunc

参数类别
hookFuncFunction回调钩子

举例

// CatController.ts
import { Path, Get, Param, Query, Before, After } from 'koa-decorate';

@Path('/api/cat')
class CatController {

  	@Get
  	@Path('/info/:type')
  	@Before((ctx, next) => {
    	// ...
  	})
  	@After((ctx, next) => {
    // ...
  	})
	async getCatInfo (@Param('type') type: string,
				  @Query('info') info: string,
	) {
    	return { type, info };
  	}
}

export { CatController };

Controller

Kind: 导出一个路由控制器集合的 HashMap

Example

// Controller/index.ts
import { CatController } from './cat';

export default {
  Cat: CatController
};

示例

koa-app

Licences

MIT

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

6 years ago

0.2.1

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.1

6 years ago