-
Notifications
You must be signed in to change notification settings - Fork 52
/
createHttp.js
executable file
·157 lines (137 loc) · 3.66 KB
/
createHttp.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// type Header
const fetch = require('node-fetch');
const _ = require('lodash');
const Middleware = require('./middleware');
const RequestMethods = {
GET: 'GET',
POST: 'POST',
DEL: 'DELETE',
};
class HttpBase {
// middleware = null;
// afterMiddleware = null;
// baseConfig = {};
constructor(baseConfig) {
this.baseConfig = baseConfig;
this.middleware = new Middleware();
this.afterMiddleware = new Middleware();
}
useBefore(middleFn) {
this.middleware.use(middleFn);
}
useAfter(middleFn) {
this.afterMiddleware.use(middleFn);
}
/**
* resolve fetch data
*
* @param {[type]} method: [method: RequestMethods Type]
* @param {[type]} data: [data: Data]
*
* @return {RequestData} [return description]
*/
resolveData(method, data = {}) {
const d = {};
_.each(data || {}, (value, key) => {
if (typeof value !== 'undefined') {
d[key] = value;
}
});
if(method === RequestMethods.POST) {
return {
query: '',
body: JSON.stringify(d),
}
} else {
return {
query: Object.keys(d).map(v => `${v}=${d[v]}`).join('&'),
body: undefined,
}
}
}
/**
* Send request
*
* @param {[type]} method: [method: description]
* @param {[type]} RequestMethods [RequestMethods description]
* @param {string} url [url description]
* @param {[type]} data: [data?: description]
*
* @return {Promise<any>} [return description]
*/
async makeRequest(method, url, data, headers) {
const { body, query } = this.resolveData(method, data || {});
const { baseUrl } = this.baseConfig;
const [uri, _query = ''] = url.split('?');
const ctx = {
url,
query: _query + (_query ? '&': '') + query ,
baseUrl,
body,
method,
headers: headers || '',
};
// run middleware
await this.middleware.run(ctx);
// console.log(ctx.query, ctx.body);
// const authHeaders: CustomObject<string> = auth(method, url + query, body || '');
const result = await fetch(ctx.baseUrl + uri + '?' + ctx.query, {
body: ctx.body,
method,
headers: ctx.headers,
}).then(res => {
if(res.json && typeof res.json === 'function'){
return res.json();
}else {
return res;
}
})
.then(async res => {
await this.afterMiddleware.run(res);
return res;
});
return result;
}
}
/**
* create Http Instance
*
* @return {[type]} [return description]
*/
module.exports = function createHttp(baseConfig) {
const middlewares = [];
const afterMiddlewares = [];
class Http {
// hook to push middleware
static useBefore(middle) {
middlewares.push(middle);
}
// hook to push middleware
static useAfter(middle) {
afterMiddlewares.push(middle);
}
static registerMiddle(httpIns) {
middlewares.forEach((md) => {
httpIns.useBefore(md);
});
afterMiddlewares.forEach((md) => {
httpIns.useAfter(md);
});
}
static createRequest(method, url, data, headers) {
const http = new HttpBase(baseConfig);
Http.registerMiddle(http);
return http.makeRequest(method, url, data, headers);
}
static POST(url, data, headers) {
return Http.createRequest(RequestMethods.POST, url, data, headers);
}
static GET(url, data, headers) {
return Http.createRequest(RequestMethods.GET, url, data, headers);
}
static DEL(url, data) {
return Http.createRequest(RequestMethods.DEL, url, data);
}
}
return Http;
}