0

I would like to dynamically change the API_BASE_URL configured in my typescript client generated by NSWAG. I would like to use the same client with one API_BASE_URL within an angular module and another API_BASE_URL within another Angular module. Is this possible ?

Thanks for your help

vcial
  • 257
  • 1
  • 3
  • 13

1 Answers1

0

Have you tried to use the providers Property in your Modules where you want to use the NSWAG Generated Clients?

Just an Example:

ModuleA
    providers: [
        {
            provide: API_BASE_URL, useValue: 'your-desired-value',
        },
        CustomerQueryClient, // Client from NSWAG Generated File
        CustomerCommandClient // Client from NSWAG Generated File
    ]

ModuleB
    providers: [
        {
            provide: API_BASE_URL, useValue: 'your-desired-value',
        },
        CustomerQueryClient, // Client from NSWAG Generated File
        CustomerCommandClient // Client from NSWAG Generated File
    ]
Crazybutch
  • 685
  • 5
  • 6
  • Thanks for your help. I've tried to configure it in 2 different modules with 2 different values and the first one wich has instantiated my client has won. What I can see in the generated client is that this API_BASE_URL is a constant ... – vcial Jun 04 '20 at 15:25
  • I've done this in 2 different modules: { provide: API_BASE_URL, useValue: 'http://test1.com', }, – vcial Jun 04 '20 at 15:30
  • I've updated my Example. Did you also provide, in each Module, your Clients, as shown above? – Crazybutch Jun 04 '20 at 15:34
  • Yes for each module with different values with "useValue" and not "value" – vcial Jun 04 '20 at 15:42
  • It is absolutely necessary to add the clients to the provider list so that they are re-instantiated for each module. – Crazybutch Jun 04 '20 at 16:19
  • That's perfect ! Thank you – vcial Jun 04 '20 at 21:52