Skip to content

API

Zova 提供了一个模块home-api,该模块基于axios提供了基本的API代码骨架。可以在此基础上添加自定义的 API 逻辑,甚至也可以直接替换掉axios底层库

$api

  • Zova 在BeanBase基类中注入了$api对象,从而可以在任何 bean 实例中通过this.$api访问axios实例
  • Zova 同时在app.meta中注入了$api对象,从而可以在 bean 实例的外部访问axios实例

比如,获取菜单数据:

src/suite/a-home/modules/home-layout/src/api/service/menu.ts

typescript
export default (app: ZovaApplication) => {
  return {
    select: () => app.meta.$api.get<any, ServiceMenuEntity[]>('/home/layout/menu/select'),
  };
};

home-api.bean.api

模块home-api提供了一个home-api.bean.apibean,可以直接在里面添加自定义逻辑

src/suite/a-home/modules/home-api/src/bean/bean.api.ts

typescript
export class BeanApi {
  private [SymbolApi]: AxiosInstance;

  protected async __init__() {
    const baseURL = `${this.app.config.api.baseURL || ''}${this.app.config.api.prefix || ''}/`;
    this[SymbolApi] = markRaw(axios.create({ baseURL }));
    // your custom logic maybe here
  }

  protected __get__(prop) {
    return this[SymbolApi] && this[SymbolApi][prop];
  }
}

基于 MIT 许可发布