@faasjs/react 8.0.0-beta.15 → 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/dist/index.cjs +102 -76
- package/dist/index.d.ts +127 -92
- package/dist/index.mjs +102 -76
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -36,9 +36,6 @@ function generateId(prefix = "", length = 18) {
|
|
|
36
36
|
* @property {T} [data] - The parsed JSON data from the response.
|
|
37
37
|
* Optional property that contains the response payload when JSON is provided.
|
|
38
38
|
*
|
|
39
|
-
* @param {ResponseProps<T>} [props] - Response properties including status, headers, body, and data.
|
|
40
|
-
* All properties are optional with sensible defaults.
|
|
41
|
-
*
|
|
42
39
|
* Notes:
|
|
43
40
|
* - status defaults to 200 if data or body is present, 204 otherwise
|
|
44
41
|
* - body is automatically populated from data if not explicitly provided
|
|
@@ -142,6 +139,12 @@ var Response = class {
|
|
|
142
139
|
headers;
|
|
143
140
|
body;
|
|
144
141
|
data;
|
|
142
|
+
/**
|
|
143
|
+
* Create a wrapped response object.
|
|
144
|
+
*
|
|
145
|
+
* @param props - Response properties including status, headers, body, and data.
|
|
146
|
+
* @returns Wrapped response instance.
|
|
147
|
+
*/
|
|
145
148
|
constructor(props = {}) {
|
|
146
149
|
this.status = props.status || (props.data || props.body ? 200 : 204);
|
|
147
150
|
this.headers = props.headers || {};
|
|
@@ -156,7 +159,6 @@ var Response = class {
|
|
|
156
159
|
* Extends the built-in Error class to provide additional information about failed requests,
|
|
157
160
|
* including HTTP status code, response headers, response body, and the original error.
|
|
158
161
|
*
|
|
159
|
-
* @class ResponseError
|
|
160
162
|
* @augments Error
|
|
161
163
|
*
|
|
162
164
|
* @property {number} status - The HTTP status code of the failed response. Defaults to 500 if not provided.
|
|
@@ -164,9 +166,6 @@ var Response = class {
|
|
|
164
166
|
* @property {any} body - The response body containing error details or the original error if available.
|
|
165
167
|
* @property {Error} [originalError] - The original Error object if this ResponseError was created from another Error.
|
|
166
168
|
*
|
|
167
|
-
* @param {string | Error | ResponseErrorProps} data - The error message, an Error object, or a ResponseErrorProps object.
|
|
168
|
-
* @param {Omit<ResponseErrorProps, 'message' | 'originalError'>} [options] - Additional options for the error (status, headers, body).
|
|
169
|
-
*
|
|
170
169
|
* @example Basic error with message
|
|
171
170
|
* ```ts
|
|
172
171
|
* throw new ResponseError('User not found')
|
|
@@ -255,6 +254,13 @@ var ResponseError = class extends Error {
|
|
|
255
254
|
headers;
|
|
256
255
|
body;
|
|
257
256
|
originalError;
|
|
257
|
+
/**
|
|
258
|
+
* Create a ResponseError from a message, Error, or structured response error payload.
|
|
259
|
+
*
|
|
260
|
+
* @param data - Error message, Error object, or structured response error props.
|
|
261
|
+
* @param options - Additional options such as status, headers, and body.
|
|
262
|
+
* @returns ResponseError instance.
|
|
263
|
+
*/
|
|
258
264
|
constructor(data, options) {
|
|
259
265
|
let props;
|
|
260
266
|
if (typeof data === "string") props = {
|
|
@@ -406,11 +412,8 @@ var FaasBrowserClient = class {
|
|
|
406
412
|
/**
|
|
407
413
|
* Creates a new FaasBrowserClient instance.
|
|
408
414
|
*
|
|
409
|
-
* @param baseUrl - Base URL for all API requests. Must end with
|
|
410
|
-
*
|
|
411
|
-
* @param options - Configuration options for the client.
|
|
412
|
-
* Supports default headers, beforeRequest hook, custom request function,
|
|
413
|
-
* baseUrl override, and streaming mode.
|
|
415
|
+
* @param baseUrl - Base URL for all API requests. Must end with `/`. Defaults to `/` for relative requests.
|
|
416
|
+
* @param options - Default request options such as headers, hooks, request override, or stream mode.
|
|
414
417
|
*
|
|
415
418
|
* @example Basic initialization
|
|
416
419
|
* ```ts
|
|
@@ -461,7 +464,7 @@ var FaasBrowserClient = class {
|
|
|
461
464
|
* })
|
|
462
465
|
* ```
|
|
463
466
|
*
|
|
464
|
-
* @throws {Error} When baseUrl does not end with
|
|
467
|
+
* @throws {Error} When `baseUrl` does not end with `/`
|
|
465
468
|
*/
|
|
466
469
|
constructor(baseUrl = "/", options = Object.create(null)) {
|
|
467
470
|
if (baseUrl && !baseUrl.endsWith("/")) throw Error("[FaasJS] baseUrl should end with /");
|
|
@@ -486,7 +489,7 @@ var FaasBrowserClient = class {
|
|
|
486
489
|
*
|
|
487
490
|
* @throws {Error} When action is not provided or is empty
|
|
488
491
|
* @throws {ResponseError} When the server returns an error response (status >= 400 or body.error exists)
|
|
489
|
-
* @throws {
|
|
492
|
+
* @throws {Error} When the request fails before a response is received
|
|
490
493
|
*
|
|
491
494
|
* Notes:
|
|
492
495
|
* - All requests are POST requests by default
|
|
@@ -540,11 +543,7 @@ var FaasBrowserClient = class {
|
|
|
540
543
|
* email: string
|
|
541
544
|
* }
|
|
542
545
|
*
|
|
543
|
-
* const response = await client.action<{
|
|
544
|
-
* action: 'user'
|
|
545
|
-
* params: { id: number }
|
|
546
|
-
* data: UserData
|
|
547
|
-
* }>('user', { id: 123 })
|
|
546
|
+
* const response = await client.action<UserData>('user', { id: 123 })
|
|
548
547
|
* console.log(response.data.name) // TypeScript knows it's a string
|
|
549
548
|
* ```
|
|
550
549
|
*
|
|
@@ -556,7 +555,7 @@ var FaasBrowserClient = class {
|
|
|
556
555
|
* } catch (error) {
|
|
557
556
|
* if (error instanceof ResponseError) {
|
|
558
557
|
* console.error(`Server error: ${error.message}`, error.status)
|
|
559
|
-
* if (error.
|
|
558
|
+
* if (error.body) console.error('Error details:', error.body)
|
|
560
559
|
* } else {
|
|
561
560
|
* console.error('Network error:', error)
|
|
562
561
|
* }
|
|
@@ -662,17 +661,20 @@ var FaasBrowserClient = class {
|
|
|
662
661
|
//#endregion
|
|
663
662
|
//#region src/faas.ts
|
|
664
663
|
/**
|
|
665
|
-
*
|
|
664
|
+
* Call the currently configured FaasReactClient.
|
|
666
665
|
*
|
|
667
|
-
* @param action
|
|
668
|
-
* @param params
|
|
669
|
-
* @
|
|
666
|
+
* @param action - Action path to invoke.
|
|
667
|
+
* @param params - Parameters sent to the action.
|
|
668
|
+
* @param options - Optional per-request overrides such as headers or base URL.
|
|
669
|
+
* @returns Response returned by the active browser client.
|
|
670
670
|
*
|
|
671
671
|
* @example
|
|
672
672
|
* ```ts
|
|
673
|
-
*
|
|
674
|
-
*
|
|
675
|
-
* })
|
|
673
|
+
* import { faas } from '@faasjs/react'
|
|
674
|
+
*
|
|
675
|
+
* const response = await faas<{ title: string }>('post/get', { id: 1 })
|
|
676
|
+
*
|
|
677
|
+
* console.log(response.data.title)
|
|
676
678
|
* ```
|
|
677
679
|
*/
|
|
678
680
|
async function faas(action, params, options) {
|
|
@@ -831,16 +833,23 @@ function withFaasData(Component, faasProps) {
|
|
|
831
833
|
//#endregion
|
|
832
834
|
//#region src/useFaas.tsx
|
|
833
835
|
/**
|
|
834
|
-
* Request
|
|
836
|
+
* Request FaasJS data and keep request state in React state.
|
|
835
837
|
*
|
|
836
|
-
*
|
|
837
|
-
*
|
|
838
|
-
*
|
|
838
|
+
* `useFaas` sends an initial request unless `skip` is enabled, and returns
|
|
839
|
+
* request state plus helpers for reloading, updating data, and handling errors.
|
|
840
|
+
*
|
|
841
|
+
* @param action - Action path to invoke.
|
|
842
|
+
* @param defaultParams - Params used for the initial request and future reloads.
|
|
843
|
+
* @param options - Optional hook configuration such as controlled data, debounce, and skip logic.
|
|
844
|
+
* @returns Request state and helper methods for the action.
|
|
839
845
|
*
|
|
840
846
|
* @example
|
|
841
847
|
* ```tsx
|
|
842
|
-
*
|
|
848
|
+
* import { useFaas } from '@faasjs/react'
|
|
849
|
+
*
|
|
850
|
+
* function Post({ id }: { id: number }) {
|
|
843
851
|
* const { data } = useFaas<{ title: string }>('post/get', { id })
|
|
852
|
+
*
|
|
844
853
|
* return <h1>{data.title}</h1>
|
|
845
854
|
* }
|
|
846
855
|
* ```
|
|
@@ -960,14 +969,20 @@ function useFaas(action, defaultParams, options = {}) {
|
|
|
960
969
|
//#region src/client.tsx
|
|
961
970
|
const clients = {};
|
|
962
971
|
/**
|
|
963
|
-
*
|
|
972
|
+
* Create and register a FaasReactClient instance.
|
|
973
|
+
*
|
|
974
|
+
* The returned client is stored by `baseUrl` and becomes the default client
|
|
975
|
+
* used by helpers such as {@link faas} and {@link useFaas}.
|
|
964
976
|
*
|
|
965
|
-
* @
|
|
977
|
+
* @param options - Client configuration including base URL, default request options, and error hooks.
|
|
978
|
+
* @returns Registered FaasReactClient instance.
|
|
966
979
|
*
|
|
967
980
|
* @example
|
|
968
981
|
* ```ts
|
|
982
|
+
* import { FaasReactClient } from '@faasjs/react'
|
|
983
|
+
*
|
|
969
984
|
* const client = FaasReactClient({
|
|
970
|
-
* baseUrl: 'localhost:8080/api/'
|
|
985
|
+
* baseUrl: 'http://localhost:8080/api/',
|
|
971
986
|
* })
|
|
972
987
|
* ```
|
|
973
988
|
*/
|
|
@@ -996,16 +1011,20 @@ function FaasReactClient({ baseUrl, options: clientOptions, onError } = { baseUr
|
|
|
996
1011
|
return reactClient;
|
|
997
1012
|
}
|
|
998
1013
|
/**
|
|
999
|
-
* Get FaasReactClient instance
|
|
1014
|
+
* Get a registered FaasReactClient instance.
|
|
1015
|
+
*
|
|
1016
|
+
* When `host` is omitted, the first registered client is returned. If no client
|
|
1017
|
+
* has been created yet, a default client is initialized automatically.
|
|
1000
1018
|
*
|
|
1001
|
-
* @param host
|
|
1002
|
-
* @returns
|
|
1019
|
+
* @param host - Registered base URL to look up. Omit it to use the default client.
|
|
1020
|
+
* @returns Registered or newly created FaasReactClient instance.
|
|
1003
1021
|
*
|
|
1004
1022
|
* @example
|
|
1005
1023
|
* ```ts
|
|
1024
|
+
* import { getClient } from '@faasjs/react'
|
|
1025
|
+
*
|
|
1006
1026
|
* getClient()
|
|
1007
|
-
*
|
|
1008
|
-
* getClient('another-host')
|
|
1027
|
+
* getClient('http://localhost:8080/api/')
|
|
1009
1028
|
* ```
|
|
1010
1029
|
*/
|
|
1011
1030
|
function getClient(host) {
|
|
@@ -1066,23 +1085,25 @@ var ErrorBoundary = class extends react.Component {
|
|
|
1066
1085
|
* Custom hook that returns a stateful value and a ref to that value.
|
|
1067
1086
|
*
|
|
1068
1087
|
* @template T - The type of the value.
|
|
1069
|
-
* @param
|
|
1070
|
-
* @returns
|
|
1088
|
+
* @param initialValue - Initial state value. When omitted, state starts as `null`.
|
|
1089
|
+
* @returns Tuple containing the current state, the state setter, and a ref that always points at the latest state.
|
|
1071
1090
|
*
|
|
1072
1091
|
* @example
|
|
1073
1092
|
* ```tsx
|
|
1074
1093
|
* import { useStateRef } from '@faasjs/react'
|
|
1075
1094
|
*
|
|
1076
1095
|
* function MyComponent() {
|
|
1077
|
-
*
|
|
1078
|
-
*
|
|
1079
|
-
*
|
|
1080
|
-
*
|
|
1081
|
-
*
|
|
1082
|
-
*
|
|
1083
|
-
*
|
|
1084
|
-
*
|
|
1085
|
-
*
|
|
1096
|
+
* const [value, setValue, ref] = useStateRef(0)
|
|
1097
|
+
*
|
|
1098
|
+
* return (
|
|
1099
|
+
* <div>
|
|
1100
|
+
* <p>Value: {value}</p>
|
|
1101
|
+
* <button onClick={() => setValue(value + 1)}>Increment</button>
|
|
1102
|
+
* <button onClick={() => console.log(ref.current)}>Submit</button>
|
|
1103
|
+
* </div>
|
|
1104
|
+
* )
|
|
1105
|
+
* }
|
|
1106
|
+
* ```
|
|
1086
1107
|
*/
|
|
1087
1108
|
function useStateRef(initialValue) {
|
|
1088
1109
|
const [state, setState] = (0, react.useState)(initialValue ?? null);
|
|
@@ -1099,15 +1120,16 @@ function useStateRef(initialValue) {
|
|
|
1099
1120
|
//#endregion
|
|
1100
1121
|
//#region src/splittingState.tsx
|
|
1101
1122
|
/**
|
|
1102
|
-
*
|
|
1123
|
+
* Create local state entries and matching setters for each key in an object.
|
|
1103
1124
|
*
|
|
1104
1125
|
* @template T - A generic type that extends a record with string keys and any values.
|
|
1105
|
-
* @param
|
|
1126
|
+
* @param initialStates - Object whose keys become state values and `setXxx` setters.
|
|
1127
|
+
* @returns Object containing the original keys plus generated setter functions.
|
|
1106
1128
|
*
|
|
1107
1129
|
* @example
|
|
1108
1130
|
* ```tsx
|
|
1109
1131
|
* function Counter() {
|
|
1110
|
-
* const { count, setCount, name, setName } = useSplittingState({ count: 0, name: 'John' })
|
|
1132
|
+
* const { count, setCount, name, setName } = useSplittingState({ count: 0, name: 'John' })
|
|
1111
1133
|
*
|
|
1112
1134
|
* return <>{name}: {count}</>
|
|
1113
1135
|
* }
|
|
@@ -1402,32 +1424,29 @@ function mergeValues(items, defaultValues = {}) {
|
|
|
1402
1424
|
return values;
|
|
1403
1425
|
}
|
|
1404
1426
|
/**
|
|
1405
|
-
*
|
|
1406
|
-
*
|
|
1427
|
+
* Render a form with context, default elements, and validation state.
|
|
1428
|
+
*
|
|
1429
|
+
* `FormContainer` merges provided elements, language strings, and rules with
|
|
1430
|
+
* the package defaults, then exposes them through form context.
|
|
1407
1431
|
*
|
|
1408
1432
|
* @template Values - The type of form values, defaults to Record<string, any>.
|
|
1409
1433
|
* @template FormElements - The type of form elements, defaults to FormElementTypes.
|
|
1410
1434
|
* @template Rules - The type of form rules, defaults to FormDefaultRules.
|
|
1411
|
-
*
|
|
1412
|
-
* @
|
|
1413
|
-
* @param {Values} props.defaultValues - The default values for the form fields.
|
|
1414
|
-
* @param {FormElements} props.Elements - The form elements to be used in the form.
|
|
1415
|
-
* @param {Rules} props.rules - The validation rules for the form fields.
|
|
1416
|
-
* @param {FormLang} props.lang - The language settings for the form.
|
|
1417
|
-
* @param {Partial<FormContextProps>} props - Additional properties for the form context.
|
|
1418
|
-
*
|
|
1419
|
-
* @returns {JSX.Element} The FormContainer component.
|
|
1435
|
+
* @param props - Form items and optional overrides for defaults, language, rules, and submit behavior.
|
|
1436
|
+
* @returns React form container with shared form context.
|
|
1420
1437
|
*
|
|
1421
1438
|
* @example
|
|
1422
1439
|
* ```tsx
|
|
1423
1440
|
* import { Form } from '@faasjs/react'
|
|
1424
1441
|
*
|
|
1425
1442
|
* function MyForm() {
|
|
1426
|
-
* return
|
|
1427
|
-
*
|
|
1428
|
-
* {
|
|
1429
|
-
*
|
|
1430
|
-
*
|
|
1443
|
+
* return (
|
|
1444
|
+
* <Form
|
|
1445
|
+
* items={[
|
|
1446
|
+
* { name: 'name' },
|
|
1447
|
+
* ]}
|
|
1448
|
+
* />
|
|
1449
|
+
* )
|
|
1431
1450
|
* }
|
|
1432
1451
|
* ```
|
|
1433
1452
|
*/
|
|
@@ -1484,14 +1503,21 @@ OptionalWrapper.displayName = "OptionalWrapper";
|
|
|
1484
1503
|
//#endregion
|
|
1485
1504
|
//#region src/useFaasStream.tsx
|
|
1486
1505
|
/**
|
|
1487
|
-
* Stream
|
|
1506
|
+
* Stream a FaasJS response into React state.
|
|
1488
1507
|
*
|
|
1489
|
-
*
|
|
1490
|
-
*
|
|
1491
|
-
*
|
|
1508
|
+
* The hook sends a streaming request, appends decoded text chunks to `data`,
|
|
1509
|
+
* and exposes reload helpers for retrying the same action.
|
|
1510
|
+
*
|
|
1511
|
+
* @param action - Action path to invoke.
|
|
1512
|
+
* @param defaultParams - Params used for the initial request and future reloads.
|
|
1513
|
+
* @param options - Optional hook configuration such as controlled data, debounce, and skip logic.
|
|
1514
|
+
* @returns Streaming request state and helper methods.
|
|
1492
1515
|
*
|
|
1493
1516
|
* @example
|
|
1494
1517
|
* ```tsx
|
|
1518
|
+
* import { useState } from 'react'
|
|
1519
|
+
* import { useFaasStream } from '@faasjs/react'
|
|
1520
|
+
*
|
|
1495
1521
|
* function Chat() {
|
|
1496
1522
|
* const [prompt, setPrompt] = useState('')
|
|
1497
1523
|
* const { data, loading, reload } = useFaasStream('chat', { prompt })
|
|
@@ -1632,8 +1658,8 @@ function useFaasStream(action, defaultParams, options = {}) {
|
|
|
1632
1658
|
* Hook to store the previous value of a state or prop.
|
|
1633
1659
|
*
|
|
1634
1660
|
* @template T - The type of the value.
|
|
1635
|
-
* @param
|
|
1636
|
-
* @returns
|
|
1661
|
+
* @param value - The current value to track.
|
|
1662
|
+
* @returns Previous value from the prior render, or `undefined` on the first render.
|
|
1637
1663
|
*/
|
|
1638
1664
|
function usePrevious(value) {
|
|
1639
1665
|
const ref = (0, react.useRef)(void 0);
|
package/dist/index.d.ts
CHANGED
|
@@ -125,11 +125,10 @@ 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
|
|
@@ -196,9 +195,6 @@ 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
|
-
* @param {ResponseProps<T>} [props] - Response properties including status, headers, body, and data.
|
|
200
|
-
* All properties are optional with sensible defaults.
|
|
201
|
-
*
|
|
202
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
|
|
@@ -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,7 +319,6 @@ 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
|
-
* @class ResponseError
|
|
321
322
|
* @augments Error
|
|
322
323
|
*
|
|
323
324
|
* @property {number} status - The HTTP status code of the failed response. Defaults to 500 if not provided.
|
|
@@ -325,9 +326,6 @@ type ResponseErrorProps = {
|
|
|
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')
|
|
@@ -425,18 +423,18 @@ 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
|
|
@@ -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,7 +699,7 @@ 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
|
|
@@ -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.
|
|
919
931
|
*
|
|
920
|
-
*
|
|
932
|
+
* The returned client is stored by `baseUrl` and becomes the default client
|
|
933
|
+
* used by helpers such as {@link faas} and {@link useFaas}.
|
|
934
|
+
*
|
|
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.
|
|
936
954
|
*
|
|
937
|
-
*
|
|
938
|
-
*
|
|
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.
|
|
957
|
+
*
|
|
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
|
*/
|
|
@@ -1385,15 +1404,16 @@ declare function createSplittingContext<T extends Record<string, any>>(defaultVa
|
|
|
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,23 +1500,25 @@ 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
|
package/dist/index.mjs
CHANGED
|
@@ -35,9 +35,6 @@ function generateId(prefix = "", length = 18) {
|
|
|
35
35
|
* @property {T} [data] - The parsed JSON data from the response.
|
|
36
36
|
* Optional property that contains the response payload when JSON is provided.
|
|
37
37
|
*
|
|
38
|
-
* @param {ResponseProps<T>} [props] - Response properties including status, headers, body, and data.
|
|
39
|
-
* All properties are optional with sensible defaults.
|
|
40
|
-
*
|
|
41
38
|
* Notes:
|
|
42
39
|
* - status defaults to 200 if data or body is present, 204 otherwise
|
|
43
40
|
* - body is automatically populated from data if not explicitly provided
|
|
@@ -141,6 +138,12 @@ var Response = class {
|
|
|
141
138
|
headers;
|
|
142
139
|
body;
|
|
143
140
|
data;
|
|
141
|
+
/**
|
|
142
|
+
* Create a wrapped response object.
|
|
143
|
+
*
|
|
144
|
+
* @param props - Response properties including status, headers, body, and data.
|
|
145
|
+
* @returns Wrapped response instance.
|
|
146
|
+
*/
|
|
144
147
|
constructor(props = {}) {
|
|
145
148
|
this.status = props.status || (props.data || props.body ? 200 : 204);
|
|
146
149
|
this.headers = props.headers || {};
|
|
@@ -155,7 +158,6 @@ var Response = class {
|
|
|
155
158
|
* Extends the built-in Error class to provide additional information about failed requests,
|
|
156
159
|
* including HTTP status code, response headers, response body, and the original error.
|
|
157
160
|
*
|
|
158
|
-
* @class ResponseError
|
|
159
161
|
* @augments Error
|
|
160
162
|
*
|
|
161
163
|
* @property {number} status - The HTTP status code of the failed response. Defaults to 500 if not provided.
|
|
@@ -163,9 +165,6 @@ var Response = class {
|
|
|
163
165
|
* @property {any} body - The response body containing error details or the original error if available.
|
|
164
166
|
* @property {Error} [originalError] - The original Error object if this ResponseError was created from another Error.
|
|
165
167
|
*
|
|
166
|
-
* @param {string | Error | ResponseErrorProps} data - The error message, an Error object, or a ResponseErrorProps object.
|
|
167
|
-
* @param {Omit<ResponseErrorProps, 'message' | 'originalError'>} [options] - Additional options for the error (status, headers, body).
|
|
168
|
-
*
|
|
169
168
|
* @example Basic error with message
|
|
170
169
|
* ```ts
|
|
171
170
|
* throw new ResponseError('User not found')
|
|
@@ -254,6 +253,13 @@ var ResponseError = class extends Error {
|
|
|
254
253
|
headers;
|
|
255
254
|
body;
|
|
256
255
|
originalError;
|
|
256
|
+
/**
|
|
257
|
+
* Create a ResponseError from a message, Error, or structured response error payload.
|
|
258
|
+
*
|
|
259
|
+
* @param data - Error message, Error object, or structured response error props.
|
|
260
|
+
* @param options - Additional options such as status, headers, and body.
|
|
261
|
+
* @returns ResponseError instance.
|
|
262
|
+
*/
|
|
257
263
|
constructor(data, options) {
|
|
258
264
|
let props;
|
|
259
265
|
if (typeof data === "string") props = {
|
|
@@ -405,11 +411,8 @@ var FaasBrowserClient = class {
|
|
|
405
411
|
/**
|
|
406
412
|
* Creates a new FaasBrowserClient instance.
|
|
407
413
|
*
|
|
408
|
-
* @param baseUrl - Base URL for all API requests. Must end with
|
|
409
|
-
*
|
|
410
|
-
* @param options - Configuration options for the client.
|
|
411
|
-
* Supports default headers, beforeRequest hook, custom request function,
|
|
412
|
-
* baseUrl override, and streaming mode.
|
|
414
|
+
* @param baseUrl - Base URL for all API requests. Must end with `/`. Defaults to `/` for relative requests.
|
|
415
|
+
* @param options - Default request options such as headers, hooks, request override, or stream mode.
|
|
413
416
|
*
|
|
414
417
|
* @example Basic initialization
|
|
415
418
|
* ```ts
|
|
@@ -460,7 +463,7 @@ var FaasBrowserClient = class {
|
|
|
460
463
|
* })
|
|
461
464
|
* ```
|
|
462
465
|
*
|
|
463
|
-
* @throws {Error} When baseUrl does not end with
|
|
466
|
+
* @throws {Error} When `baseUrl` does not end with `/`
|
|
464
467
|
*/
|
|
465
468
|
constructor(baseUrl = "/", options = Object.create(null)) {
|
|
466
469
|
if (baseUrl && !baseUrl.endsWith("/")) throw Error("[FaasJS] baseUrl should end with /");
|
|
@@ -485,7 +488,7 @@ var FaasBrowserClient = class {
|
|
|
485
488
|
*
|
|
486
489
|
* @throws {Error} When action is not provided or is empty
|
|
487
490
|
* @throws {ResponseError} When the server returns an error response (status >= 400 or body.error exists)
|
|
488
|
-
* @throws {
|
|
491
|
+
* @throws {Error} When the request fails before a response is received
|
|
489
492
|
*
|
|
490
493
|
* Notes:
|
|
491
494
|
* - All requests are POST requests by default
|
|
@@ -539,11 +542,7 @@ var FaasBrowserClient = class {
|
|
|
539
542
|
* email: string
|
|
540
543
|
* }
|
|
541
544
|
*
|
|
542
|
-
* const response = await client.action<{
|
|
543
|
-
* action: 'user'
|
|
544
|
-
* params: { id: number }
|
|
545
|
-
* data: UserData
|
|
546
|
-
* }>('user', { id: 123 })
|
|
545
|
+
* const response = await client.action<UserData>('user', { id: 123 })
|
|
547
546
|
* console.log(response.data.name) // TypeScript knows it's a string
|
|
548
547
|
* ```
|
|
549
548
|
*
|
|
@@ -555,7 +554,7 @@ var FaasBrowserClient = class {
|
|
|
555
554
|
* } catch (error) {
|
|
556
555
|
* if (error instanceof ResponseError) {
|
|
557
556
|
* console.error(`Server error: ${error.message}`, error.status)
|
|
558
|
-
* if (error.
|
|
557
|
+
* if (error.body) console.error('Error details:', error.body)
|
|
559
558
|
* } else {
|
|
560
559
|
* console.error('Network error:', error)
|
|
561
560
|
* }
|
|
@@ -661,17 +660,20 @@ var FaasBrowserClient = class {
|
|
|
661
660
|
//#endregion
|
|
662
661
|
//#region src/faas.ts
|
|
663
662
|
/**
|
|
664
|
-
*
|
|
663
|
+
* Call the currently configured FaasReactClient.
|
|
665
664
|
*
|
|
666
|
-
* @param action
|
|
667
|
-
* @param params
|
|
668
|
-
* @
|
|
665
|
+
* @param action - Action path to invoke.
|
|
666
|
+
* @param params - Parameters sent to the action.
|
|
667
|
+
* @param options - Optional per-request overrides such as headers or base URL.
|
|
668
|
+
* @returns Response returned by the active browser client.
|
|
669
669
|
*
|
|
670
670
|
* @example
|
|
671
671
|
* ```ts
|
|
672
|
-
*
|
|
673
|
-
*
|
|
674
|
-
* })
|
|
672
|
+
* import { faas } from '@faasjs/react'
|
|
673
|
+
*
|
|
674
|
+
* const response = await faas<{ title: string }>('post/get', { id: 1 })
|
|
675
|
+
*
|
|
676
|
+
* console.log(response.data.title)
|
|
675
677
|
* ```
|
|
676
678
|
*/
|
|
677
679
|
async function faas(action, params, options) {
|
|
@@ -827,16 +829,23 @@ function withFaasData(Component, faasProps) {
|
|
|
827
829
|
//#endregion
|
|
828
830
|
//#region src/useFaas.tsx
|
|
829
831
|
/**
|
|
830
|
-
* Request
|
|
832
|
+
* Request FaasJS data and keep request state in React state.
|
|
831
833
|
*
|
|
832
|
-
*
|
|
833
|
-
*
|
|
834
|
-
*
|
|
834
|
+
* `useFaas` sends an initial request unless `skip` is enabled, and returns
|
|
835
|
+
* request state plus helpers for reloading, updating data, and handling errors.
|
|
836
|
+
*
|
|
837
|
+
* @param action - Action path to invoke.
|
|
838
|
+
* @param defaultParams - Params used for the initial request and future reloads.
|
|
839
|
+
* @param options - Optional hook configuration such as controlled data, debounce, and skip logic.
|
|
840
|
+
* @returns Request state and helper methods for the action.
|
|
835
841
|
*
|
|
836
842
|
* @example
|
|
837
843
|
* ```tsx
|
|
838
|
-
*
|
|
844
|
+
* import { useFaas } from '@faasjs/react'
|
|
845
|
+
*
|
|
846
|
+
* function Post({ id }: { id: number }) {
|
|
839
847
|
* const { data } = useFaas<{ title: string }>('post/get', { id })
|
|
848
|
+
*
|
|
840
849
|
* return <h1>{data.title}</h1>
|
|
841
850
|
* }
|
|
842
851
|
* ```
|
|
@@ -956,14 +965,20 @@ function useFaas(action, defaultParams, options = {}) {
|
|
|
956
965
|
//#region src/client.tsx
|
|
957
966
|
const clients = {};
|
|
958
967
|
/**
|
|
959
|
-
*
|
|
968
|
+
* Create and register a FaasReactClient instance.
|
|
969
|
+
*
|
|
970
|
+
* The returned client is stored by `baseUrl` and becomes the default client
|
|
971
|
+
* used by helpers such as {@link faas} and {@link useFaas}.
|
|
960
972
|
*
|
|
961
|
-
* @
|
|
973
|
+
* @param options - Client configuration including base URL, default request options, and error hooks.
|
|
974
|
+
* @returns Registered FaasReactClient instance.
|
|
962
975
|
*
|
|
963
976
|
* @example
|
|
964
977
|
* ```ts
|
|
978
|
+
* import { FaasReactClient } from '@faasjs/react'
|
|
979
|
+
*
|
|
965
980
|
* const client = FaasReactClient({
|
|
966
|
-
* baseUrl: 'localhost:8080/api/'
|
|
981
|
+
* baseUrl: 'http://localhost:8080/api/',
|
|
967
982
|
* })
|
|
968
983
|
* ```
|
|
969
984
|
*/
|
|
@@ -992,16 +1007,20 @@ function FaasReactClient({ baseUrl, options: clientOptions, onError } = { baseUr
|
|
|
992
1007
|
return reactClient;
|
|
993
1008
|
}
|
|
994
1009
|
/**
|
|
995
|
-
* Get FaasReactClient instance
|
|
1010
|
+
* Get a registered FaasReactClient instance.
|
|
1011
|
+
*
|
|
1012
|
+
* When `host` is omitted, the first registered client is returned. If no client
|
|
1013
|
+
* has been created yet, a default client is initialized automatically.
|
|
996
1014
|
*
|
|
997
|
-
* @param host
|
|
998
|
-
* @returns
|
|
1015
|
+
* @param host - Registered base URL to look up. Omit it to use the default client.
|
|
1016
|
+
* @returns Registered or newly created FaasReactClient instance.
|
|
999
1017
|
*
|
|
1000
1018
|
* @example
|
|
1001
1019
|
* ```ts
|
|
1020
|
+
* import { getClient } from '@faasjs/react'
|
|
1021
|
+
*
|
|
1002
1022
|
* getClient()
|
|
1003
|
-
*
|
|
1004
|
-
* getClient('another-host')
|
|
1023
|
+
* getClient('http://localhost:8080/api/')
|
|
1005
1024
|
* ```
|
|
1006
1025
|
*/
|
|
1007
1026
|
function getClient(host) {
|
|
@@ -1062,23 +1081,25 @@ var ErrorBoundary = class extends Component {
|
|
|
1062
1081
|
* Custom hook that returns a stateful value and a ref to that value.
|
|
1063
1082
|
*
|
|
1064
1083
|
* @template T - The type of the value.
|
|
1065
|
-
* @param
|
|
1066
|
-
* @returns
|
|
1084
|
+
* @param initialValue - Initial state value. When omitted, state starts as `null`.
|
|
1085
|
+
* @returns Tuple containing the current state, the state setter, and a ref that always points at the latest state.
|
|
1067
1086
|
*
|
|
1068
1087
|
* @example
|
|
1069
1088
|
* ```tsx
|
|
1070
1089
|
* import { useStateRef } from '@faasjs/react'
|
|
1071
1090
|
*
|
|
1072
1091
|
* function MyComponent() {
|
|
1073
|
-
*
|
|
1074
|
-
*
|
|
1075
|
-
*
|
|
1076
|
-
*
|
|
1077
|
-
*
|
|
1078
|
-
*
|
|
1079
|
-
*
|
|
1080
|
-
*
|
|
1081
|
-
*
|
|
1092
|
+
* const [value, setValue, ref] = useStateRef(0)
|
|
1093
|
+
*
|
|
1094
|
+
* return (
|
|
1095
|
+
* <div>
|
|
1096
|
+
* <p>Value: {value}</p>
|
|
1097
|
+
* <button onClick={() => setValue(value + 1)}>Increment</button>
|
|
1098
|
+
* <button onClick={() => console.log(ref.current)}>Submit</button>
|
|
1099
|
+
* </div>
|
|
1100
|
+
* )
|
|
1101
|
+
* }
|
|
1102
|
+
* ```
|
|
1082
1103
|
*/
|
|
1083
1104
|
function useStateRef(initialValue) {
|
|
1084
1105
|
const [state, setState] = useState(initialValue ?? null);
|
|
@@ -1095,15 +1116,16 @@ function useStateRef(initialValue) {
|
|
|
1095
1116
|
//#endregion
|
|
1096
1117
|
//#region src/splittingState.tsx
|
|
1097
1118
|
/**
|
|
1098
|
-
*
|
|
1119
|
+
* Create local state entries and matching setters for each key in an object.
|
|
1099
1120
|
*
|
|
1100
1121
|
* @template T - A generic type that extends a record with string keys and any values.
|
|
1101
|
-
* @param
|
|
1122
|
+
* @param initialStates - Object whose keys become state values and `setXxx` setters.
|
|
1123
|
+
* @returns Object containing the original keys plus generated setter functions.
|
|
1102
1124
|
*
|
|
1103
1125
|
* @example
|
|
1104
1126
|
* ```tsx
|
|
1105
1127
|
* function Counter() {
|
|
1106
|
-
* const { count, setCount, name, setName } = useSplittingState({ count: 0, name: 'John' })
|
|
1128
|
+
* const { count, setCount, name, setName } = useSplittingState({ count: 0, name: 'John' })
|
|
1107
1129
|
*
|
|
1108
1130
|
* return <>{name}: {count}</>
|
|
1109
1131
|
* }
|
|
@@ -1398,32 +1420,29 @@ function mergeValues(items, defaultValues = {}) {
|
|
|
1398
1420
|
return values;
|
|
1399
1421
|
}
|
|
1400
1422
|
/**
|
|
1401
|
-
*
|
|
1402
|
-
*
|
|
1423
|
+
* Render a form with context, default elements, and validation state.
|
|
1424
|
+
*
|
|
1425
|
+
* `FormContainer` merges provided elements, language strings, and rules with
|
|
1426
|
+
* the package defaults, then exposes them through form context.
|
|
1403
1427
|
*
|
|
1404
1428
|
* @template Values - The type of form values, defaults to Record<string, any>.
|
|
1405
1429
|
* @template FormElements - The type of form elements, defaults to FormElementTypes.
|
|
1406
1430
|
* @template Rules - The type of form rules, defaults to FormDefaultRules.
|
|
1407
|
-
*
|
|
1408
|
-
* @
|
|
1409
|
-
* @param {Values} props.defaultValues - The default values for the form fields.
|
|
1410
|
-
* @param {FormElements} props.Elements - The form elements to be used in the form.
|
|
1411
|
-
* @param {Rules} props.rules - The validation rules for the form fields.
|
|
1412
|
-
* @param {FormLang} props.lang - The language settings for the form.
|
|
1413
|
-
* @param {Partial<FormContextProps>} props - Additional properties for the form context.
|
|
1414
|
-
*
|
|
1415
|
-
* @returns {JSX.Element} The FormContainer component.
|
|
1431
|
+
* @param props - Form items and optional overrides for defaults, language, rules, and submit behavior.
|
|
1432
|
+
* @returns React form container with shared form context.
|
|
1416
1433
|
*
|
|
1417
1434
|
* @example
|
|
1418
1435
|
* ```tsx
|
|
1419
1436
|
* import { Form } from '@faasjs/react'
|
|
1420
1437
|
*
|
|
1421
1438
|
* function MyForm() {
|
|
1422
|
-
* return
|
|
1423
|
-
*
|
|
1424
|
-
* {
|
|
1425
|
-
*
|
|
1426
|
-
*
|
|
1439
|
+
* return (
|
|
1440
|
+
* <Form
|
|
1441
|
+
* items={[
|
|
1442
|
+
* { name: 'name' },
|
|
1443
|
+
* ]}
|
|
1444
|
+
* />
|
|
1445
|
+
* )
|
|
1427
1446
|
* }
|
|
1428
1447
|
* ```
|
|
1429
1448
|
*/
|
|
@@ -1480,14 +1499,21 @@ OptionalWrapper.displayName = "OptionalWrapper";
|
|
|
1480
1499
|
//#endregion
|
|
1481
1500
|
//#region src/useFaasStream.tsx
|
|
1482
1501
|
/**
|
|
1483
|
-
* Stream
|
|
1502
|
+
* Stream a FaasJS response into React state.
|
|
1484
1503
|
*
|
|
1485
|
-
*
|
|
1486
|
-
*
|
|
1487
|
-
*
|
|
1504
|
+
* The hook sends a streaming request, appends decoded text chunks to `data`,
|
|
1505
|
+
* and exposes reload helpers for retrying the same action.
|
|
1506
|
+
*
|
|
1507
|
+
* @param action - Action path to invoke.
|
|
1508
|
+
* @param defaultParams - Params used for the initial request and future reloads.
|
|
1509
|
+
* @param options - Optional hook configuration such as controlled data, debounce, and skip logic.
|
|
1510
|
+
* @returns Streaming request state and helper methods.
|
|
1488
1511
|
*
|
|
1489
1512
|
* @example
|
|
1490
1513
|
* ```tsx
|
|
1514
|
+
* import { useState } from 'react'
|
|
1515
|
+
* import { useFaasStream } from '@faasjs/react'
|
|
1516
|
+
*
|
|
1491
1517
|
* function Chat() {
|
|
1492
1518
|
* const [prompt, setPrompt] = useState('')
|
|
1493
1519
|
* const { data, loading, reload } = useFaasStream('chat', { prompt })
|
|
@@ -1628,8 +1654,8 @@ function useFaasStream(action, defaultParams, options = {}) {
|
|
|
1628
1654
|
* Hook to store the previous value of a state or prop.
|
|
1629
1655
|
*
|
|
1630
1656
|
* @template T - The type of the value.
|
|
1631
|
-
* @param
|
|
1632
|
-
* @returns
|
|
1657
|
+
* @param value - The current value to track.
|
|
1658
|
+
* @returns Previous value from the prior render, or `undefined` on the first render.
|
|
1633
1659
|
*/
|
|
1634
1660
|
function usePrevious(value) {
|
|
1635
1661
|
const ref = useRef(void 0);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/react",
|
|
3
|
-
"version": "8.0.0-beta.
|
|
3
|
+
"version": "8.0.0-beta.16",
|
|
4
4
|
"homepage": "https://faasjs.com/doc/react/",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/faasjs/faasjs/issues"
|
|
@@ -27,12 +27,12 @@
|
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@faasjs/types": ">=8.0.0-beta.
|
|
30
|
+
"@faasjs/types": ">=8.0.0-beta.16",
|
|
31
31
|
"@types/react": "^19.0.0",
|
|
32
32
|
"react": "^19.0.0"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
|
-
"@faasjs/types": ">=8.0.0-beta.
|
|
35
|
+
"@faasjs/types": ">=8.0.0-beta.16"
|
|
36
36
|
},
|
|
37
37
|
"engines": {
|
|
38
38
|
"node": ">=24.0.0",
|