0.3.4 • Published 4 years ago

luren v0.3.4

Weekly downloads
8
License
MIT
Repository
github
Last release
4 years ago

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')
})
0.3.2

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.0

4 years ago

0.3.1

4 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.1

4 years ago

0.2.2

4 years ago

0.2.0

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.41

5 years ago

0.0.40

5 years ago

0.0.39

5 years ago

0.0.38

5 years ago

0.0.37

5 years ago

0.0.36

5 years ago

0.0.35

5 years ago

0.0.34

5 years ago

0.0.33

5 years ago

0.0.32

5 years ago

0.0.31

5 years ago

0.0.30

5 years ago

0.0.29

5 years ago

0.0.28

5 years ago

0.0.27

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8-18

5 years ago

0.0.8-17

5 years ago

0.0.8-16

5 years ago

0.0.8-15

5 years ago

0.0.8-14

5 years ago

0.0.8-13

5 years ago

0.0.8-12

5 years ago

0.0.8-11

5 years ago

0.0.8-10

5 years ago

0.0.8-9

5 years ago

0.0.8-8

5 years ago

0.0.8-7

5 years ago

0.0.8-6

5 years ago

0.0.8-5

5 years ago

0.0.8-4

5 years ago

0.0.8-3

5 years ago

0.0.8-2

5 years ago

0.0.8-1

5 years ago

0.0.8

5 years ago

0.0.7-18

5 years ago

0.0.7-17

5 years ago

0.0.7-16

5 years ago

0.0.7-15

5 years ago

0.0.7-14

5 years ago

0.0.7-13

5 years ago

0.0.7-12

5 years ago

0.0.7-11

5 years ago

0.0.7-10

5 years ago

0.0.7-9

5 years ago

0.0.7-8

5 years ago

0.0.7-7

5 years ago

0.0.7-6

5 years ago

0.0.7-5

5 years ago

0.0.7-4

5 years ago

0.0.7-3

5 years ago

0.0.7-2

5 years ago

0.0.7-1

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 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