@zenweb/api
TypeScript icon, indicating that this package has built-in type declarations

2.3.5 • Public • Published

ZenWeb API module

ZenWeb

接口调用资源返回的统一处理方法,返回格式为 JSON

演示

import { Context, mapping } from 'zenweb';

export class APIController {
  @mapping()
  hello(ctx: Context) {
    ctx.success('Hello');
  }

  @mapping()
  error(ctx: Context) {
    ctx.fail('error info'); // 在调用 fail 方法后会直接跳出方法并输出
    console.log('这行不会执行');
  }
}

可配置项

类型 默认值 说明
failCode number 失败代码,调用 fail 方法时返回的数据 code 代码
failStatus number 422 失败时的 http 状态码
success function(ctx: Context, data?: any) return { data } 成功时数据处理函数
fail function(ctx: Context, err: ApiFail) return { code: err.code, data: err.data, message: err.message } 错误时数据处理函数

方法说明

成功输出

接口调用成功时统一输出结果,注意!这里并不会跳出控制器函数,而是需要手动 return;

ctx.success(data?: any): void;

失败输出

ctx.fail(msg: string | ApiFailDetail): throw ApiFail;

interface ApiFailDetail {
  message?: string;
  code?: number;
  status?: number;
  data?: any;
}

Package Sidebar

Install

npm i @zenweb/api

Weekly Downloads

0

Version

2.3.5

License

MIT

Unpacked Size

5.43 kB

Total Files

6

Last publish

Collaborators

  • yefei777