@faasjs/react 0.0.2-beta.353 → 0.0.2-beta.357
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/dist/index.d.ts +15 -6
- package/dist/index.js +32 -10
- package/dist/index.mjs +36 -11
- package/package.json +6 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { Options, ResponseError
|
|
1
|
+
import { Response, Options, ResponseError } from '@faasjs/browser';
|
|
2
2
|
export { FaasBrowserClient, Options, Response, ResponseError, ResponseHeaders } from '@faasjs/browser';
|
|
3
|
-
import { FaasAction,
|
|
3
|
+
import { FaasAction, FaasData, FaasParams } from '@faasjs/types';
|
|
4
4
|
|
|
5
5
|
declare type FaasDataInjection<Data = any> = {
|
|
6
|
+
action: string | any;
|
|
7
|
+
params: Record<string, any>;
|
|
6
8
|
loading: boolean;
|
|
7
9
|
data: Data;
|
|
8
10
|
error: any;
|
|
@@ -13,17 +15,21 @@ declare type FaasDataInjection<Data = any> = {
|
|
|
13
15
|
setPromise: React.Dispatch<React.SetStateAction<Promise<Response<Data>>>>;
|
|
14
16
|
setError: React.Dispatch<React.SetStateAction<any>>;
|
|
15
17
|
};
|
|
16
|
-
declare type
|
|
17
|
-
|
|
18
|
+
declare type FaasDataWrapperProps<PathOrData extends FaasAction> = {
|
|
19
|
+
render?(args: FaasDataInjection<FaasData<PathOrData>>): JSX.Element;
|
|
18
20
|
fallback?: JSX.Element | false;
|
|
19
21
|
action: string;
|
|
20
22
|
params?: FaasParams<PathOrData>;
|
|
21
23
|
onDataChange?(args: FaasDataInjection<FaasData<PathOrData>>): void;
|
|
24
|
+
/** use custom data, should work with setData */
|
|
25
|
+
data?: FaasData<PathOrData>;
|
|
26
|
+
/** use custom setData, should work with data */
|
|
27
|
+
setData?: React.Dispatch<React.SetStateAction<FaasData<PathOrData>>>;
|
|
22
28
|
};
|
|
23
29
|
declare type FaasReactClientInstance = {
|
|
24
30
|
faas: <PathOrData extends FaasAction>(action: string | PathOrData, params: FaasParams<PathOrData>) => Promise<Response<FaasData<PathOrData>>>;
|
|
25
31
|
useFaas: <PathOrData extends FaasAction>(action: string | PathOrData, defaultParams: FaasParams<PathOrData>) => FaasDataInjection<FaasData<PathOrData>>;
|
|
26
|
-
|
|
32
|
+
FaasDataWrapper<PathOrData extends FaasAction>(props: FaasDataWrapperProps<PathOrData>): JSX.Element;
|
|
27
33
|
};
|
|
28
34
|
declare function FaasReactClient({ domain, options, onError }: {
|
|
29
35
|
domain: string;
|
|
@@ -31,5 +37,8 @@ declare function FaasReactClient({ domain, options, onError }: {
|
|
|
31
37
|
onError?: (action: string, params: Record<string, any>) => (res: ResponseError) => Promise<void>;
|
|
32
38
|
}): FaasReactClientInstance;
|
|
33
39
|
declare function getClient(domain?: string): FaasReactClientInstance;
|
|
40
|
+
declare function FaasDataWrapper<PathOrData extends FaasAction>(props: FaasDataWrapperProps<PathOrData> & {
|
|
41
|
+
client?: FaasReactClientInstance;
|
|
42
|
+
}): JSX.Element;
|
|
34
43
|
|
|
35
|
-
export { FaasReactClient, getClient };
|
|
44
|
+
export { FaasDataInjection, FaasDataWrapper, FaasDataWrapperProps, FaasReactClient, FaasReactClientInstance, getClient };
|
package/dist/index.js
CHANGED
|
@@ -21,9 +21,10 @@ var __toCommonJS = /* @__PURE__ */ ((cache) => {
|
|
|
21
21
|
};
|
|
22
22
|
})(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
|
|
23
23
|
|
|
24
|
-
// src/index.
|
|
24
|
+
// src/index.tsx
|
|
25
25
|
var src_exports = {};
|
|
26
26
|
__export(src_exports, {
|
|
27
|
+
FaasDataWrapper: () => FaasDataWrapper,
|
|
27
28
|
FaasReactClient: () => FaasReactClient,
|
|
28
29
|
getClient: () => getClient
|
|
29
30
|
});
|
|
@@ -44,7 +45,9 @@ function FaasReactClient({
|
|
|
44
45
|
});
|
|
45
46
|
return client.action(action, params);
|
|
46
47
|
}
|
|
47
|
-
function useFaas(action, defaultParams) {
|
|
48
|
+
function useFaas(action, defaultParams, options2) {
|
|
49
|
+
if (!options2)
|
|
50
|
+
options2 = {};
|
|
48
51
|
const [loading, setLoading] = (0, import_react.useState)(true);
|
|
49
52
|
const [data, setData] = (0, import_react.useState)();
|
|
50
53
|
const [error, setError] = (0, import_react.useState)();
|
|
@@ -60,7 +63,7 @@ function FaasReactClient({
|
|
|
60
63
|
setLoading(true);
|
|
61
64
|
const request = client.action(action, params);
|
|
62
65
|
setPromise(request);
|
|
63
|
-
request.then((r) => setData(r.data)).catch(async (e) => {
|
|
66
|
+
request.then((r) => (options2 == null ? void 0 : options2.setData) ? options2.setData(r.data) : setData(r.data)).catch(async (e) => {
|
|
64
67
|
if (onError)
|
|
65
68
|
try {
|
|
66
69
|
await onError(action, params)(e);
|
|
@@ -78,8 +81,10 @@ function FaasReactClient({
|
|
|
78
81
|
reloadTimes
|
|
79
82
|
]);
|
|
80
83
|
return {
|
|
84
|
+
action,
|
|
85
|
+
params,
|
|
81
86
|
loading,
|
|
82
|
-
data,
|
|
87
|
+
data: (options2 == null ? void 0 : options2.data) || data,
|
|
83
88
|
error,
|
|
84
89
|
promise,
|
|
85
90
|
async reload(params2) {
|
|
@@ -88,7 +93,7 @@ function FaasReactClient({
|
|
|
88
93
|
setReloadTimes(reloadTimes + 1);
|
|
89
94
|
return promise;
|
|
90
95
|
},
|
|
91
|
-
setData,
|
|
96
|
+
setData: (options2 == null ? void 0 : options2.setData) || setData,
|
|
92
97
|
setLoading,
|
|
93
98
|
setPromise,
|
|
94
99
|
setError
|
|
@@ -97,14 +102,19 @@ function FaasReactClient({
|
|
|
97
102
|
const reactClient = {
|
|
98
103
|
faas,
|
|
99
104
|
useFaas,
|
|
100
|
-
|
|
105
|
+
FaasDataWrapper({
|
|
101
106
|
action,
|
|
102
107
|
params,
|
|
103
108
|
fallback,
|
|
104
|
-
|
|
105
|
-
onDataChange
|
|
109
|
+
render,
|
|
110
|
+
onDataChange,
|
|
111
|
+
data,
|
|
112
|
+
setData
|
|
106
113
|
}) {
|
|
107
|
-
const request = useFaas(action, params
|
|
114
|
+
const request = useFaas(action, params, {
|
|
115
|
+
data,
|
|
116
|
+
setData
|
|
117
|
+
});
|
|
108
118
|
const [loaded, setLoaded] = (0, import_react.useState)(false);
|
|
109
119
|
(0, import_react.useEffect)(function() {
|
|
110
120
|
if (!loaded && !request.loading)
|
|
@@ -115,7 +125,7 @@ function FaasReactClient({
|
|
|
115
125
|
onDataChange(request);
|
|
116
126
|
}, [JSON.stringify(request.data)]);
|
|
117
127
|
if (loaded)
|
|
118
|
-
return
|
|
128
|
+
return render(request);
|
|
119
129
|
return fallback || null;
|
|
120
130
|
}
|
|
121
131
|
};
|
|
@@ -128,9 +138,21 @@ function getClient(domain) {
|
|
|
128
138
|
throw Error("FaasReactClient is not initialized");
|
|
129
139
|
return client;
|
|
130
140
|
}
|
|
141
|
+
function FaasDataWrapper(props) {
|
|
142
|
+
const [client, setClient] = (0, import_react.useState)(props.client);
|
|
143
|
+
(0, import_react.useEffect)(() => {
|
|
144
|
+
if (client)
|
|
145
|
+
return;
|
|
146
|
+
setClient(getClient());
|
|
147
|
+
}, []);
|
|
148
|
+
if (!client)
|
|
149
|
+
return props.fallback || null;
|
|
150
|
+
return (0, import_react.createElement)(client.FaasDataWrapper, props);
|
|
151
|
+
}
|
|
131
152
|
module.exports = __toCommonJS(src_exports);
|
|
132
153
|
// Annotate the CommonJS export names for ESM import in node:
|
|
133
154
|
0 && (module.exports = {
|
|
155
|
+
FaasDataWrapper,
|
|
134
156
|
FaasReactClient,
|
|
135
157
|
getClient
|
|
136
158
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
// src/index.
|
|
1
|
+
// src/index.tsx
|
|
2
2
|
import {
|
|
3
3
|
FaasBrowserClient
|
|
4
4
|
} from "@faasjs/browser";
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
useState,
|
|
7
|
+
useEffect,
|
|
8
|
+
createElement
|
|
9
|
+
} from "react";
|
|
6
10
|
var clients = {};
|
|
7
11
|
function FaasReactClient({
|
|
8
12
|
domain,
|
|
@@ -18,7 +22,9 @@ function FaasReactClient({
|
|
|
18
22
|
});
|
|
19
23
|
return client.action(action, params);
|
|
20
24
|
}
|
|
21
|
-
function useFaas(action, defaultParams) {
|
|
25
|
+
function useFaas(action, defaultParams, options2) {
|
|
26
|
+
if (!options2)
|
|
27
|
+
options2 = {};
|
|
22
28
|
const [loading, setLoading] = useState(true);
|
|
23
29
|
const [data, setData] = useState();
|
|
24
30
|
const [error, setError] = useState();
|
|
@@ -34,7 +40,7 @@ function FaasReactClient({
|
|
|
34
40
|
setLoading(true);
|
|
35
41
|
const request = client.action(action, params);
|
|
36
42
|
setPromise(request);
|
|
37
|
-
request.then((r) => setData(r.data)).catch(async (e) => {
|
|
43
|
+
request.then((r) => (options2 == null ? void 0 : options2.setData) ? options2.setData(r.data) : setData(r.data)).catch(async (e) => {
|
|
38
44
|
if (onError)
|
|
39
45
|
try {
|
|
40
46
|
await onError(action, params)(e);
|
|
@@ -52,8 +58,10 @@ function FaasReactClient({
|
|
|
52
58
|
reloadTimes
|
|
53
59
|
]);
|
|
54
60
|
return {
|
|
61
|
+
action,
|
|
62
|
+
params,
|
|
55
63
|
loading,
|
|
56
|
-
data,
|
|
64
|
+
data: (options2 == null ? void 0 : options2.data) || data,
|
|
57
65
|
error,
|
|
58
66
|
promise,
|
|
59
67
|
async reload(params2) {
|
|
@@ -62,7 +70,7 @@ function FaasReactClient({
|
|
|
62
70
|
setReloadTimes(reloadTimes + 1);
|
|
63
71
|
return promise;
|
|
64
72
|
},
|
|
65
|
-
setData,
|
|
73
|
+
setData: (options2 == null ? void 0 : options2.setData) || setData,
|
|
66
74
|
setLoading,
|
|
67
75
|
setPromise,
|
|
68
76
|
setError
|
|
@@ -71,14 +79,19 @@ function FaasReactClient({
|
|
|
71
79
|
const reactClient = {
|
|
72
80
|
faas,
|
|
73
81
|
useFaas,
|
|
74
|
-
|
|
82
|
+
FaasDataWrapper({
|
|
75
83
|
action,
|
|
76
84
|
params,
|
|
77
85
|
fallback,
|
|
78
|
-
|
|
79
|
-
onDataChange
|
|
86
|
+
render,
|
|
87
|
+
onDataChange,
|
|
88
|
+
data,
|
|
89
|
+
setData
|
|
80
90
|
}) {
|
|
81
|
-
const request = useFaas(action, params
|
|
91
|
+
const request = useFaas(action, params, {
|
|
92
|
+
data,
|
|
93
|
+
setData
|
|
94
|
+
});
|
|
82
95
|
const [loaded, setLoaded] = useState(false);
|
|
83
96
|
useEffect(function() {
|
|
84
97
|
if (!loaded && !request.loading)
|
|
@@ -89,7 +102,7 @@ function FaasReactClient({
|
|
|
89
102
|
onDataChange(request);
|
|
90
103
|
}, [JSON.stringify(request.data)]);
|
|
91
104
|
if (loaded)
|
|
92
|
-
return
|
|
105
|
+
return render(request);
|
|
93
106
|
return fallback || null;
|
|
94
107
|
}
|
|
95
108
|
};
|
|
@@ -102,7 +115,19 @@ function getClient(domain) {
|
|
|
102
115
|
throw Error("FaasReactClient is not initialized");
|
|
103
116
|
return client;
|
|
104
117
|
}
|
|
118
|
+
function FaasDataWrapper(props) {
|
|
119
|
+
const [client, setClient] = useState(props.client);
|
|
120
|
+
useEffect(() => {
|
|
121
|
+
if (client)
|
|
122
|
+
return;
|
|
123
|
+
setClient(getClient());
|
|
124
|
+
}, []);
|
|
125
|
+
if (!client)
|
|
126
|
+
return props.fallback || null;
|
|
127
|
+
return createElement(client.FaasDataWrapper, props);
|
|
128
|
+
}
|
|
105
129
|
export {
|
|
130
|
+
FaasDataWrapper,
|
|
106
131
|
FaasReactClient,
|
|
107
132
|
getClient
|
|
108
133
|
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/react",
|
|
3
|
-
"version": "0.0.2-beta.
|
|
3
|
+
"version": "0.0.2-beta.357",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
6
7
|
"types": "dist/index.d.ts",
|
|
7
8
|
"homepage": "https://faasjs.com/doc/react.html",
|
|
8
9
|
"repository": {
|
|
@@ -15,15 +16,15 @@
|
|
|
15
16
|
},
|
|
16
17
|
"funding": "https://github.com/sponsors/faasjs",
|
|
17
18
|
"scripts": {
|
|
18
|
-
"build": "tsup src/index.
|
|
19
|
-
"build:types": "tsup-node src/index.
|
|
19
|
+
"build": "tsup src/index.tsx --format esm,cjs",
|
|
20
|
+
"build:types": "tsup-node src/index.tsx --dts-only"
|
|
20
21
|
},
|
|
21
22
|
"files": [
|
|
22
23
|
"dist"
|
|
23
24
|
],
|
|
24
25
|
"peerDependencies": {
|
|
25
|
-
"@faasjs/browser": "^0.0.2-beta.
|
|
26
|
-
"@faasjs/types": "^0.0.2-beta.
|
|
26
|
+
"@faasjs/browser": "^0.0.2-beta.357",
|
|
27
|
+
"@faasjs/types": "^0.0.2-beta.357",
|
|
27
28
|
"react": "*"
|
|
28
29
|
},
|
|
29
30
|
"devDependencies": {
|