github.com/minio/console@v1.4.1/web-app/src/common/api/index.ts (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  import request from "superagent";
    18  import get from "lodash/get";
    19  import { clearSession } from "../utils";
    20  import { ErrorResponseHandler } from "../types";
    21  import { baseUrl } from "../../history";
    22  
    23  type RequestHeaders = { [name: string]: string };
    24  
    25  export class API {
    26    invoke(method: string, url: string, data?: object, headers?: RequestHeaders) {
    27      let targetURL = url;
    28      if (targetURL[0] === "/") {
    29        targetURL = targetURL.slice(1);
    30      }
    31      let req = request(method, targetURL);
    32  
    33      if (headers) {
    34        for (let k in headers) {
    35          req.set(k, headers[k]);
    36        }
    37      }
    38  
    39      return req
    40        .send(data)
    41        .then((res) => res.body)
    42        .catch((err) => {
    43          // if we get unauthorized and we are not doing login, kick out the user
    44          if (
    45            err.status === 401 &&
    46            localStorage.getItem("userLoggedIn") &&
    47            !targetURL.includes("api/v1/login")
    48          ) {
    49            if (window.location.pathname !== "/") {
    50              localStorage.setItem("redirect-path", window.location.pathname);
    51            }
    52            clearSession();
    53            // Refresh the whole page to ensure cache is clear
    54            // and we dont end on an infinite loop
    55            window.location.href = `${baseUrl}login`;
    56            return;
    57          }
    58  
    59          return this.onError(err);
    60        });
    61    }
    62  
    63    onError(err: any) {
    64      if (err.status) {
    65        const errMessage = get(
    66          err.response,
    67          "body.message",
    68          `Error ${err.status.toString()}`,
    69        );
    70  
    71        let detailedMessage = get(err.response, "body.detailedMessage", "");
    72  
    73        if (errMessage === detailedMessage) {
    74          detailedMessage = "";
    75        }
    76  
    77        const capMessage =
    78          errMessage.charAt(0).toUpperCase() + errMessage.slice(1);
    79        const capDetailed =
    80          detailedMessage.charAt(0).toUpperCase() + detailedMessage.slice(1);
    81  
    82        const throwMessage: ErrorResponseHandler = {
    83          errorMessage: capMessage,
    84          detailedError: capDetailed,
    85          statusCode: err.status,
    86        };
    87  
    88        return Promise.reject(throwMessage);
    89      } else {
    90        clearSession();
    91        window.location.href = `${baseUrl}login`;
    92      }
    93    }
    94  }
    95  
    96  const api = new API();
    97  export default api;