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

koa-decorate

v0.2.5

Published

🔥 Provides decorators for router middleware of Koa .

Downloads

3

Readme

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 实例。

| 参数 | 类别 | 描述 | | --- | --- | --- | | [opts] | Object | | | [opts.router] | Object | koa-router 实例 | | [opts.controllers] | Object | 路由控制器类 |

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 ,控制器可以执行逻辑操作,否则拒绝访问。

| 参数 | 类别 | 描述 | | --- | --- | --- | | path | String | | | [authFunc] | Function => 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 支持)

| 参数 | 类别 |描述 | | --- | --- | --- | | name | String | |

举例

// 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

| 参数 | 类别 | 描 | | --- | --- | --- | | [hookFunc] | Function | 回调钩子 |

举例

// 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