egg-ecarx-interface

1.0.9 • Public • Published

egg-ecarx-interface

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Install

$ npm i egg-ecarx-interface --save

Usage

// {app_root}/config/plugin.js
exports.interface = {
  enable: true,
  package: 'egg-ecarx-interface',
};
 
// {app_root}/app.js
module.exports = app => {
  app.status = app.config.interface
};
 

Configuration

// {app_root}/config/config.default.js
// 如果觉得对象太大,自行 exports.interface = require('path/to/interface.js|json')
exports.interface = {
  locale: true, //是否 对message进行国际化处理
  success: true, //是否 输出success字段
  codeField: 'code', //配置错误码的字段名
  messageField: 'message', // 配合错误内容的字段名
  defaultSuccess: 'success', 默认成功的status
  defaultFail: 'fail', //默认失败的status
  status: {
    success: {
      success: true,
      code: 200,
      message: 'success',
    },
 
    fail: {
      success: false,
      code: 400,
      message: 'fail',
    },
 
    not_found: {
      success: false,
      code: 404,
      message: '%s not found',
    },
 
    invalid_param: {
      success: false,
      code: 422,
      message: 'params error %s',
    },
 
    server_error: {
      success: false,
      code: 500,
      message: 'server error %s',
    }
  }
};

Use

@method ctx.success(data, message)
@args
    - data  返回值里里的data
    - message 成功输出语句, 默认'success'

@example
    ctx.success()
    ctx.success({msg: '成功内容对象'})
    ctx.success({msg: '成功内容对象'}, '成功提示语句')
{
  "success": true,
  "code": 200,
  "message": "成功语句",
  "data": {
    "msg": "成功内容对象"
  }
}

@method ctx.fail(status, data, ...message)
@args
    - status 错误对象,默认 app.status.fail
    - data  返回值里里的data, 如果data是string, 会被当做是message, 请分装成 obj/array
    - ...message 错误输出语句, 和 status的message进行合并处理,合并规则如下
     如果 status 的 message 是非字符串模板,则会进行替换message,如 ctx.fail(app.status.fail, '操作错误') => 'fail' + "操作错误' => '操作错误'
     如果 status 的 message 是字符串模板,则会进行模板拼接,如 ctx.fail(app.status.not_found, '某数据') => '%s not found' + "某数据' => '某数据 not found'

@example
    ctx.fail(app.status.fail, '错误message')
    ctx.fail(app.status.fail, {msg: '错误内容对象'})
    ctx.fail(app.status.fail, {msg: '错误内容对象'}, '错误message')
    //如果是不是字符串模板
        app.status.notmuban = {code:1001, message: 'muban muban'}
        ctx.fail(app.status.notmuban, '错误message') => "错误message"
    //如果是字符串模板
        app.status.muban = {code:1001, message: 'muban %s muban'}
        ctx.fail(app.status.muban, '错误message') => "muban 错误message muban"
    //如果 locale=true 需要国际化
        app.status.muban = {code:1001, message: 'muban %s muban'}
        国际化配置 zh-CN.js 里 'muban %s muban': '模板 %s 模板'
        ctx.fail(app.status.muban, '错误message') => "模板 错误message 模板"

国际化

如果 locale: true 开启了国际化,用户的 message 和 status里的 message 都会先 国际化后,再进行拼接,比如上面的例子
   //如果目标语言是 zh-CN
   //zh-CN.js
   module.exports = {
     'fail': '失败',
     '%s not found': '%s不存在'
   };

   ctx.fail(app.status.not_found, '某数据') => '%s not found' + "某数据' => 国际化 => '%s不存在' + "某数据' =>'某数据不存在'

Package Sidebar

Install

npm i egg-ecarx-interface

Weekly Downloads

0

Version

1.0.9

License

MIT

Unpacked Size

11.4 kB

Total Files

7

Last publish

Collaborators

  • hdumok