OSDN Git Service

update key logic
[bytom/Bytom-JS-SDK.git] / src / http.js
1 import axios from 'axios';
2 import {handleApiError, handleAxiosError} from './utils/http';
3 import { camelize } from "./utils/utils";
4
5 // const basePath = 'api/v1/btm';
6
7 export function serverHttp(host) {
8     this.host = host;
9     this.request = function(path, body, net, method) {
10         var config = {
11             url: this.host[net] ? `${this.host[net]}${path}`: `${this.host}${path}`,
12             method: method ? method : 'POST' ,
13             headers: {
14                 Accept: 'application/json',
15             },
16             data: body,
17             timeout: 25000
18         };
19
20         //return Promise
21         return axios.request(config)
22             .then(function(resp){
23                 if (resp.status !== 200 || resp.data.code !== 200) {
24                     throw handleApiError(resp);
25                 } else if (resp.data.code === 200) {
26                     return camelize(resp.data.data);
27                 }
28                 return resp.data;
29             }).catch(error=>{
30                 throw handleAxiosError(error);
31             });
32     };
33 }
34
35 export function http(baseUrl) {
36     this.baseUrl = baseUrl;
37     this.request = function(path, body, method) {
38         var config = {
39             url: `${this.baseUrl}${path}`,
40             method:  method ? method : 'POST' ,
41             headers: {
42                 Accept: 'application/json',
43             },
44             data: body,
45             timeout: 25000
46         };
47
48         //return Promise
49         return axios.request(config)
50             .then(function(resp){
51                 if (resp.status !== 200 || resp.data.code !== 200) {
52                     throw handleApiError(resp);
53                 } else if (resp.data.code === 200) {
54                     return camelize(resp.data.data);
55                 }
56                 return resp.data;
57             }).catch(error=>{
58                 throw error;
59             });
60     };
61 }