@faasjs/react 0.0.2-beta.293 → 0.0.2-beta.294
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/browser/src/index.d.ts +7 -4
- package/lib/index.js +20 -57
- package/package.json +3 -3
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
export declare type Params = {
|
|
2
2
|
[key: string]: any;
|
|
3
3
|
};
|
|
4
|
-
export declare type Options = {
|
|
5
|
-
|
|
4
|
+
export declare type Options = RequestInit & {
|
|
5
|
+
headers?: {
|
|
6
|
+
[key: string]: string;
|
|
7
|
+
};
|
|
8
|
+
beforeRequest?: ({ action, params, options }: {
|
|
6
9
|
action: string;
|
|
7
10
|
params: Params;
|
|
8
|
-
|
|
11
|
+
options: Options;
|
|
9
12
|
}) => Promise<void> | void;
|
|
10
13
|
};
|
|
11
14
|
export declare type ResponseHeaders = {
|
|
@@ -47,6 +50,6 @@ export declare class FaasBrowserClient {
|
|
|
47
50
|
* @param params {any} 动作参数
|
|
48
51
|
* @param options {object} 默认配置项
|
|
49
52
|
*/
|
|
50
|
-
action<T = any>(action: string, params
|
|
53
|
+
action<T = any>(action: string, params?: Params, options?: Options): Promise<Response<T>>;
|
|
51
54
|
}
|
|
52
55
|
export default FaasBrowserClient;
|
package/lib/index.js
CHANGED
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
constructor(baseUrl, options) {
|
|
33
33
|
this.host = baseUrl[baseUrl.length - 1] === '/' ? baseUrl : baseUrl + '/';
|
|
34
34
|
this.defaultOptions = options || Object.create(null);
|
|
35
|
-
console.debug('[
|
|
35
|
+
console.debug('[Faas] baseUrl: ' + this.host);
|
|
36
36
|
}
|
|
37
37
|
/**
|
|
38
38
|
* 发起请求
|
|
@@ -40,71 +40,34 @@
|
|
|
40
40
|
* @param params {any} 动作参数
|
|
41
41
|
* @param options {object} 默认配置项
|
|
42
42
|
*/
|
|
43
|
-
async action(action, params, options = {}) {
|
|
43
|
+
async action(action, params = {}, options = {}) {
|
|
44
44
|
const url = this.host + action.toLowerCase() + '?_=' + new Date().getTime().toString();
|
|
45
|
-
options = Object.assign(Object.assign({}, this.defaultOptions), options);
|
|
46
|
-
const xhr = new XMLHttpRequest();
|
|
47
|
-
xhr.open('POST', url);
|
|
48
|
-
xhr.withCredentials = true;
|
|
49
|
-
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
|
45
|
+
options = Object.assign(Object.assign({ method: 'POST', headers: { 'Content-Type': 'application/json; charset=UTF-8' }, mode: 'cors', body: JSON.stringify(params) }, this.defaultOptions), options);
|
|
50
46
|
if (options.beforeRequest)
|
|
51
47
|
await options.beforeRequest({
|
|
52
48
|
action,
|
|
53
49
|
params,
|
|
54
|
-
|
|
50
|
+
options
|
|
55
51
|
});
|
|
56
|
-
return
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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,
|
|
52
|
+
return fetch(url, options)
|
|
53
|
+
.then(async (response) => {
|
|
54
|
+
const headers = {};
|
|
55
|
+
response.headers.forEach((value, key) => headers[key] = value);
|
|
56
|
+
return response.json().then(res => {
|
|
57
|
+
if (res.error && res.error.message)
|
|
58
|
+
return Promise.reject(new ResponseError({
|
|
59
|
+
message: res.error.message,
|
|
60
|
+
status: response.status,
|
|
86
61
|
headers,
|
|
87
|
-
|
|
62
|
+
body: response
|
|
88
63
|
}));
|
|
89
|
-
else
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
message: xhr.statusText || xhr.status.toString(),
|
|
93
|
-
status: xhr.status,
|
|
64
|
+
else
|
|
65
|
+
return new Response({
|
|
66
|
+
status: response.status,
|
|
94
67
|
headers,
|
|
95
|
-
|
|
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));
|
|
68
|
+
data: res.data
|
|
69
|
+
});
|
|
70
|
+
});
|
|
108
71
|
});
|
|
109
72
|
}
|
|
110
73
|
}
|
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.294",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/react/src/index.d.ts",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"lib"
|
|
18
18
|
],
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"@faasjs/browser": "
|
|
20
|
+
"@faasjs/browser": "^0.0.2-beta.280",
|
|
21
21
|
"react": "*"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
@@ -27,5 +27,5 @@
|
|
|
27
27
|
"rollup-plugin-typescript2": "*",
|
|
28
28
|
"typescript": "*"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "da80db41bf31736e49762c06c7c2ffe0f1496c72"
|
|
31
31
|
}
|