@faasjs/react 6.3.1 → 6.4.0
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/README.md +1 -0
- package/dist/index.cjs +40 -34
- package/dist/index.d.ts +16 -18
- package/dist/index.mjs +41 -35
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -83,6 +83,7 @@ npm install @faasjs/react react
|
|
|
83
83
|
- [FaasData](type-aliases/FaasData.md)
|
|
84
84
|
- [FaasDataInjection](type-aliases/FaasDataInjection.md)
|
|
85
85
|
- [FaasDataWrapperProps](type-aliases/FaasDataWrapperProps.md)
|
|
86
|
+
- [FaasDataWrapperRef](type-aliases/FaasDataWrapperRef.md)
|
|
86
87
|
- [FaasParams](type-aliases/FaasParams.md)
|
|
87
88
|
- [FaasReactClientInstance](type-aliases/FaasReactClientInstance.md)
|
|
88
89
|
- [FaasReactClientOptions](type-aliases/FaasReactClientOptions.md)
|
package/dist/index.cjs
CHANGED
|
@@ -151,40 +151,46 @@ function useStateRef(initialValue) {
|
|
|
151
151
|
}, [state]);
|
|
152
152
|
return [state, setState, ref];
|
|
153
153
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
props.
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
154
|
+
var fixedForwardRef = react.forwardRef;
|
|
155
|
+
var FaasDataWrapper = fixedForwardRef(
|
|
156
|
+
(props, ref) => {
|
|
157
|
+
const request = getClient(props.baseUrl).useFaas(
|
|
158
|
+
props.action,
|
|
159
|
+
props.params,
|
|
160
|
+
{
|
|
161
|
+
data: props.data,
|
|
162
|
+
setData: props.setData
|
|
163
|
+
}
|
|
164
|
+
);
|
|
165
|
+
const [loaded, setLoaded] = react.useState(false);
|
|
166
|
+
react.useImperativeHandle(ref, () => request, [request]);
|
|
167
|
+
useEqualEffect(() => {
|
|
168
|
+
if (!request.loading) setLoaded((prev) => prev === false ? true : prev);
|
|
169
|
+
}, [request.loading]);
|
|
170
|
+
useEqualEffect(() => {
|
|
171
|
+
if (props.onDataChange) props.onDataChange(request);
|
|
172
|
+
}, [request.data]);
|
|
173
|
+
const child = useEqualMemo(() => {
|
|
174
|
+
if (loaded) {
|
|
175
|
+
if (props.children) return react.cloneElement(props.children, request);
|
|
176
|
+
if (props.render) return props.render(request);
|
|
177
|
+
}
|
|
178
|
+
return props.fallback || null;
|
|
179
|
+
}, [
|
|
180
|
+
loaded,
|
|
181
|
+
request.action,
|
|
182
|
+
request.params,
|
|
183
|
+
request.data,
|
|
184
|
+
request.error,
|
|
185
|
+
request.loading
|
|
186
|
+
]);
|
|
187
|
+
return child;
|
|
188
|
+
}
|
|
189
|
+
);
|
|
190
|
+
Object.assign(FaasDataWrapper, {
|
|
191
|
+
displayName: "FaasDataWrapper",
|
|
192
|
+
whyDidYouRender: true
|
|
193
|
+
});
|
|
188
194
|
function withFaasData(Component2, faasProps) {
|
|
189
195
|
return (props) => /* @__PURE__ */ jsxRuntime.jsx(FaasDataWrapper, { ...faasProps, children: /* @__PURE__ */ jsxRuntime.jsx(Component2, { ...props }) });
|
|
190
196
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FaasActionUnionType, FaasAction, FaasParams, FaasData } from '@faasjs/types';
|
|
2
2
|
export { FaasAction, FaasData, FaasParams } from '@faasjs/types';
|
|
3
3
|
import { Response, BaseUrl, ResponseError, Options, FaasBrowserClient } from '@faasjs/browser';
|
|
4
4
|
export { Options, Response, ResponseError, ResponseHeaders } from '@faasjs/browser';
|
|
@@ -225,9 +225,9 @@ declare function useStateRef<T = any>(initialValue?: T): [T, Dispatch<SetStateAc
|
|
|
225
225
|
/**
|
|
226
226
|
* Injects FaasData props.
|
|
227
227
|
*/
|
|
228
|
-
type FaasDataInjection<PathOrData extends
|
|
229
|
-
action: PathOrData
|
|
230
|
-
params:
|
|
228
|
+
type FaasDataInjection<PathOrData extends FaasActionUnionType = any> = {
|
|
229
|
+
action: FaasAction<PathOrData>;
|
|
230
|
+
params: FaasParams<PathOrData>;
|
|
231
231
|
loading: boolean;
|
|
232
232
|
reloadTimes: number;
|
|
233
233
|
data: FaasData<PathOrData>;
|
|
@@ -244,11 +244,11 @@ type FaasDataInjection<PathOrData extends FaasAction = any> = {
|
|
|
244
244
|
setPromise: React.Dispatch<React.SetStateAction<Promise<Response<FaasData<PathOrData>>>>>;
|
|
245
245
|
setError: React.Dispatch<React.SetStateAction<any>>;
|
|
246
246
|
};
|
|
247
|
-
type FaasDataWrapperProps<PathOrData extends
|
|
247
|
+
type FaasDataWrapperProps<PathOrData extends FaasActionUnionType> = {
|
|
248
248
|
render?(args: FaasDataInjection<PathOrData>): JSX.Element | JSX.Element[];
|
|
249
249
|
children?: React.ReactElement<Partial<FaasDataInjection<PathOrData>>>;
|
|
250
250
|
fallback?: JSX.Element | false;
|
|
251
|
-
action: PathOrData
|
|
251
|
+
action: FaasAction<PathOrData>;
|
|
252
252
|
params?: FaasParams<PathOrData>;
|
|
253
253
|
onDataChange?(args: FaasDataInjection<PathOrData>): void;
|
|
254
254
|
/** use custom data, should work with setData */
|
|
@@ -256,12 +256,10 @@ type FaasDataWrapperProps<PathOrData extends FaasAction> = {
|
|
|
256
256
|
/** use custom setData, should work with data */
|
|
257
257
|
setData?: React.Dispatch<React.SetStateAction<FaasData<PathOrData>>>;
|
|
258
258
|
baseUrl?: BaseUrl;
|
|
259
|
+
ref?: React.Ref<FaasDataWrapperRef<PathOrData>>;
|
|
259
260
|
};
|
|
260
|
-
|
|
261
|
-
declare
|
|
262
|
-
var displayName: string;
|
|
263
|
-
var whyDidYouRender: boolean;
|
|
264
|
-
}
|
|
261
|
+
type FaasDataWrapperRef<PathOrData extends FaasActionUnionType = any> = FaasDataInjection<PathOrData>;
|
|
262
|
+
declare const FaasDataWrapper: <PathOrData extends FaasActionUnionType = any>(props: FaasDataWrapperProps<PathOrData> & react.RefAttributes<FaasDataWrapperRef<PathOrData>>) => React.ReactElement | null;
|
|
265
263
|
/**
|
|
266
264
|
* HOC to wrap a component with FaasDataWrapper
|
|
267
265
|
*
|
|
@@ -270,7 +268,7 @@ declare namespace FaasDataWrapper {
|
|
|
270
268
|
* const MyComponent = withFaasData(({ data }) => <div>{data.name}</div>, { action: 'test', params: { a: 1 } })
|
|
271
269
|
* ```
|
|
272
270
|
*/
|
|
273
|
-
declare function withFaasData<PathOrData extends FaasAction, TComponentProps extends Required<FaasDataInjection<PathOrData>> = Required<FaasDataInjection<PathOrData>>>(Component: React.FC<TComponentProps
|
|
271
|
+
declare function withFaasData<PathOrData extends FaasAction, TComponentProps extends Required<FaasDataInjection<PathOrData>> = Required<FaasDataInjection<PathOrData>>>(Component: React.FC<TComponentProps>, faasProps: FaasDataWrapperProps<PathOrData>): React.FC<Omit<TComponentProps, keyof FaasDataInjection<PathOrData>> & Record<string, any>>;
|
|
274
272
|
|
|
275
273
|
type useFaasOptions<PathOrData extends FaasAction> = {
|
|
276
274
|
params?: FaasParams<PathOrData>;
|
|
@@ -301,7 +299,7 @@ type useFaasOptions<PathOrData extends FaasAction> = {
|
|
|
301
299
|
* }
|
|
302
300
|
* ```
|
|
303
301
|
*/
|
|
304
|
-
declare function useFaas<PathOrData extends
|
|
302
|
+
declare function useFaas<PathOrData extends FaasActionUnionType>(action: FaasAction<PathOrData>, defaultParams: FaasParams<PathOrData>, options?: useFaasOptions<PathOrData>): FaasDataInjection<PathOrData>;
|
|
305
303
|
declare namespace useFaas {
|
|
306
304
|
var whyDidYouRender: boolean;
|
|
307
305
|
}
|
|
@@ -323,9 +321,9 @@ type FaasReactClientOptions = {
|
|
|
323
321
|
};
|
|
324
322
|
type FaasReactClientInstance = {
|
|
325
323
|
id: string;
|
|
326
|
-
faas: <PathOrData extends
|
|
327
|
-
useFaas: <PathOrData extends
|
|
328
|
-
FaasDataWrapper<PathOrData extends
|
|
324
|
+
faas: <PathOrData extends FaasActionUnionType>(action: FaasAction<PathOrData>, params: FaasParams<PathOrData>, options?: Options) => Promise<Response<FaasData<PathOrData>>>;
|
|
325
|
+
useFaas: <PathOrData extends FaasActionUnionType>(action: FaasAction<PathOrData>, defaultParams: FaasParams<PathOrData>, options?: useFaasOptions<PathOrData>) => FaasDataInjection<PathOrData>;
|
|
326
|
+
FaasDataWrapper<PathOrData extends FaasActionUnionType>(props: FaasDataWrapperProps<PathOrData>): JSX.Element;
|
|
329
327
|
onError?: OnError;
|
|
330
328
|
browserClient: FaasBrowserClient;
|
|
331
329
|
};
|
|
@@ -373,7 +371,7 @@ declare function getClient(host?: string): FaasReactClientInstance;
|
|
|
373
371
|
* })
|
|
374
372
|
* ```
|
|
375
373
|
*/
|
|
376
|
-
declare function faas<PathOrData extends
|
|
374
|
+
declare function faas<PathOrData extends FaasActionUnionType>(action: FaasAction<PathOrData>, params: FaasParams<PathOrData>, options?: Options): Promise<Response<FaasData<PathOrData>>>;
|
|
377
375
|
|
|
378
376
|
interface ErrorBoundaryProps {
|
|
379
377
|
children?: ReactNode;
|
|
@@ -626,4 +624,4 @@ declare const FormContextProvider: <NewT extends FormContextProps<Record<string,
|
|
|
626
624
|
}) => react.ReactNode;
|
|
627
625
|
declare const useFormContext: <NewT extends FormContextProps<Record<string, any>, FormElementTypes, FormRules> = FormContextProps<Record<string, any>, FormElementTypes, FormRules>>() => Readonly<NewT>;
|
|
628
626
|
|
|
629
|
-
export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, FormContainer as Form, type FormButtonElementProps, type FormContextProps, FormContextProvider, FormDefaultElements, FormDefaultLang, FormDefaultRules, type FormDefaultRulesOptions, type FormElementTypes, type FormInputElementProps, FormItem, type FormItemName, type FormItemProps, type FormLabelElementProps, type FormLang, type FormProps, type FormRule, type FormRules, type InferFormRulesOptions, type OnError, OptionalWrapper, type OptionalWrapperProps, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, type useFaasOptions, useFormContext, usePrevious, useSplittingState, useStateRef, validValues, withFaasData };
|
|
627
|
+
export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, type FaasDataWrapperRef, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, FormContainer as Form, type FormButtonElementProps, type FormContextProps, FormContextProvider, FormDefaultElements, FormDefaultLang, FormDefaultRules, type FormDefaultRulesOptions, type FormElementTypes, type FormInputElementProps, FormItem, type FormItemName, type FormItemProps, type FormLabelElementProps, type FormLang, type FormProps, type FormRule, type FormRules, type InferFormRulesOptions, type OnError, OptionalWrapper, type OptionalWrapperProps, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, type useFaasOptions, useFormContext, usePrevious, useSplittingState, useStateRef, validValues, withFaasData };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { forwardRef, useRef, useMemo, useEffect, useCallback,
|
|
1
|
+
import { useState, useImperativeHandle, cloneElement, forwardRef, useRef, useMemo, useEffect, useCallback, createContext, Component, useContext } from 'react';
|
|
2
2
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
3
3
|
import { FaasBrowserClient } from '@faasjs/browser';
|
|
4
4
|
|
|
@@ -149,40 +149,46 @@ function useStateRef(initialValue) {
|
|
|
149
149
|
}, [state]);
|
|
150
150
|
return [state, setState, ref];
|
|
151
151
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
props.
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
152
|
+
var fixedForwardRef = forwardRef;
|
|
153
|
+
var FaasDataWrapper = fixedForwardRef(
|
|
154
|
+
(props, ref) => {
|
|
155
|
+
const request = getClient(props.baseUrl).useFaas(
|
|
156
|
+
props.action,
|
|
157
|
+
props.params,
|
|
158
|
+
{
|
|
159
|
+
data: props.data,
|
|
160
|
+
setData: props.setData
|
|
161
|
+
}
|
|
162
|
+
);
|
|
163
|
+
const [loaded, setLoaded] = useState(false);
|
|
164
|
+
useImperativeHandle(ref, () => request, [request]);
|
|
165
|
+
useEqualEffect(() => {
|
|
166
|
+
if (!request.loading) setLoaded((prev) => prev === false ? true : prev);
|
|
167
|
+
}, [request.loading]);
|
|
168
|
+
useEqualEffect(() => {
|
|
169
|
+
if (props.onDataChange) props.onDataChange(request);
|
|
170
|
+
}, [request.data]);
|
|
171
|
+
const child = useEqualMemo(() => {
|
|
172
|
+
if (loaded) {
|
|
173
|
+
if (props.children) return cloneElement(props.children, request);
|
|
174
|
+
if (props.render) return props.render(request);
|
|
175
|
+
}
|
|
176
|
+
return props.fallback || null;
|
|
177
|
+
}, [
|
|
178
|
+
loaded,
|
|
179
|
+
request.action,
|
|
180
|
+
request.params,
|
|
181
|
+
request.data,
|
|
182
|
+
request.error,
|
|
183
|
+
request.loading
|
|
184
|
+
]);
|
|
185
|
+
return child;
|
|
186
|
+
}
|
|
187
|
+
);
|
|
188
|
+
Object.assign(FaasDataWrapper, {
|
|
189
|
+
displayName: "FaasDataWrapper",
|
|
190
|
+
whyDidYouRender: true
|
|
191
|
+
});
|
|
186
192
|
function withFaasData(Component2, faasProps) {
|
|
187
193
|
return (props) => /* @__PURE__ */ jsx(FaasDataWrapper, { ...faasProps, children: /* @__PURE__ */ jsx(Component2, { ...props }) });
|
|
188
194
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/react",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.4.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -30,10 +30,10 @@
|
|
|
30
30
|
"dist"
|
|
31
31
|
],
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@faasjs/browser": "6.
|
|
33
|
+
"@faasjs/browser": "6.4.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@faasjs/browser": "6.
|
|
36
|
+
"@faasjs/browser": "6.4.0",
|
|
37
37
|
"@types/react": "*",
|
|
38
38
|
"react": "*"
|
|
39
39
|
},
|