antDesignPro6: How to use openapi to automatically generate interface declaration documents & interface calls (4 steps)

1. Usage steps:

1. Configure the openAPI field in config.ts.

openAPI: Array type, multiple different module interfaces can be configured.

export default defineConfig({
    /**
   * @name openAPI 插件的配置
   * @description 基于 openapi 的规范生成serve 和mock,能减少很多样板代码
   * @doc https://pro.ant.design/zh-cn/docs/openapi/
   */
   openAPI: [
    {
      requestLibPath: "import { request } from 'umi'", // 要使用的请求库
      // schemaPath: join(__dirname, 'oneapi.json'),
      // schemaPath: swagger不同模块接口对应的文档路径(根据后端提供地址获取,如下截图)
      schemaPath: 'http://xxx_test.com:端口号/tms-user/v2/api-docs', 
      projectName: 'tms-user', // 生成的api目录名(一般按模块划分)
      apiPrefix: 'process.env.TMS_USER', // 接口声明文档中请求的前缀名
    },
    {
      requestLibPath: "import { request } from 'umi'",
      schemaPath: 'http://xxx_test.com:端口号/tms-waybill/v2/api-docs',
      projectName: 'tms-waybill',
      apiPrefix: 'process.env.TMS_WAYBILL',
    },
  ],
  define: {  // 定义一些全局变量
    'process.env': {
      TMS_USER: '/tms-user',
      TMS_WAYBILL: '/tms-waybill',
    },
  },
})
The interface path page xx.html provided by the back-end staff

2. Configure the relevant interface request proxy in proxy.ts (local development).

Multiple environments can be configured, such as test and pre environments. -- Determine the current development environment by configuring different scripts commands in package.json (such as: npm run start:test, npm run start:pre).

/**
 * @name 代理的配置
 * @see 在生产环境 代理是无法生效的,所以这里没有生产环境的配置
 * -------------------------------
 * The agent cannot take effect in the production environment
 * so there is no configuration of the production environment
 * For details, please see
 * https://pro.ant.design/docs/deploy
 *
 * @doc https://umijs.org/docs/guides/proxy
 */
export default {
  /**
   * @name 详细的代理配置
   * @doc https://github.com/chimurai/http-proxy-middleware
   */
  test: {
    '/tms-waybill': {
      target: 'http://xxx_test.com:端口号', // 要代理的具体test环境接口域名
      changeOrigin: true,
    },
  },
  pre: {
    '/xxx': {
      target: 'your pre url', // 要代理的pre接口接口域名
      changeOrigin: true,
      pathRewrite: { '^': '' },
    },
  },
};

3. Run the command & automatically generate the interface document directory.

Run the command: npm run openapi   -》 The system will automatically generate the interface module directory & different api files in the src/services directory -》 Then run npm run start:test   -》 Start calling the interface on the page.

Screenshots of the 2 directories & api files automatically generated by the system
Screenshots of different environment commands in package.json

4. Just use the interface on the page.

// xxx.tsx页面

import api from '@/services/tms-waybill'

const getList = async (params: object) => {
    const { data } = await api.ProjectManageController.getListUsingPOST({ ...params });
    console.log('后台返回的是', data);
};

2. Frequently Asked Questions & Solutions

1. When using the command npm run openapi to generate the latest interface document, the vscode console reports an error: can't read version property value of undefind, xxx.

Answer: Because the background interface is uploading code (release), you can re-run the npm run openapi command after the release.

 

Guess you like

Origin blog.csdn.net/qq_38969618/article/details/129988110