@faasjs/react 0.0.2-beta.310 → 0.0.2-beta.320
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/react/src → dist}/index.d.ts +33 -31
- package/dist/index.js +125 -0
- package/dist/index.mjs +98 -0
- package/package.json +10 -10
- package/lib/index.js +0 -97
|
@@ -1,31 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { FaasAction,
|
|
4
|
-
|
|
5
|
-
declare type FaasDataInjection<Data = any> = {
|
|
6
|
-
loading: boolean;
|
|
7
|
-
data: Data;
|
|
8
|
-
error: any;
|
|
9
|
-
promise: Promise<Response<Data>>;
|
|
10
|
-
reload(params?: Record<string, any>): Promise<Response<Data>>;
|
|
11
|
-
setData: React.Dispatch<React.SetStateAction<Data>>;
|
|
12
|
-
setLoading: React.Dispatch<React.SetStateAction<boolean>>;
|
|
13
|
-
setPromise: React.Dispatch<React.SetStateAction<Promise<Response<Data>>>>;
|
|
14
|
-
setError: React.Dispatch<React.SetStateAction<any>>;
|
|
15
|
-
};
|
|
16
|
-
declare type FaasDataProps<PathOrData extends FaasAction> = {
|
|
17
|
-
element(args: FaasDataInjection<FaasData<PathOrData>>): JSX.Element;
|
|
18
|
-
fallback?: JSX.Element | false;
|
|
19
|
-
action: string;
|
|
20
|
-
params?: FaasParams<PathOrData>;
|
|
21
|
-
onDataChange?(args: FaasDataInjection<FaasData<PathOrData>>): void;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
domain: string;
|
|
25
|
-
options?: Options;
|
|
26
|
-
onError?: (action: string, params: Record<string, any>) => (res: ResponseError) => Promise<void>;
|
|
27
|
-
}): {
|
|
28
|
-
faas: <PathOrData extends FaasAction>(action: string | PathOrData, params: FaasParams<PathOrData>) => Promise<Response<FaasData<PathOrData>>>;
|
|
29
|
-
useFaas: <PathOrData extends FaasAction>(action: string | PathOrData, defaultParams: FaasParams<PathOrData>) => FaasDataInjection<FaasData<PathOrData>>;
|
|
30
|
-
FaasData<PathOrData extends FaasAction>(props: FaasDataProps<PathOrData>): JSX.Element;
|
|
31
|
-
};
|
|
1
|
+
import { Options, ResponseError, Response } from '@faasjs/browser';
|
|
2
|
+
export { FaasBrowserClient, Options, Response, ResponseError, ResponseHeaders } from '@faasjs/browser';
|
|
3
|
+
import { FaasAction, FaasParams, FaasData } from '@faasjs/types';
|
|
4
|
+
|
|
5
|
+
declare type FaasDataInjection<Data = any> = {
|
|
6
|
+
loading: boolean;
|
|
7
|
+
data: Data;
|
|
8
|
+
error: any;
|
|
9
|
+
promise: Promise<Response<Data>>;
|
|
10
|
+
reload(params?: Record<string, any>): Promise<Response<Data>>;
|
|
11
|
+
setData: React.Dispatch<React.SetStateAction<Data>>;
|
|
12
|
+
setLoading: React.Dispatch<React.SetStateAction<boolean>>;
|
|
13
|
+
setPromise: React.Dispatch<React.SetStateAction<Promise<Response<Data>>>>;
|
|
14
|
+
setError: React.Dispatch<React.SetStateAction<any>>;
|
|
15
|
+
};
|
|
16
|
+
declare type FaasDataProps<PathOrData extends FaasAction> = {
|
|
17
|
+
element(args: FaasDataInjection<FaasData<PathOrData>>): JSX.Element;
|
|
18
|
+
fallback?: JSX.Element | false;
|
|
19
|
+
action: string;
|
|
20
|
+
params?: FaasParams<PathOrData>;
|
|
21
|
+
onDataChange?(args: FaasDataInjection<FaasData<PathOrData>>): void;
|
|
22
|
+
};
|
|
23
|
+
declare function FaasReactClient({ domain, options, onError }: {
|
|
24
|
+
domain: string;
|
|
25
|
+
options?: Options;
|
|
26
|
+
onError?: (action: string, params: Record<string, any>) => (res: ResponseError) => Promise<void>;
|
|
27
|
+
}): {
|
|
28
|
+
faas: <PathOrData extends FaasAction>(action: string | PathOrData, params: FaasParams<PathOrData>) => Promise<Response<FaasData<PathOrData>>>;
|
|
29
|
+
useFaas: <PathOrData extends FaasAction>(action: string | PathOrData, defaultParams: FaasParams<PathOrData>) => FaasDataInjection<FaasData<PathOrData>>;
|
|
30
|
+
FaasData<PathOrData extends FaasAction>(props: FaasDataProps<PathOrData>): JSX.Element;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export { FaasReactClient };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __reExport = (target, module2, copyDefault, desc) => {
|
|
11
|
+
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(module2))
|
|
13
|
+
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
|
|
14
|
+
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return target;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = /* @__PURE__ */ ((cache) => {
|
|
19
|
+
return (module2, temp) => {
|
|
20
|
+
return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
|
|
21
|
+
};
|
|
22
|
+
})(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
|
|
23
|
+
|
|
24
|
+
// src/index.ts
|
|
25
|
+
var src_exports = {};
|
|
26
|
+
__export(src_exports, {
|
|
27
|
+
FaasReactClient: () => FaasReactClient
|
|
28
|
+
});
|
|
29
|
+
var import_browser = require("@faasjs/browser");
|
|
30
|
+
var import_react = require("react");
|
|
31
|
+
function FaasReactClient({
|
|
32
|
+
domain,
|
|
33
|
+
options,
|
|
34
|
+
onError
|
|
35
|
+
}) {
|
|
36
|
+
const client = new import_browser.FaasBrowserClient(domain, options);
|
|
37
|
+
async function faas(action, params) {
|
|
38
|
+
if (onError)
|
|
39
|
+
return client.action(action, params).catch(async (res) => {
|
|
40
|
+
await onError(action, params)(res);
|
|
41
|
+
return Promise.reject(res);
|
|
42
|
+
});
|
|
43
|
+
return client.action(action, params);
|
|
44
|
+
}
|
|
45
|
+
function useFaas(action, defaultParams) {
|
|
46
|
+
const [loading, setLoading] = (0, import_react.useState)(true);
|
|
47
|
+
const [data, setData] = (0, import_react.useState)();
|
|
48
|
+
const [error, setError] = (0, import_react.useState)();
|
|
49
|
+
const [promise, setPromise] = (0, import_react.useState)();
|
|
50
|
+
const [params, setParams] = (0, import_react.useState)(defaultParams);
|
|
51
|
+
const [reloadTimes, setReloadTimes] = (0, import_react.useState)(0);
|
|
52
|
+
(0, import_react.useEffect)(function() {
|
|
53
|
+
if (JSON.stringify(defaultParams) !== JSON.stringify(params)) {
|
|
54
|
+
setParams(defaultParams);
|
|
55
|
+
}
|
|
56
|
+
}, [JSON.stringify(defaultParams)]);
|
|
57
|
+
(0, import_react.useEffect)(function() {
|
|
58
|
+
setLoading(true);
|
|
59
|
+
const request = client.action(action, params);
|
|
60
|
+
setPromise(request);
|
|
61
|
+
request.then((r) => setData(r.data)).catch(async (e) => {
|
|
62
|
+
if (onError)
|
|
63
|
+
try {
|
|
64
|
+
await onError(action, params)(e);
|
|
65
|
+
} catch (error2) {
|
|
66
|
+
setError(error2);
|
|
67
|
+
}
|
|
68
|
+
else
|
|
69
|
+
setError(e);
|
|
70
|
+
return Promise.reject(e);
|
|
71
|
+
}).finally(() => setLoading(false));
|
|
72
|
+
return () => setLoading(false);
|
|
73
|
+
}, [
|
|
74
|
+
action,
|
|
75
|
+
JSON.stringify(params),
|
|
76
|
+
reloadTimes
|
|
77
|
+
]);
|
|
78
|
+
return {
|
|
79
|
+
loading,
|
|
80
|
+
data,
|
|
81
|
+
error,
|
|
82
|
+
promise,
|
|
83
|
+
async reload(params2) {
|
|
84
|
+
if (params2)
|
|
85
|
+
setParams(params2);
|
|
86
|
+
setReloadTimes(reloadTimes + 1);
|
|
87
|
+
return promise;
|
|
88
|
+
},
|
|
89
|
+
setData,
|
|
90
|
+
setLoading,
|
|
91
|
+
setPromise,
|
|
92
|
+
setError
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
faas,
|
|
97
|
+
useFaas,
|
|
98
|
+
FaasData({
|
|
99
|
+
action,
|
|
100
|
+
params,
|
|
101
|
+
fallback,
|
|
102
|
+
element,
|
|
103
|
+
onDataChange
|
|
104
|
+
}) {
|
|
105
|
+
const request = useFaas(action, params);
|
|
106
|
+
const [loaded, setLoaded] = (0, import_react.useState)(false);
|
|
107
|
+
(0, import_react.useEffect)(function() {
|
|
108
|
+
if (!loaded && !request.loading)
|
|
109
|
+
setLoaded(true);
|
|
110
|
+
}, [request.loading]);
|
|
111
|
+
(0, import_react.useEffect)(function() {
|
|
112
|
+
if (onDataChange)
|
|
113
|
+
onDataChange(request);
|
|
114
|
+
}, [JSON.stringify(request.data)]);
|
|
115
|
+
if (loaded)
|
|
116
|
+
return element(request);
|
|
117
|
+
return fallback || null;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
module.exports = __toCommonJS(src_exports);
|
|
122
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
123
|
+
0 && (module.exports = {
|
|
124
|
+
FaasReactClient
|
|
125
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import {
|
|
3
|
+
FaasBrowserClient
|
|
4
|
+
} from "@faasjs/browser";
|
|
5
|
+
import { useState, useEffect } from "react";
|
|
6
|
+
function FaasReactClient({
|
|
7
|
+
domain,
|
|
8
|
+
options,
|
|
9
|
+
onError
|
|
10
|
+
}) {
|
|
11
|
+
const client = new FaasBrowserClient(domain, options);
|
|
12
|
+
async function faas(action, params) {
|
|
13
|
+
if (onError)
|
|
14
|
+
return client.action(action, params).catch(async (res) => {
|
|
15
|
+
await onError(action, params)(res);
|
|
16
|
+
return Promise.reject(res);
|
|
17
|
+
});
|
|
18
|
+
return client.action(action, params);
|
|
19
|
+
}
|
|
20
|
+
function useFaas(action, defaultParams) {
|
|
21
|
+
const [loading, setLoading] = useState(true);
|
|
22
|
+
const [data, setData] = useState();
|
|
23
|
+
const [error, setError] = useState();
|
|
24
|
+
const [promise, setPromise] = useState();
|
|
25
|
+
const [params, setParams] = useState(defaultParams);
|
|
26
|
+
const [reloadTimes, setReloadTimes] = useState(0);
|
|
27
|
+
useEffect(function() {
|
|
28
|
+
if (JSON.stringify(defaultParams) !== JSON.stringify(params)) {
|
|
29
|
+
setParams(defaultParams);
|
|
30
|
+
}
|
|
31
|
+
}, [JSON.stringify(defaultParams)]);
|
|
32
|
+
useEffect(function() {
|
|
33
|
+
setLoading(true);
|
|
34
|
+
const request = client.action(action, params);
|
|
35
|
+
setPromise(request);
|
|
36
|
+
request.then((r) => setData(r.data)).catch(async (e) => {
|
|
37
|
+
if (onError)
|
|
38
|
+
try {
|
|
39
|
+
await onError(action, params)(e);
|
|
40
|
+
} catch (error2) {
|
|
41
|
+
setError(error2);
|
|
42
|
+
}
|
|
43
|
+
else
|
|
44
|
+
setError(e);
|
|
45
|
+
return Promise.reject(e);
|
|
46
|
+
}).finally(() => setLoading(false));
|
|
47
|
+
return () => setLoading(false);
|
|
48
|
+
}, [
|
|
49
|
+
action,
|
|
50
|
+
JSON.stringify(params),
|
|
51
|
+
reloadTimes
|
|
52
|
+
]);
|
|
53
|
+
return {
|
|
54
|
+
loading,
|
|
55
|
+
data,
|
|
56
|
+
error,
|
|
57
|
+
promise,
|
|
58
|
+
async reload(params2) {
|
|
59
|
+
if (params2)
|
|
60
|
+
setParams(params2);
|
|
61
|
+
setReloadTimes(reloadTimes + 1);
|
|
62
|
+
return promise;
|
|
63
|
+
},
|
|
64
|
+
setData,
|
|
65
|
+
setLoading,
|
|
66
|
+
setPromise,
|
|
67
|
+
setError
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
faas,
|
|
72
|
+
useFaas,
|
|
73
|
+
FaasData({
|
|
74
|
+
action,
|
|
75
|
+
params,
|
|
76
|
+
fallback,
|
|
77
|
+
element,
|
|
78
|
+
onDataChange
|
|
79
|
+
}) {
|
|
80
|
+
const request = useFaas(action, params);
|
|
81
|
+
const [loaded, setLoaded] = useState(false);
|
|
82
|
+
useEffect(function() {
|
|
83
|
+
if (!loaded && !request.loading)
|
|
84
|
+
setLoaded(true);
|
|
85
|
+
}, [request.loading]);
|
|
86
|
+
useEffect(function() {
|
|
87
|
+
if (onDataChange)
|
|
88
|
+
onDataChange(request);
|
|
89
|
+
}, [JSON.stringify(request.data)]);
|
|
90
|
+
if (loaded)
|
|
91
|
+
return element(request);
|
|
92
|
+
return fallback || null;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
export {
|
|
97
|
+
FaasReactClient
|
|
98
|
+
};
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/react",
|
|
3
|
-
"version": "0.0.2-beta.
|
|
3
|
+
"version": "0.0.2-beta.320",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"main": "
|
|
6
|
-
"types": "
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"module": "dist/index.mjs",
|
|
7
8
|
"homepage": "https://faasjs.com/doc/react.html",
|
|
8
9
|
"repository": {
|
|
9
10
|
"type": "git",
|
|
@@ -15,24 +16,23 @@
|
|
|
15
16
|
},
|
|
16
17
|
"funding": "https://github.com/sponsors/faasjs",
|
|
17
18
|
"scripts": {
|
|
18
|
-
"
|
|
19
|
+
"build": "rm -rf ./dist && tsup src/index.ts --format esm,cjs --dts"
|
|
19
20
|
},
|
|
20
21
|
"files": [
|
|
21
|
-
"
|
|
22
|
+
"dist"
|
|
22
23
|
],
|
|
23
24
|
"peerDependencies": {
|
|
24
|
-
"@faasjs/browser": "^0.0.2-beta.
|
|
25
|
+
"@faasjs/browser": "^0.0.2-beta.320",
|
|
26
|
+
"@faasjs/types": "^0.0.2-beta.320",
|
|
25
27
|
"react": "*"
|
|
26
28
|
},
|
|
27
29
|
"devDependencies": {
|
|
28
30
|
"@types/react": "*",
|
|
29
|
-
"
|
|
30
|
-
"rollup": "*",
|
|
31
|
-
"rollup-plugin-typescript2": "*",
|
|
31
|
+
"tsup": "*",
|
|
32
32
|
"typescript": "*"
|
|
33
33
|
},
|
|
34
34
|
"engines": {
|
|
35
35
|
"npm": ">=8.0.0"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "39d192f58071b851ec483dfca81fa4458307e3a2"
|
|
38
38
|
}
|
package/lib/index.js
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
(function (global, factory) {
|
|
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';
|
|
6
|
-
|
|
7
|
-
function FaasReactClient({ domain, options, onError }) {
|
|
8
|
-
const client = new browser.FaasBrowserClient(domain, options);
|
|
9
|
-
async function faas(action, params) {
|
|
10
|
-
if (onError)
|
|
11
|
-
return client.action(action, params)
|
|
12
|
-
.catch(async (res) => {
|
|
13
|
-
await onError(action, params)(res);
|
|
14
|
-
return Promise.reject(res);
|
|
15
|
-
});
|
|
16
|
-
return client.action(action, params);
|
|
17
|
-
}
|
|
18
|
-
function useFaas(action, defaultParams) {
|
|
19
|
-
const [loading, setLoading] = react.useState(true);
|
|
20
|
-
const [data, setData] = react.useState();
|
|
21
|
-
const [error, setError] = react.useState();
|
|
22
|
-
const [promise, setPromise] = react.useState();
|
|
23
|
-
const [params, setParams] = react.useState(defaultParams);
|
|
24
|
-
const [reloadTimes, setReloadTimes] = react.useState(0);
|
|
25
|
-
react.useEffect(function () {
|
|
26
|
-
if (JSON.stringify(defaultParams) !== JSON.stringify(params)) {
|
|
27
|
-
setParams(defaultParams);
|
|
28
|
-
}
|
|
29
|
-
}, [JSON.stringify(defaultParams)]);
|
|
30
|
-
react.useEffect(function () {
|
|
31
|
-
setLoading(true);
|
|
32
|
-
const request = client.action(action, params);
|
|
33
|
-
setPromise(request);
|
|
34
|
-
request
|
|
35
|
-
.then(r => setData(r.data))
|
|
36
|
-
.catch(async (e) => {
|
|
37
|
-
if (onError)
|
|
38
|
-
try {
|
|
39
|
-
await onError(action, params)(e);
|
|
40
|
-
}
|
|
41
|
-
catch (error) {
|
|
42
|
-
setError(error);
|
|
43
|
-
}
|
|
44
|
-
else
|
|
45
|
-
setError(e);
|
|
46
|
-
return Promise.reject(e);
|
|
47
|
-
})
|
|
48
|
-
.finally(() => setLoading(false));
|
|
49
|
-
return () => setLoading(false);
|
|
50
|
-
}, [
|
|
51
|
-
action,
|
|
52
|
-
JSON.stringify(params),
|
|
53
|
-
reloadTimes
|
|
54
|
-
]);
|
|
55
|
-
return {
|
|
56
|
-
loading,
|
|
57
|
-
data,
|
|
58
|
-
error,
|
|
59
|
-
promise,
|
|
60
|
-
async reload(params) {
|
|
61
|
-
if (params)
|
|
62
|
-
setParams(params);
|
|
63
|
-
setReloadTimes(reloadTimes + 1);
|
|
64
|
-
return promise;
|
|
65
|
-
},
|
|
66
|
-
setData,
|
|
67
|
-
setLoading,
|
|
68
|
-
setPromise,
|
|
69
|
-
setError,
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
return {
|
|
73
|
-
faas,
|
|
74
|
-
useFaas,
|
|
75
|
-
FaasData({ action, params, fallback, element, onDataChange }) {
|
|
76
|
-
const request = useFaas(action, params);
|
|
77
|
-
const [loaded, setLoaded] = react.useState(false);
|
|
78
|
-
react.useEffect(function () {
|
|
79
|
-
if (!loaded && !request.loading)
|
|
80
|
-
setLoaded(true);
|
|
81
|
-
}, [request.loading]);
|
|
82
|
-
react.useEffect(function () {
|
|
83
|
-
if (onDataChange)
|
|
84
|
-
onDataChange(request);
|
|
85
|
-
}, [JSON.stringify(request.data)]);
|
|
86
|
-
if (loaded)
|
|
87
|
-
return element(request);
|
|
88
|
-
return fallback || null;
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
exports.FaasReactClient = FaasReactClient;
|
|
94
|
-
|
|
95
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
96
|
-
|
|
97
|
-
}));
|