Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set BaseUrl for Typescript clients? #1198

Open
hfirst opened this issue Feb 16, 2018 · 10 comments
Open

Set BaseUrl for Typescript clients? #1198

hfirst opened this issue Feb 16, 2018 · 10 comments

Comments

@hfirst
Copy link

hfirst commented Feb 16, 2018

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.

@RicoSuter
Copy link
Owner

Which template did you select?

@hfirst
Copy link
Author

hfirst commented Feb 16, 2018

Using the default Fetch template.

I am making the calls from a react app.

@RicoSuter
Copy link
Owner

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

@hfirst
Copy link
Author

hfirst commented Feb 16, 2018

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.

@RicoSuter
Copy link
Owner

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...

@hfirst
Copy link
Author

hfirst commented Feb 20, 2018

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.

@arutkowski00
Copy link

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

@Silthus
Copy link

Silthus commented Jun 2, 2018

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://";

@artyompetrov
Copy link

Hello! Is there any update with this issue?

It seems it still generates the same:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants