Forum Discussion

nexttech's avatar
nexttech
New Contributor
5 years ago

Swagger JS Client example in detail

Hi all,

I started to use the Swagger JS Client (https://github.com/swagger-api/swagger-js) in my Vue.JS project to connect an OpenAPI 3.0. Anyway, is this a good approach? Or are there better solutions out there?

Now I am struggeling to pass parameters to my backend. What are the minimal lines to make an API call using swagger-js? And how can I define the method?

 

Swagger('https://my-api/auth/login')
  .then(function (swaggerClient) {
    console.log('Swagger then swaggerClient', swaggerClient.apis)
    return swaggerClient.apis.Auth.loginUser({
      username: 'test',
      password: 'test123'
    })
  }, function (reason) {
    console.error('(Swagger Client) Failed to load the spec' + reason)
  })
  .then(function (loginUser) {
    console.log('Swagger then loginUser', loginUser.obj)
    // you may return more promises, if necessary
  }, function (reason) {
    console.error('(Swagger Client) Failed on API call ' + reason)
  })

The first funciton.then(function (swaggerClient) already makes an API call but without sending parameters. How can I send paramters?

I haven't found out why the second one .then(function (loginUser) is in the example. Can I remove this?

Unfortulately I haven't found a good and self-explaning example. So my question here - can someone help out?

Thx so far.

 

1 Reply

  • nexttech's avatar
    nexttech
    New Contributor
    Swagger('https://my-api/auth/login')
      .then((client) => {
        client
          .apis
          .Auth
          .loginUser({
            requestBody: {
              username: 'test',
              password: 'test123'
            }
          })
      })

    Passing the props within requestBody does the job I wanted.

     

    But I am still wondering if this is the perfect way to call an OpenAPI?