0.1.5 • Published 5 years ago

kvl v0.1.5

Weekly downloads
24
License
ISC
Repository
github
Last release
5 years ago

Kvl

封装了`express`/`typescript`开发的nodejs简易架构
//全局安装kvl
$ npm install kvl@latest -g
//快速创建项目
$ kvl init [项目名称]
$ cd [项目名称]
$ npm install
//启动项目
$ kvl dev
//实例(server.ts)
import * as Kvl from 'kvl';
import { Router, config } from 'kvl';

@Router() 
class HelloWord{

 @config({ url: '/hello', name: 'hello', type: 'get' })
 private hello(req, res): void {
  res.end('<h1>Hello, world</h1>')
 }

}

//实例化kvl
const kvlInit = Kvl({
  port: 4457,
  router: [ HelloWord ],
  this: true,
  static: path.join()
})

Api说明

//Kvl返回一个object:{ app, server }, app是express对象(express()),server是http对象(http.createServer())
const { app, server } = Kvl({
  port: number,  			            //服务端口号,
  router?: string[],		          //路由,通过Router/config
  baseUrl?: string,		            //服务根路径,后续所有路由都会带有这个路径
  this?: boolean,			            //指定class所在方法的this对象是否执行class,默认false
  static?: string,			          //静态服务器路径,详情见express.static方法,不说明此属性则不创建
  headers?: { string: string },	  //设置全局header对象
  use?: Function[],			          //数据function会填充到express().use方法中
  interceptor?: Function[],		    //全局拦截器
})

//用于清晰化管理路由模块,及管理class内的全部路由
@Router({
  name?: string,			            //模块名称,方便管理使用
  url?: string,				            //路由根路径,class内路由都会带有这个路径
  interceptor?: Function[],		    //拦截器
  interceptorLevel?: 1			      //拦截器级别,如果设置成1,抛弃全局路由
})
class HelloWorld{
 //创建路由,只有config的方法才会加入到路由中
 @config({ 
  url: string,	 			                 //路由路径
  name: string,				                 //路由名称,方便管理使用
  type: string | string[],		         //路由请求方法 (dmeo: 'get' | ['get','post'])
  validation?: Kvl.Validation,         //用于数据验证
  interceptor?: Function[] | Function, //拦截器
  interceptorLevel?: 1 | 2 | 3		     //拦截器级别,1:抛弃全局得拦截器,2:抛弃所在class得拦截器和全局得拦截器,3:抛弃所在class得拦截器,但是保留全局得拦截器
 })
 private hello(req, res): void {
  res.end('<h1>Hello, world</h1>')
 }
}

validation拦截器, 一半般用于进行接收参数得完整性验证

const validation: Kvl.Validation = { 
  template: {                          //返回的消息模板形式,[非必要]
    empty: '请输入${name}',            //参数为空得情况下返回得错误
    regular: '${name}格式不正确'       //参数正则验证不通过返回得错误
  },
  name: {                             //传进来的参数
    type: 'string',                   //参数类型
    required: true,                   //是否必须传值,默认true,[非必要]
    name: '用户名',                   //别名
    description: '描述述'             //参数描述
  }, 
  age: { 
    type: 'number',
    required: false,
    name: '年龄',
    done: function(err, response) {    //为参数单独设置返回消息,返回类型response为express得app对象内response),[非必要]
      response.json({
        code: 0,
        msg: '年龄格式不正确呦'
      })
    } 
  },
  phone: { 
    type: 'phone',
    name: '手机号'
  },
  done: function (err, response) {    //为整个验证器设置返回消息,[非必要]
    response.json({
      code: 0,
      msg: err.errorMsg
    })
  },
  disable: false                      //是否禁用加载器,默认true,[非必要]
}
//kvl内部默认定义了一套验证器返回消息,及消息模板,
let ValidationTemplate = {
  empty: '${name}不能为空',
  regular: '${name}格式不正确'
}
let ValidationDone = function(err, response){
  response.json({ code: 0, msg: err })
}

//当前你也可以通过自定义得方式修改这些内容
import { ValidationDone, ValidationTemplate } from 'kvl';
ValidationDone((error, response) => {
  response.json({
    status: 0,
    message: error,
    other: 'Helllo, 这里是自定义得'
  })
})

ValidationTemplate({
  empty: '${name},╮(╯-╰)╭不能为空',
  regular: '${name}( ╯□╰ )格式错了'
})

//验证器里存在得type类型,也是可以自己手动添加得
//目前kvl内自带得type类型
let ValidationType = {
  phone: /^[1][3,4,5,6,7,8,9][0-9]{9}$/,
  number: /^[0-9]*$/,
  required: /\S/
}

//为kvl添加验证器参数类型
import { ValidationType } from 'kvl';
ValidationType.email = /^[a-z0-9]+([._\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/
0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.62

5 years ago

0.0.61

5 years ago

0.0.60

5 years ago

0.0.59

5 years ago

0.0.58

5 years ago

0.0.57

5 years ago

0.0.56

5 years ago

0.0.55

5 years ago

0.0.54

5 years ago

0.0.53

5 years ago

0.0.52

5 years ago

0.0.51

5 years ago

0.0.50

5 years ago

0.0.49

5 years ago

0.0.48

5 years ago

0.0.47

5 years ago

0.0.46

5 years ago

0.0.45

5 years ago

0.0.44

5 years ago

0.0.43

6 years ago

0.0.42

6 years ago

0.0.41

6 years ago

0.0.39

6 years ago

0.0.38

6 years ago

0.0.37

6 years ago

0.0.36

6 years ago

0.0.35

6 years ago

0.0.34

6 years ago

0.0.33

6 years ago

0.0.4

6 years ago

0.0.1

6 years ago

0.0.32

6 years ago

0.0.31

6 years ago

0.0.3

6 years ago

0.0.29

6 years ago

0.0.28

6 years ago

0.0.27

6 years ago

0.0.26

6 years ago

0.0.25

6 years ago

0.0.24

6 years ago

0.0.23

6 years ago

0.0.22

6 years ago

0.0.21

6 years ago

0.0.2

6 years ago