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

Can swagger-client support local files #1410

Closed
dodtsair opened this issue Dec 8, 2018 · 5 comments
Closed

Can swagger-client support local files #1410

dodtsair opened this issue Dec 8, 2018 · 5 comments

Comments

@dodtsair
Copy link

dodtsair commented Dec 8, 2018

Can swagger-client import API Docs via a file instead of an http url.

Something like:

const swaggerClient = require('swagger-client');

const devZone = swaggerClient('file://api-doc.json');

We want to use swagger-client to exercise our rest apis where the rest apis are sitting in the repo next to the code that uses swagger-client. In particular we have some integration test cases that we use to test our rest apis.

@bestmike007
Copy link

Swagger client uses fetch (or cross-fetch/polyfill) as the default client to load doc from url; it does not support file url (data url neither).

However, you can use a temporary server in your scenario:

https://gist.github.com/bestmike007/a63ea5b21a24fdd34508164a85dc1e4b

@shockey
Copy link
Contributor

shockey commented Jan 4, 2019

Quoting myself from #1044:

I'm going to close this one out, here's why:

file:// URLs are not really useful, since they're very difficult to use in browsers due to Same-Origin Policy and very few Node request libraries support them.

As for loading in a definition that you already have in memory: you can pass your definition as a JS object under the spec key.

Feel free to reply if you disagree with any of this, and I'll consider reopening. Thanks everyone!

As mentioned, the best approach is to pull your local file in manually, and pass it as an object convert it to a JavaScript object, and pass it to Swagger Client using the spec option.

Closing!

@shockey shockey closed this as completed Jan 4, 2019
@mm-gmbd
Copy link

mm-gmbd commented May 1, 2019

@shockey -- you said:

pull your local file in manually and pass it as an object to Swagger Client

To me, this is a different suggestion than what @bestmike007 suggestion (which is fine), and I think you mean basically load the Swagger locally as an object, and then pass that to the constructor:

//testing locally, so I've got the Swagger spec on my machine

const Swagger = require('swagger-client');
const fs = require('fs');
const yaml = require('js-yaml');

var url = path.join(__dirname, "./local_path/api.yaml");
var spec = yaml.safeLoad(fs.readFileSync(url), 'utf8');

Swagger({ spec: spec })
  .then(function(client){
    console.log("Got client!");
  })
  .catch(function(ex){
    console.log("Error!");
    console.log(ex): //We get the exception "TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received type undefined"
  })

I'm certain the spec var is an object containing the Swagger spec, so is the way that you suggested actually supported? (or did I misunderstand your suggestion?)

@shockey
Copy link
Contributor

shockey commented May 2, 2019

@mm-gmbd, I worded it poorly! you'd actually want to do it like Swagger({ spec: spec }).

I'm going to edit your comment so Googlers get the right snippet, but for any historians out there, the line was originally Swagger(spec).

@mm-gmbd
Copy link

mm-gmbd commented May 2, 2019

@shockey - thanks for the clarification! Also, thanks for your work on this particular module, it's very helpful! I'd love a little more documentation / examples here and there, but this beggar won't try and be a chooser 😄 (best case scenario - maybe I'll actually get some time to contribute!)

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

No branches or pull requests

4 participants