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

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

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;

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;

Share Improve this question asked May 25, 2023 at 9:11 VillarVillar 1331 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745398800a4626005.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信