storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/browser/app/js/web.js (about)

     1  /*
     2   * MinIO Cloud Storage (C) 2016, 2018 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import JSONrpc from './jsonrpc'
    18  import { minioBrowserPrefix } from './constants.js'
    19  import Moment from 'moment'
    20  import storage from 'local-storage-fallback'
    21  
    22  class Web {
    23    constructor(endpoint) {
    24      const namespace = 'web'
    25      this.JSONrpc = new JSONrpc({
    26        endpoint,
    27        namespace
    28      })
    29    }
    30    makeCall(method, options) {
    31      return this.JSONrpc.call(method, {
    32        params: options
    33      }, storage.getItem('token'))
    34        .catch(err => {
    35          if (err.status === 401) {
    36            storage.removeItem('token')
    37            location.reload()
    38            throw new Error('Please re-login.')
    39          }
    40          if (err.status)
    41            throw new Error(`Server returned error [${err.status}]`)
    42          throw new Error('MinIO server is unreachable')
    43        })
    44        .then(res => {
    45          let json = JSON.parse(res.text)
    46          let result = json.result
    47          let error = json.error
    48          if (error) {
    49            throw new Error(error.message)
    50          }
    51          if (!Moment(result.uiVersion).isValid()) {
    52            throw new Error("Invalid UI version in the JSON-RPC response")
    53          }
    54          if (result.uiVersion !== currentUiVersion
    55            && currentUiVersion !== 'MINIO_UI_VERSION') {
    56            storage.setItem('newlyUpdated', true)
    57            location.reload()
    58          }
    59          return result
    60        })
    61    }
    62    LoggedIn() {
    63      return !!storage.getItem('token')
    64    }
    65    Login(args) {
    66      return this.makeCall('Login', args)
    67        .then(res => {
    68          storage.setItem('token', `${res.token}`)
    69          return res
    70        })
    71    }
    72    Logout() {
    73      storage.removeItem('token')
    74    }
    75    GetToken() {
    76      return storage.getItem('token')
    77    }
    78    GetDiscoveryDoc() {
    79      return this.makeCall("GetDiscoveryDoc")
    80    }
    81    LoginSTS(args) {
    82      return this.makeCall('LoginSTS', args)
    83        .then(res => {
    84          storage.setItem('token', `${res.token}`)
    85          return res
    86        })
    87    }
    88    ServerInfo() {
    89      return this.makeCall('ServerInfo')
    90    }
    91    StorageInfo() {
    92      return this.makeCall('StorageInfo')
    93    }
    94    ListBuckets() {
    95      return this.makeCall('ListBuckets')
    96    }
    97    MakeBucket(args) {
    98      return this.makeCall('MakeBucket', args)
    99    }
   100    DeleteBucket(args) {
   101      return this.makeCall('DeleteBucket', args)
   102    }
   103    ListObjects(args) {
   104      return this.makeCall('ListObjects', args)
   105    }
   106    PresignedGet(args) {
   107      return this.makeCall('PresignedGet', args)
   108    }
   109    PutObjectURL(args) {
   110      return this.makeCall('PutObjectURL', args)
   111    }
   112    RemoveObject(args) {
   113      return this.makeCall('RemoveObject', args)
   114    }
   115    SetAuth(args) {
   116      return this.makeCall('SetAuth', args)
   117        .then(res => {
   118          storage.setItem('token', `${res.token}`)
   119          return res
   120        })
   121    }
   122    CreateURLToken() {
   123      return this.makeCall('CreateURLToken')
   124    }
   125    GetBucketPolicy(args) {
   126      return this.makeCall('GetBucketPolicy', args)
   127    }
   128    SetBucketPolicy(args) {
   129      return this.makeCall('SetBucketPolicy', args)
   130    }
   131    ListAllBucketPolicies(args) {
   132      return this.makeCall('ListAllBucketPolicies', args)
   133    }
   134  }
   135  
   136  const web = new Web(`${window.location.protocol}//${window.location.host}${minioBrowserPrefix}/webrpc`);
   137  
   138  export default web;