@ic-reactor/react 3.12.5 → 4.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +236 -423
- package/dist/index.d.ts +259 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +470 -17
- package/dist/index.js.map +1 -1
- package/llms.txt +82 -54
- package/package.json +12 -28
- package/src/index.tsx +611 -0
- package/dist/auth/authentication-manager.d.ts +0 -123
- package/dist/auth/authentication-manager.d.ts.map +0 -1
- package/dist/auth/authentication-manager.js +0 -767
- package/dist/auth/authentication-manager.js.map +0 -1
- package/dist/auth/constants.d.ts +0 -24
- package/dist/auth/constants.d.ts.map +0 -1
- package/dist/auth/constants.js +0 -24
- package/dist/auth/constants.js.map +0 -1
- package/dist/auth/createIdentityAttributeHooks.d.ts +0 -14
- package/dist/auth/createIdentityAttributeHooks.d.ts.map +0 -1
- package/dist/auth/createIdentityAttributeHooks.js +0 -106
- package/dist/auth/createIdentityAttributeHooks.js.map +0 -1
- package/dist/auth/identity-attributes-manager.d.ts +0 -26
- package/dist/auth/identity-attributes-manager.d.ts.map +0 -1
- package/dist/auth/identity-attributes-manager.js +0 -107
- package/dist/auth/identity-attributes-manager.js.map +0 -1
- package/dist/auth/identity-attributes.d.ts +0 -19
- package/dist/auth/identity-attributes.d.ts.map +0 -1
- package/dist/auth/identity-attributes.js +0 -170
- package/dist/auth/identity-attributes.js.map +0 -1
- package/dist/auth/index.d.ts +0 -8
- package/dist/auth/index.d.ts.map +0 -1
- package/dist/auth/index.js +0 -8
- package/dist/auth/index.js.map +0 -1
- package/dist/auth/local-ii-probe.d.ts +0 -46
- package/dist/auth/local-ii-probe.d.ts.map +0 -1
- package/dist/auth/local-ii-probe.js +0 -102
- package/dist/auth/local-ii-probe.js.map +0 -1
- package/dist/auth/types.d.ts +0 -179
- package/dist/auth/types.d.ts.map +0 -1
- package/dist/auth/types.js +0 -2
- package/dist/auth/types.js.map +0 -1
- package/dist/createActorHooks.d.ts +0 -52
- package/dist/createActorHooks.d.ts.map +0 -1
- package/dist/createActorHooks.js +0 -17
- package/dist/createActorHooks.js.map +0 -1
- package/dist/createInfiniteQuery.d.ts +0 -144
- package/dist/createInfiniteQuery.d.ts.map +0 -1
- package/dist/createInfiniteQuery.js +0 -174
- package/dist/createInfiniteQuery.js.map +0 -1
- package/dist/createMutation.d.ts +0 -30
- package/dist/createMutation.d.ts.map +0 -1
- package/dist/createMutation.js +0 -192
- package/dist/createMutation.js.map +0 -1
- package/dist/createQuery.d.ts +0 -30
- package/dist/createQuery.d.ts.map +0 -1
- package/dist/createQuery.js +0 -117
- package/dist/createQuery.js.map +0 -1
- package/dist/createSuspenseInfiniteQuery.d.ts +0 -147
- package/dist/createSuspenseInfiniteQuery.d.ts.map +0 -1
- package/dist/createSuspenseInfiniteQuery.js +0 -177
- package/dist/createSuspenseInfiniteQuery.js.map +0 -1
- package/dist/createSuspenseQuery.d.ts +0 -25
- package/dist/createSuspenseQuery.d.ts.map +0 -1
- package/dist/createSuspenseQuery.js +0 -111
- package/dist/createSuspenseQuery.js.map +0 -1
- package/dist/defineReactor.d.ts +0 -125
- package/dist/defineReactor.d.ts.map +0 -1
- package/dist/defineReactor.js +0 -180
- package/dist/defineReactor.js.map +0 -1
- package/dist/hooks/createAuthHooks.d.ts +0 -43
- package/dist/hooks/createAuthHooks.d.ts.map +0 -1
- package/dist/hooks/createAuthHooks.js +0 -131
- package/dist/hooks/createAuthHooks.js.map +0 -1
- package/dist/hooks/index.d.ts +0 -21
- package/dist/hooks/index.d.ts.map +0 -1
- package/dist/hooks/index.js +0 -24
- package/dist/hooks/index.js.map +0 -1
- package/dist/hooks/useActorInfiniteQuery.d.ts +0 -39
- package/dist/hooks/useActorInfiniteQuery.d.ts.map +0 -1
- package/dist/hooks/useActorInfiniteQuery.js +0 -56
- package/dist/hooks/useActorInfiniteQuery.js.map +0 -1
- package/dist/hooks/useActorMethod.d.ts +0 -115
- package/dist/hooks/useActorMethod.d.ts.map +0 -1
- package/dist/hooks/useActorMethod.js +0 -250
- package/dist/hooks/useActorMethod.js.map +0 -1
- package/dist/hooks/useActorMutation.d.ts +0 -48
- package/dist/hooks/useActorMutation.d.ts.map +0 -1
- package/dist/hooks/useActorMutation.js +0 -69
- package/dist/hooks/useActorMutation.js.map +0 -1
- package/dist/hooks/useActorQuery.d.ts +0 -32
- package/dist/hooks/useActorQuery.d.ts.map +0 -1
- package/dist/hooks/useActorQuery.js +0 -46
- package/dist/hooks/useActorQuery.js.map +0 -1
- package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts +0 -39
- package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts.map +0 -1
- package/dist/hooks/useActorSuspenseInfiniteQuery.js +0 -56
- package/dist/hooks/useActorSuspenseInfiniteQuery.js.map +0 -1
- package/dist/hooks/useActorSuspenseQuery.d.ts +0 -32
- package/dist/hooks/useActorSuspenseQuery.d.ts.map +0 -1
- package/dist/hooks/useActorSuspenseQuery.js +0 -47
- package/dist/hooks/useActorSuspenseQuery.js.map +0 -1
- package/dist/types.d.ts +0 -270
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -5
- package/dist/types.js.map +0 -1
- package/dist/utils.d.ts +0 -51
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -105
- package/dist/utils.js.map +0 -1
- package/dist/validation.d.ts +0 -131
- package/dist/validation.d.ts.map +0 -1
- package/dist/validation.js +0 -125
- package/dist/validation.js.map +0 -1
- package/src/auth/authentication-manager.ts +0 -860
- package/src/auth/constants.ts +0 -32
- package/src/auth/createIdentityAttributeHooks.ts +0 -143
- package/src/auth/identity-attributes-manager.ts +0 -131
- package/src/auth/identity-attributes.ts +0 -270
- package/src/auth/index.ts +0 -7
- package/src/auth/local-ii-probe.ts +0 -147
- package/src/auth/types.ts +0 -200
- package/src/createActorHooks.ts +0 -220
- package/src/createInfiniteQuery.ts +0 -578
- package/src/createMutation.ts +0 -341
- package/src/createQuery.ts +0 -237
- package/src/createSuspenseInfiniteQuery.ts +0 -601
- package/src/createSuspenseQuery.ts +0 -234
- package/src/defineReactor.ts +0 -324
- package/src/hooks/createAuthHooks.ts +0 -189
- package/src/hooks/index.ts +0 -103
- package/src/hooks/useActorInfiniteQuery.ts +0 -205
- package/src/hooks/useActorMethod.ts +0 -507
- package/src/hooks/useActorMutation.ts +0 -205
- package/src/hooks/useActorQuery.ts +0 -124
- package/src/hooks/useActorSuspenseInfiniteQuery.ts +0 -205
- package/src/hooks/useActorSuspenseQuery.ts +0 -132
- package/src/index.ts +0 -23
- package/src/types.ts +0 -499
- package/src/utils.ts +0 -121
- package/src/validation.ts +0 -202
package/dist/utils.js
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared internal utilities for query and mutation factories.
|
|
3
|
-
*/
|
|
4
|
-
import { generateKey } from "@ic-reactor/core";
|
|
5
|
-
/**
|
|
6
|
-
* Internal query-key segment used to distinguish per-call factory args
|
|
7
|
-
* from the base query key. Not part of the public API.
|
|
8
|
-
*/
|
|
9
|
-
export const FACTORY_KEY_ARGS_QUERY_KEY = "__ic_reactor_factory_key_args";
|
|
10
|
-
/** Convert a direct reactor result into a value TanStack Query can cache. */
|
|
11
|
-
export const normalizeQueryData = (value) => (value === undefined ? null : value);
|
|
12
|
-
/**
|
|
13
|
-
* Merge a base query key, optional per-call query key, and optional key-args
|
|
14
|
-
* into a single query key array.
|
|
15
|
-
*
|
|
16
|
-
* Used by createInfiniteQueryFactory and createSuspenseInfiniteQueryFactory to
|
|
17
|
-
* ensure each unique set of factory args produces a distinct cache entry.
|
|
18
|
-
*/
|
|
19
|
-
export function mergeFactoryQueryKey(baseQueryKey, callQueryKey, keyArgs) {
|
|
20
|
-
const merged = [];
|
|
21
|
-
if (baseQueryKey)
|
|
22
|
-
merged.push(...baseQueryKey);
|
|
23
|
-
if (callQueryKey)
|
|
24
|
-
merged.push(...callQueryKey);
|
|
25
|
-
if (keyArgs !== undefined) {
|
|
26
|
-
// Serialize keyArgs through generateKey so BigInt values (and other
|
|
27
|
-
// non-JSON-serializable Candid types such as Principal) are safely
|
|
28
|
-
// converted to strings before React Query hashes the key.
|
|
29
|
-
const safeKeyArgs = generateKey(Array.isArray(keyArgs) ? keyArgs : [keyArgs]);
|
|
30
|
-
merged.push({ [FACTORY_KEY_ARGS_QUERY_KEY]: safeKeyArgs });
|
|
31
|
-
}
|
|
32
|
-
return merged.length > 0 ? merged : undefined;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Build a chained select that applies the config-level select (if any) and then
|
|
36
|
-
* the hook-level select (if any).
|
|
37
|
-
*
|
|
38
|
-
* Returns the caller's own function untouched when only one of the two is
|
|
39
|
-
* present, and `undefined` when neither is — allocating a wrapper only for the
|
|
40
|
-
* genuinely chained case. Identity matters here: `QueryObserver` memoizes a
|
|
41
|
-
* select result on `options.select === previousSelectFn`, so a wrapper rebuilt
|
|
42
|
-
* on every render defeats that check. The select then re-runs each render, and
|
|
43
|
-
* for a select returning a non-plain value (a `Map`, a `Date`, a `Principal`)
|
|
44
|
-
* `replaceEqualDeep` cannot structurally share the result either, so `data`
|
|
45
|
-
* gets a fresh reference every render and a `useEffect([data])` that sets state
|
|
46
|
-
* becomes an unbounded loop.
|
|
47
|
-
*
|
|
48
|
-
* Callers should still memoize the result, since the chained case allocates.
|
|
49
|
-
*/
|
|
50
|
-
export function buildChainedSelect(configSelect, hookSelect) {
|
|
51
|
-
if (!configSelect) {
|
|
52
|
-
// `hookSelect` receives the raw data when there is no config-level select,
|
|
53
|
-
// matching the previous behaviour.
|
|
54
|
-
return hookSelect;
|
|
55
|
-
}
|
|
56
|
-
if (!hookSelect)
|
|
57
|
-
return configSelect;
|
|
58
|
-
return (rawData) => hookSelect(configSelect(rawData));
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* How many memoized query objects an args-late factory keeps.
|
|
62
|
-
*
|
|
63
|
-
* The memo exists so `getBalance(sameArgs)` returns the same object twice; it
|
|
64
|
-
* was unbounded, so an open-ended argument space — a per-principal balance in a
|
|
65
|
-
* long-lived SPA, a search-as-you-type filter, a cursor-keyed list — grew it
|
|
66
|
-
* forever (measured at ~2.9 kB per entry, 55.8 MB for 20k). A few hundred
|
|
67
|
-
* covers any realistic working set while capping the worst case.
|
|
68
|
-
*/
|
|
69
|
-
const FACTORY_CACHE_LIMIT = 256;
|
|
70
|
-
/**
|
|
71
|
-
* Smallest useful LRU: a `Map` already iterates in insertion order, so
|
|
72
|
-
* re-inserting on read is enough to track recency.
|
|
73
|
-
*
|
|
74
|
-
* Eviction is safe — it costs memoization, never correctness. A query object is
|
|
75
|
-
* a closure over the reactor and config, so one rebuilt after eviction behaves
|
|
76
|
-
* identically; only its reference identity differs.
|
|
77
|
-
*/
|
|
78
|
-
export function createBoundedCache(limit = FACTORY_CACHE_LIMIT) {
|
|
79
|
-
const entries = new Map();
|
|
80
|
-
return {
|
|
81
|
-
get(key) {
|
|
82
|
-
const value = entries.get(key);
|
|
83
|
-
if (value === undefined)
|
|
84
|
-
return undefined;
|
|
85
|
-
// Touch: move to the most-recent end.
|
|
86
|
-
entries.delete(key);
|
|
87
|
-
entries.set(key, value);
|
|
88
|
-
return value;
|
|
89
|
-
},
|
|
90
|
-
set(key, value) {
|
|
91
|
-
if (entries.has(key))
|
|
92
|
-
entries.delete(key);
|
|
93
|
-
else if (entries.size >= limit) {
|
|
94
|
-
const oldest = entries.keys().next().value;
|
|
95
|
-
if (oldest !== undefined)
|
|
96
|
-
entries.delete(oldest);
|
|
97
|
-
}
|
|
98
|
-
entries.set(key, value);
|
|
99
|
-
},
|
|
100
|
-
get size() {
|
|
101
|
-
return entries.size;
|
|
102
|
-
},
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
//# sourceMappingURL=utils.js.map
|
package/dist/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":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|
3
|
-
*
|
|
4
|
-
* Helpers for working with ValidationError in React components,
|
|
5
|
-
* especially for form integration.
|
|
6
|
-
*/
|
|
7
|
-
import { ValidationError, isValidationError, ValidationIssue } from "@ic-reactor/core";
|
|
8
|
-
export { isValidationError, ValidationError };
|
|
9
|
-
export type { ValidationIssue };
|
|
10
|
-
/**
|
|
11
|
-
* A map of field names to error messages.
|
|
12
|
-
* This format is compatible with most form libraries.
|
|
13
|
-
*/
|
|
14
|
-
export type FieldErrors = Record<string, string>;
|
|
15
|
-
/**
|
|
16
|
-
* A map of field names to arrays of error messages.
|
|
17
|
-
* Use when you need to show multiple errors per field.
|
|
18
|
-
*/
|
|
19
|
-
export type FieldErrorsMultiple = Record<string, string[]>;
|
|
20
|
-
/**
|
|
21
|
-
* Options for mapValidationErrors
|
|
22
|
-
*/
|
|
23
|
-
export interface MapValidationErrorsOptions {
|
|
24
|
-
/**
|
|
25
|
-
* If true, returns all messages for each field as an array.
|
|
26
|
-
* If false (default), returns only the first message for each field.
|
|
27
|
-
*/
|
|
28
|
-
multiple?: boolean;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Maps validation error issues to a simple field -> message object.
|
|
32
|
-
* Returns the first error message for each field path.
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```tsx
|
|
36
|
-
* const { mutate } = useActorMutation({
|
|
37
|
-
* functionName: "transfer",
|
|
38
|
-
* onError: (error) => {
|
|
39
|
-
* if (isValidationError(error)) {
|
|
40
|
-
* const fieldErrors = mapValidationErrors(error)
|
|
41
|
-
* // { to: "Recipient is required", amount: "Must be a number" }
|
|
42
|
-
*
|
|
43
|
-
* // Use with React Hook Form
|
|
44
|
-
* Object.entries(fieldErrors).forEach(([field, message]) => {
|
|
45
|
-
* form.setError(field, { message })
|
|
46
|
-
* })
|
|
47
|
-
* }
|
|
48
|
-
* }
|
|
49
|
-
* })
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
export declare function mapValidationErrors(error: ValidationError): FieldErrors;
|
|
53
|
-
export declare function mapValidationErrors(error: ValidationError, options: {
|
|
54
|
-
multiple: true;
|
|
55
|
-
}): FieldErrorsMultiple;
|
|
56
|
-
export declare function mapValidationErrors(error: ValidationError, options: {
|
|
57
|
-
multiple: false;
|
|
58
|
-
}): FieldErrors;
|
|
59
|
-
/**
|
|
60
|
-
* Gets error message for a specific field from a ValidationError.
|
|
61
|
-
* Returns undefined if no error exists for that field.
|
|
62
|
-
*
|
|
63
|
-
* @example
|
|
64
|
-
* ```tsx
|
|
65
|
-
* if (isValidationError(error)) {
|
|
66
|
-
* const toError = getFieldError(error, "to")
|
|
67
|
-
* const amountError = getFieldError(error, "amount")
|
|
68
|
-
* }
|
|
69
|
-
* ```
|
|
70
|
-
*/
|
|
71
|
-
export declare function getFieldError(error: ValidationError, fieldName: string): string | undefined;
|
|
72
|
-
/**
|
|
73
|
-
* Gets all error messages for a specific field from a ValidationError.
|
|
74
|
-
* Returns empty array if no errors exist for that field.
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* ```tsx
|
|
78
|
-
* if (isValidationError(error)) {
|
|
79
|
-
* const amountErrors = getFieldErrors(error, "amount")
|
|
80
|
-
* // ["Amount is required", "Amount must be positive"]
|
|
81
|
-
* }
|
|
82
|
-
* ```
|
|
83
|
-
*/
|
|
84
|
-
export declare function getFieldErrors(error: ValidationError, fieldName: string): string[];
|
|
85
|
-
/**
|
|
86
|
-
* Checks if a given error is a ValidationError and optionally
|
|
87
|
-
* extracts field errors in one step.
|
|
88
|
-
*
|
|
89
|
-
* @example
|
|
90
|
-
* ```tsx
|
|
91
|
-
* const { mutateAsync } = useActorMutation({ functionName: "transfer" })
|
|
92
|
-
*
|
|
93
|
-
* const handleSubmit = async (data) => {
|
|
94
|
-
* try {
|
|
95
|
-
* await mutateAsync([data])
|
|
96
|
-
* } catch (error) {
|
|
97
|
-
* const fieldErrors = extractValidationErrors(error)
|
|
98
|
-
* if (fieldErrors) {
|
|
99
|
-
* // Handle validation errors
|
|
100
|
-
* Object.entries(fieldErrors).forEach(([field, message]) => {
|
|
101
|
-
* form.setError(field, { message })
|
|
102
|
-
* })
|
|
103
|
-
* } else {
|
|
104
|
-
* // Handle other errors
|
|
105
|
-
* console.error(error)
|
|
106
|
-
* }
|
|
107
|
-
* }
|
|
108
|
-
* }
|
|
109
|
-
* ```
|
|
110
|
-
*/
|
|
111
|
-
export declare function extractValidationErrors(error: unknown): FieldErrors | null;
|
|
112
|
-
/**
|
|
113
|
-
* React hook-friendly error handler that extracts validation errors.
|
|
114
|
-
* Returns a callback suitable for onError handlers.
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
* ```tsx
|
|
118
|
-
* const [fieldErrors, setFieldErrors] = useState<FieldErrors>({})
|
|
119
|
-
*
|
|
120
|
-
* const { mutate } = useActorMutation({
|
|
121
|
-
* functionName: "transfer",
|
|
122
|
-
* onError: handleValidationError(setFieldErrors),
|
|
123
|
-
* })
|
|
124
|
-
*
|
|
125
|
-
* // In JSX
|
|
126
|
-
* <input name="to" />
|
|
127
|
-
* {fieldErrors.to && <span className="error">{fieldErrors.to}</span>}
|
|
128
|
-
* ```
|
|
129
|
-
*/
|
|
130
|
-
export declare function handleValidationError(setFieldErrors: (errors: FieldErrors) => void, onOtherError?: (error: Error) => void): (error: Error) => void;
|
|
131
|
-
//# sourceMappingURL=validation.d.ts.map
|
package/dist/validation.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,eAAe,EACf,iBAAiB,EACjB,eAAe,EAChB,MAAM,kBAAkB,CAAA;AAGzB,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,CAAA;AAC7C,YAAY,EAAE,eAAe,EAAE,CAAA;AAE/B;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MA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3
|
-
*
|
|
4
|
-
* Helpers for working with ValidationError in React components,
|
|
5
|
-
* especially for form integration.
|
|
6
|
-
*/
|
|
7
|
-
import { ValidationError, isValidationError, } from "@ic-reactor/core";
|
|
8
|
-
// Re-export for convenience
|
|
9
|
-
export { isValidationError, ValidationError };
|
|
10
|
-
export function mapValidationErrors(error, options) {
|
|
11
|
-
if (options?.multiple) {
|
|
12
|
-
const result = {};
|
|
13
|
-
for (const issue of error.issues) {
|
|
14
|
-
const fieldName = String(issue.path[0] ?? "");
|
|
15
|
-
if (!result[fieldName]) {
|
|
16
|
-
result[fieldName] = [];
|
|
17
|
-
}
|
|
18
|
-
result[fieldName].push(issue.message);
|
|
19
|
-
}
|
|
20
|
-
return result;
|
|
21
|
-
}
|
|
22
|
-
const result = {};
|
|
23
|
-
for (const issue of error.issues) {
|
|
24
|
-
const fieldName = String(issue.path[0] ?? "");
|
|
25
|
-
if (!result[fieldName]) {
|
|
26
|
-
result[fieldName] = issue.message;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return result;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Gets error message for a specific field from a ValidationError.
|
|
33
|
-
* Returns undefined if no error exists for that field.
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* ```tsx
|
|
37
|
-
* if (isValidationError(error)) {
|
|
38
|
-
* const toError = getFieldError(error, "to")
|
|
39
|
-
* const amountError = getFieldError(error, "amount")
|
|
40
|
-
* }
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
export function getFieldError(error, fieldName) {
|
|
44
|
-
const issue = error.issues.find((i) => String(i.path[0]) === fieldName);
|
|
45
|
-
return issue?.message;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Gets all error messages for a specific field from a ValidationError.
|
|
49
|
-
* Returns empty array if no errors exist for that field.
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
52
|
-
* ```tsx
|
|
53
|
-
* if (isValidationError(error)) {
|
|
54
|
-
* const amountErrors = getFieldErrors(error, "amount")
|
|
55
|
-
* // ["Amount is required", "Amount must be positive"]
|
|
56
|
-
* }
|
|
57
|
-
* ```
|
|
58
|
-
*/
|
|
59
|
-
export function getFieldErrors(error, fieldName) {
|
|
60
|
-
return error.issues
|
|
61
|
-
.filter((i) => String(i.path[0]) === fieldName)
|
|
62
|
-
.map((i) => i.message);
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Checks if a given error is a ValidationError and optionally
|
|
66
|
-
* extracts field errors in one step.
|
|
67
|
-
*
|
|
68
|
-
* @example
|
|
69
|
-
* ```tsx
|
|
70
|
-
* const { mutateAsync } = useActorMutation({ functionName: "transfer" })
|
|
71
|
-
*
|
|
72
|
-
* const handleSubmit = async (data) => {
|
|
73
|
-
* try {
|
|
74
|
-
* await mutateAsync([data])
|
|
75
|
-
* } catch (error) {
|
|
76
|
-
* const fieldErrors = extractValidationErrors(error)
|
|
77
|
-
* if (fieldErrors) {
|
|
78
|
-
* // Handle validation errors
|
|
79
|
-
* Object.entries(fieldErrors).forEach(([field, message]) => {
|
|
80
|
-
* form.setError(field, { message })
|
|
81
|
-
* })
|
|
82
|
-
* } else {
|
|
83
|
-
* // Handle other errors
|
|
84
|
-
* console.error(error)
|
|
85
|
-
* }
|
|
86
|
-
* }
|
|
87
|
-
* }
|
|
88
|
-
* ```
|
|
89
|
-
*/
|
|
90
|
-
export function extractValidationErrors(error) {
|
|
91
|
-
if (isValidationError(error)) {
|
|
92
|
-
return mapValidationErrors(error);
|
|
93
|
-
}
|
|
94
|
-
return null;
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* React hook-friendly error handler that extracts validation errors.
|
|
98
|
-
* Returns a callback suitable for onError handlers.
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
* ```tsx
|
|
102
|
-
* const [fieldErrors, setFieldErrors] = useState<FieldErrors>({})
|
|
103
|
-
*
|
|
104
|
-
* const { mutate } = useActorMutation({
|
|
105
|
-
* functionName: "transfer",
|
|
106
|
-
* onError: handleValidationError(setFieldErrors),
|
|
107
|
-
* })
|
|
108
|
-
*
|
|
109
|
-
* // In JSX
|
|
110
|
-
* <input name="to" />
|
|
111
|
-
* {fieldErrors.to && <span className="error">{fieldErrors.to}</span>}
|
|
112
|
-
* ```
|
|
113
|
-
*/
|
|
114
|
-
export function handleValidationError(setFieldErrors, onOtherError) {
|
|
115
|
-
return (error) => {
|
|
116
|
-
const fieldErrors = extractValidationErrors(error);
|
|
117
|
-
if (fieldErrors) {
|
|
118
|
-
setFieldErrors(fieldErrors);
|
|
119
|
-
}
|
|
120
|
-
else if (onOtherError) {
|
|
121
|
-
onOtherError(error);
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
//# sourceMappingURL=validation.js.map
|
package/dist/validation.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../src/validation.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,eAAe,EACf,iBAAiB,GAElB,MAAM,kBAAkB,CAAA;AAEzB,4BAA4B;AAC5B,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,CAAA;AAyD7C,MAAM,UAAU,mBAAmB,CACjC,KAAsB,EACtB,OAAoC;IAEpC,IAAI,OAAO,EAAE,QAAQ,EAAE,CAAC;QACtB,MAAM,MAAM,GAAwB,EAAE,CAAA;QACtC,KAAK,MAAM,KAAK,IAAI,KAAK,CAAC,MAAM,EAAE,CAAC;YACjC,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAA;YAC7C,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC;gBACvB,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE,CAAA;YACxB,CAAC;YACD,MAAM,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,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