Nswag: Set BaseUrl for Typescript clients?

Created on 16 Feb 2018  ·  9Comments  ·  Source: RicoSuter/NSwag

NSwagStudio v11.15.3.0. Generating typescript client for C# web api controllers. My GUI is served from one url and the webapi is from another url (on the same machine). Just the path is different. I see in the generated ts file a baseUrl variable. This appears tied to the Studio's ServiceHost setting. I don't see any way to just provide a different path. If I set just the path into studio, '/MyProjectWebApi', it's prepending http: onto the beginning and it doesn't work. If I hand edit baseUrl to '/MyProjectWebApi', it does work.

Is there any way for me to set just an alternate path? but have it keep using whatever the app schema and host name was used?

If the ServiceHost starts with a '/', could it stop adding http? I tried experimenting with a '.', but no luck.

help wanted enhancement

Most helpful comment

I having the same problem. I am trying to use the .as the ServiceHost but it generates the baseUrl as http://:

this.baseUrl = baseUrl ? baseUrl : "http://";

All 9 comments

Which template did you select?

Using the default Fetch template.

I am making the calls from a react app.

You can provide the baseUrl with the first parameter of the constructor:

https://github.com/RSuter/NSwag/blob/master/src/NSwag.CodeGeneration.TypeScript/Templates/FetchClient.liquid#L19

Set server url to “.” without quotes

I'm not going to hardcode a basePath into every call where I new a client. They're all going to be the same. All the client classes are from the same webapi project. Would be nice if there was a way in studio to set it. Or maybe if you generated some global class representing that webapi and then I set the basePath for that webapi config from typescript.

I think you can just use the parameter /serviceHost:. (or serviceHost in the nswag.json) then it removes the service host and you will not have a baseUrl set in the generated clients...

My nswag.json is using the ServiceHost parameter. I am typing into the ServiceHost textbox in nswag studio. The issue is that I want to set a base PATH for the web api, not an entire url. I want the baseUrl to be the same as what the client is launched at, but the webapi is served from a different project. This is a common pattern. One project for the gui and one for the webapi on the same machine.

To me there is: [SCHEME]://[HOST]/[PATH]

What I have found using the service host setting is:
1) If I leave ServiceHost blank, then baseUrl remains blank in the generated code.
2) As soon as I enter anything into ServiceHost (beyond a single period), baseUrl in the generated code now starts with "http://".
3) If I set just a "." into ServiceHost, baseUrl stays empty
4) If I set "/ProjectWebApi" into ServiceHost, baseUrl becomes http:///ProjectWebApi
5) If I set "./ProjectWebApi" into ServiceHost, baseUrl becomes http://./ProjectWebApi

I would like baseUrl to just be "/ProjectWebApi". When I hand edit the generated code to just "/ProjectWebApi", everything works great. I just can't seem to get nswag to generate that way. I would like to keep the generated client api simple with simple constructors. The client objects should be able to take care of themselves, I shouldn't need to be entering everywhere the base PATH when I new the client objects.

If I leave ServiceHost blank, then baseUrl remains blank in the generated code.

@hfirst It actually sets the baseUrl to host value from swagger if you leave ServiceHost empty

I having the same problem. I am trying to use the .as the ServiceHost but it generates the baseUrl as http://:

this.baseUrl = baseUrl ? baseUrl : "http://";
Was this page helpful?
0 / 5 - 0 ratings