@faasjs/react 8.0.0-beta.14 → 8.0.0-beta.16
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 +0 -41
- package/dist/index.cjs +147 -114
- package/dist/index.d.ts +145 -110
- package/dist/index.mjs +147 -114
- package/package.json +3 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { FaasAction, FaasAction as FaasAction$1, FaasActionUnionType, FaasActionUnionType as FaasActionUnionType$1, FaasData, FaasData as FaasData$1, FaasParams, FaasParams as FaasParams$1 } from "@faasjs/types";
|
|
1
2
|
import * as react from "react";
|
|
2
3
|
import { Component, ComponentProps, ComponentType, Dispatch, ErrorInfo, JSX, JSXElementConstructor, ReactElement, ReactNode, RefObject, SetStateAction } from "react";
|
|
3
4
|
import * as react_jsx_runtime0 from "react/jsx-runtime";
|
|
4
|
-
import { FaasAction, FaasAction as FaasAction$1, FaasActionUnionType, FaasActionUnionType as FaasActionUnionType$1, FaasData, FaasData as FaasData$1, FaasParams, FaasParams as FaasParams$1 } from "@faasjs/types";
|
|
5
5
|
|
|
6
6
|
//#region src/generateId.d.ts
|
|
7
7
|
/**
|
|
@@ -24,7 +24,7 @@ declare function generateId(prefix?: string, length?: number): string;
|
|
|
24
24
|
* Ensures that base URLs used in FaasJS requests always have a trailing '/' character,
|
|
25
25
|
* which is required for proper URL construction when appending action paths.
|
|
26
26
|
*
|
|
27
|
-
*
|
|
27
|
+
* Notes:
|
|
28
28
|
* - Type only accepts strings ending with '/' (e.g., 'https://api.example.com/', '/')
|
|
29
29
|
* - Strings without trailing '/' will fail TypeScript type checking
|
|
30
30
|
* - Used by FaasBrowserClient constructor and Options type
|
|
@@ -41,7 +41,7 @@ type BaseUrl = `${string}/`;
|
|
|
41
41
|
* Extends the standard RequestInit interface with FaasJS-specific options for
|
|
42
42
|
* customizing request behavior, adding request hooks, and overriding defaults.
|
|
43
43
|
*
|
|
44
|
-
*
|
|
44
|
+
* Notes:
|
|
45
45
|
* - Options can be provided at client creation (defaultOptions) or per-request
|
|
46
46
|
* - Per-request options override client default options
|
|
47
47
|
* - headers are merged: per-request headers override default headers
|
|
@@ -73,7 +73,7 @@ type BaseUrl = `${string}/`;
|
|
|
73
73
|
* Useful for processing large data incrementally or working with binary data streams.
|
|
74
74
|
* When false or undefined, returns a wrapped Response with automatic JSON parsing.
|
|
75
75
|
*
|
|
76
|
-
* @
|
|
76
|
+
* @augments RequestInit
|
|
77
77
|
* @see FaasBrowserClient for client creation
|
|
78
78
|
* @see Response for response object structure
|
|
79
79
|
*/
|
|
@@ -103,7 +103,7 @@ type Options = RequestInit & {
|
|
|
103
103
|
* @property {string} [key] - Dynamic string keys for header names (e.g., 'Content-Type', 'Authorization').
|
|
104
104
|
* Values must be strings. Multiple values for the same key are not supported.
|
|
105
105
|
*
|
|
106
|
-
*
|
|
106
|
+
* Notes:
|
|
107
107
|
* - Headers are case-insensitive in HTTP but stored with exact casing in this object
|
|
108
108
|
* - Common headers include: Content-Type, Authorization, X-Request-Id, X-Custom-Header
|
|
109
109
|
* - No support for multi-value headers (use comma-separated values instead)
|
|
@@ -125,13 +125,12 @@ type ResponseHeaders = {
|
|
|
125
125
|
*
|
|
126
126
|
* @template PathOrData - The function path or data type for type safety
|
|
127
127
|
*
|
|
128
|
-
* @param
|
|
129
|
-
* @param
|
|
130
|
-
* @param
|
|
131
|
-
*
|
|
132
|
-
* @returns {Promise<Response<FaasData<PathOrData>> | Response>} - A Promise resolving to a Response object
|
|
128
|
+
* @param action - The function path to call.
|
|
129
|
+
* @param params - Optional parameters for the function.
|
|
130
|
+
* @param options - Optional request overrides.
|
|
131
|
+
* @returns Promise resolving to the request response. In streaming mode the runtime returns the native fetch response.
|
|
133
132
|
*
|
|
134
|
-
*
|
|
133
|
+
* Notes:
|
|
135
134
|
* - Used internally by FaasBrowserClient.action method
|
|
136
135
|
* - Provides type-safe action method signature
|
|
137
136
|
* - Return type includes both typed and untyped Response variants
|
|
@@ -163,7 +162,7 @@ type FaasBrowserClientAction = <PathOrData extends FaasActionUnionType$1>(action
|
|
|
163
162
|
* @property {T} [data] - The parsed JSON data to include in the response.
|
|
164
163
|
* Optional: contains the response payload when JSON data is provided.
|
|
165
164
|
*
|
|
166
|
-
*
|
|
165
|
+
* Notes:
|
|
167
166
|
* - All properties are optional
|
|
168
167
|
* - At least one of data or body should be provided for meaningful responses
|
|
169
168
|
* - The Response class automatically defaults status to 200 or 204 based on content
|
|
@@ -196,10 +195,7 @@ type ResponseProps<T = any> = {
|
|
|
196
195
|
* @property {T} [data] - The parsed JSON data from the response.
|
|
197
196
|
* Optional property that contains the response payload when JSON is provided.
|
|
198
197
|
*
|
|
199
|
-
*
|
|
200
|
-
* All properties are optional with sensible defaults.
|
|
201
|
-
*
|
|
202
|
-
* @remarks
|
|
198
|
+
* Notes:
|
|
203
199
|
* - status defaults to 200 if data or body is present, 204 otherwise
|
|
204
200
|
* - body is automatically populated from data if not explicitly provided
|
|
205
201
|
* - headers defaults to an empty object if not provided
|
|
@@ -302,6 +298,12 @@ declare class Response<T = any> {
|
|
|
302
298
|
readonly headers: ResponseHeaders;
|
|
303
299
|
readonly body: any;
|
|
304
300
|
readonly data?: T;
|
|
301
|
+
/**
|
|
302
|
+
* Create a wrapped response object.
|
|
303
|
+
*
|
|
304
|
+
* @param props - Response properties including status, headers, body, and data.
|
|
305
|
+
* @returns Wrapped response instance.
|
|
306
|
+
*/
|
|
305
307
|
constructor(props?: ResponseProps<T>);
|
|
306
308
|
}
|
|
307
309
|
type ResponseErrorProps = {
|
|
@@ -317,17 +319,13 @@ type ResponseErrorProps = {
|
|
|
317
319
|
* Extends the built-in Error class to provide additional information about failed requests,
|
|
318
320
|
* including HTTP status code, response headers, response body, and the original error.
|
|
319
321
|
*
|
|
320
|
-
* @
|
|
321
|
-
* @extends {Error}
|
|
322
|
+
* @augments Error
|
|
322
323
|
*
|
|
323
324
|
* @property {number} status - The HTTP status code of the failed response. Defaults to 500 if not provided.
|
|
324
325
|
* @property {ResponseHeaders} headers - The response headers from the failed request.
|
|
325
326
|
* @property {any} body - The response body containing error details or the original error if available.
|
|
326
327
|
* @property {Error} [originalError] - The original Error object if this ResponseError was created from another Error.
|
|
327
328
|
*
|
|
328
|
-
* @param {string | Error | ResponseErrorProps} data - The error message, an Error object, or a ResponseErrorProps object.
|
|
329
|
-
* @param {Omit<ResponseErrorProps, 'message' | 'originalError'>} [options] - Additional options for the error (status, headers, body).
|
|
330
|
-
*
|
|
331
329
|
* @example Basic error with message
|
|
332
330
|
* ```ts
|
|
333
331
|
* throw new ResponseError('User not found')
|
|
@@ -399,7 +397,7 @@ type ResponseErrorProps = {
|
|
|
399
397
|
* })
|
|
400
398
|
* ```
|
|
401
399
|
*
|
|
402
|
-
*
|
|
400
|
+
* Notes:
|
|
403
401
|
* - ResponseError is automatically thrown by the action method when the server returns an error (status >= 400)
|
|
404
402
|
* - The error message from server responses is extracted from body.error.message if available
|
|
405
403
|
* - When created from an Error object, the original error is preserved in the originalError property
|
|
@@ -425,23 +423,23 @@ declare class ResponseError extends Error {
|
|
|
425
423
|
* Defines the signature for functions that can mock API requests during testing.
|
|
426
424
|
* Mock handlers receive request parameters and return simulated responses or errors.
|
|
427
425
|
*
|
|
428
|
-
* @param
|
|
426
|
+
* @param action - The function path/action being requested (for example, `user` or `data/list`).
|
|
429
427
|
* Converted to lowercase by the client before being passed to the handler.
|
|
430
428
|
*
|
|
431
|
-
* @param
|
|
429
|
+
* @param params - The parameters passed to the action.
|
|
432
430
|
* May be undefined if the action was called without parameters.
|
|
433
431
|
* Parameters are passed as a plain object (already JSON-serialized if needed).
|
|
434
432
|
*
|
|
435
|
-
* @param
|
|
433
|
+
* @param options - The full request options including headers, beforeRequest hook, and other config.
|
|
436
434
|
* Includes X-FaasJS-Request-Id header in the headers object.
|
|
437
435
|
* Contains merged client defaults and per-request options.
|
|
438
436
|
*
|
|
439
|
-
* @returns
|
|
437
|
+
* @returns A promise resolving to:
|
|
440
438
|
* - ResponseProps: Mock response data (status, headers, body, data)
|
|
441
439
|
* - void: Returns an empty response (204 No Content)
|
|
442
440
|
* - Error: Throws ResponseError when returning an Error object
|
|
443
441
|
*
|
|
444
|
-
*
|
|
442
|
+
* Notes:
|
|
445
443
|
* - Used by setMock() function to mock API calls during tests
|
|
446
444
|
* - Affects all FaasBrowserClient instances when set globally
|
|
447
445
|
* - Can return different responses based on action or params
|
|
@@ -568,7 +566,7 @@ declare function setMock(handler: MockHandler | ResponseProps | Response | null)
|
|
|
568
566
|
* - Streaming support for large responses
|
|
569
567
|
* - Multiple instance support with unique IDs
|
|
570
568
|
*
|
|
571
|
-
*
|
|
569
|
+
* Notes:
|
|
572
570
|
* - All requests are POST requests by default
|
|
573
571
|
* - Automatically adds X-FaasJS-Request-Id header for request tracking
|
|
574
572
|
* - baseUrl must end with '/' (will throw Error if not)
|
|
@@ -630,11 +628,8 @@ declare class FaasBrowserClient {
|
|
|
630
628
|
/**
|
|
631
629
|
* Creates a new FaasBrowserClient instance.
|
|
632
630
|
*
|
|
633
|
-
* @param baseUrl - Base URL for all API requests. Must end with
|
|
634
|
-
*
|
|
635
|
-
* @param options - Configuration options for the client.
|
|
636
|
-
* Supports default headers, beforeRequest hook, custom request function,
|
|
637
|
-
* baseUrl override, and streaming mode.
|
|
631
|
+
* @param baseUrl - Base URL for all API requests. Must end with `/`. Defaults to `/` for relative requests.
|
|
632
|
+
* @param options - Default request options such as headers, hooks, request override, or stream mode.
|
|
638
633
|
*
|
|
639
634
|
* @example Basic initialization
|
|
640
635
|
* ```ts
|
|
@@ -685,7 +680,7 @@ declare class FaasBrowserClient {
|
|
|
685
680
|
* })
|
|
686
681
|
* ```
|
|
687
682
|
*
|
|
688
|
-
* @throws {Error} When baseUrl does not end with
|
|
683
|
+
* @throws {Error} When `baseUrl` does not end with `/`
|
|
689
684
|
*/
|
|
690
685
|
constructor(baseUrl?: BaseUrl, options?: Options);
|
|
691
686
|
/**
|
|
@@ -704,9 +699,9 @@ declare class FaasBrowserClient {
|
|
|
704
699
|
*
|
|
705
700
|
* @throws {Error} When action is not provided or is empty
|
|
706
701
|
* @throws {ResponseError} When the server returns an error response (status >= 400 or body.error exists)
|
|
707
|
-
* @throws {
|
|
702
|
+
* @throws {Error} When the request fails before a response is received
|
|
708
703
|
*
|
|
709
|
-
*
|
|
704
|
+
* Notes:
|
|
710
705
|
* - All requests are POST requests by default
|
|
711
706
|
* - Action path is automatically converted to lowercase
|
|
712
707
|
* - A unique request ID is generated for each request and sent in X-FaasJS-Request-Id header
|
|
@@ -758,11 +753,7 @@ declare class FaasBrowserClient {
|
|
|
758
753
|
* email: string
|
|
759
754
|
* }
|
|
760
755
|
*
|
|
761
|
-
* const response = await client.action<{
|
|
762
|
-
* action: 'user'
|
|
763
|
-
* params: { id: number }
|
|
764
|
-
* data: UserData
|
|
765
|
-
* }>('user', { id: 123 })
|
|
756
|
+
* const response = await client.action<UserData>('user', { id: 123 })
|
|
766
757
|
* console.log(response.data.name) // TypeScript knows it's a string
|
|
767
758
|
* ```
|
|
768
759
|
*
|
|
@@ -774,7 +765,7 @@ declare class FaasBrowserClient {
|
|
|
774
765
|
* } catch (error) {
|
|
775
766
|
* if (error instanceof ResponseError) {
|
|
776
767
|
* console.error(`Server error: ${error.message}`, error.status)
|
|
777
|
-
* if (error.
|
|
768
|
+
* if (error.body) console.error('Error details:', error.body)
|
|
778
769
|
* } else {
|
|
779
770
|
* console.error('Network error:', error)
|
|
780
771
|
* }
|
|
@@ -798,17 +789,20 @@ declare class FaasBrowserClient {
|
|
|
798
789
|
//#endregion
|
|
799
790
|
//#region src/faas.d.ts
|
|
800
791
|
/**
|
|
801
|
-
*
|
|
792
|
+
* Call the currently configured FaasReactClient.
|
|
802
793
|
*
|
|
803
|
-
* @param action
|
|
804
|
-
* @param params
|
|
805
|
-
* @
|
|
794
|
+
* @param action - Action path to invoke.
|
|
795
|
+
* @param params - Parameters sent to the action.
|
|
796
|
+
* @param options - Optional per-request overrides such as headers or base URL.
|
|
797
|
+
* @returns Response returned by the active browser client.
|
|
806
798
|
*
|
|
807
799
|
* @example
|
|
808
800
|
* ```ts
|
|
809
|
-
*
|
|
810
|
-
*
|
|
811
|
-
* })
|
|
801
|
+
* import { faas } from '@faasjs/react'
|
|
802
|
+
*
|
|
803
|
+
* const response = await faas<{ title: string }>('post/get', { id: 1 })
|
|
804
|
+
*
|
|
805
|
+
* console.log(response.data.title)
|
|
812
806
|
* ```
|
|
813
807
|
*/
|
|
814
808
|
declare function faas<PathOrData extends FaasActionUnionType$1>(action: FaasAction$1<PathOrData>, params: FaasParams$1<PathOrData>, options?: Options): Promise<Response<FaasData$1<PathOrData>>>;
|
|
@@ -861,9 +855,12 @@ declare const FaasDataWrapper: <PathOrData extends FaasActionUnionType$1 = any>(
|
|
|
861
855
|
declare function withFaasData<PathOrData extends FaasActionUnionType$1, 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>>;
|
|
862
856
|
//#endregion
|
|
863
857
|
//#region src/useFaas.d.ts
|
|
858
|
+
/**
|
|
859
|
+
* Options for {@link useFaas}.
|
|
860
|
+
*/
|
|
864
861
|
type useFaasOptions<PathOrData extends FaasActionUnionType$1> = {
|
|
865
|
-
params?: FaasParams$1<PathOrData>;
|
|
866
|
-
data?: FaasData$1<PathOrData>;
|
|
862
|
+
/** Override the request params without changing the hook's stored params state. */params?: FaasParams$1<PathOrData>; /** Controlled data value used instead of the hook's internal state. */
|
|
863
|
+
data?: FaasData$1<PathOrData>; /** Controlled setter that is called instead of the hook's internal `setData`. */
|
|
867
864
|
setData?: React.Dispatch<React.SetStateAction<FaasData$1<PathOrData>>>;
|
|
868
865
|
/**
|
|
869
866
|
* If skip is true, the request will not be sent.
|
|
@@ -871,20 +868,27 @@ type useFaasOptions<PathOrData extends FaasActionUnionType$1> = {
|
|
|
871
868
|
* However, you can still use reload to send the request.
|
|
872
869
|
*/
|
|
873
870
|
skip?: boolean | ((params: FaasParams$1<PathOrData>) => boolean); /** Send the last request after milliseconds */
|
|
874
|
-
debounce?: number;
|
|
871
|
+
debounce?: number; /** Override the default base URL for this hook instance. */
|
|
875
872
|
baseUrl?: BaseUrl;
|
|
876
873
|
};
|
|
877
874
|
/**
|
|
878
|
-
* Request
|
|
875
|
+
* Request FaasJS data and keep request state in React state.
|
|
876
|
+
*
|
|
877
|
+
* `useFaas` sends an initial request unless `skip` is enabled, and returns
|
|
878
|
+
* request state plus helpers for reloading, updating data, and handling errors.
|
|
879
879
|
*
|
|
880
|
-
* @param action
|
|
881
|
-
* @param defaultParams
|
|
882
|
-
* @
|
|
880
|
+
* @param action - Action path to invoke.
|
|
881
|
+
* @param defaultParams - Params used for the initial request and future reloads.
|
|
882
|
+
* @param options - Optional hook configuration such as controlled data, debounce, and skip logic.
|
|
883
|
+
* @returns Request state and helper methods for the action.
|
|
883
884
|
*
|
|
884
885
|
* @example
|
|
885
886
|
* ```tsx
|
|
886
|
-
*
|
|
887
|
+
* import { useFaas } from '@faasjs/react'
|
|
888
|
+
*
|
|
889
|
+
* function Post({ id }: { id: number }) {
|
|
887
890
|
* const { data } = useFaas<{ title: string }>('post/get', { id })
|
|
891
|
+
*
|
|
888
892
|
* return <h1>{data.title}</h1>
|
|
889
893
|
* }
|
|
890
894
|
* ```
|
|
@@ -893,10 +897,15 @@ declare function useFaas<PathOrData extends FaasActionUnionType$1>(action: FaasA
|
|
|
893
897
|
//#endregion
|
|
894
898
|
//#region src/client.d.ts
|
|
895
899
|
type OnError = (action: string, params: Record<string, any>) => (res: ResponseError) => Promise<void>;
|
|
900
|
+
/**
|
|
901
|
+
* Options for creating a {@link FaasReactClient} instance.
|
|
902
|
+
*/
|
|
896
903
|
type FaasReactClientOptions = {
|
|
897
|
-
/** @default `/` */baseUrl?: BaseUrl;
|
|
904
|
+
/** @default `/` */baseUrl?: BaseUrl; /** Default request options forwarded to the underlying browser client. */
|
|
898
905
|
options?: Options;
|
|
899
906
|
/**
|
|
907
|
+
* Error hook invoked when `faas` or `useFaas` receives a failed response.
|
|
908
|
+
*
|
|
900
909
|
* @example
|
|
901
910
|
* ```ts
|
|
902
911
|
* onError: (action, params) => async (res) => {
|
|
@@ -906,6 +915,9 @@ type FaasReactClientOptions = {
|
|
|
906
915
|
*/
|
|
907
916
|
onError?: OnError;
|
|
908
917
|
};
|
|
918
|
+
/**
|
|
919
|
+
* Public interface returned by {@link FaasReactClient}.
|
|
920
|
+
*/
|
|
909
921
|
type FaasReactClientInstance = {
|
|
910
922
|
id: string;
|
|
911
923
|
faas: typeof faas;
|
|
@@ -915,14 +927,20 @@ type FaasReactClientInstance = {
|
|
|
915
927
|
browserClient: FaasBrowserClient;
|
|
916
928
|
};
|
|
917
929
|
/**
|
|
918
|
-
*
|
|
930
|
+
* Create and register a FaasReactClient instance.
|
|
931
|
+
*
|
|
932
|
+
* The returned client is stored by `baseUrl` and becomes the default client
|
|
933
|
+
* used by helpers such as {@link faas} and {@link useFaas}.
|
|
919
934
|
*
|
|
920
|
-
* @
|
|
935
|
+
* @param options - Client configuration including base URL, default request options, and error hooks.
|
|
936
|
+
* @returns Registered FaasReactClient instance.
|
|
921
937
|
*
|
|
922
938
|
* @example
|
|
923
939
|
* ```ts
|
|
940
|
+
* import { FaasReactClient } from '@faasjs/react'
|
|
941
|
+
*
|
|
924
942
|
* const client = FaasReactClient({
|
|
925
|
-
* baseUrl: 'localhost:8080/api/'
|
|
943
|
+
* baseUrl: 'http://localhost:8080/api/',
|
|
926
944
|
* })
|
|
927
945
|
* ```
|
|
928
946
|
*/
|
|
@@ -932,16 +950,20 @@ declare function FaasReactClient({
|
|
|
932
950
|
onError
|
|
933
951
|
}?: FaasReactClientOptions): FaasReactClientInstance;
|
|
934
952
|
/**
|
|
935
|
-
* Get FaasReactClient instance
|
|
953
|
+
* Get a registered FaasReactClient instance.
|
|
954
|
+
*
|
|
955
|
+
* When `host` is omitted, the first registered client is returned. If no client
|
|
956
|
+
* has been created yet, a default client is initialized automatically.
|
|
936
957
|
*
|
|
937
|
-
* @param host
|
|
938
|
-
* @returns
|
|
958
|
+
* @param host - Registered base URL to look up. Omit it to use the default client.
|
|
959
|
+
* @returns Registered or newly created FaasReactClient instance.
|
|
939
960
|
*
|
|
940
961
|
* @example
|
|
941
962
|
* ```ts
|
|
963
|
+
* import { getClient } from '@faasjs/react'
|
|
964
|
+
*
|
|
942
965
|
* getClient()
|
|
943
|
-
*
|
|
944
|
-
* getClient('another-host')
|
|
966
|
+
* getClient('http://localhost:8080/api/')
|
|
945
967
|
* ```
|
|
946
968
|
*/
|
|
947
969
|
declare function getClient(host?: string): FaasReactClientInstance;
|
|
@@ -1172,32 +1194,29 @@ type FormProps<Values extends Record<string, any> = Record<string, any>, FormEle
|
|
|
1172
1194
|
rules?: typeof FormDefaultRules & Rules;
|
|
1173
1195
|
};
|
|
1174
1196
|
/**
|
|
1175
|
-
*
|
|
1176
|
-
*
|
|
1197
|
+
* Render a form with context, default elements, and validation state.
|
|
1198
|
+
*
|
|
1199
|
+
* `FormContainer` merges provided elements, language strings, and rules with
|
|
1200
|
+
* the package defaults, then exposes them through form context.
|
|
1177
1201
|
*
|
|
1178
1202
|
* @template Values - The type of form values, defaults to Record<string, any>.
|
|
1179
1203
|
* @template FormElements - The type of form elements, defaults to FormElementTypes.
|
|
1180
1204
|
* @template Rules - The type of form rules, defaults to FormDefaultRules.
|
|
1181
|
-
*
|
|
1182
|
-
* @
|
|
1183
|
-
* @param {Values} props.defaultValues - The default values for the form fields.
|
|
1184
|
-
* @param {FormElements} props.Elements - The form elements to be used in the form.
|
|
1185
|
-
* @param {Rules} props.rules - The validation rules for the form fields.
|
|
1186
|
-
* @param {FormLang} props.lang - The language settings for the form.
|
|
1187
|
-
* @param {Partial<FormContextProps>} props - Additional properties for the form context.
|
|
1188
|
-
*
|
|
1189
|
-
* @returns {JSX.Element} The FormContainer component.
|
|
1205
|
+
* @param props - Form items and optional overrides for defaults, language, rules, and submit behavior.
|
|
1206
|
+
* @returns React form container with shared form context.
|
|
1190
1207
|
*
|
|
1191
1208
|
* @example
|
|
1192
1209
|
* ```tsx
|
|
1193
1210
|
* import { Form } from '@faasjs/react'
|
|
1194
1211
|
*
|
|
1195
1212
|
* function MyForm() {
|
|
1196
|
-
* return
|
|
1197
|
-
*
|
|
1198
|
-
* {
|
|
1199
|
-
*
|
|
1200
|
-
*
|
|
1213
|
+
* return (
|
|
1214
|
+
* <Form
|
|
1215
|
+
* items={[
|
|
1216
|
+
* { name: 'name' },
|
|
1217
|
+
* ]}
|
|
1218
|
+
* />
|
|
1219
|
+
* )
|
|
1201
1220
|
* }
|
|
1202
1221
|
* ```
|
|
1203
1222
|
*/
|
|
@@ -1228,13 +1247,13 @@ type FormContextProps<Values extends Record<string, any> = Record<string, any>,
|
|
|
1228
1247
|
setErrors: Dispatch<SetStateAction<Record<string, Error>>>;
|
|
1229
1248
|
valuesRef: RefObject<Values>;
|
|
1230
1249
|
};
|
|
1231
|
-
declare const FormContextProvider: <NewT extends FormContextProps<Record<string, any>, FormElementTypes, FormRules> = FormContextProps<Record<string, any>, FormElementTypes, FormRules>>(props: {
|
|
1250
|
+
declare const FormContextProvider: <NewT extends FormContextProps<Record<string, any>, FormElementTypes, FormRules> = FormContextProps<Record<string, any>, FormElementTypes, FormRules>>(this: void, props: {
|
|
1232
1251
|
value?: Partial<NewT>;
|
|
1233
1252
|
children: react.ReactNode;
|
|
1234
1253
|
memo?: true | any[];
|
|
1235
1254
|
initializeStates?: Partial<NewT>;
|
|
1236
1255
|
}) => react.ReactNode;
|
|
1237
|
-
declare const useFormContext: <NewT extends FormContextProps<Record<string, any>, FormElementTypes, FormRules> = FormContextProps<Record<string, any>, FormElementTypes, FormRules>>() => Readonly<NewT>;
|
|
1256
|
+
declare const useFormContext: <NewT extends FormContextProps<Record<string, any>, FormElementTypes, FormRules> = FormContextProps<Record<string, any>, FormElementTypes, FormRules>>(this: void) => Readonly<NewT>;
|
|
1238
1257
|
//#endregion
|
|
1239
1258
|
//#region src/OptionalWrapper.d.ts
|
|
1240
1259
|
type OptionalWrapperProps<TWrapper extends ComponentType<{
|
|
@@ -1337,7 +1356,7 @@ declare function createSplittingContext<T extends Record<string, any>>(defaultVa
|
|
|
1337
1356
|
* }
|
|
1338
1357
|
* ```
|
|
1339
1358
|
*/
|
|
1340
|
-
Provider<NewT extends T = T>(props: {
|
|
1359
|
+
Provider<NewT extends T = T>(this: void, props: {
|
|
1341
1360
|
value?: Partial<NewT>;
|
|
1342
1361
|
children: ReactNode;
|
|
1343
1362
|
/**
|
|
@@ -1378,22 +1397,23 @@ declare function createSplittingContext<T extends Record<string, any>>(defaultVa
|
|
|
1378
1397
|
* }
|
|
1379
1398
|
* ```
|
|
1380
1399
|
*/
|
|
1381
|
-
use: <NewT extends T = T>() => Readonly<NewT>;
|
|
1400
|
+
use: <NewT extends T = T>(this: void) => Readonly<NewT>;
|
|
1382
1401
|
};
|
|
1383
1402
|
//#endregion
|
|
1384
1403
|
//#region src/splittingState.d.ts
|
|
1385
1404
|
type StateSetters<T> = { [K in keyof T as K extends string ? K extends `${infer First}${infer Rest}` ? `set${Capitalize<First>}${Rest}` : never : never]: Dispatch<SetStateAction<T[K]>> };
|
|
1386
1405
|
type StatesWithSetters<T> = T & StateSetters<T>;
|
|
1387
1406
|
/**
|
|
1388
|
-
*
|
|
1407
|
+
* Create local state entries and matching setters for each key in an object.
|
|
1389
1408
|
*
|
|
1390
1409
|
* @template T - A generic type that extends a record with string keys and any values.
|
|
1391
|
-
* @param
|
|
1410
|
+
* @param initialStates - Object whose keys become state values and `setXxx` setters.
|
|
1411
|
+
* @returns Object containing the original keys plus generated setter functions.
|
|
1392
1412
|
*
|
|
1393
1413
|
* @example
|
|
1394
1414
|
* ```tsx
|
|
1395
1415
|
* function Counter() {
|
|
1396
|
-
* const { count, setCount, name, setName } = useSplittingState({ count: 0, name: 'John' })
|
|
1416
|
+
* const { count, setCount, name, setName } = useSplittingState({ count: 0, name: 'John' })
|
|
1397
1417
|
*
|
|
1398
1418
|
* return <>{name}: {count}</>
|
|
1399
1419
|
* }
|
|
@@ -1402,9 +1422,12 @@ type StatesWithSetters<T> = T & StateSetters<T>;
|
|
|
1402
1422
|
declare function useSplittingState<T extends Record<string, unknown>>(initialStates: T): StatesWithSetters<T>;
|
|
1403
1423
|
//#endregion
|
|
1404
1424
|
//#region src/useFaasStream.d.ts
|
|
1425
|
+
/**
|
|
1426
|
+
* Options for {@link useFaasStream}.
|
|
1427
|
+
*/
|
|
1405
1428
|
type UseFaasStreamOptions = {
|
|
1406
|
-
params?: Record<string, any>;
|
|
1407
|
-
data?: string;
|
|
1429
|
+
/** Override the request params without changing the hook's stored params state. */params?: Record<string, any>; /** Controlled stream text used instead of the hook's internal state. */
|
|
1430
|
+
data?: string; /** Controlled setter that is called instead of the hook's internal `setData`. */
|
|
1408
1431
|
setData?: React.Dispatch<React.SetStateAction<string>>;
|
|
1409
1432
|
/**
|
|
1410
1433
|
* If skip is true, the request will not be sent.
|
|
@@ -1412,9 +1435,12 @@ type UseFaasStreamOptions = {
|
|
|
1412
1435
|
* However, you can still use reload to send the request.
|
|
1413
1436
|
*/
|
|
1414
1437
|
skip?: boolean | ((params: Record<string, any>) => boolean); /** Send the last request after milliseconds */
|
|
1415
|
-
debounce?: number;
|
|
1438
|
+
debounce?: number; /** Override the default base URL for this hook instance. */
|
|
1416
1439
|
baseUrl?: BaseUrl;
|
|
1417
1440
|
};
|
|
1441
|
+
/**
|
|
1442
|
+
* Result returned by {@link useFaasStream}.
|
|
1443
|
+
*/
|
|
1418
1444
|
type UseFaasStreamResult = {
|
|
1419
1445
|
action: string;
|
|
1420
1446
|
params: Record<string, any>;
|
|
@@ -1428,14 +1454,21 @@ type UseFaasStreamResult = {
|
|
|
1428
1454
|
setError: React.Dispatch<React.SetStateAction<any>>;
|
|
1429
1455
|
};
|
|
1430
1456
|
/**
|
|
1431
|
-
* Stream
|
|
1457
|
+
* Stream a FaasJS response into React state.
|
|
1458
|
+
*
|
|
1459
|
+
* The hook sends a streaming request, appends decoded text chunks to `data`,
|
|
1460
|
+
* and exposes reload helpers for retrying the same action.
|
|
1432
1461
|
*
|
|
1433
|
-
* @param action
|
|
1434
|
-
* @param defaultParams
|
|
1435
|
-
* @
|
|
1462
|
+
* @param action - Action path to invoke.
|
|
1463
|
+
* @param defaultParams - Params used for the initial request and future reloads.
|
|
1464
|
+
* @param options - Optional hook configuration such as controlled data, debounce, and skip logic.
|
|
1465
|
+
* @returns Streaming request state and helper methods.
|
|
1436
1466
|
*
|
|
1437
1467
|
* @example
|
|
1438
1468
|
* ```tsx
|
|
1469
|
+
* import { useState } from 'react'
|
|
1470
|
+
* import { useFaasStream } from '@faasjs/react'
|
|
1471
|
+
*
|
|
1439
1472
|
* function Chat() {
|
|
1440
1473
|
* const [prompt, setPrompt] = useState('')
|
|
1441
1474
|
* const { data, loading, reload } = useFaasStream('chat', { prompt })
|
|
@@ -1457,8 +1490,8 @@ declare function useFaasStream(action: string, defaultParams: Record<string, any
|
|
|
1457
1490
|
* Hook to store the previous value of a state or prop.
|
|
1458
1491
|
*
|
|
1459
1492
|
* @template T - The type of the value.
|
|
1460
|
-
* @param
|
|
1461
|
-
* @returns
|
|
1493
|
+
* @param value - The current value to track.
|
|
1494
|
+
* @returns Previous value from the prior render, or `undefined` on the first render.
|
|
1462
1495
|
*/
|
|
1463
1496
|
declare function usePrevious<T = any>(value: T): T | undefined;
|
|
1464
1497
|
//#endregion
|
|
@@ -1467,24 +1500,26 @@ declare function usePrevious<T = any>(value: T): T | undefined;
|
|
|
1467
1500
|
* Custom hook that returns a stateful value and a ref to that value.
|
|
1468
1501
|
*
|
|
1469
1502
|
* @template T - The type of the value.
|
|
1470
|
-
* @param
|
|
1471
|
-
* @returns
|
|
1503
|
+
* @param initialValue - Initial state value. When omitted, state starts as `null`.
|
|
1504
|
+
* @returns Tuple containing the current state, the state setter, and a ref that always points at the latest state.
|
|
1472
1505
|
*
|
|
1473
1506
|
* @example
|
|
1474
1507
|
* ```tsx
|
|
1475
1508
|
* import { useStateRef } from '@faasjs/react'
|
|
1476
1509
|
*
|
|
1477
1510
|
* function MyComponent() {
|
|
1478
|
-
*
|
|
1479
|
-
*
|
|
1480
|
-
*
|
|
1481
|
-
*
|
|
1482
|
-
*
|
|
1483
|
-
*
|
|
1484
|
-
*
|
|
1485
|
-
*
|
|
1486
|
-
*
|
|
1511
|
+
* const [value, setValue, ref] = useStateRef(0)
|
|
1512
|
+
*
|
|
1513
|
+
* return (
|
|
1514
|
+
* <div>
|
|
1515
|
+
* <p>Value: {value}</p>
|
|
1516
|
+
* <button onClick={() => setValue(value + 1)}>Increment</button>
|
|
1517
|
+
* <button onClick={() => console.log(ref.current)}>Submit</button>
|
|
1518
|
+
* </div>
|
|
1519
|
+
* )
|
|
1520
|
+
* }
|
|
1521
|
+
* ```
|
|
1487
1522
|
*/
|
|
1488
1523
|
declare function useStateRef<T = any>(initialValue?: T): [T | null, Dispatch<SetStateAction<T | null>>, RefObject<T | null>];
|
|
1489
1524
|
//#endregion
|
|
1490
|
-
export { BaseUrl, ErrorBoundary, ErrorBoundaryProps, ErrorChildrenProps, type FaasAction, type FaasActionUnionType, FaasBrowserClient, FaasBrowserClientAction, type FaasData, FaasDataInjection, FaasDataWrapper, FaasDataWrapperProps, FaasDataWrapperRef, type FaasParams, FaasReactClient, FaasReactClientInstance, FaasReactClientOptions, FormContainer as Form, type FormButtonElementProps, FormContextProps, FormContextProvider, FormDefaultElements, FormDefaultLang, FormDefaultRules, FormDefaultRulesOptions, FormElementTypes, FormInput, type FormInputElementProps, FormInputProps, FormItem, FormItemName, FormItemProps, type FormLabelElementProps, FormLang,
|
|
1525
|
+
export { BaseUrl, ErrorBoundary, ErrorBoundaryProps, ErrorChildrenProps, type FaasAction, type FaasActionUnionType, FaasBrowserClient, FaasBrowserClientAction, type FaasData, FaasDataInjection, FaasDataWrapper, FaasDataWrapperProps, FaasDataWrapperRef, type FaasParams, FaasReactClient, FaasReactClientInstance, FaasReactClientOptions, FormContainer as Form, type FormButtonElementProps, FormContextProps, FormContextProvider, FormDefaultElements, FormDefaultLang, FormDefaultRules, FormDefaultRulesOptions, FormElementTypes, FormInput, type FormInputElementProps, FormInputProps, FormItem, FormItemName, FormItemProps, type FormLabelElementProps, FormLang, FormProps, FormRule, FormRules, InferFormInputProps, InferFormRulesOptions, InferRuleOption, MockHandler, OnError, OptionalWrapper, OptionalWrapperProps, Options, Response, ResponseError, ResponseErrorProps, ResponseHeaders, ResponseProps, StateSetters, StatesWithSetters, UseFaasStreamOptions, UseFaasStreamResult, createSplittingContext, equal, faas, generateId, getClient, setMock, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, useFaasOptions, useFaasStream, useFormContext, usePrevious, useSplittingState, useStateRef, validValues, withFaasData };
|