@faasjs/react 0.0.2-beta.293 → 0.0.2-beta.297
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 +26 -130
- package/lib/react/src/index.d.ts +6 -12
- package/package.json +3 -3
- package/lib/browser/src/index.d.ts +0 -52
package/lib/index.js
CHANGED
|
@@ -1,157 +1,53 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
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
|
-
})(this, (function (exports,
|
|
6
|
-
|
|
7
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
-
|
|
9
|
-
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
10
|
-
|
|
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
|
-
const xhr = new XMLHttpRequest();
|
|
47
|
-
xhr.open('POST', url);
|
|
48
|
-
xhr.withCredentials = true;
|
|
49
|
-
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
|
50
|
-
if (options.beforeRequest)
|
|
51
|
-
await options.beforeRequest({
|
|
52
|
-
action,
|
|
53
|
-
params,
|
|
54
|
-
xhr
|
|
55
|
-
});
|
|
56
|
-
return new Promise(function (resolve, reject) {
|
|
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
|
-
}
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@faasjs/browser'), require('react')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', '@faasjs/browser', 'react'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.FaasReactClient = {}, global.browser, global.React));
|
|
5
|
+
})(this, (function (exports, browser, react) { 'use strict';
|
|
111
6
|
|
|
112
7
|
function FaasReactClient({ domain, options, onError }) {
|
|
113
|
-
const client = new FaasBrowserClient(domain, options);
|
|
8
|
+
const client = new browser.FaasBrowserClient(domain, options);
|
|
114
9
|
// async function faas<Path extends keyof FaasActions> (action: Path, params: FaasActions[Path]['request']): Promise<Response<FaasActions[Path]['response']>>
|
|
115
10
|
async function faas(action, params) {
|
|
116
11
|
if (onError)
|
|
117
12
|
return client.action(action, params)
|
|
118
|
-
.catch(
|
|
13
|
+
.catch(async (res) => {
|
|
14
|
+
await onError(action, params)(res);
|
|
15
|
+
return Promise.reject(res);
|
|
16
|
+
});
|
|
119
17
|
return client.action(action, params);
|
|
120
18
|
}
|
|
121
19
|
function useFaas(action, defaultParams) {
|
|
122
|
-
const [loading, setLoading] =
|
|
123
|
-
const [data, setData] =
|
|
124
|
-
const [error, setError] =
|
|
125
|
-
const [promise, setPromise] =
|
|
126
|
-
const [params, setParams] =
|
|
127
|
-
const [reloadTimes, setReloadTimes] =
|
|
128
|
-
|
|
20
|
+
const [loading, setLoading] = react.useState(true);
|
|
21
|
+
const [data, setData] = react.useState();
|
|
22
|
+
const [error, setError] = react.useState();
|
|
23
|
+
const [promise, setPromise] = react.useState();
|
|
24
|
+
const [params, setParams] = react.useState(defaultParams);
|
|
25
|
+
const [reloadTimes, setReloadTimes] = react.useState(0);
|
|
26
|
+
react.useEffect(function () {
|
|
129
27
|
if (JSON.stringify(defaultParams) !== JSON.stringify(params)) {
|
|
130
28
|
setParams(defaultParams);
|
|
131
29
|
}
|
|
132
30
|
}, [JSON.stringify(defaultParams)]);
|
|
133
|
-
|
|
31
|
+
react.useEffect(function () {
|
|
134
32
|
setLoading(true);
|
|
135
33
|
const request = client.action(action, params);
|
|
136
34
|
setPromise(request);
|
|
137
35
|
request
|
|
138
|
-
.then(r =>
|
|
139
|
-
setData(r === null || r === void 0 ? void 0 : r.data);
|
|
140
|
-
})
|
|
36
|
+
.then(r => setData(r.data))
|
|
141
37
|
.catch(async (e) => {
|
|
142
38
|
if (onError)
|
|
143
39
|
try {
|
|
144
|
-
|
|
40
|
+
await onError(action, params)(e);
|
|
145
41
|
}
|
|
146
42
|
catch (error) {
|
|
147
43
|
setError(error);
|
|
148
44
|
}
|
|
149
|
-
|
|
45
|
+
else
|
|
46
|
+
setError(e);
|
|
47
|
+
return Promise.reject(e);
|
|
150
48
|
})
|
|
151
49
|
.finally(() => setLoading(false));
|
|
152
|
-
return () =>
|
|
153
|
-
setLoading(false);
|
|
154
|
-
};
|
|
50
|
+
return () => setLoading(false);
|
|
155
51
|
}, [
|
|
156
52
|
action,
|
|
157
53
|
JSON.stringify(params),
|
|
@@ -179,12 +75,12 @@
|
|
|
179
75
|
useFaas,
|
|
180
76
|
FaasData({ action, params, fallback, element, onDataChange }) {
|
|
181
77
|
const request = useFaas(action, params);
|
|
182
|
-
const [loaded, setLoaded] =
|
|
183
|
-
|
|
78
|
+
const [loaded, setLoaded] = react.useState(false);
|
|
79
|
+
react.useEffect(function () {
|
|
184
80
|
if (!loaded && !request.loading)
|
|
185
81
|
setLoaded(true);
|
|
186
82
|
}, [request.loading]);
|
|
187
|
-
|
|
83
|
+
react.useEffect(() => {
|
|
188
84
|
if (onDataChange)
|
|
189
85
|
onDataChange(request);
|
|
190
86
|
}, [JSON.stringify(request.data)]);
|
package/lib/react/src/index.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import { Options, Params, Response, ResponseError } from '
|
|
3
|
-
export type { FaasBrowserClient, Options, Params, Response, ResponseHeaders, ResponseError } from '
|
|
2
|
+
import { Options, Params, Response, ResponseError } from '@faasjs/browser';
|
|
3
|
+
export type { FaasBrowserClient, Options, Params, Response, ResponseHeaders, ResponseError } from '@faasjs/browser';
|
|
4
4
|
declare type FaasDataInjection<T = any> = {
|
|
5
5
|
loading: boolean;
|
|
6
6
|
data: T;
|
|
7
7
|
error: any;
|
|
8
8
|
promise: Promise<Response<T>>;
|
|
9
9
|
reload(params?: Params): Promise<Response<T>>;
|
|
10
|
-
setData
|
|
11
|
-
setLoading
|
|
12
|
-
setPromise
|
|
13
|
-
setError
|
|
10
|
+
setData: React.Dispatch<React.SetStateAction<T>>;
|
|
11
|
+
setLoading: React.Dispatch<React.SetStateAction<boolean>>;
|
|
12
|
+
setPromise: React.Dispatch<React.SetStateAction<Promise<Response<T>>>>;
|
|
13
|
+
setError: React.Dispatch<React.SetStateAction<any>>;
|
|
14
14
|
};
|
|
15
15
|
declare type FaasDataProps<T = any> = {
|
|
16
16
|
element(args: FaasDataInjection<T>): JSX.Element;
|
|
@@ -19,12 +19,6 @@ declare type FaasDataProps<T = any> = {
|
|
|
19
19
|
params?: Params;
|
|
20
20
|
onDataChange?(args: FaasDataInjection<T>): void;
|
|
21
21
|
};
|
|
22
|
-
export interface FaasActions {
|
|
23
|
-
[path: string]: {
|
|
24
|
-
request: any;
|
|
25
|
-
response: any;
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
22
|
export declare function FaasReactClient({ domain, options, onError }: {
|
|
29
23
|
domain: string;
|
|
30
24
|
options?: Options;
|
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.297",
|
|
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": "894043c85eb3b67682be19a0b473e30f3550b061"
|
|
31
31
|
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
export declare type Params = {
|
|
2
|
-
[key: string]: any;
|
|
3
|
-
};
|
|
4
|
-
export declare type Options = {
|
|
5
|
-
beforeRequest?: ({ action, params, xhr }: {
|
|
6
|
-
action: string;
|
|
7
|
-
params: Params;
|
|
8
|
-
xhr: XMLHttpRequest;
|
|
9
|
-
}) => Promise<void> | void;
|
|
10
|
-
};
|
|
11
|
-
export declare type ResponseHeaders = {
|
|
12
|
-
[key: string]: string;
|
|
13
|
-
};
|
|
14
|
-
export declare class Response<T = any> {
|
|
15
|
-
readonly status: number;
|
|
16
|
-
readonly headers: ResponseHeaders;
|
|
17
|
-
readonly data: T;
|
|
18
|
-
constructor({ status, headers, data }: {
|
|
19
|
-
status: number;
|
|
20
|
-
headers: ResponseHeaders;
|
|
21
|
-
data: T;
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
export declare class ResponseError extends Error {
|
|
25
|
-
readonly status: number;
|
|
26
|
-
readonly headers: ResponseHeaders;
|
|
27
|
-
readonly body: any;
|
|
28
|
-
constructor({ message, status, headers, body }: {
|
|
29
|
-
message: string;
|
|
30
|
-
status: number;
|
|
31
|
-
headers: ResponseHeaders;
|
|
32
|
-
body: any;
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
export declare class FaasBrowserClient {
|
|
36
|
-
host: string;
|
|
37
|
-
defaultOptions: Options;
|
|
38
|
-
/**
|
|
39
|
-
* 创建 FaasJS 浏览器客户端
|
|
40
|
-
* @param baseUrl {string} 网关地址
|
|
41
|
-
* @param options {object} 默认配置项
|
|
42
|
-
*/
|
|
43
|
-
constructor(baseUrl: string, options?: Options);
|
|
44
|
-
/**
|
|
45
|
-
* 发起请求
|
|
46
|
-
* @param action {string} 动作名称
|
|
47
|
-
* @param params {any} 动作参数
|
|
48
|
-
* @param options {object} 默认配置项
|
|
49
|
-
*/
|
|
50
|
-
action<T = any>(action: string, params: Params, options?: Options): Promise<Response<T>>;
|
|
51
|
-
}
|
|
52
|
-
export default FaasBrowserClient;
|