@faasjs/react 0.0.2-beta.294 → 0.0.2-beta.299
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 -93
- package/lib/react/src/index.d.ts +7 -13
- package/package.json +2 -2
- package/lib/browser/src/index.d.ts +0 -55
package/lib/index.js
CHANGED
|
@@ -1,120 +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({ method: 'POST', headers: { 'Content-Type': 'application/json; charset=UTF-8' }, mode: 'cors', body: JSON.stringify(params) }, this.defaultOptions), options);
|
|
46
|
-
if (options.beforeRequest)
|
|
47
|
-
await options.beforeRequest({
|
|
48
|
-
action,
|
|
49
|
-
params,
|
|
50
|
-
options
|
|
51
|
-
});
|
|
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,
|
|
61
|
-
headers,
|
|
62
|
-
body: response
|
|
63
|
-
}));
|
|
64
|
-
else
|
|
65
|
-
return new Response({
|
|
66
|
-
status: response.status,
|
|
67
|
-
headers,
|
|
68
|
-
data: res.data
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
}
|
|
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';
|
|
74
6
|
|
|
75
7
|
function FaasReactClient({ domain, options, onError }) {
|
|
76
|
-
const client = new FaasBrowserClient(domain, options);
|
|
8
|
+
const client = new browser.FaasBrowserClient(domain, options);
|
|
77
9
|
// async function faas<Path extends keyof FaasActions> (action: Path, params: FaasActions[Path]['request']): Promise<Response<FaasActions[Path]['response']>>
|
|
78
10
|
async function faas(action, params) {
|
|
79
11
|
if (onError)
|
|
80
12
|
return client.action(action, params)
|
|
81
|
-
.catch(
|
|
13
|
+
.catch(async (res) => {
|
|
14
|
+
await onError(action, params)(res);
|
|
15
|
+
return Promise.reject(res);
|
|
16
|
+
});
|
|
82
17
|
return client.action(action, params);
|
|
83
18
|
}
|
|
84
19
|
function useFaas(action, defaultParams) {
|
|
85
|
-
const [loading, setLoading] =
|
|
86
|
-
const [data, setData] =
|
|
87
|
-
const [error, setError] =
|
|
88
|
-
const [promise, setPromise] =
|
|
89
|
-
const [params, setParams] =
|
|
90
|
-
const [reloadTimes, setReloadTimes] =
|
|
91
|
-
|
|
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 () {
|
|
92
27
|
if (JSON.stringify(defaultParams) !== JSON.stringify(params)) {
|
|
93
28
|
setParams(defaultParams);
|
|
94
29
|
}
|
|
95
30
|
}, [JSON.stringify(defaultParams)]);
|
|
96
|
-
|
|
31
|
+
react.useEffect(function () {
|
|
97
32
|
setLoading(true);
|
|
98
33
|
const request = client.action(action, params);
|
|
99
34
|
setPromise(request);
|
|
100
35
|
request
|
|
101
|
-
.then(r =>
|
|
102
|
-
setData(r === null || r === void 0 ? void 0 : r.data);
|
|
103
|
-
})
|
|
36
|
+
.then(r => setData(r.data))
|
|
104
37
|
.catch(async (e) => {
|
|
105
38
|
if (onError)
|
|
106
39
|
try {
|
|
107
|
-
|
|
40
|
+
await onError(action, params)(e);
|
|
108
41
|
}
|
|
109
42
|
catch (error) {
|
|
110
43
|
setError(error);
|
|
111
44
|
}
|
|
112
|
-
|
|
45
|
+
else
|
|
46
|
+
setError(e);
|
|
47
|
+
return Promise.reject(e);
|
|
113
48
|
})
|
|
114
49
|
.finally(() => setLoading(false));
|
|
115
|
-
return () =>
|
|
116
|
-
setLoading(false);
|
|
117
|
-
};
|
|
50
|
+
return () => setLoading(false);
|
|
118
51
|
}, [
|
|
119
52
|
action,
|
|
120
53
|
JSON.stringify(params),
|
|
@@ -142,12 +75,12 @@
|
|
|
142
75
|
useFaas,
|
|
143
76
|
FaasData({ action, params, fallback, element, onDataChange }) {
|
|
144
77
|
const request = useFaas(action, params);
|
|
145
|
-
const [loaded, setLoaded] =
|
|
146
|
-
|
|
78
|
+
const [loaded, setLoaded] = react.useState(false);
|
|
79
|
+
react.useEffect(function () {
|
|
147
80
|
if (!loaded && !request.loading)
|
|
148
81
|
setLoaded(true);
|
|
149
82
|
}, [request.loading]);
|
|
150
|
-
|
|
83
|
+
react.useEffect(() => {
|
|
151
84
|
if (onDataChange)
|
|
152
85
|
onDataChange(request);
|
|
153
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,16 +19,10 @@ 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;
|
|
31
|
-
onError?: (action: string, params: Params) => (res: ResponseError) => Promise<
|
|
25
|
+
onError?: (action: string, params: Params) => (res: ResponseError) => Promise<void>;
|
|
32
26
|
}): {
|
|
33
27
|
faas: <T = any>(action: string, params: Params) => Promise<Response<T>>;
|
|
34
28
|
useFaas: <T_1 = any>(action: string, defaultParams: Params) => FaasDataInjection<T_1>;
|
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.299",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/react/src/index.d.ts",
|
|
@@ -27,5 +27,5 @@
|
|
|
27
27
|
"rollup-plugin-typescript2": "*",
|
|
28
28
|
"typescript": "*"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "e59f67b45534eb87764e6419fbfbbb0d24e3271d"
|
|
31
31
|
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
export declare type Params = {
|
|
2
|
-
[key: string]: any;
|
|
3
|
-
};
|
|
4
|
-
export declare type Options = RequestInit & {
|
|
5
|
-
headers?: {
|
|
6
|
-
[key: string]: string;
|
|
7
|
-
};
|
|
8
|
-
beforeRequest?: ({ action, params, options }: {
|
|
9
|
-
action: string;
|
|
10
|
-
params: Params;
|
|
11
|
-
options: Options;
|
|
12
|
-
}) => Promise<void> | void;
|
|
13
|
-
};
|
|
14
|
-
export declare type ResponseHeaders = {
|
|
15
|
-
[key: string]: string;
|
|
16
|
-
};
|
|
17
|
-
export declare class Response<T = any> {
|
|
18
|
-
readonly status: number;
|
|
19
|
-
readonly headers: ResponseHeaders;
|
|
20
|
-
readonly data: T;
|
|
21
|
-
constructor({ status, headers, data }: {
|
|
22
|
-
status: number;
|
|
23
|
-
headers: ResponseHeaders;
|
|
24
|
-
data: T;
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
export declare class ResponseError extends Error {
|
|
28
|
-
readonly status: number;
|
|
29
|
-
readonly headers: ResponseHeaders;
|
|
30
|
-
readonly body: any;
|
|
31
|
-
constructor({ message, status, headers, body }: {
|
|
32
|
-
message: string;
|
|
33
|
-
status: number;
|
|
34
|
-
headers: ResponseHeaders;
|
|
35
|
-
body: any;
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
export declare class FaasBrowserClient {
|
|
39
|
-
host: string;
|
|
40
|
-
defaultOptions: Options;
|
|
41
|
-
/**
|
|
42
|
-
* 创建 FaasJS 浏览器客户端
|
|
43
|
-
* @param baseUrl {string} 网关地址
|
|
44
|
-
* @param options {object} 默认配置项
|
|
45
|
-
*/
|
|
46
|
-
constructor(baseUrl: string, options?: Options);
|
|
47
|
-
/**
|
|
48
|
-
* 发起请求
|
|
49
|
-
* @param action {string} 动作名称
|
|
50
|
-
* @param params {any} 动作参数
|
|
51
|
-
* @param options {object} 默认配置项
|
|
52
|
-
*/
|
|
53
|
-
action<T = any>(action: string, params?: Params, options?: Options): Promise<Response<T>>;
|
|
54
|
-
}
|
|
55
|
-
export default FaasBrowserClient;
|