# All files / src/sdk store.js

0% Statements 0/21
0% Branches 0/13
0% Functions 0/5
0% Lines 0/15

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94

     /// <reference path='store.d.ts' />

    import fetch from "@36node/fetch";

    export default class SDK {
      /** @type {string} */
      base;
      /** @type {string} */
      auth;

      /**
       * Init store sdk
       *
       * @param {Object} opt
       * @param {string} opt.base  base url
       * @param {string} opt.token token fro authorization
       */
      constructor(opt = {}) {
        this.base = opt.base || ""; // default use swagger.info.server
        // 如果 openapi.yml 中有 securitySchemes.bearerAuth
        if (opt.token) {
          this.auth = `Bearer ${opt.token}`;
        }
      }

      /**
       * pet's methods
       */

      pet = {
        /**
         * List all pets
         *
         * @param {ListPetsRequest} req ListPets request
         * @returns {Promise<ListPetsResponse>} A paged array of pets
         */
        listPets: req => {
          const { query } = req;

          return fetch(`${this.base}/pets`, {
            query,
            // TODO: operation 中是否有 auth 要求，或者有全局的
            //  security:
            //   - bearerAuth: []
            headers: {
              Authorization: this.auth,
            },
          });
        },

        /**
         * Create a pet
         *
         * @param {CreatePetsRequest} req CreatePets request
         * @returns {Promise<CreatePetsResponse>} The Pet created
         */
        createPets: req => {
          const { body } = req;
          if (!body) throw new Error("requestBody is required for CreatePets");

          return fetch(`${this.base}/pets`, {
            body,
            // TODO: operation 中是否有 auth 要求，或者有全局的
            //  security:
            //   - bearerAuth: []
            headers: {
              Authorization: this.auth,
            },
          });
        },

        /**
         * Find pet by id
         *
         * @param {ShowPetByIdRequest} req ShowPetById request
         * @returns {Promise<ShowPetByIdResponse>} Expected response to a valid request
         */

        showPetById: req => {
          const { petId } = req;
          if (!petId) throw new Error("petId is required for ShowPetById");

          return fetch(`${this.base}/pets/${petId}`, {
            // TODO: operation 中是否有 auth 要求，或者有全局的
            //  security:
            //   - bearerAuth: []
            headers: {
              Authorization: this.auth,
            },
          });
        },
      };
    }