可以将 AxiosInstance 传递给由 openapi-generator 生成的 TypeScript 类吗?

huangapple go评论74阅读模式
英文:

Is it possible to pass an AxiosInstance to openapi-generator generated TypeScript classes?

问题

You can pass your AxiosInstance to classes generated by the openapi-generator TypeScript Axios template by extending the generated class and setting the AxiosInstance in the constructor. Here's an example:

import { HotelsRestServiceApi, Configuration } from '../../generated/openapi';
import { axiosClientNoAuth } from '../index'; // Your AxiosInstance

class CustomHotelsRestServiceApi extends HotelsRestServiceApi {
  constructor(config: Configuration, axiosInstance: AxiosInstance) {
    super(config, axiosInstance);
  }
}

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

const customAxios = new CustomHotelsRestServiceApi(config, axiosClientNoAuth);

This way, you create a custom class that extends the generated class and passes your AxiosInstance to it in the constructor.

英文:

How can i pass my AxiosInstance to classes generated by openapi-generator typescript-axios template?

I tried change prototype of BaseAPI class, but it doesn't work. And there are no places where i can pass my axios instance

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;

答案1

得分: 1

修改模板解决了问题:

  • openapi-templates下载 baseApi.mustache 文件

  • 将其放在项目中的某个位置,例如 .openapi-generator/templates

  • 修改导入 globalaxios 为你的 axios 实例:

    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
    

(我的 openapi-generator-config.yaml)

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

Got solved it by modifying template:

  • download baseApi.mustache file from openapi-templates

  • put it somewhere in your project, for example in .openapi-generator/templates

  • modify import globalaxios to your axiosinstance:

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

  • generate new code with 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

huangapple
  • 本文由 发表于 2023年5月25日 17:11:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定