1

I'm using swagger-react-native-client and trying to request data from petstore

I Followed the tutorial and did this

Swagger('https://petstore.swagger.io/v2/swagger.json')
  .then((client) => {
    console.log(client);
    console.log('---client---');

    client.apis.pet
      .findByStatus({status: 'available'})
      // client
      //   .execute({
      //     operationId: 'findByStatus',
      //     parameters: {status: 'available'},
      //   })
      .then((result) => {
        console.log('---result---');
        console.log(result);
      })
      .catch((err) => {
        console.log('---error----');
        console.log(err.message);
        throw err;
      });
  })
  .catch((err) => {
    console.log('---error----');
    console.log(err.message);
    throw err;
  });

and I'm getting the error

 client.apis.pet.findByStatus is not a function. (In 'client.apis.pet.findByStatus({
            status: 'available'
          })', 'client.apis.pet.findByStatus' is undefined)
[Fri Sep 04 2020 12:33:27.700]  WARN     Possible Unhandled Promise Rejection (id: 0):
TypeError: client.apis.pet.findByStatus is not a function. (In 'client.apis.pet.findByStatus({
            status: 'available'
          })', 'client.apis.pet.findByStatus' is undefined)

Since, there is no tutorial available online. I just tried some code and it's not working.

If my code is wrong or if I'm missing something kindly correct me.
How to request data from petstore ?

Bas van der Linden
  • 11,895
  • 4
  • 22
  • 47

1 Answers1

1

TL;DR

I think you are looking for the function findPetsByStatus instead of findByStatus:

Swagger('http://petstore.swagger.io/v2/swagger.json').then((client) => {
  client.apis.pet.findPetsByStatus({status: 'available'}).then((result) => {
    console.log(result);
  });
});

How I found the correct function name

I agree that it's kind of confusing because the swagger api does list a GET request /pet​/findByStatus and the function name doesn't correspond with this.

I found the correct function name by logging the response from Swagger(...), called client in your example, and looking through the properties and values this object has:

Swagger('http://petstore.swagger.io/v2/swagger.json').then((client) => {
  console.log(client) 
});

Explanation about Promises relating to your question

In the documentation we can read that Calling Swagger(url, options) returns a Promise (https://www.npmjs.com/package/swagger-react-native-client#constructor-and-methods).

The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

So this means that in the then clause we have access to the resulting value of the (successful) asynchronous operation that is the Swagger(...) call. You've called this result client in your example.

This result is an object that holds a lot of different data, but importantly for your question; it has a property called apis which has an object as a value. This object contains all the api related functions you can use.

Representation of object returned from Swagger Promise

Here is a highly formatted and slightly adjusted (Adding quotes to [Object] and [Function anonymous] instances) representation of what you get when you log client in your example:

{
  "apis": {
    "pet": {
      "addPet": "[Function anonymous]",
      "deletePet": "[Function anonymous]",
      "findPetsByStatus": "[Function anonymous]",
      "findPetsByTags": "[Function anonymous]",
      "getPetById": "[Function anonymous]",
      "updatePet": "[Function anonymous]",
      "updatePetWithForm": "[Function anonymous]",
      "uploadFile": "[Function anonymous]"
    },
    "store": {
      "deleteOrder": "[Function anonymous]",
      "getInventory": "[Function anonymous]",
      "getOrderById": "[Function anonymous]",
      "placeOrder": "[Function anonymous]"
    },
    "user": {
      "createUser": "[Function anonymous]",
      "createUsersWithArrayInput": "[Function anonymous]",
      "createUsersWithListInput": "[Function anonymous]",
      "deleteUser": "[Function anonymous]",
      "getUserByName": "[Function anonymous]",
      "loginUser": "[Function anonymous]",
      "logoutUser": "[Function anonymous]",
      "updateUser": "[Function anonymous]"
    }
  },
  "errors": [],
  "originalSpec": "undefined",
  "spec": {
    "$$normalized": true,
    "basePath": "/v2",
    "definitions": {
      "ApiResponse": "[Object]",
      "Category": "[Object]",
      "Order": "[Object]",
      "Pet": "[Object]",
      "Tag": "[Object]",
      "User": "[Object]"
    },
    "externalDocs": {
      "description": "Find out more about Swagger",
      "url": "http://swagger.io"
    },
    "host": "petstore.swagger.io",
    "info": {
      "contact": "[Object]",
      "description": "This is a sample server Petstore server.  You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).  For this sample, you can use the api key `special-key` to test the authorization filters.",
      "license": "[Object]",
      "termsOfService": "http://swagger.io/terms/",
      "title": "Swagger Petstore",
      "version": "1.0.5"
    },
    "paths": {
      "/pet": "[Object]",
      "/pet/findByStatus": "[Object]",
      "/pet/findByTags": "[Object]",
      "/pet/{petId}": "[Object]",
      "/pet/{petId}/uploadImage": "[Object]",
      "/store/inventory": "[Object]",
      "/store/order": "[Object]",
      "/store/order/{orderId}": "[Object]",
      "/user": "[Object]",
      "/user/createWithArray": "[Object]",
      "/user/createWithList": "[Object]",
      "/user/login": "[Object]",
      "/user/logout": "[Object]",
      "/user/{username}": "[Object]"
    },
    "schemes": [
      "https",
      "http"
    ],
    "securityDefinitions": {
      "api_key": "[Object]",
      "petstore_auth": "[Object]"
    },
    "swagger": "2.0",
    "tags": [
      "[Object]",
      "[Object]",
      "[Object]"
    ]
  },
  "url": "http://petstore.swagger.io/v2/swagger.json"
}
Bas van der Linden
  • 11,895
  • 4
  • 22
  • 47
  • Can you just show some lines of code on how client object looks and how you find it. Anyhow, I got the answer and I marked it as answer. But, It took some time for me to solve the problem based on your answer. –  Sep 06 '20 at 05:14
  • @rock I've updated my answer and added more explanation. – Bas van der Linden Sep 06 '20 at 08:32
  • I have checked. That's great . –  Sep 06 '20 at 08:36