swagger-js icon indicating copy to clipboard operation
swagger-js copied to clipboard

use this client with a swagger spec on local machine?

Open gunjpan opened this issue 10 years ago • 6 comments

I want to use the client as following in my code:

var SwaggerClient = require('swagger-client');

var client = new SwaggerClient({
  url: '../swagger/sampleSwaggerAPI.yaml',
  success: function() {
   ...  
 }
});

Is that doable or does this work only with hosted swagger specs only?

@fehguy ^ thoughts please.

gunjpan avatar Apr 05 '16 15:04 gunjpan

aah, I went thru the code and it seems, it's fetching swagger.json/yaml file by HTTP GET call to a URL. So, this could be a feature request or is there anyway to achieve it?

gunjpan avatar Apr 06 '16 20:04 gunjpan

@fehguy: Hi Tony, I would really appreciate if could you please share your views on the above (new to Swagger world). Thank you much!

gunjpan avatar Apr 12 '16 12:04 gunjpan

hi @gunjpan, can you load the contents of your ../swagger/sampleSwaggerApi.yaml as an object? If so, you can easily do this:

// you load the `spec` as an object
var spec = { swagger: '2.0', ....};
new SwaggerClient({
  url: 'STILL_NEEDED!',
  spec: spec,
  ...
}

Then the object can be loaded however you like, and the swagger client will process it. Note! The url field is still needed. Why might you ask? Because there may be references to other swagger constructs inside your object, and the client will need that for resolving them.

fehguy avatar Apr 13 '16 06:04 fehguy

@fehguy : thank you for your reply. Yeah, I figured it out yesterday that I can pass the Swagger specification object to options.spec and got it working by parsing swagger spec file.

Re:

The url field is still needed.

So, in a case, let's assume, I only have a swagger spec file (i.e. spec obj) and url is not defined, how can this be dealt with? I mean, should the url be set to SwaggerObject.host?! FYI, I stored petStore.json spec file on local, passed it to the spec and was able to make calls successfully!

gunjpan avatar Apr 13 '16 13:04 gunjpan

Hi GunjPan, I am also trying to do the same.Can you please share how you had did the same.I have my product.yaml on my code and want to validate model against the yaml file.

waytoharish avatar Apr 20 '17 05:04 waytoharish

@waytoharish : here is where I'm using SwaggerClient : https://github.com/strongloop/loopback-connector-swagger/blob/a656eedd5490869152ed3e62a708bc4de13bf00d/lib/swagger-connector.js#L107-L119

and here is the function where I'm parsing a local file: https://github.com/strongloop/loopback-connector-swagger/blob/a656eedd5490869152ed3e62a708bc4de13bf00d/lib/spec-resolver.js#L14

Hope that helps

gunjpan avatar Apr 24 '17 16:04 gunjpan