@faasjs/react 0.0.2-beta.266 → 0.0.2-beta.275
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +169 -303
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -1,313 +1,179 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.FaasReactClient = {}, global.React));
|
|
5
5
|
})(this, (function (exports, React) { 'use strict';
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
10
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
|
-
|
|
11
|
+
class Response {
|
|
12
|
+
constructor({ status, headers, data }) {
|
|
13
|
+
this.status = status;
|
|
14
|
+
this.headers = headers;
|
|
15
|
+
this.data = data;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
class ResponseError extends Error {
|
|
19
|
+
constructor({ message, status, headers, body }) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.status = status;
|
|
22
|
+
this.headers = headers;
|
|
23
|
+
this.body = body;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
class FaasBrowserClient {
|
|
27
|
+
/**
|
|
28
|
+
* 创建 FaasJS 浏览器客户端
|
|
29
|
+
* @param baseUrl {string} 网关地址
|
|
30
|
+
* @param options {object} 默认配置项
|
|
31
|
+
*/
|
|
32
|
+
constructor(baseUrl, options) {
|
|
33
|
+
this.host = baseUrl[baseUrl.length - 1] === '/' ? baseUrl : baseUrl + '/';
|
|
34
|
+
this.defaultOptions = options || Object.create(null);
|
|
35
|
+
console.debug('[faas] baseUrl: ' + this.host);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 发起请求
|
|
39
|
+
* @param action {string} 动作名称
|
|
40
|
+
* @param params {any} 动作参数
|
|
41
|
+
* @param options {object} 默认配置项
|
|
42
|
+
*/
|
|
43
|
+
async action(action, params, options = {}) {
|
|
44
|
+
const url = this.host + action.toLowerCase() + '?_=' + new Date().getTime().toString();
|
|
45
|
+
options = Object.assign(Object.assign({}, this.defaultOptions), options);
|
|
46
|
+
return new Promise(function (resolve, reject) {
|
|
47
|
+
const xhr = new XMLHttpRequest();
|
|
48
|
+
xhr.open('POST', url);
|
|
49
|
+
xhr.withCredentials = true;
|
|
50
|
+
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
|
51
|
+
if (options.beforeRequest)
|
|
52
|
+
options.beforeRequest({
|
|
53
|
+
action,
|
|
54
|
+
params,
|
|
55
|
+
xhr
|
|
56
|
+
});
|
|
57
|
+
xhr.onload = function () {
|
|
58
|
+
var _a;
|
|
59
|
+
let res = xhr.response;
|
|
60
|
+
const headersList = xhr.getAllResponseHeaders().trim().split(/[\r\n]+/);
|
|
61
|
+
const headers = {};
|
|
62
|
+
headersList.forEach(function (line) {
|
|
63
|
+
const parts = line.split(': ');
|
|
64
|
+
const key = parts.shift();
|
|
65
|
+
const value = parts.join(': ');
|
|
66
|
+
if (key)
|
|
67
|
+
headers[key] = value;
|
|
68
|
+
});
|
|
69
|
+
if (xhr.response && ((_a = xhr.getResponseHeader('Content-Type')) === null || _a === void 0 ? void 0 : _a.includes('json')))
|
|
70
|
+
try {
|
|
71
|
+
res = JSON.parse(xhr.response);
|
|
72
|
+
if (res.error && res.error.message)
|
|
73
|
+
reject(new ResponseError({
|
|
74
|
+
message: res.error.message,
|
|
75
|
+
status: xhr.status,
|
|
76
|
+
headers,
|
|
77
|
+
body: res
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
console.error(error);
|
|
82
|
+
}
|
|
83
|
+
if (xhr.status >= 200 && xhr.status < 300)
|
|
84
|
+
resolve(new Response({
|
|
85
|
+
status: xhr.status,
|
|
86
|
+
headers,
|
|
87
|
+
data: res.data
|
|
88
|
+
}));
|
|
89
|
+
else {
|
|
90
|
+
console.error(xhr, res);
|
|
91
|
+
reject(new ResponseError({
|
|
92
|
+
message: xhr.statusText || xhr.status.toString(),
|
|
93
|
+
status: xhr.status,
|
|
94
|
+
headers,
|
|
95
|
+
body: res
|
|
96
|
+
}));
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
xhr.onerror = function () {
|
|
100
|
+
reject(new ResponseError({
|
|
101
|
+
message: 'Network Error',
|
|
102
|
+
status: xhr.status,
|
|
103
|
+
headers: {},
|
|
104
|
+
body: null
|
|
105
|
+
}));
|
|
106
|
+
};
|
|
107
|
+
xhr.send(JSON.stringify(params));
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
107
111
|
|
|
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
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
var parts = line.split(': ');
|
|
171
|
-
var key = parts.shift();
|
|
172
|
-
var value = parts.join(': ');
|
|
173
|
-
if (key)
|
|
174
|
-
headers[key] = value;
|
|
175
|
-
});
|
|
176
|
-
if (xhr.response && ((_a = xhr.getResponseHeader('Content-Type')) === null || _a === void 0 ? void 0 : _a.includes('json')))
|
|
177
|
-
try {
|
|
178
|
-
res = JSON.parse(xhr.response);
|
|
179
|
-
if (res.error && res.error.message)
|
|
180
|
-
reject(new ResponseError({
|
|
181
|
-
message: res.error.message,
|
|
182
|
-
status: xhr.status,
|
|
183
|
-
headers: headers,
|
|
184
|
-
body: res
|
|
185
|
-
}));
|
|
186
|
-
}
|
|
187
|
-
catch (error) {
|
|
188
|
-
console.error(error);
|
|
189
|
-
}
|
|
190
|
-
if (xhr.status >= 200 && xhr.status < 300)
|
|
191
|
-
resolve(new Response({
|
|
192
|
-
status: xhr.status,
|
|
193
|
-
headers: headers,
|
|
194
|
-
data: res.data
|
|
195
|
-
}));
|
|
196
|
-
else {
|
|
197
|
-
console.error(xhr, res);
|
|
198
|
-
reject(new ResponseError({
|
|
199
|
-
message: xhr.statusText || xhr.status.toString(),
|
|
200
|
-
status: xhr.status,
|
|
201
|
-
headers: headers,
|
|
202
|
-
body: res
|
|
203
|
-
}));
|
|
204
|
-
}
|
|
205
|
-
};
|
|
206
|
-
xhr.onerror = function () {
|
|
207
|
-
reject(new ResponseError({
|
|
208
|
-
message: 'Network Error',
|
|
209
|
-
status: xhr.status,
|
|
210
|
-
headers: {},
|
|
211
|
-
body: null
|
|
212
|
-
}));
|
|
213
|
-
};
|
|
214
|
-
xhr.send(JSON.stringify(params));
|
|
215
|
-
})];
|
|
216
|
-
});
|
|
217
|
-
});
|
|
218
|
-
};
|
|
219
|
-
return FaasBrowserClient;
|
|
220
|
-
}());
|
|
112
|
+
function FaasReactClient({ domain, options, onError }) {
|
|
113
|
+
const client = new FaasBrowserClient(domain, options);
|
|
114
|
+
return {
|
|
115
|
+
async faas(action, params) {
|
|
116
|
+
if (onError)
|
|
117
|
+
return client.action(action, params).catch(onError(action, params));
|
|
118
|
+
return client.action(action, params);
|
|
119
|
+
},
|
|
120
|
+
useFaas(action, defaultParams) {
|
|
121
|
+
const [loading, setLoading] = React__default["default"].useState(false);
|
|
122
|
+
const [data, setData] = React__default["default"].useState();
|
|
123
|
+
const [error, setError] = React__default["default"].useState();
|
|
124
|
+
const [promise, setPromise] = React__default["default"].useState();
|
|
125
|
+
const [params, setParams] = React__default["default"].useState(defaultParams);
|
|
126
|
+
const [reloadTimes, setReloadTimes] = React__default["default"].useState(0);
|
|
127
|
+
React__default["default"].useEffect(function () {
|
|
128
|
+
if (JSON.stringify(defaultParams) !== JSON.stringify(params)) {
|
|
129
|
+
setParams(defaultParams);
|
|
130
|
+
}
|
|
131
|
+
}, [defaultParams]);
|
|
132
|
+
React__default["default"].useEffect(function () {
|
|
133
|
+
setLoading(true);
|
|
134
|
+
const request = client.action(action, params);
|
|
135
|
+
setPromise(request);
|
|
136
|
+
request
|
|
137
|
+
.then(r => {
|
|
138
|
+
setData(r === null || r === void 0 ? void 0 : r.data);
|
|
139
|
+
})
|
|
140
|
+
.catch(async (e) => {
|
|
141
|
+
if (onError)
|
|
142
|
+
try {
|
|
143
|
+
setData(await onError(action, params)(e));
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
setError(error);
|
|
147
|
+
}
|
|
148
|
+
setError(e);
|
|
149
|
+
})
|
|
150
|
+
.finally(() => setLoading(false));
|
|
151
|
+
return () => {
|
|
152
|
+
setLoading(false);
|
|
153
|
+
};
|
|
154
|
+
}, [
|
|
155
|
+
action,
|
|
156
|
+
JSON.stringify(params),
|
|
157
|
+
reloadTimes
|
|
158
|
+
]);
|
|
159
|
+
return {
|
|
160
|
+
loading,
|
|
161
|
+
data,
|
|
162
|
+
error,
|
|
163
|
+
promise,
|
|
164
|
+
async reload(params) {
|
|
165
|
+
if (params)
|
|
166
|
+
setParams(params);
|
|
167
|
+
setReloadTimes(reloadTimes + 1);
|
|
168
|
+
return promise;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
221
174
|
|
|
222
|
-
|
|
223
|
-
var domain = _a.domain, options = _a.options, onError = _a.onError;
|
|
224
|
-
var client = new FaasBrowserClient(domain, options);
|
|
225
|
-
return {
|
|
226
|
-
faas: function (action, params) {
|
|
227
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
228
|
-
return __generator(this, function (_a) {
|
|
229
|
-
if (onError)
|
|
230
|
-
return [2 /*return*/, client.action(action, params).catch(onError(action, params))];
|
|
231
|
-
return [2 /*return*/, client.action(action, params)];
|
|
232
|
-
});
|
|
233
|
-
});
|
|
234
|
-
},
|
|
235
|
-
useFaas: function (action, defaultParams) {
|
|
236
|
-
var _a = __read(React__default["default"].useState(false), 2), loading = _a[0], setLoading = _a[1];
|
|
237
|
-
var _b = __read(React__default["default"].useState(), 2), data = _b[0], setData = _b[1];
|
|
238
|
-
var _c = __read(React__default["default"].useState(), 2), error = _c[0], setError = _c[1];
|
|
239
|
-
var _d = __read(React__default["default"].useState(), 2), promise = _d[0], setPromise = _d[1];
|
|
240
|
-
var _e = __read(React__default["default"].useState(defaultParams), 2), params = _e[0], setParams = _e[1];
|
|
241
|
-
var _f = __read(React__default["default"].useState(0), 2), reloadTimes = _f[0], setReloadTimes = _f[1];
|
|
242
|
-
React__default["default"].useEffect(function () {
|
|
243
|
-
if (JSON.stringify(defaultParams) !== JSON.stringify(params)) {
|
|
244
|
-
setParams(defaultParams);
|
|
245
|
-
}
|
|
246
|
-
}, [defaultParams]);
|
|
247
|
-
React__default["default"].useEffect(function () {
|
|
248
|
-
var _this = this;
|
|
249
|
-
setLoading(true);
|
|
250
|
-
var request = client.action(action, params);
|
|
251
|
-
setPromise(request);
|
|
252
|
-
request
|
|
253
|
-
.then(function (r) {
|
|
254
|
-
setData(r === null || r === void 0 ? void 0 : r.data);
|
|
255
|
-
})
|
|
256
|
-
.catch(function (e) { return __awaiter(_this, void 0, void 0, function () {
|
|
257
|
-
var _a, error_1;
|
|
258
|
-
return __generator(this, function (_b) {
|
|
259
|
-
switch (_b.label) {
|
|
260
|
-
case 0:
|
|
261
|
-
if (!onError) return [3 /*break*/, 4];
|
|
262
|
-
_b.label = 1;
|
|
263
|
-
case 1:
|
|
264
|
-
_b.trys.push([1, 3, , 4]);
|
|
265
|
-
_a = setData;
|
|
266
|
-
return [4 /*yield*/, onError(action, params)(e)];
|
|
267
|
-
case 2:
|
|
268
|
-
_a.apply(void 0, [_b.sent()]);
|
|
269
|
-
return [3 /*break*/, 4];
|
|
270
|
-
case 3:
|
|
271
|
-
error_1 = _b.sent();
|
|
272
|
-
setError(error_1);
|
|
273
|
-
return [3 /*break*/, 4];
|
|
274
|
-
case 4:
|
|
275
|
-
setError(e);
|
|
276
|
-
return [2 /*return*/];
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
}); })
|
|
280
|
-
.finally(function () { return setLoading(false); });
|
|
281
|
-
return function () {
|
|
282
|
-
setLoading(false);
|
|
283
|
-
};
|
|
284
|
-
}, [
|
|
285
|
-
action,
|
|
286
|
-
JSON.stringify(params),
|
|
287
|
-
reloadTimes
|
|
288
|
-
]);
|
|
289
|
-
return {
|
|
290
|
-
loading: loading,
|
|
291
|
-
data: data,
|
|
292
|
-
error: error,
|
|
293
|
-
promise: promise,
|
|
294
|
-
reload: function (params) {
|
|
295
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
296
|
-
return __generator(this, function (_a) {
|
|
297
|
-
if (params)
|
|
298
|
-
setParams(params);
|
|
299
|
-
setReloadTimes(reloadTimes + 1);
|
|
300
|
-
return [2 /*return*/, promise];
|
|
301
|
-
});
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
};
|
|
307
|
-
}
|
|
175
|
+
exports.FaasReactClient = FaasReactClient;
|
|
308
176
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
177
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
312
178
|
|
|
313
179
|
}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/react",
|
|
3
|
-
"version": "0.0.2-beta.
|
|
3
|
+
"version": "0.0.2-beta.275",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/react/src/index.d.ts",
|
|
@@ -26,5 +26,5 @@
|
|
|
26
26
|
"rollup-plugin-typescript2": "*",
|
|
27
27
|
"typescript": "*"
|
|
28
28
|
},
|
|
29
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "25f9712888cd6f4a984517ed853e3404a0a5c062"
|
|
30
30
|
}
|