summaryrefslogtreecommitdiffstats
path: root/usecaseui-portal/src/app/shared/utils/http.ts
blob: c0fc3f2900c43d9f83d0ae829bdb8a8a5c89110f (plain)
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
import axios from 'axios';


export default function http (url:string,data:object = {},method:any = 'get',query?:string | object):any {
  return new Promise((resolve,reject) => {
    method = method.trim().toLocaleLowerCase()
    let promise:any;
    if(method === 'get' || method === 'delete'){
      let options:object;
      if(JSON.stringify(data) === '{}'){
        options = { method, url };
      }else{
        options = { method, url, params: data};
      }

      if(method === 'delete'){

      }

      promise = axios(options);

    }else if (method === 'post' || method === 'put') {
      if(method === 'post' && query){
        let params:string = '';
        if(<string>query){
          query = JSON.parse((<string>query));
        }
        Object.keys(query).forEach(item => {
          params += '&' + item + '=' + query[item]; 
        })
        params = params.slice(1);
        url += '?' + params;
      }
      promise = axios({
        method,
        url,
        data,
      })
    }
    promise
      .then((response) => {
        if(response.status === 200 || 304){
          resolve(response.data)
        }else{
          reject(response)
        }
      })
      .catch((error) => {
        reject(error.message)
      })

  })
}