2.3.5 • Published 3 years ago

@zenweb/api v2.3.5

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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('这行不会执行');
  }
}

可配置项

类型默认值说明
failCodenumber失败代码,调用 fail 方法时返回的数据 code 代码
failStatusnumber422失败时的 http 状态码
successfunction(ctx: Context, data?: any)return { data }成功时数据处理函数
failfunction(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;
}
2.3.5

3 years ago

2.3.0

4 years ago

2.1.1

4 years ago

2.3.2

4 years ago

2.3.1

4 years ago

2.3.4

3 years ago

2.3.3

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago