openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

Full example for how and where to set OpenApi config for overwriting defaults

Open timganter opened this issue 2 years ago • 3 comments

Hello! I don't think this is a bug. I'm pretty sure I'm just doing this wrong.

I would like to set OpenApi.WITH_CREDENTIALS = true. I was originally trying to do this during the code gen process. I read the docs and I thought that maybe the OpenApi object was supposed to be set during the build/generation process.

So, I wrote a file like this for my generation process. Which generated the library but didn't overwrite the defaults.

const OpenAPI = require('openapi-typescript-codegen')

OpenAPI.WITH_CREDENTIALS = true
OpenAPI.BASE = 'http://www.yahoo.com'

OpenAPI.generate({
  input: '../openapi/openapi.yaml',
  output: './openapi-typescript-codegen',
})

console.log('OpenAPI', OpenAPI)

The console.log('OpenAPI', OpenAPI) output looks like this...

OpenAPI {
  HttpClient: {
    FETCH: 'fetch',
    XHR: 'xhr',
    NODE: 'node',
    AXIOS: 'axios',
    ANGULAR: 'angular'
  },
  Indent: { SPACE_4: '4', SPACE_2: '2', TAB: 'tab' },
  default: {
    HttpClient: {
      FETCH: 'fetch',
      XHR: 'xhr',
      NODE: 'node',
      AXIOS: 'axios',
      ANGULAR: 'angular'
    },
    generate: [AsyncFunction: dr]
  },
  generate: [AsyncFunction: dr],
  WITH_CREDENTIALS: true,
  BASE: 'http://www.yahoo.com'
}

After some poking around the issue queue I found this issue which clued me in that the OpenApi should be used client side when using the library, not when generating the library.

So, I have something like the following client-side now...

  import { MyService, OpenAPI } from 'generated'
  OpenAPI.WITH_CREDENTIALS = true
  OpenAPI.BASE = 'https://yahoo.com' // == don't need this, but just trying to set it so that I can confirm I'm successfully overwriting the config.
  
  const stuff = async () => await MyService.stuff()

  stuff()

However, I don't see the BASE url change or the WITH_CREDENTIALS true being set. MyService.stuff() is still using the defaults set in my OpenApi yaml files.

What am I don't wrong here? A full example would be extremely helpful! 🙏 🙇

timganter avatar Feb 07 '23 06:02 timganter

Nevermind! It was a caching issue. 🤦 This worked on the client side after all...

  import { MyService, OpenAPI } from 'generated'
  OpenAPI.WITH_CREDENTIALS = true
  OpenAPI.BASE = 'https://yahoo.com' // == don't need this, but just trying to set it so that I can confirm I'm successfully overwriting the config.
  
  const stuff = async () => await MyService.stuff()

  stuff()

Leaving this issue open and will let you decide if you'd like to close it or not. A full example would still be nice!

timganter avatar Feb 07 '23 06:02 timganter

Thank you. I was similarly confused as to where to configure the OpenAPI properties

Naught0 avatar Sep 06 '23 02:09 Naught0

This seems like it should be in the docs.

dmexs avatar Nov 28 '23 20:11 dmexs