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

luren

v0.3.4

Published

Luren is a simple framework based on Koa2

Downloads

381

Readme

Luren

npm version Dependencies Status Build Status Coverage Status

Luren是基于Koa一个简单web框架,可以快速方便的生成RESTFUL风格的API,提供依赖注入InversifyJS和RESTFUL API的文档Swagger的支持.Luren是基于Decorator来设置Controller的,所以ts中必须开启decorator支持。Luren在启动时会自动加载工作目录下的boot, middleware,controllers,models四个目录下的ts/js文件。

src
├── boot
├── controllers
├── middlewares
├── models

Controller

Controller提供API的组件,也是Luren中最重要的一个组件,一个controller即代表一个资源,controller中包含多个action,即资源相关的API。下面的controller会生成一个POST /api/v1/demos/foo的API, 当接受请求时会检查相应的参数,如header,query,body等然后处理之后传递给相应的action函数,在action函数返回结果之后,会将结果根据Response类型进行处理然后返回。

@injectable()
@Controller({ prefix: '/api', version: 'v1' })
export default class DemoController {

  @Action({ method: HttpMethod.POST, path: '/foo' })
  @Response({ type: 'string' })
  public async foo( @InBody('name','string', true) name: string) {
  	return `Hello ${name}`
  }

Middleware

Middleware是一个普通函数或者继承Processor或实现IProcessor接口的对象

async function handle(ctx: Context, next: INext) {
  // do something
  await next()
}
class Authorization extends Processor<boolean> {
  public async process(@InQuery('name') name: string) {
    return name === 'foo'
  }
}

Models

通过luren-schema对model类进行注解,可在其他地方直接引用该类型, 同时可以链接到相应的DataSource。

@Collection({datasource: 'mongodb', database: 'demo' })
@Schema()
export default class User {
  @Prop()
  public firstName: string
  @Prop()
  public lastName: string
  @Prop({type: 'number', required: true})
  public age: number
}

Boot

boot文件下包含需要随应用一起启动的内容, 文件以文件名的顺序加载。

依赖注入

Luren支持使用InversifyJS来加载controller

@injectable()
@Controller({ prefix: '/api', version: 'v1' })
export default class DemoController {
  @Action({ path: '/foo' })
  @Response({ type: Person })
  public async bar(@InQuery('name') name: string) {
    return null
  }

  // create server with inversify container
  const server = new Luren({ container })

Swagger

luren-swagger可以作为插件加载,会根据controller的注解自动生成Swagger文档。

const server = new Luren({ container })
const swagger = new Swagger({
  info: { title: 'demo', version: '1.0' },
  servers: [{ url: '/', description: 'demo api' }]
})
server.plugin(swagger.pluginify())

代码示例

import jwt from 'jsonwebtoken'
import _ from 'lodash'
import { APITokenAuthentication, Luren } from 'luren'
import { Swagger } from 'luren-swagger'
import dataSource from './dataSource'
import container from './inversify'

// create server with inversify container
const server = new Luren({ container })
// set work directory
server.setWorkDirectory(__dirname)
// set data source
server.setDefaultDataSource(dataSource)
// authentication
server.setDefaultAuthentication(
  new APITokenAuthentication({
    key: 'Authorization',
    source: 'header',
    async validate(accessToken: string) {
      const data = jwt.verify(accessToken, 'jwt-key')
      return data ? true : false
    }
  })
)
// serve files
server.serve('/public', { root: '/', maxage: 30 * 24 * 60 * 60 * 1000, defer: true })

// setup swagger plugin
const swagger = new Swagger({
  info: { title: 'demo', version: '1.0' },
  servers: [{ url: '/', description: 'demo api' }]
})
server.plugin(swagger.pluginify())

// start server
server.listen(3000).then(async () => {
  logger.info('Server started')
}er.info('Server started')
})