javascript 是否可以将AxiosInstance传递给openapi生成器生成的TypeScript类?

fykwrbwg  于 2023-05-27  发布在  Java
关注(0)|答案(1)|浏览(361)

如何将AxiosInstance传递给openapi-generator typescript-axios模板生成的类?
我尝试改变BaseAPI类的原型,但它不工作。而且没有地方可以传递我的axios示例

import { HotelsRestServiceApi, Configuration } from '../../generated/openapi';
import { BaseAPI } from '../../generated/openapi/base'; //Base api class generated by openapi
import { axiosClientNoAuth } from '../index'; //My AxiosInstance

const config = new Configuration({
  basePath: 'http://localhost:8080',
});

const baseApi = new BaseAPI(config, 'http://localhost:8080', axiosClientNoAuth);

const axios = new RestServiceApi(config);

axios.prototype.__proto__ = baseApi.prototype;
qcbq4gxm

qcbq4gxm1#

通过修改模板解决:

  • openapi-templates下载baseApi.mustache文件
  • 将其放在项目中的某个位置,例如.openapi-generator/templates
  • 修改import globalaxios到你的axiosinstance:

import { axiosClient as globalAxios } from '../../api';

  • 使用cli生成新代码:
rm -rf src/generated/openapi; openapi-generator-cli generate -t .openapi-generator/templates -c openapi-generator-config.yaml

(My openapi-generator-config.yaml)

generatorName: typescript-axios
outputDir: src/generated/openapi
cleanupOutput: true
inputSpec: ../app/backend/target/openapi.yaml
additionalProperties:
  supportsES6: true

相关问题