@faasjs/react 8.0.0-beta.17 → 8.0.0-beta.19
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.cjs +459 -58
- package/dist/index.d.ts +605 -131
- package/dist/index.mjs +459 -58
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -5,14 +5,18 @@ import * as react_jsx_runtime0 from "react/jsx-runtime";
|
|
|
5
5
|
|
|
6
6
|
//#region src/generateId.d.ts
|
|
7
7
|
/**
|
|
8
|
-
* Generate random
|
|
8
|
+
* Generate a random identifier with an optional prefix.
|
|
9
9
|
*
|
|
10
|
-
* @param prefix
|
|
11
|
-
* @param length
|
|
10
|
+
* @param prefix - Prefix prepended to the generated identifier.
|
|
11
|
+
* @param length - Length of the generated identifier excluding `prefix`. Must be between `8` and `18`.
|
|
12
|
+
* @returns Generated identifier string.
|
|
13
|
+
* @throws {Error} When `length` is outside the supported `8` to `18` range.
|
|
12
14
|
*
|
|
13
15
|
* @example
|
|
14
16
|
* ```ts
|
|
15
|
-
* generateId('prefix-')
|
|
17
|
+
* const id = generateId('prefix-')
|
|
18
|
+
*
|
|
19
|
+
* id.startsWith('prefix-') // true
|
|
16
20
|
* ```
|
|
17
21
|
*/
|
|
18
22
|
declare function generateId(prefix?: string, length?: number): string;
|
|
@@ -78,7 +82,7 @@ type BaseUrl = `${string}/`;
|
|
|
78
82
|
* @see Response for response object structure
|
|
79
83
|
*/
|
|
80
84
|
type Options = RequestInit & {
|
|
81
|
-
headers?: Record<string, string>; /**
|
|
85
|
+
headers?: Record<string, string>; /** Async hook called after request options are merged but before the request is sent. */
|
|
82
86
|
beforeRequest?: ({
|
|
83
87
|
action,
|
|
84
88
|
params,
|
|
@@ -89,9 +93,9 @@ type Options = RequestInit & {
|
|
|
89
93
|
params?: Record<string, any> | undefined;
|
|
90
94
|
options: Options;
|
|
91
95
|
headers: Record<string, string>;
|
|
92
|
-
}) => Promise<void>; /**
|
|
93
|
-
request?: <PathOrData extends FaasActionUnionType$1>(url: string, options: Options) => Promise<Response<FaasData$1<PathOrData>>>;
|
|
94
|
-
baseUrl?: BaseUrl;
|
|
96
|
+
}) => Promise<void>; /** Custom request implementation used instead of the native `fetch`. */
|
|
97
|
+
request?: <PathOrData extends FaasActionUnionType$1>(url: string, options: Options) => Promise<Response<FaasData$1<PathOrData>>>; /** Base URL override for the current request. */
|
|
98
|
+
baseUrl?: BaseUrl; /** When `true`, return the native fetch response so callers can consume the stream manually. */
|
|
95
99
|
stream?: boolean;
|
|
96
100
|
};
|
|
97
101
|
/**
|
|
@@ -128,6 +132,8 @@ type ResponseHeaders = {
|
|
|
128
132
|
* @param action - The function path to call.
|
|
129
133
|
* @param params - Optional parameters for the function.
|
|
130
134
|
* @param options - Optional request overrides.
|
|
135
|
+
* See {@link Options} for supported request fields such as `headers`, `beforeRequest`,
|
|
136
|
+
* `request`, `baseUrl`, and `stream`.
|
|
131
137
|
* @returns Promise resolving to the request response. In streaming mode the runtime returns the native fetch response.
|
|
132
138
|
*
|
|
133
139
|
* Notes:
|
|
@@ -294,23 +300,42 @@ type ResponseProps<T = any> = {
|
|
|
294
300
|
* @see FaasBrowserClient.action for method returning Response
|
|
295
301
|
*/
|
|
296
302
|
declare class Response<T = any> {
|
|
303
|
+
/**
|
|
304
|
+
* HTTP status code exposed to callers.
|
|
305
|
+
*/
|
|
297
306
|
readonly status: number;
|
|
307
|
+
/**
|
|
308
|
+
* Response headers keyed by header name.
|
|
309
|
+
*/
|
|
298
310
|
readonly headers: ResponseHeaders;
|
|
311
|
+
/**
|
|
312
|
+
* Raw response body.
|
|
313
|
+
*/
|
|
299
314
|
readonly body: any;
|
|
315
|
+
/**
|
|
316
|
+
* Parsed response payload when JSON data is available.
|
|
317
|
+
*/
|
|
300
318
|
readonly data?: T;
|
|
301
319
|
/**
|
|
302
320
|
* Create a wrapped response object.
|
|
303
321
|
*
|
|
304
322
|
* @param props - Response properties including status, headers, body, and data.
|
|
323
|
+
* @param props.status - HTTP status code. Defaults to `200` when `data` or `body` exists, otherwise `204`.
|
|
324
|
+
* @param props.headers - Response headers keyed by header name.
|
|
325
|
+
* @param props.body - Raw response body to expose without additional parsing.
|
|
326
|
+
* @param props.data - Parsed response payload to expose on `response.data`.
|
|
305
327
|
* @returns Wrapped response instance.
|
|
306
328
|
*/
|
|
307
329
|
constructor(props?: ResponseProps<T>);
|
|
308
330
|
}
|
|
331
|
+
/**
|
|
332
|
+
* Input accepted by the {@link ResponseError} constructor.
|
|
333
|
+
*/
|
|
309
334
|
type ResponseErrorProps = {
|
|
310
|
-
message: string; /** @default 500 */
|
|
311
|
-
status?: number; /** @default {} */
|
|
312
|
-
headers?: ResponseHeaders; /** @default { error:
|
|
313
|
-
body?: any;
|
|
335
|
+
/** User-facing error message. */message: string; /** HTTP status code reported for the error. @default 500 */
|
|
336
|
+
status?: number; /** Response headers returned with the error. @default {} */
|
|
337
|
+
headers?: ResponseHeaders; /** Raw error body or structured error payload. @default { error: { message } } */
|
|
338
|
+
body?: any; /** Original error preserved when this instance wraps another exception. */
|
|
314
339
|
originalError?: Error;
|
|
315
340
|
};
|
|
316
341
|
/**
|
|
@@ -410,12 +435,39 @@ type ResponseErrorProps = {
|
|
|
410
435
|
* @see setMock for mocking errors in tests
|
|
411
436
|
*/
|
|
412
437
|
declare class ResponseError extends Error {
|
|
438
|
+
/**
|
|
439
|
+
* HTTP status code reported for the failed request.
|
|
440
|
+
*/
|
|
413
441
|
readonly status: number;
|
|
442
|
+
/**
|
|
443
|
+
* Response headers returned with the error.
|
|
444
|
+
*/
|
|
414
445
|
readonly headers: ResponseHeaders;
|
|
446
|
+
/**
|
|
447
|
+
* Raw error body or fallback error payload.
|
|
448
|
+
*/
|
|
415
449
|
readonly body: any;
|
|
450
|
+
/**
|
|
451
|
+
* Original error used to construct this instance, when available.
|
|
452
|
+
*/
|
|
416
453
|
readonly originalError?: Error;
|
|
417
|
-
|
|
418
|
-
|
|
454
|
+
/**
|
|
455
|
+
* Create a ResponseError from a message, Error, or structured response error payload.
|
|
456
|
+
*
|
|
457
|
+
* @param data - Error message, Error object, or structured response error props.
|
|
458
|
+
* @param data.message - User-facing error message when `data` is a structured object.
|
|
459
|
+
* @param data.status - HTTP status code when `data` is a structured object.
|
|
460
|
+
* @param data.headers - Response headers returned with the error when `data` is a structured object.
|
|
461
|
+
* @param data.body - Raw error body or structured error payload when `data` is a structured object.
|
|
462
|
+
* @param data.originalError - Original error preserved on the instance when `data` is a structured object.
|
|
463
|
+
* @param options - Additional options such as status, headers, and body.
|
|
464
|
+
* @param options.status - HTTP status override used when `data` is a string or `Error`.
|
|
465
|
+
* @param options.headers - Response headers override used when `data` is a string or `Error`.
|
|
466
|
+
* @param options.body - Raw error body override used when `data` is a string or `Error`.
|
|
467
|
+
* @returns ResponseError instance.
|
|
468
|
+
*/
|
|
469
|
+
constructor(data: string | Error, options?: Omit<ResponseErrorProps, 'message' | 'originalError'>);
|
|
470
|
+
constructor(data: ResponseErrorProps);
|
|
419
471
|
}
|
|
420
472
|
/**
|
|
421
473
|
* Mock handler function type for testing FaasJS requests.
|
|
@@ -433,6 +485,8 @@ declare class ResponseError extends Error {
|
|
|
433
485
|
* @param options - The full request options including headers, beforeRequest hook, and other config.
|
|
434
486
|
* Includes X-FaasJS-Request-Id header in the headers object.
|
|
435
487
|
* Contains merged client defaults and per-request options.
|
|
488
|
+
* See {@link Options} for supported request fields such as `headers`, `beforeRequest`,
|
|
489
|
+
* `request`, `baseUrl`, and `stream`.
|
|
436
490
|
*
|
|
437
491
|
* @returns A promise resolving to:
|
|
438
492
|
* - ResponseProps: Mock response data (status, headers, body, data)
|
|
@@ -499,7 +553,7 @@ declare class ResponseError extends Error {
|
|
|
499
553
|
*/
|
|
500
554
|
type MockHandler = (action: string, params: Record<string, any> | undefined, options: Options) => Promise<ResponseProps> | Promise<void> | Promise<Error>;
|
|
501
555
|
/**
|
|
502
|
-
* Set global mock handler
|
|
556
|
+
* Set the global mock handler used by all {@link FaasBrowserClient} instances.
|
|
503
557
|
*
|
|
504
558
|
* @param handler - Mock handler, can be:
|
|
505
559
|
* - MockHandler function: receives (action, params, options) and returns response data
|
|
@@ -507,24 +561,52 @@ type MockHandler = (action: string, params: Record<string, any> | undefined, opt
|
|
|
507
561
|
* - Response instance: pre-configured Response object
|
|
508
562
|
* - null or undefined: clear mock
|
|
509
563
|
*
|
|
564
|
+
* @example Reset in Vitest shared setup
|
|
565
|
+
* ```ts
|
|
566
|
+
* import { afterEach } from 'vitest'
|
|
567
|
+
*
|
|
568
|
+
* afterEach(() => {
|
|
569
|
+
* setMock(null)
|
|
570
|
+
* })
|
|
571
|
+
* ```
|
|
572
|
+
*
|
|
573
|
+
* @example Use ResponseProps object
|
|
574
|
+
* ```ts
|
|
575
|
+
* setMock({
|
|
576
|
+
* data: { name: 'FaasJS' },
|
|
577
|
+
* })
|
|
578
|
+
*
|
|
579
|
+
* setMock({
|
|
580
|
+
* status: 500,
|
|
581
|
+
* data: { message: 'Internal Server Error' },
|
|
582
|
+
* })
|
|
583
|
+
* ```
|
|
584
|
+
*
|
|
510
585
|
* @example Use MockHandler function
|
|
511
586
|
* ```ts
|
|
512
|
-
* setMock(async (action
|
|
513
|
-
* if (action === '
|
|
514
|
-
* return { data: { name: '
|
|
587
|
+
* setMock(async (action) => {
|
|
588
|
+
* if (action === '/pages/users/get') {
|
|
589
|
+
* return { data: { id: 1, name: 'FaasJS' } }
|
|
515
590
|
* }
|
|
516
|
-
*
|
|
591
|
+
*
|
|
592
|
+
* return { status: 404, data: { message: 'Not Found' } }
|
|
517
593
|
* })
|
|
518
594
|
*
|
|
519
|
-
* const response = await client.action('
|
|
595
|
+
* const response = await client.action('/pages/users/get')
|
|
520
596
|
* ```
|
|
521
597
|
*
|
|
522
|
-
* @example
|
|
598
|
+
* @example Branch by action and params
|
|
523
599
|
* ```ts
|
|
524
|
-
* setMock({
|
|
525
|
-
*
|
|
526
|
-
*
|
|
527
|
-
*
|
|
600
|
+
* setMock(async (action, params) => {
|
|
601
|
+
* if (action === '/pages/users/get' && params?.id === 1) {
|
|
602
|
+
* return { data: { id: 1, name: 'Admin' } }
|
|
603
|
+
* }
|
|
604
|
+
*
|
|
605
|
+
* if (action === '/pages/users/get' && params?.id === 2) {
|
|
606
|
+
* return { data: { id: 2, name: 'Editor' } }
|
|
607
|
+
* }
|
|
608
|
+
*
|
|
609
|
+
* return { status: 404, data: { message: 'User not found' } }
|
|
528
610
|
* })
|
|
529
611
|
* ```
|
|
530
612
|
*
|
|
@@ -536,11 +618,22 @@ type MockHandler = (action: string, params: Record<string, any> | undefined, opt
|
|
|
536
618
|
* }))
|
|
537
619
|
* ```
|
|
538
620
|
*
|
|
621
|
+
* @example Streaming response
|
|
622
|
+
* ```ts
|
|
623
|
+
* setMock({
|
|
624
|
+
* body: new ReadableStream({
|
|
625
|
+
* start(controller) {
|
|
626
|
+
* controller.enqueue(new TextEncoder().encode('hello'))
|
|
627
|
+
* controller.enqueue(new TextEncoder().encode(' world'))
|
|
628
|
+
* controller.close()
|
|
629
|
+
* },
|
|
630
|
+
* }),
|
|
631
|
+
* })
|
|
632
|
+
* ```
|
|
633
|
+
*
|
|
539
634
|
* @example Clear mock
|
|
540
635
|
* ```ts
|
|
541
636
|
* setMock(null)
|
|
542
|
-
* // or
|
|
543
|
-
* setMock(undefined)
|
|
544
637
|
* ```
|
|
545
638
|
*
|
|
546
639
|
* @example Handle errors
|
|
@@ -551,7 +644,7 @@ type MockHandler = (action: string, params: Record<string, any> | undefined, opt
|
|
|
551
644
|
* // This will reject with ResponseError
|
|
552
645
|
* ```
|
|
553
646
|
*/
|
|
554
|
-
declare function setMock(handler: MockHandler | ResponseProps | Response | null): void;
|
|
647
|
+
declare function setMock(handler: MockHandler | ResponseProps | Response | null | undefined): void;
|
|
555
648
|
/**
|
|
556
649
|
* Browser client for FaasJS - provides HTTP client functionality for making API requests from web applications.
|
|
557
650
|
*
|
|
@@ -622,14 +715,25 @@ declare function setMock(handler: MockHandler | ResponseProps | Response | null)
|
|
|
622
715
|
* @see ResponseError for error handling
|
|
623
716
|
*/
|
|
624
717
|
declare class FaasBrowserClient {
|
|
718
|
+
/**
|
|
719
|
+
* Unique identifier for this client instance.
|
|
720
|
+
*/
|
|
625
721
|
readonly id: string;
|
|
722
|
+
/**
|
|
723
|
+
* Base URL used to build action request URLs.
|
|
724
|
+
*/
|
|
626
725
|
baseUrl: BaseUrl;
|
|
726
|
+
/**
|
|
727
|
+
* Default request options merged into every request.
|
|
728
|
+
*/
|
|
627
729
|
defaultOptions: Options;
|
|
628
730
|
/**
|
|
629
731
|
* Creates a new FaasBrowserClient instance.
|
|
630
732
|
*
|
|
631
733
|
* @param baseUrl - Base URL for all API requests. Must end with `/`. Defaults to `/` for relative requests.
|
|
632
734
|
* @param options - Default request options such as headers, hooks, request override, or stream mode.
|
|
735
|
+
* See {@link Options} for supported request fields such as `headers`, `beforeRequest`,
|
|
736
|
+
* `request`, `baseUrl`, and `stream`.
|
|
633
737
|
*
|
|
634
738
|
* @example Basic initialization
|
|
635
739
|
* ```ts
|
|
@@ -693,9 +797,11 @@ declare class FaasBrowserClient {
|
|
|
693
797
|
* Optional if the function accepts no parameters.
|
|
694
798
|
* @param options - Optional request options that override client defaults.
|
|
695
799
|
* Supports headers, beforeRequest hook, custom request function, baseUrl override, and streaming mode.
|
|
800
|
+
* See {@link Options} for supported request fields such as `headers`, `beforeRequest`,
|
|
801
|
+
* `request`, `baseUrl`, and `stream`.
|
|
696
802
|
*
|
|
697
|
-
* @returns A
|
|
698
|
-
*
|
|
803
|
+
* @returns A promise resolving to the wrapped FaasJS response. When `options.stream`
|
|
804
|
+
* is `true`, the runtime returns the native fetch response so callers can read the stream.
|
|
699
805
|
*
|
|
700
806
|
* @throws {Error} When action is not provided or is empty
|
|
701
807
|
* @throws {ResponseError} When the server returns an error response (status >= 400 or body.error exists)
|
|
@@ -791,16 +897,24 @@ declare class FaasBrowserClient {
|
|
|
791
897
|
/**
|
|
792
898
|
* Call the currently configured FaasReactClient.
|
|
793
899
|
*
|
|
900
|
+
* This helper forwards the request to `getClient`. When the registered
|
|
901
|
+
* client defines `onError`, the hook is invoked before the promise rejects.
|
|
902
|
+
*
|
|
903
|
+
* @template PathOrData - Action path or response data type used for inference.
|
|
904
|
+
*
|
|
794
905
|
* @param action - Action path to invoke.
|
|
795
906
|
* @param params - Parameters sent to the action.
|
|
796
907
|
* @param options - Optional per-request overrides such as headers or base URL.
|
|
908
|
+
* See the request `Options` type for supported fields such as `headers`, `beforeRequest`,
|
|
909
|
+
* `request`, `baseUrl`, and `stream`.
|
|
797
910
|
* @returns Response returned by the active browser client.
|
|
911
|
+
* @throws {ResponseError} When the request fails and the active client does not recover inside `onError`.
|
|
798
912
|
*
|
|
799
913
|
* @example
|
|
800
914
|
* ```ts
|
|
801
915
|
* import { faas } from '@faasjs/react'
|
|
802
916
|
*
|
|
803
|
-
* const response = await faas
|
|
917
|
+
* const response = await faas('posts/get', { id: 1 })
|
|
804
918
|
*
|
|
805
919
|
* console.log(response.data.title)
|
|
806
920
|
* ```
|
|
@@ -809,57 +923,182 @@ declare function faas<PathOrData extends FaasActionUnionType$1>(action: FaasActi
|
|
|
809
923
|
//#endregion
|
|
810
924
|
//#region src/FaasDataWrapper.d.ts
|
|
811
925
|
/**
|
|
812
|
-
*
|
|
926
|
+
* Request state injected by {@link useFaas}, {@link FaasDataWrapper}, and {@link withFaasData}.
|
|
927
|
+
*
|
|
928
|
+
* @template PathOrData - Action path or response data type used for inference.
|
|
813
929
|
*/
|
|
814
930
|
type FaasDataInjection<PathOrData extends FaasActionUnionType$1 = any> = {
|
|
815
|
-
action: FaasAction$1<PathOrData>;
|
|
816
|
-
params: FaasParams$1<PathOrData>;
|
|
817
|
-
loading: boolean;
|
|
818
|
-
reloadTimes: number;
|
|
819
|
-
data: FaasData$1<PathOrData>;
|
|
820
|
-
error: any;
|
|
931
|
+
/** Action path associated with the current request state. */action: FaasAction$1<PathOrData>; /** Params used for the most recent request attempt. */
|
|
932
|
+
params: FaasParams$1<PathOrData>; /** Whether the request is currently in flight. */
|
|
933
|
+
loading: boolean; /** Number of times `reload()` has triggered a new request. */
|
|
934
|
+
reloadTimes: number; /** Current resolved data value. */
|
|
935
|
+
data: FaasData$1<PathOrData>; /** Last request error, if one occurred. */
|
|
936
|
+
error: any; /** Promise representing the latest request. */
|
|
821
937
|
promise: Promise<Response<FaasData$1<PathOrData>>>;
|
|
822
938
|
/**
|
|
823
939
|
* Reloads data with new or existing parameters.
|
|
824
940
|
*
|
|
825
|
-
*
|
|
941
|
+
* When the source hook is currently skipped, calling `reload` clears the skip
|
|
942
|
+
* flag before starting the next request.
|
|
826
943
|
*/
|
|
827
|
-
reload(params?: Record<string, any>): Promise<FaasData$1<PathOrData>>;
|
|
828
|
-
setData: React.Dispatch<React.SetStateAction<FaasData$1<PathOrData>>>;
|
|
829
|
-
setLoading: React.Dispatch<React.SetStateAction<boolean>>;
|
|
830
|
-
setPromise: React.Dispatch<React.SetStateAction<Promise<Response<FaasData$1<PathOrData>>>>>;
|
|
944
|
+
reload(params?: Record<string, any>): Promise<FaasData$1<PathOrData>>; /** Controlled or internal setter for the resolved data value. */
|
|
945
|
+
setData: React.Dispatch<React.SetStateAction<FaasData$1<PathOrData>>>; /** Setter for the loading flag. */
|
|
946
|
+
setLoading: React.Dispatch<React.SetStateAction<boolean>>; /** Setter for the latest request promise. */
|
|
947
|
+
setPromise: React.Dispatch<React.SetStateAction<Promise<Response<FaasData$1<PathOrData>>>>>; /** Setter for the last request error. */
|
|
831
948
|
setError: React.Dispatch<React.SetStateAction<any>>;
|
|
832
949
|
};
|
|
950
|
+
/**
|
|
951
|
+
* Props for the {@link FaasDataWrapper} render-prop component.
|
|
952
|
+
*
|
|
953
|
+
* @template PathOrData - Action path or response data type used for inference.
|
|
954
|
+
*/
|
|
833
955
|
type FaasDataWrapperProps<PathOrData extends FaasActionUnionType$1> = {
|
|
834
|
-
render?(args: FaasDataInjection<PathOrData>): JSX.Element | JSX.Element[];
|
|
835
|
-
children?: React.ReactElement<Partial<FaasDataInjection<PathOrData>>>;
|
|
836
|
-
fallback?: JSX.Element | false;
|
|
837
|
-
action: FaasAction$1<PathOrData>;
|
|
838
|
-
params?: FaasParams$1<PathOrData>;
|
|
839
|
-
onDataChange?(args: FaasDataInjection<PathOrData>): void; /**
|
|
840
|
-
data?: FaasData$1<PathOrData>; /**
|
|
841
|
-
setData?: React.Dispatch<React.SetStateAction<FaasData$1<PathOrData>>>;
|
|
842
|
-
baseUrl?: BaseUrl;
|
|
956
|
+
/** Render prop invoked with the resolved request state after the first load completes. */render?(args: FaasDataInjection<PathOrData>): JSX.Element | JSX.Element[]; /** Child element cloned with injected request state after the first load completes. */
|
|
957
|
+
children?: React.ReactElement<Partial<FaasDataInjection<PathOrData>>>; /** Element rendered before the first successful load. */
|
|
958
|
+
fallback?: JSX.Element | false; /** Action path to request. */
|
|
959
|
+
action: FaasAction$1<PathOrData>; /** Params sent to the action. */
|
|
960
|
+
params?: FaasParams$1<PathOrData>; /** Callback invoked whenever the resolved data value changes. */
|
|
961
|
+
onDataChange?(args: FaasDataInjection<PathOrData>): void; /** Controlled data value used instead of internal state. */
|
|
962
|
+
data?: FaasData$1<PathOrData>; /** Controlled setter used instead of internal state. */
|
|
963
|
+
setData?: React.Dispatch<React.SetStateAction<FaasData$1<PathOrData>>>; /** Base URL override used for this wrapper instance. */
|
|
964
|
+
baseUrl?: BaseUrl; /** Imperative ref exposing the current injected request state. */
|
|
843
965
|
ref?: React.Ref<FaasDataWrapperRef<PathOrData>>;
|
|
844
966
|
};
|
|
967
|
+
/**
|
|
968
|
+
* Imperative ref shape exposed by {@link FaasDataWrapper}.
|
|
969
|
+
*
|
|
970
|
+
* @template PathOrData - Action path or response data type used for inference.
|
|
971
|
+
*/
|
|
845
972
|
type FaasDataWrapperRef<PathOrData extends FaasActionUnionType$1 = any> = FaasDataInjection<PathOrData>;
|
|
973
|
+
/**
|
|
974
|
+
* Fetch FaasJS data and inject the result into a render prop or child element.
|
|
975
|
+
*
|
|
976
|
+
* The wrapper defers rendering `children` or `render` until the first request
|
|
977
|
+
* completes, then keeps passing the latest request state to the rendered output.
|
|
978
|
+
*
|
|
979
|
+
* @param props - Wrapper props controlling the request and rendered fallback.
|
|
980
|
+
* @param props.render - Render prop that receives the resolved Faas request state.
|
|
981
|
+
* @param props.children - Child element cloned with injected Faas request state.
|
|
982
|
+
* @param props.fallback - Element rendered before the first successful load.
|
|
983
|
+
* @param props.action - Action path to request.
|
|
984
|
+
* @param props.params - Params sent to the action.
|
|
985
|
+
* @param props.onDataChange - Callback invoked when the resolved data value changes.
|
|
986
|
+
* @param props.data - Controlled data value used instead of internal state.
|
|
987
|
+
* @param props.setData - Controlled setter used instead of internal state.
|
|
988
|
+
* @param props.baseUrl - Base URL override used for this wrapper instance.
|
|
989
|
+
*
|
|
990
|
+
* @example
|
|
991
|
+
* ```tsx
|
|
992
|
+
* import { FaasDataWrapper } from '@faasjs/react'
|
|
993
|
+
*
|
|
994
|
+
* type User = {
|
|
995
|
+
* name: string
|
|
996
|
+
* }
|
|
997
|
+
*
|
|
998
|
+
* function UserView(props: {
|
|
999
|
+
* data?: User
|
|
1000
|
+
* error?: Error
|
|
1001
|
+
* reload?: () => void
|
|
1002
|
+
* }) {
|
|
1003
|
+
* if (props.error) {
|
|
1004
|
+
* return (
|
|
1005
|
+
* <div>
|
|
1006
|
+
* <p>Failed to load user: {props.error.message}</p>
|
|
1007
|
+
* <button type="button" onClick={() => props.reload?.()}>
|
|
1008
|
+
* Retry
|
|
1009
|
+
* </button>
|
|
1010
|
+
* </div>
|
|
1011
|
+
* )
|
|
1012
|
+
* }
|
|
1013
|
+
*
|
|
1014
|
+
* return <div>Hello, {props.data?.name}</div>
|
|
1015
|
+
* }
|
|
1016
|
+
*
|
|
1017
|
+
* // Render-prop mode
|
|
1018
|
+
* export function UserProfile(props: { id: number }) {
|
|
1019
|
+
* return (
|
|
1020
|
+
* <FaasDataWrapper<User>
|
|
1021
|
+
* action="/pages/users/get"
|
|
1022
|
+
* params={{ id: props.id }}
|
|
1023
|
+
* fallback={<div>Loading user...</div>}
|
|
1024
|
+
* render={({ data, error, reload }) => {
|
|
1025
|
+
* if (error) {
|
|
1026
|
+
* return (
|
|
1027
|
+
* <div>
|
|
1028
|
+
* <p>Failed to load user: {error.message}</p>
|
|
1029
|
+
* <button type="button" onClick={() => reload()}>
|
|
1030
|
+
* Retry
|
|
1031
|
+
* </button>
|
|
1032
|
+
* </div>
|
|
1033
|
+
* )
|
|
1034
|
+
* }
|
|
1035
|
+
*
|
|
1036
|
+
* return <div>Hello, {data.name}</div>
|
|
1037
|
+
* }}
|
|
1038
|
+
* />
|
|
1039
|
+
* )
|
|
1040
|
+
* }
|
|
1041
|
+
*
|
|
1042
|
+
* // Children injection mode
|
|
1043
|
+
* export function UserProfileWithChildren(props: { id: number }) {
|
|
1044
|
+
* return (
|
|
1045
|
+
* <FaasDataWrapper<User>
|
|
1046
|
+
* action="/pages/users/get"
|
|
1047
|
+
* params={{ id: props.id }}
|
|
1048
|
+
* fallback={<div>Loading user...</div>}
|
|
1049
|
+
* >
|
|
1050
|
+
* <UserView />
|
|
1051
|
+
* </FaasDataWrapper>
|
|
1052
|
+
* )
|
|
1053
|
+
* }
|
|
1054
|
+
* ```
|
|
1055
|
+
*
|
|
1056
|
+
* When a ref is provided, it exposes the current Faas request state imperatively.
|
|
1057
|
+
*/
|
|
846
1058
|
declare const FaasDataWrapper: <PathOrData extends FaasActionUnionType$1 = any>(props: FaasDataWrapperProps<PathOrData> & react.RefAttributes<FaasDataWrapperRef<PathOrData>>) => React.ReactElement | null;
|
|
847
1059
|
/**
|
|
848
|
-
*
|
|
1060
|
+
* Wrap a component with {@link FaasDataWrapper} and inject Faas request state as props.
|
|
1061
|
+
*
|
|
1062
|
+
* `withFaasData` is most useful for wrapper-style exports or compatibility with
|
|
1063
|
+
* an existing component boundary. For new code, prefer `useFaas` or
|
|
1064
|
+
* `FaasDataWrapper` when they express the request ownership more directly.
|
|
1065
|
+
*
|
|
1066
|
+
* @template PathOrData - Action path or response data type used for inference.
|
|
1067
|
+
* @template TComponentProps - Component props including injected Faas data fields.
|
|
1068
|
+
* @param Component - Component that consumes injected Faas data props.
|
|
1069
|
+
* @param faasProps - Request configuration forwarded to `FaasDataWrapper`.
|
|
1070
|
+
* @returns Component that accepts the original props minus the injected Faas data fields.
|
|
849
1071
|
*
|
|
850
1072
|
* @example
|
|
851
1073
|
* ```tsx
|
|
852
|
-
*
|
|
1074
|
+
* import { withFaasData } from '@faasjs/react'
|
|
1075
|
+
*
|
|
1076
|
+
* const MyComponent = withFaasData(
|
|
1077
|
+
* ({ data, error, reload }) => {
|
|
1078
|
+
* if (error) {
|
|
1079
|
+
* return (
|
|
1080
|
+
* <button type="button" onClick={() => reload()}>
|
|
1081
|
+
* Retry
|
|
1082
|
+
* </button>
|
|
1083
|
+
* )
|
|
1084
|
+
* }
|
|
1085
|
+
*
|
|
1086
|
+
* return <div>{data.name}</div>
|
|
1087
|
+
* },
|
|
1088
|
+
* { action: '/pages/users/get', params: { id: 1 } },
|
|
1089
|
+
* )
|
|
853
1090
|
* ```
|
|
854
1091
|
*/
|
|
855
1092
|
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>>;
|
|
856
1093
|
//#endregion
|
|
857
1094
|
//#region src/useFaas.d.ts
|
|
858
1095
|
/**
|
|
859
|
-
* Options
|
|
1096
|
+
* Options that customize the {@link useFaas} request lifecycle.
|
|
1097
|
+
*
|
|
1098
|
+
* @template PathOrData - Action path or response data type used for inference.
|
|
860
1099
|
*/
|
|
861
1100
|
type useFaasOptions<PathOrData extends FaasActionUnionType$1> = {
|
|
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. */
|
|
1101
|
+
/** Override the current 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
1102
|
data?: FaasData$1<PathOrData>; /** Controlled setter that is called instead of the hook's internal `setData`. */
|
|
864
1103
|
setData?: React.Dispatch<React.SetStateAction<FaasData$1<PathOrData>>>;
|
|
865
1104
|
/**
|
|
@@ -867,35 +1106,71 @@ type useFaasOptions<PathOrData extends FaasActionUnionType$1> = {
|
|
|
867
1106
|
*
|
|
868
1107
|
* However, you can still use reload to send the request.
|
|
869
1108
|
*/
|
|
870
|
-
skip?: boolean | ((params: FaasParams$1<PathOrData>) => boolean); /**
|
|
1109
|
+
skip?: boolean | ((params: FaasParams$1<PathOrData>) => boolean); /** Delay the latest automatic request by the given number of milliseconds. */
|
|
871
1110
|
debounce?: number; /** Override the default base URL for this hook instance. */
|
|
872
1111
|
baseUrl?: BaseUrl;
|
|
873
1112
|
};
|
|
874
1113
|
/**
|
|
875
1114
|
* Request FaasJS data and keep request state in React state.
|
|
876
1115
|
*
|
|
877
|
-
* `useFaas`
|
|
878
|
-
*
|
|
1116
|
+
* `useFaas` is the default hook for standard FaasJS request-response flows in React.
|
|
1117
|
+
* It sends an initial request unless `skip` is enabled, and returns request state
|
|
1118
|
+
* plus helpers for reloading, updating data, and handling errors.
|
|
1119
|
+
*
|
|
1120
|
+
* @template PathOrData - Action path or response data type used for inference.
|
|
879
1121
|
*
|
|
880
1122
|
* @param action - Action path to invoke.
|
|
881
1123
|
* @param defaultParams - Params used for the initial request and future reloads.
|
|
882
1124
|
* @param options - Optional hook configuration such as controlled data, debounce, and skip logic.
|
|
883
|
-
* @
|
|
1125
|
+
* @param options.params - Request params override used without mutating the hook's stored params state.
|
|
1126
|
+
* @param options.data - Controlled data value used instead of the hook's internal state.
|
|
1127
|
+
* @param options.setData - Controlled setter used instead of the hook's internal `setData`.
|
|
1128
|
+
* @param options.skip - Boolean or predicate that suppresses the automatic request until `reload()` runs.
|
|
1129
|
+
* @param options.debounce - Milliseconds to wait before sending the latest request.
|
|
1130
|
+
* @param options.baseUrl - Base URL override used for this hook instance.
|
|
1131
|
+
* @returns Request state and helper methods described by {@link FaasDataInjection}.
|
|
884
1132
|
*
|
|
885
1133
|
* @example
|
|
886
1134
|
* ```tsx
|
|
887
1135
|
* import { useFaas } from '@faasjs/react'
|
|
888
1136
|
*
|
|
889
|
-
* function
|
|
890
|
-
* const { data } = useFaas
|
|
1137
|
+
* function Profile({ id }: { id: number }) {
|
|
1138
|
+
* const { data, error, loading, reload } = useFaas('/pages/users/get', { id })
|
|
1139
|
+
*
|
|
1140
|
+
* if (loading) return <div>Loading...</div>
|
|
891
1141
|
*
|
|
892
|
-
*
|
|
1142
|
+
* if (error) {
|
|
1143
|
+
* return (
|
|
1144
|
+
* <div>
|
|
1145
|
+
* <div>Load failed: {error.message}</div>
|
|
1146
|
+
* <button type="button" onClick={() => reload()}>
|
|
1147
|
+
* Retry
|
|
1148
|
+
* </button>
|
|
1149
|
+
* </div>
|
|
1150
|
+
* )
|
|
1151
|
+
* }
|
|
1152
|
+
*
|
|
1153
|
+
* return (
|
|
1154
|
+
* <div>
|
|
1155
|
+
* <span>{data.name}</span>
|
|
1156
|
+
* <button type="button" onClick={() => reload()}>
|
|
1157
|
+
* Refresh
|
|
1158
|
+
* </button>
|
|
1159
|
+
* </div>
|
|
1160
|
+
* )
|
|
893
1161
|
* }
|
|
894
1162
|
* ```
|
|
895
1163
|
*/
|
|
896
1164
|
declare function useFaas<PathOrData extends FaasActionUnionType$1>(action: FaasAction$1<PathOrData>, defaultParams: FaasParams$1<PathOrData>, options?: useFaasOptions<PathOrData>): FaasDataInjection<PathOrData>;
|
|
897
1165
|
//#endregion
|
|
898
1166
|
//#region src/client.d.ts
|
|
1167
|
+
/**
|
|
1168
|
+
* Factory for per-request error handlers used by {@link FaasReactClient}.
|
|
1169
|
+
*
|
|
1170
|
+
* @param action - Action name that failed.
|
|
1171
|
+
* @param params - Params sent with the failed request.
|
|
1172
|
+
* @returns Async callback invoked with the resulting {@link ResponseError}.
|
|
1173
|
+
*/
|
|
899
1174
|
type OnError = (action: string, params: Record<string, any>) => (res: ResponseError) => Promise<void>;
|
|
900
1175
|
/**
|
|
901
1176
|
* Options for creating a {@link FaasReactClient} instance.
|
|
@@ -908,8 +1183,15 @@ type FaasReactClientOptions = {
|
|
|
908
1183
|
*
|
|
909
1184
|
* @example
|
|
910
1185
|
* ```ts
|
|
1186
|
+
* import { ResponseError } from '@faasjs/react'
|
|
1187
|
+
*
|
|
911
1188
|
* onError: (action, params) => async (res) => {
|
|
912
|
-
*
|
|
1189
|
+
* if (res instanceof ResponseError) {
|
|
1190
|
+
* reportErrorToSentry(res, {
|
|
1191
|
+
* tags: { action },
|
|
1192
|
+
* extra: { params },
|
|
1193
|
+
* })
|
|
1194
|
+
* }
|
|
913
1195
|
* }
|
|
914
1196
|
* ```
|
|
915
1197
|
*/
|
|
@@ -919,11 +1201,11 @@ type FaasReactClientOptions = {
|
|
|
919
1201
|
* Public interface returned by {@link FaasReactClient}.
|
|
920
1202
|
*/
|
|
921
1203
|
type FaasReactClientInstance = {
|
|
922
|
-
id: string;
|
|
923
|
-
faas: typeof faas;
|
|
924
|
-
useFaas: typeof useFaas;
|
|
925
|
-
FaasDataWrapper: typeof FaasDataWrapper;
|
|
926
|
-
onError?: OnError;
|
|
1204
|
+
/** Unique identifier inherited from the underlying browser client. */id: string; /** Promise-based request helper bound to the registered base URL. */
|
|
1205
|
+
faas: typeof faas; /** Hook bound to the registered base URL. */
|
|
1206
|
+
useFaas: typeof useFaas; /** Wrapper component bound to the registered base URL. */
|
|
1207
|
+
FaasDataWrapper: typeof FaasDataWrapper; /** Optional error hook shared by `faas` and `useFaas`. */
|
|
1208
|
+
onError?: OnError; /** Underlying browser client used for the actual HTTP requests. */
|
|
927
1209
|
browserClient: FaasBrowserClient;
|
|
928
1210
|
};
|
|
929
1211
|
/**
|
|
@@ -933,37 +1215,58 @@ type FaasReactClientInstance = {
|
|
|
933
1215
|
* used by helpers such as {@link faas} and {@link useFaas}.
|
|
934
1216
|
*
|
|
935
1217
|
* @param options - Client configuration including base URL, default request options, and error hooks.
|
|
1218
|
+
* @param options.baseUrl - Base URL used to register and route the client instance.
|
|
1219
|
+
* @param options.options - Default browser-client request options forwarded to `FaasBrowserClient`.
|
|
1220
|
+
* @param options.onError - Hook factory used to handle failed `faas` and `useFaas` requests.
|
|
1221
|
+
* See {@link Options} for supported browser-client request fields such as `headers`,
|
|
1222
|
+
* `beforeRequest`, `request`, `baseUrl`, and `stream`.
|
|
936
1223
|
* @returns Registered FaasReactClient instance.
|
|
937
1224
|
*
|
|
938
1225
|
* @example
|
|
939
1226
|
* ```ts
|
|
940
|
-
* import { FaasReactClient } from '@faasjs/react'
|
|
1227
|
+
* import { FaasReactClient, ResponseError } from '@faasjs/react'
|
|
941
1228
|
*
|
|
942
1229
|
* const client = FaasReactClient({
|
|
943
1230
|
* baseUrl: 'http://localhost:8080/api/',
|
|
1231
|
+
* onError: (action, params) => async (res) => {
|
|
1232
|
+
* if (res instanceof ResponseError) {
|
|
1233
|
+
* reportErrorToSentry(res, {
|
|
1234
|
+
* tags: { action },
|
|
1235
|
+
* extra: { params },
|
|
1236
|
+
* })
|
|
1237
|
+
* }
|
|
1238
|
+
* },
|
|
944
1239
|
* })
|
|
945
1240
|
* ```
|
|
946
1241
|
*/
|
|
947
|
-
declare function FaasReactClient(
|
|
948
|
-
baseUrl,
|
|
949
|
-
options: clientOptions,
|
|
950
|
-
onError
|
|
951
|
-
}?: FaasReactClientOptions): FaasReactClientInstance;
|
|
1242
|
+
declare function FaasReactClient(options?: FaasReactClientOptions): FaasReactClientInstance;
|
|
952
1243
|
/**
|
|
953
1244
|
* Get a registered FaasReactClient instance.
|
|
954
1245
|
*
|
|
955
1246
|
* When `host` is omitted, the first registered client is returned. If no client
|
|
956
1247
|
* has been created yet, a default client is initialized automatically.
|
|
1248
|
+
* Use `getClient` only for special cases such as multiple Faas clients with
|
|
1249
|
+
* different base URLs. In normal single-client app code, prefer the default
|
|
1250
|
+
* `faas`, `useFaas`, or `FaasReactClient` setup directly.
|
|
957
1251
|
*
|
|
958
1252
|
* @param host - Registered base URL to look up. Omit it to use the default client.
|
|
959
1253
|
* @returns Registered or newly created FaasReactClient instance.
|
|
960
1254
|
*
|
|
961
1255
|
* @example
|
|
962
1256
|
* ```ts
|
|
963
|
-
* import { getClient } from '@faasjs/react'
|
|
1257
|
+
* import { FaasReactClient, getClient } from '@faasjs/react'
|
|
1258
|
+
*
|
|
1259
|
+
* FaasReactClient({
|
|
1260
|
+
* baseUrl: 'https://service-a.example.com/api/',
|
|
1261
|
+
* })
|
|
1262
|
+
*
|
|
1263
|
+
* FaasReactClient({
|
|
1264
|
+
* baseUrl: 'https://service-b.example.com/api/',
|
|
1265
|
+
* })
|
|
1266
|
+
*
|
|
1267
|
+
* const client = getClient('https://service-b.example.com/api/')
|
|
964
1268
|
*
|
|
965
|
-
*
|
|
966
|
-
* getClient('http://localhost:8080/api/')
|
|
1269
|
+
* await client.faas('/pages/posts/get', { id: 1 })
|
|
967
1270
|
* ```
|
|
968
1271
|
*/
|
|
969
1272
|
declare function getClient(host?: string): FaasReactClientInstance;
|
|
@@ -971,28 +1274,91 @@ declare function getClient(host?: string): FaasReactClientInstance;
|
|
|
971
1274
|
//#region src/constant.d.ts
|
|
972
1275
|
/**
|
|
973
1276
|
* Returns a constant value that is created by the given function.
|
|
1277
|
+
*
|
|
1278
|
+
* @template T - Constant value type returned by the initializer.
|
|
1279
|
+
* @param fn - Initializer that runs only once for the current component instance.
|
|
1280
|
+
*
|
|
1281
|
+
* @example
|
|
1282
|
+
* ```tsx
|
|
1283
|
+
* import { useConstant } from '@faasjs/react'
|
|
1284
|
+
*
|
|
1285
|
+
* function Page() {
|
|
1286
|
+
* const requestId = useConstant(() => crypto.randomUUID())
|
|
1287
|
+
*
|
|
1288
|
+
* return <span>{requestId}</span>
|
|
1289
|
+
* }
|
|
1290
|
+
* ```
|
|
974
1291
|
*/
|
|
975
1292
|
declare function useConstant<T>(fn: () => T): T;
|
|
976
1293
|
//#endregion
|
|
977
1294
|
//#region src/ErrorBoundary.d.ts
|
|
1295
|
+
/**
|
|
1296
|
+
* Props for the {@link ErrorBoundary} component.
|
|
1297
|
+
*/
|
|
978
1298
|
interface ErrorBoundaryProps {
|
|
1299
|
+
/** Descendant elements protected by the boundary. */
|
|
979
1300
|
children?: ReactNode;
|
|
1301
|
+
/** Callback invoked after a descendant throws during rendering or lifecycle work. */
|
|
980
1302
|
onError?: (error: Error | null, info: any) => void;
|
|
1303
|
+
/** Custom fallback element cloned with captured error details. */
|
|
981
1304
|
errorChildren?: ReactElement<ErrorChildrenProps>;
|
|
982
1305
|
}
|
|
1306
|
+
/**
|
|
1307
|
+
* Props injected into a custom error fallback element.
|
|
1308
|
+
*/
|
|
983
1309
|
type ErrorChildrenProps = {
|
|
984
|
-
error?: Error;
|
|
985
|
-
info?: any;
|
|
986
|
-
errorMessage?: string;
|
|
1310
|
+
/** Captured error instance. */error?: Error; /** React component stack metadata for the captured error. */
|
|
1311
|
+
info?: any; /** Stringified error message shown by the default fallback. */
|
|
1312
|
+
errorMessage?: string; /** Component stack description shown by the default fallback. */
|
|
987
1313
|
errorDescription?: string;
|
|
988
1314
|
};
|
|
1315
|
+
/**
|
|
1316
|
+
* React error boundary with an optional custom fallback element.
|
|
1317
|
+
*
|
|
1318
|
+
* The boundary renders its children until a descendant throws. After that it
|
|
1319
|
+
* either clones `errorChildren` with injected error details or renders a simple
|
|
1320
|
+
* built-in fallback.
|
|
1321
|
+
*
|
|
1322
|
+
* @example
|
|
1323
|
+
* ```tsx
|
|
1324
|
+
* import { ErrorBoundary } from '@faasjs/react'
|
|
1325
|
+
*
|
|
1326
|
+
* function Fallback({ errorMessage }: { errorMessage?: string }) {
|
|
1327
|
+
* return <div>{errorMessage}</div>
|
|
1328
|
+
* }
|
|
1329
|
+
*
|
|
1330
|
+
* <ErrorBoundary errorChildren={<Fallback />}>
|
|
1331
|
+
* <DangerousWidget />
|
|
1332
|
+
* </ErrorBoundary>
|
|
1333
|
+
* ```
|
|
1334
|
+
*/
|
|
989
1335
|
declare class ErrorBoundary extends Component<ErrorBoundaryProps, {
|
|
990
1336
|
error: Error | null;
|
|
991
1337
|
info: ErrorInfo;
|
|
992
1338
|
}> {
|
|
1339
|
+
/**
|
|
1340
|
+
* Stable display name used by React DevTools.
|
|
1341
|
+
*/
|
|
993
1342
|
static displayName: string;
|
|
1343
|
+
/**
|
|
1344
|
+
* Create an error boundary with empty error state.
|
|
1345
|
+
*
|
|
1346
|
+
* @param props - Boundary props.
|
|
1347
|
+
* @param props.children - Descendant elements protected by the boundary.
|
|
1348
|
+
* @param props.onError - Callback invoked after a render error is captured.
|
|
1349
|
+
* @param props.errorChildren - Custom fallback element that receives error details.
|
|
1350
|
+
*/
|
|
994
1351
|
constructor(props: ErrorBoundaryProps);
|
|
1352
|
+
/**
|
|
1353
|
+
* Capture rendering errors from descendant components.
|
|
1354
|
+
*
|
|
1355
|
+
* @param error - Caught render error.
|
|
1356
|
+
* @param info - React component stack metadata.
|
|
1357
|
+
*/
|
|
995
1358
|
componentDidCatch(error: Error, info: ErrorInfo): void;
|
|
1359
|
+
/**
|
|
1360
|
+
* Render children or the configured fallback for the captured error.
|
|
1361
|
+
*/
|
|
996
1362
|
render(): string | number | bigint | boolean | react_jsx_runtime0.JSX.Element | Iterable<ReactNode> | Promise<string | number | bigint | boolean | react.ReactPortal | ReactElement<unknown, string | react.JSXElementConstructor<any>> | Iterable<ReactNode> | null | undefined> | null;
|
|
997
1363
|
}
|
|
998
1364
|
//#endregion
|
|
@@ -1007,6 +1373,14 @@ declare class ErrorBoundary extends Component<ErrorBoundaryProps, {
|
|
|
1007
1373
|
* @param a - The first value to compare.
|
|
1008
1374
|
* @param b - The second value to compare.
|
|
1009
1375
|
* @returns `true` if the values are deeply equal, `false` otherwise.
|
|
1376
|
+
*
|
|
1377
|
+
* @example
|
|
1378
|
+
* ```ts
|
|
1379
|
+
* import { equal } from '@faasjs/react'
|
|
1380
|
+
*
|
|
1381
|
+
* equal({ page: 1, filters: ['a'] }, { page: 1, filters: ['a'] }) // true
|
|
1382
|
+
* equal({ page: 1 }, { page: 2 }) // false
|
|
1383
|
+
* ```
|
|
1010
1384
|
*/
|
|
1011
1385
|
declare function equal(a: any, b: any): boolean;
|
|
1012
1386
|
/**
|
|
@@ -1014,6 +1388,17 @@ declare function equal(a: any, b: any): boolean;
|
|
|
1014
1388
|
*
|
|
1015
1389
|
* @param value - The value to be memoized.
|
|
1016
1390
|
* @returns The memoized value.
|
|
1391
|
+
*
|
|
1392
|
+
* @example
|
|
1393
|
+
* ```tsx
|
|
1394
|
+
* import { useEqualMemoize } from '@faasjs/react'
|
|
1395
|
+
*
|
|
1396
|
+
* function Filters({ filters }: { filters: Record<string, any> }) {
|
|
1397
|
+
* const memoizedFilters = useEqualMemoize(filters)
|
|
1398
|
+
*
|
|
1399
|
+
* return <pre>{JSON.stringify(memoizedFilters)}</pre>
|
|
1400
|
+
* }
|
|
1401
|
+
* ```
|
|
1017
1402
|
*/
|
|
1018
1403
|
declare function useEqualMemoize(value: any): any;
|
|
1019
1404
|
/**
|
|
@@ -1022,36 +1407,89 @@ declare function useEqualMemoize(value: any): any;
|
|
|
1022
1407
|
* @param callback - The effect callback function to run.
|
|
1023
1408
|
* @param dependencies - The list of dependencies for the effect.
|
|
1024
1409
|
* @returns The result of the `useEffect` hook with memoized dependencies.
|
|
1410
|
+
*
|
|
1411
|
+
* @example
|
|
1412
|
+
* ```tsx
|
|
1413
|
+
* import { useEqualEffect } from '@faasjs/react'
|
|
1414
|
+
*
|
|
1415
|
+
* function Page({ filters }: { filters: Record<string, any> }) {
|
|
1416
|
+
* useEqualEffect(() => {
|
|
1417
|
+
* console.log('filters changed', filters)
|
|
1418
|
+
* }, [filters])
|
|
1419
|
+
*
|
|
1420
|
+
* return null
|
|
1421
|
+
* }
|
|
1422
|
+
* ```
|
|
1025
1423
|
*/
|
|
1026
1424
|
declare function useEqualEffect(callback: React.EffectCallback, dependencies: any[]): void;
|
|
1027
1425
|
/**
|
|
1028
1426
|
* Custom hook that works like `useMemo` but uses deep comparison on dependencies.
|
|
1029
1427
|
*
|
|
1428
|
+
* @template T - Memoized value type returned by the callback.
|
|
1429
|
+
*
|
|
1030
1430
|
* @param callback - The callback function to run.
|
|
1031
1431
|
* @param dependencies - The list of dependencies.
|
|
1032
1432
|
* @returns The result of the `useMemo` hook with memoized dependencies.
|
|
1433
|
+
*
|
|
1434
|
+
* @example
|
|
1435
|
+
* ```tsx
|
|
1436
|
+
* import { useEqualMemo } from '@faasjs/react'
|
|
1437
|
+
*
|
|
1438
|
+
* function Page({ filters }: { filters: Record<string, any> }) {
|
|
1439
|
+
* const queryString = useEqualMemo(() => JSON.stringify(filters), [filters])
|
|
1440
|
+
*
|
|
1441
|
+
* return <span>{queryString}</span>
|
|
1442
|
+
* }
|
|
1443
|
+
* ```
|
|
1033
1444
|
*/
|
|
1034
1445
|
declare function useEqualMemo<T>(callback: () => T, dependencies: any[]): T;
|
|
1035
1446
|
/**
|
|
1036
1447
|
* Custom hook that works like `useCallback` but uses deep comparison on dependencies.
|
|
1037
1448
|
*
|
|
1449
|
+
* @template T - Callback signature to memoize.
|
|
1450
|
+
*
|
|
1038
1451
|
* @param callback - The callback function to run.
|
|
1039
1452
|
* @param dependencies - The list of dependencies.
|
|
1040
1453
|
* @returns The result of the `useCallback` hook with memoized dependencies.
|
|
1454
|
+
*
|
|
1455
|
+
* @example
|
|
1456
|
+
* ```tsx
|
|
1457
|
+
* import { useEqualCallback } from '@faasjs/react'
|
|
1458
|
+
*
|
|
1459
|
+
* function Search({ filters }: { filters: Record<string, any> }) {
|
|
1460
|
+
* const handleSubmit = useEqualCallback(() => {
|
|
1461
|
+
* console.log(filters)
|
|
1462
|
+
* }, [filters])
|
|
1463
|
+
*
|
|
1464
|
+
* return <button onClick={handleSubmit}>Search</button>
|
|
1465
|
+
* }
|
|
1466
|
+
* ```
|
|
1041
1467
|
*/
|
|
1042
1468
|
declare function useEqualCallback<T extends (...args: any[]) => any>(callback: T, dependencies: any[]): T;
|
|
1043
1469
|
//#endregion
|
|
1044
1470
|
//#region src/OptionalWrapper.d.ts
|
|
1471
|
+
/**
|
|
1472
|
+
* Props for the {@link OptionalWrapper} helper component.
|
|
1473
|
+
*
|
|
1474
|
+
* @template TWrapper - Wrapper component type used when `condition` is true.
|
|
1475
|
+
*/
|
|
1045
1476
|
type OptionalWrapperProps<TWrapper extends ComponentType<{
|
|
1046
1477
|
children: ReactNode;
|
|
1047
1478
|
}> = any> = {
|
|
1048
|
-
condition: boolean;
|
|
1049
|
-
Wrapper: TWrapper;
|
|
1050
|
-
wrapperProps?: ComponentProps<TWrapper>;
|
|
1479
|
+
/** When `true`, render `children` inside `Wrapper`. */condition: boolean; /** Wrapper component used when `condition` passes. */
|
|
1480
|
+
Wrapper: TWrapper; /** Props forwarded to `Wrapper` together with `children`. */
|
|
1481
|
+
wrapperProps?: ComponentProps<TWrapper>; /** Content rendered directly or inside the wrapper. */
|
|
1051
1482
|
children: ReactNode;
|
|
1052
1483
|
};
|
|
1053
1484
|
/**
|
|
1054
|
-
*
|
|
1485
|
+
* Conditionally wrap children with another component.
|
|
1486
|
+
*
|
|
1487
|
+
* @param props - Wrapper condition, wrapper component, and child content.
|
|
1488
|
+
* @param props.condition - When `true`, wrap children with `Wrapper`.
|
|
1489
|
+
* @param props.Wrapper - Component used as the wrapper when the condition passes.
|
|
1490
|
+
* @param props.wrapperProps - Props forwarded to the wrapper component.
|
|
1491
|
+
* @param props.children - Content rendered directly or inside the wrapper.
|
|
1492
|
+
* @returns Wrapped children or the original children when `condition` is false.
|
|
1055
1493
|
*
|
|
1056
1494
|
* @example
|
|
1057
1495
|
* ```tsx
|
|
@@ -1068,21 +1506,22 @@ type OptionalWrapperProps<TWrapper extends ComponentType<{
|
|
|
1068
1506
|
* )
|
|
1069
1507
|
* ```
|
|
1070
1508
|
*/
|
|
1071
|
-
declare function OptionalWrapper(
|
|
1072
|
-
condition,
|
|
1073
|
-
Wrapper,
|
|
1074
|
-
wrapperProps,
|
|
1075
|
-
children
|
|
1076
|
-
}: OptionalWrapperProps): string | number | bigint | boolean | react_jsx_runtime0.JSX.Element | Iterable<ReactNode> | Promise<string | number | bigint | boolean | react.ReactPortal | react.ReactElement<unknown, string | react.JSXElementConstructor<any>> | Iterable<ReactNode> | null | undefined> | null | undefined;
|
|
1509
|
+
declare function OptionalWrapper(props: OptionalWrapperProps): string | number | bigint | boolean | react_jsx_runtime0.JSX.Element | Iterable<ReactNode> | Promise<string | number | bigint | boolean | react.ReactPortal | react.ReactElement<unknown, string | react.JSXElementConstructor<any>> | Iterable<ReactNode> | null | undefined> | null | undefined;
|
|
1077
1510
|
declare namespace OptionalWrapper {
|
|
1078
1511
|
var displayName: string;
|
|
1079
1512
|
}
|
|
1080
1513
|
//#endregion
|
|
1081
1514
|
//#region src/splittingContext.d.ts
|
|
1082
1515
|
/**
|
|
1083
|
-
*
|
|
1516
|
+
* Create a context whose keys can be consumed independently.
|
|
1084
1517
|
*
|
|
1085
|
-
*
|
|
1518
|
+
* `createSplittingContext` returns a `Provider` and a `use` hook. Each key in
|
|
1519
|
+
* the provided shape is backed by a separate React context so readers only
|
|
1520
|
+
* subscribe to the values they access.
|
|
1521
|
+
*
|
|
1522
|
+
* @template T - Context value shape exposed by the provider and hook.
|
|
1523
|
+
* @param defaultValue - Default value map or key list used to create split contexts.
|
|
1524
|
+
* @returns Provider and hook helpers for the split context.
|
|
1086
1525
|
*
|
|
1087
1526
|
* @example
|
|
1088
1527
|
* ```tsx
|
|
@@ -1143,34 +1582,36 @@ declare function createSplittingContext<T extends Record<string, any>>(defaultVa
|
|
|
1143
1582
|
* ```
|
|
1144
1583
|
*/
|
|
1145
1584
|
Provider<NewT extends T = T>(this: void, props: {
|
|
1146
|
-
value?: Partial<NewT>;
|
|
1585
|
+
/** Partial context value supplied by the caller. */value?: Partial<NewT>; /** Descendant elements that should read from the split contexts. */
|
|
1147
1586
|
children: ReactNode;
|
|
1148
1587
|
/**
|
|
1149
|
-
*
|
|
1588
|
+
* Memoization mode for `children`.
|
|
1150
1589
|
*
|
|
1151
1590
|
* @default false
|
|
1152
1591
|
*
|
|
1153
|
-
* `true
|
|
1154
|
-
*
|
|
1592
|
+
* Pass `true` to memoize without dependencies or an array to control the
|
|
1593
|
+
* deep-equality dependency list manually.
|
|
1155
1594
|
*/
|
|
1156
1595
|
memo?: true | any[];
|
|
1157
1596
|
/**
|
|
1158
|
-
*
|
|
1597
|
+
* Initial values converted into local state via `useSplittingState`.
|
|
1598
|
+
*
|
|
1599
|
+
* Each key produces both a state value and its matching setter using the
|
|
1600
|
+
* `value` / `setValue` naming convention.
|
|
1159
1601
|
*
|
|
1160
1602
|
* @example
|
|
1161
1603
|
* ```tsx
|
|
1162
|
-
* <Provider
|
|
1163
|
-
*
|
|
1164
|
-
* value: 0,
|
|
1165
|
-
* }}
|
|
1166
|
-
* >
|
|
1167
|
-
* // Children will have access to: value, setValue
|
|
1604
|
+
* <Provider initializeStates={{ value: 0 }}>
|
|
1605
|
+
* <Child />
|
|
1168
1606
|
* </Provider>
|
|
1607
|
+
*
|
|
1608
|
+
* // `Child` can read `value` and `setValue`
|
|
1609
|
+
* ```
|
|
1169
1610
|
*/
|
|
1170
1611
|
initializeStates?: Partial<NewT>;
|
|
1171
1612
|
}): ReactNode;
|
|
1172
1613
|
/**
|
|
1173
|
-
*
|
|
1614
|
+
* Hook used to read values from the splitting context.
|
|
1174
1615
|
*
|
|
1175
1616
|
* @see https://faasjs.com/doc/react/functions/createSplittingContext.html#use
|
|
1176
1617
|
*
|
|
@@ -1187,7 +1628,17 @@ declare function createSplittingContext<T extends Record<string, any>>(defaultVa
|
|
|
1187
1628
|
};
|
|
1188
1629
|
//#endregion
|
|
1189
1630
|
//#region src/splittingState.d.ts
|
|
1631
|
+
/**
|
|
1632
|
+
* Setter map generated by {@link useSplittingState} for each state key.
|
|
1633
|
+
*
|
|
1634
|
+
* @template T - Object shape whose keys receive generated setter functions.
|
|
1635
|
+
*/
|
|
1190
1636
|
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]>> };
|
|
1637
|
+
/**
|
|
1638
|
+
* State object returned by {@link useSplittingState}, including generated setters.
|
|
1639
|
+
*
|
|
1640
|
+
* @template T - Object shape returned together with generated setter functions.
|
|
1641
|
+
*/
|
|
1191
1642
|
type StatesWithSetters<T> = T & StateSetters<T>;
|
|
1192
1643
|
/**
|
|
1193
1644
|
* Create local state entries and matching setters for each key in an object.
|
|
@@ -1209,10 +1660,10 @@ declare function useSplittingState<T extends Record<string, unknown>>(initialSta
|
|
|
1209
1660
|
//#endregion
|
|
1210
1661
|
//#region src/useFaasStream.d.ts
|
|
1211
1662
|
/**
|
|
1212
|
-
* Options
|
|
1663
|
+
* Options that customize the {@link useFaasStream} request lifecycle.
|
|
1213
1664
|
*/
|
|
1214
1665
|
type UseFaasStreamOptions = {
|
|
1215
|
-
/** 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. */
|
|
1666
|
+
/** Override the current 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. */
|
|
1216
1667
|
data?: string; /** Controlled setter that is called instead of the hook's internal `setData`. */
|
|
1217
1668
|
setData?: React.Dispatch<React.SetStateAction<string>>;
|
|
1218
1669
|
/**
|
|
@@ -1220,7 +1671,7 @@ type UseFaasStreamOptions = {
|
|
|
1220
1671
|
*
|
|
1221
1672
|
* However, you can still use reload to send the request.
|
|
1222
1673
|
*/
|
|
1223
|
-
skip?: boolean | ((params: Record<string, any>) => boolean); /**
|
|
1674
|
+
skip?: boolean | ((params: Record<string, any>) => boolean); /** Delay the latest automatic request by the given number of milliseconds. */
|
|
1224
1675
|
debounce?: number; /** Override the default base URL for this hook instance. */
|
|
1225
1676
|
baseUrl?: BaseUrl;
|
|
1226
1677
|
};
|
|
@@ -1228,44 +1679,56 @@ type UseFaasStreamOptions = {
|
|
|
1228
1679
|
* Result returned by {@link useFaasStream}.
|
|
1229
1680
|
*/
|
|
1230
1681
|
type UseFaasStreamResult = {
|
|
1231
|
-
action: string;
|
|
1232
|
-
params: Record<string, any>;
|
|
1233
|
-
loading: boolean;
|
|
1234
|
-
reloadTimes: number;
|
|
1235
|
-
data: string;
|
|
1236
|
-
error: any;
|
|
1237
|
-
reload: (params?: Record<string, any>) => Promise<string>;
|
|
1238
|
-
setData: React.Dispatch<React.SetStateAction<string>>;
|
|
1239
|
-
setLoading: React.Dispatch<React.SetStateAction<boolean>>;
|
|
1682
|
+
/** Action path currently associated with the stream request. */action: string; /** Params used for the most recent request attempt. */
|
|
1683
|
+
params: Record<string, any>; /** Whether the hook is currently waiting for stream data. */
|
|
1684
|
+
loading: boolean; /** Number of times `reload()` has triggered a new request. */
|
|
1685
|
+
reloadTimes: number; /** Accumulated text decoded from the stream response. */
|
|
1686
|
+
data: string; /** Last error raised while opening or consuming the stream. */
|
|
1687
|
+
error: any; /** Trigger a new streaming request with optional params. */
|
|
1688
|
+
reload: (params?: Record<string, any>) => Promise<string>; /** Controlled or internal setter for the accumulated text. */
|
|
1689
|
+
setData: React.Dispatch<React.SetStateAction<string>>; /** Setter for the loading flag. */
|
|
1690
|
+
setLoading: React.Dispatch<React.SetStateAction<boolean>>; /** Setter for the last stream error. */
|
|
1240
1691
|
setError: React.Dispatch<React.SetStateAction<any>>;
|
|
1241
1692
|
};
|
|
1242
1693
|
/**
|
|
1243
1694
|
* Stream a FaasJS response into React state.
|
|
1244
1695
|
*
|
|
1245
|
-
*
|
|
1246
|
-
*
|
|
1696
|
+
* `useFaasStream` is the default hook for streaming FaasJS responses in React.
|
|
1697
|
+
* It sends a streaming request, appends decoded text chunks to `data`, and
|
|
1698
|
+
* exposes reload helpers for retrying the same action.
|
|
1247
1699
|
*
|
|
1248
1700
|
* @param action - Action path to invoke.
|
|
1249
1701
|
* @param defaultParams - Params used for the initial request and future reloads.
|
|
1250
1702
|
* @param options - Optional hook configuration such as controlled data, debounce, and skip logic.
|
|
1251
|
-
* @
|
|
1703
|
+
* @param options.params - Request params override used without mutating the hook's stored params state.
|
|
1704
|
+
* @param options.data - Controlled stream text used instead of the hook's internal state.
|
|
1705
|
+
* @param options.setData - Controlled setter used instead of the hook's internal `setData`.
|
|
1706
|
+
* @param options.skip - Boolean or predicate that suppresses the automatic request until `reload()` runs.
|
|
1707
|
+
* @param options.debounce - Milliseconds to wait before sending the latest request.
|
|
1708
|
+
* @param options.baseUrl - Base URL override used for this hook instance.
|
|
1709
|
+
* @returns Streaming request state and helper methods described by {@link UseFaasStreamResult}.
|
|
1252
1710
|
*
|
|
1253
1711
|
* @example
|
|
1254
1712
|
* ```tsx
|
|
1255
|
-
* import { useState } from 'react'
|
|
1256
1713
|
* import { useFaasStream } from '@faasjs/react'
|
|
1257
1714
|
*
|
|
1258
|
-
* function Chat() {
|
|
1259
|
-
* const
|
|
1260
|
-
* const { data, loading, reload } = useFaasStream('chat', { prompt })
|
|
1715
|
+
* function Chat({ prompt }: { prompt: string }) {
|
|
1716
|
+
* const { data, error, loading, reload } = useFaasStream('/pages/chat/stream', { prompt })
|
|
1261
1717
|
*
|
|
1262
|
-
* return
|
|
1263
|
-
*
|
|
1264
|
-
*
|
|
1265
|
-
*
|
|
1266
|
-
* <div>
|
|
1267
|
-
*
|
|
1268
|
-
*
|
|
1718
|
+
* if (loading) return <div>Streaming...</div>
|
|
1719
|
+
*
|
|
1720
|
+
* if (error) {
|
|
1721
|
+
* return (
|
|
1722
|
+
* <div>
|
|
1723
|
+
* <div>Stream failed: {error.message}</div>
|
|
1724
|
+
* <button type="button" onClick={() => reload()}>
|
|
1725
|
+
* Retry
|
|
1726
|
+
* </button>
|
|
1727
|
+
* </div>
|
|
1728
|
+
* )
|
|
1729
|
+
* }
|
|
1730
|
+
*
|
|
1731
|
+
* return <pre>{data}</pre>
|
|
1269
1732
|
* }
|
|
1270
1733
|
* ```
|
|
1271
1734
|
*/
|
|
@@ -1278,6 +1741,17 @@ declare function useFaasStream(action: string, defaultParams: Record<string, any
|
|
|
1278
1741
|
* @template T - The type of the value.
|
|
1279
1742
|
* @param value - The current value to track.
|
|
1280
1743
|
* @returns Previous value from the prior render, or `undefined` on the first render.
|
|
1744
|
+
*
|
|
1745
|
+
* @example
|
|
1746
|
+
* ```tsx
|
|
1747
|
+
* import { usePrevious } from '@faasjs/react'
|
|
1748
|
+
*
|
|
1749
|
+
* function Counter({ count }: { count: number }) {
|
|
1750
|
+
* const previous = usePrevious(count)
|
|
1751
|
+
*
|
|
1752
|
+
* return <span>{previous} -> {count}</span>
|
|
1753
|
+
* }
|
|
1754
|
+
* ```
|
|
1281
1755
|
*/
|
|
1282
1756
|
declare function usePrevious<T = any>(value: T): T | undefined;
|
|
1283
1757
|
//#endregion
|