@ic-reactor/react 3.13.0 → 4.0.0-beta.2
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 +237 -787
- package/dist/index.d.ts +260 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +471 -23
- package/dist/index.js.map +1 -1
- package/llms.txt +82 -278
- package/package.json +11 -39
- package/src/index.tsx +612 -0
- package/dist/auth/auth-client-compat.d.ts +0 -122
- package/dist/auth/auth-client-compat.d.ts.map +0 -1
- package/dist/auth/auth-client-compat.js +0 -162
- package/dist/auth/auth-client-compat.js.map +0 -1
- package/dist/auth/authentication-manager.d.ts +0 -405
- package/dist/auth/authentication-manager.d.ts.map +0 -1
- package/dist/auth/authentication-manager.js +0 -1537
- 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 -122
- package/dist/auth/createIdentityAttributeHooks.js.map +0 -1
- package/dist/auth/identity-attributes-manager.d.ts +0 -27
- package/dist/auth/identity-attributes-manager.d.ts.map +0 -1
- package/dist/auth/identity-attributes-manager.js +0 -191
- 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 -227
- 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 -57
- package/dist/auth/local-ii-probe.d.ts.map +0 -1
- package/dist/auth/local-ii-probe.js +0 -121
- package/dist/auth/local-ii-probe.js.map +0 -1
- package/dist/auth/types.d.ts +0 -222
- 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 -41
- 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 -185
- package/dist/createInfiniteQuery.d.ts.map +0 -1
- package/dist/createInfiniteQuery.js +0 -198
- package/dist/createInfiniteQuery.js.map +0 -1
- package/dist/createMutation.d.ts +0 -33
- package/dist/createMutation.d.ts.map +0 -1
- package/dist/createMutation.js +0 -199
- package/dist/createMutation.js.map +0 -1
- package/dist/createQuery.d.ts +0 -63
- package/dist/createQuery.d.ts.map +0 -1
- package/dist/createQuery.js +0 -204
- package/dist/createQuery.js.map +0 -1
- package/dist/createReactorProvider.d.ts +0 -158
- package/dist/createReactorProvider.d.ts.map +0 -1
- package/dist/createReactorProvider.js +0 -256
- package/dist/createReactorProvider.js.map +0 -1
- package/dist/createSuspenseInfiniteQuery.d.ts +0 -154
- package/dist/createSuspenseInfiniteQuery.d.ts.map +0 -1
- package/dist/createSuspenseInfiniteQuery.js +0 -209
- package/dist/createSuspenseInfiniteQuery.js.map +0 -1
- package/dist/createSuspenseQuery.d.ts +0 -46
- package/dist/createSuspenseQuery.d.ts.map +0 -1
- package/dist/createSuspenseQuery.js +0 -158
- package/dist/createSuspenseQuery.js.map +0 -1
- package/dist/defineDisplayReactor.d.ts +0 -43
- package/dist/defineDisplayReactor.d.ts.map +0 -1
- package/dist/defineDisplayReactor.js +0 -42
- package/dist/defineDisplayReactor.js.map +0 -1
- package/dist/defineReactor.d.ts +0 -99
- package/dist/defineReactor.d.ts.map +0 -1
- package/dist/defineReactor.js +0 -15
- package/dist/defineReactor.js.map +0 -1
- package/dist/defineReactorShared.d.ts +0 -84
- package/dist/defineReactorShared.d.ts.map +0 -1
- package/dist/defineReactorShared.js +0 -139
- package/dist/defineReactorShared.js.map +0 -1
- package/dist/hooks/createAuthHooks.d.ts +0 -50
- package/dist/hooks/createAuthHooks.d.ts.map +0 -1
- package/dist/hooks/createAuthHooks.js +0 -291
- 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 -67
- package/dist/hooks/useActorInfiniteQuery.d.ts.map +0 -1
- package/dist/hooks/useActorInfiniteQuery.js +0 -89
- package/dist/hooks/useActorInfiniteQuery.js.map +0 -1
- package/dist/hooks/useActorMethod.d.ts +0 -148
- package/dist/hooks/useActorMethod.d.ts.map +0 -1
- package/dist/hooks/useActorMethod.js +0 -394
- package/dist/hooks/useActorMethod.js.map +0 -1
- package/dist/hooks/useActorMutation.d.ts +0 -51
- package/dist/hooks/useActorMutation.d.ts.map +0 -1
- package/dist/hooks/useActorMutation.js +0 -70
- package/dist/hooks/useActorMutation.js.map +0 -1
- package/dist/hooks/useActorQuery.d.ts +0 -45
- package/dist/hooks/useActorQuery.d.ts.map +0 -1
- package/dist/hooks/useActorQuery.js +0 -67
- package/dist/hooks/useActorQuery.js.map +0 -1
- package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts +0 -51
- package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts.map +0 -1
- package/dist/hooks/useActorSuspenseInfiniteQuery.js +0 -76
- 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 -58
- package/dist/hooks/useActorSuspenseQuery.js.map +0 -1
- package/dist/ownedAuthentication.d.ts +0 -52
- package/dist/ownedAuthentication.d.ts.map +0 -1
- package/dist/ownedAuthentication.js +0 -49
- package/dist/ownedAuthentication.js.map +0 -1
- package/dist/server.d.ts +0 -21
- package/dist/server.d.ts.map +0 -1
- package/dist/server.js +0 -23
- package/dist/server.js.map +0 -1
- package/dist/testing.d.ts +0 -19
- package/dist/testing.d.ts.map +0 -1
- package/dist/testing.js +0 -19
- package/dist/testing.js.map +0 -1
- package/dist/types.d.ts +0 -671
- 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 -207
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -405
- package/dist/utils.js.map +0 -1
- package/dist/validation.d.ts +0 -136
- package/dist/validation.d.ts.map +0 -1
- package/dist/validation.js +0 -144
- package/dist/validation.js.map +0 -1
- package/src/auth/auth-client-compat.ts +0 -273
- package/src/auth/authentication-manager.ts +0 -1682
- package/src/auth/constants.ts +0 -32
- package/src/auth/createIdentityAttributeHooks.ts +0 -169
- package/src/auth/identity-attributes-manager.ts +0 -226
- package/src/auth/identity-attributes.ts +0 -345
- package/src/auth/index.ts +0 -7
- package/src/auth/local-ii-probe.ts +0 -173
- package/src/auth/types.ts +0 -243
- package/src/createActorHooks.ts +0 -208
- package/src/createInfiniteQuery.ts +0 -670
- package/src/createMutation.ts +0 -324
- package/src/createQuery.ts +0 -369
- package/src/createReactorProvider.ts +0 -365
- package/src/createSuspenseInfiniteQuery.ts +0 -651
- package/src/createSuspenseQuery.ts +0 -304
- package/src/defineDisplayReactor.ts +0 -62
- package/src/defineReactor.ts +0 -142
- package/src/defineReactorShared.ts +0 -268
- package/src/hooks/createAuthHooks.ts +0 -371
- package/src/hooks/index.ts +0 -103
- package/src/hooks/useActorInfiniteQuery.ts +0 -278
- package/src/hooks/useActorMethod.ts +0 -710
- package/src/hooks/useActorMutation.ts +0 -205
- package/src/hooks/useActorQuery.ts +0 -157
- package/src/hooks/useActorSuspenseInfiniteQuery.ts +0 -248
- package/src/hooks/useActorSuspenseQuery.ts +0 -147
- package/src/index.ts +0 -31
- package/src/ownedAuthentication.ts +0 -81
- package/src/server.ts +0 -23
- package/src/testing.ts +0 -18
- package/src/types.ts +0 -948
- package/src/utils.ts +0 -505
- package/src/validation.ts +0 -226
package/src/validation.ts
DELETED
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Validation utilities for React
|
|
3
|
-
*
|
|
4
|
-
* Helpers for working with ValidationError in React components,
|
|
5
|
-
* especially for form integration.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
ValidationError,
|
|
10
|
-
isValidationError,
|
|
11
|
-
ValidationIssue,
|
|
12
|
-
} from "@ic-reactor/core"
|
|
13
|
-
|
|
14
|
-
// Re-export for convenience
|
|
15
|
-
export { isValidationError, ValidationError }
|
|
16
|
-
export type { ValidationIssue }
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* A map of field names to error messages.
|
|
20
|
-
* This format is compatible with most form libraries.
|
|
21
|
-
*/
|
|
22
|
-
export type FieldErrors = Record<string, string>
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* A map of field names to arrays of error messages.
|
|
26
|
-
* Use when you need to show multiple errors per field.
|
|
27
|
-
*/
|
|
28
|
-
export type FieldErrorsMultiple = Record<string, string[]>
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Options for mapValidationErrors
|
|
32
|
-
*/
|
|
33
|
-
export interface MapValidationErrorsOptions {
|
|
34
|
-
/**
|
|
35
|
-
* If true, returns all messages for each field as an array.
|
|
36
|
-
* If false (default), returns only the first message for each field.
|
|
37
|
-
*/
|
|
38
|
-
multiple?: boolean
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* The field an issue belongs to: the first segment of its path, as a string.
|
|
43
|
-
* An issue with an empty path is about the whole argument (a zod object-level
|
|
44
|
-
* `.refine()`, or any issue of a primitive argument) and belongs to `""`.
|
|
45
|
-
*/
|
|
46
|
-
function fieldNameOf(issue: ValidationIssue): string {
|
|
47
|
-
return String(issue.path[0] ?? "")
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Maps validation error issues to a simple field -> message object.
|
|
52
|
-
* Returns the first error message for each field path, or every message with
|
|
53
|
-
* `{ multiple: true }`. Issues about the whole argument (an empty path) are
|
|
54
|
-
* filed under `""`.
|
|
55
|
-
*
|
|
56
|
-
* @example
|
|
57
|
-
* ```tsx
|
|
58
|
-
* const { mutate } = useActorMutation({
|
|
59
|
-
* functionName: "transfer",
|
|
60
|
-
* onError: (error) => {
|
|
61
|
-
* if (isValidationError(error)) {
|
|
62
|
-
* const fieldErrors = mapValidationErrors(error)
|
|
63
|
-
* // { to: "Recipient is required", amount: "Must be a number" }
|
|
64
|
-
*
|
|
65
|
-
* // Use with React Hook Form
|
|
66
|
-
* Object.entries(fieldErrors).forEach(([field, message]) => {
|
|
67
|
-
* form.setError(field, { message })
|
|
68
|
-
* })
|
|
69
|
-
* }
|
|
70
|
-
* }
|
|
71
|
-
* })
|
|
72
|
-
* ```
|
|
73
|
-
*/
|
|
74
|
-
export function mapValidationErrors(
|
|
75
|
-
error: ValidationError,
|
|
76
|
-
options?: { multiple?: false }
|
|
77
|
-
): FieldErrors
|
|
78
|
-
export function mapValidationErrors(
|
|
79
|
-
error: ValidationError,
|
|
80
|
-
options: { multiple: true }
|
|
81
|
-
): FieldErrorsMultiple
|
|
82
|
-
export function mapValidationErrors(
|
|
83
|
-
error: ValidationError,
|
|
84
|
-
options?: MapValidationErrorsOptions
|
|
85
|
-
): FieldErrors | FieldErrorsMultiple
|
|
86
|
-
export function mapValidationErrors(
|
|
87
|
-
error: ValidationError,
|
|
88
|
-
options?: MapValidationErrorsOptions
|
|
89
|
-
): FieldErrors | FieldErrorsMultiple {
|
|
90
|
-
// Collected in a Map, not a plain object: a field named like an
|
|
91
|
-
// Object.prototype member (`constructor`, `toString`, `__proto__`) read that
|
|
92
|
-
// member back as if the field were already filled, which dropped its issue,
|
|
93
|
-
// and threw on `.push` with `multiple`. `Object.fromEntries` then creates
|
|
94
|
-
// every field as an own property, `__proto__` included.
|
|
95
|
-
if (options?.multiple) {
|
|
96
|
-
const messages = new Map<string, string[]>()
|
|
97
|
-
for (const issue of error.issues) {
|
|
98
|
-
const fieldName = fieldNameOf(issue)
|
|
99
|
-
const fieldMessages = messages.get(fieldName)
|
|
100
|
-
if (fieldMessages) {
|
|
101
|
-
fieldMessages.push(issue.message)
|
|
102
|
-
} else {
|
|
103
|
-
messages.set(fieldName, [issue.message])
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return Object.fromEntries(messages)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const messages = new Map<string, string>()
|
|
110
|
-
for (const issue of error.issues) {
|
|
111
|
-
const fieldName = fieldNameOf(issue)
|
|
112
|
-
if (!messages.get(fieldName)) {
|
|
113
|
-
messages.set(fieldName, issue.message)
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
return Object.fromEntries(messages)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Gets error message for a specific field from a ValidationError.
|
|
121
|
-
* Returns undefined if no error exists for that field. Pass `""` for issues
|
|
122
|
-
* about the whole argument (an empty path), the key `mapValidationErrors`
|
|
123
|
-
* files them under.
|
|
124
|
-
*
|
|
125
|
-
* @example
|
|
126
|
-
* ```tsx
|
|
127
|
-
* if (isValidationError(error)) {
|
|
128
|
-
* const toError = getFieldError(error, "to")
|
|
129
|
-
* const amountError = getFieldError(error, "amount")
|
|
130
|
-
* }
|
|
131
|
-
* ```
|
|
132
|
-
*/
|
|
133
|
-
export function getFieldError(
|
|
134
|
-
error: ValidationError,
|
|
135
|
-
fieldName: string
|
|
136
|
-
): string | undefined {
|
|
137
|
-
const issue = error.issues.find((i) => fieldNameOf(i) === fieldName)
|
|
138
|
-
return issue?.message
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Gets all error messages for a specific field from a ValidationError.
|
|
143
|
-
* Returns empty array if no errors exist for that field. Pass `""` for issues
|
|
144
|
-
* about the whole argument (an empty path).
|
|
145
|
-
*
|
|
146
|
-
* @example
|
|
147
|
-
* ```tsx
|
|
148
|
-
* if (isValidationError(error)) {
|
|
149
|
-
* const amountErrors = getFieldErrors(error, "amount")
|
|
150
|
-
* // ["Amount is required", "Amount must be positive"]
|
|
151
|
-
* }
|
|
152
|
-
* ```
|
|
153
|
-
*/
|
|
154
|
-
export function getFieldErrors(
|
|
155
|
-
error: ValidationError,
|
|
156
|
-
fieldName: string
|
|
157
|
-
): string[] {
|
|
158
|
-
return error.issues
|
|
159
|
-
.filter((i) => fieldNameOf(i) === fieldName)
|
|
160
|
-
.map((i) => i.message)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Checks if a given error is a ValidationError and optionally
|
|
165
|
-
* extracts field errors in one step.
|
|
166
|
-
*
|
|
167
|
-
* @example
|
|
168
|
-
* ```tsx
|
|
169
|
-
* const { mutateAsync } = useActorMutation({ functionName: "transfer" })
|
|
170
|
-
*
|
|
171
|
-
* const handleSubmit = async (data) => {
|
|
172
|
-
* try {
|
|
173
|
-
* await mutateAsync([data])
|
|
174
|
-
* } catch (error) {
|
|
175
|
-
* const fieldErrors = extractValidationErrors(error)
|
|
176
|
-
* if (fieldErrors) {
|
|
177
|
-
* // Handle validation errors
|
|
178
|
-
* Object.entries(fieldErrors).forEach(([field, message]) => {
|
|
179
|
-
* form.setError(field, { message })
|
|
180
|
-
* })
|
|
181
|
-
* } else {
|
|
182
|
-
* // Handle other errors
|
|
183
|
-
* console.error(error)
|
|
184
|
-
* }
|
|
185
|
-
* }
|
|
186
|
-
* }
|
|
187
|
-
* ```
|
|
188
|
-
*/
|
|
189
|
-
export function extractValidationErrors(error: unknown): FieldErrors | null {
|
|
190
|
-
if (isValidationError(error)) {
|
|
191
|
-
return mapValidationErrors(error)
|
|
192
|
-
}
|
|
193
|
-
return null
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* React hook-friendly error handler that extracts validation errors.
|
|
198
|
-
* Returns a callback suitable for onError handlers.
|
|
199
|
-
*
|
|
200
|
-
* @example
|
|
201
|
-
* ```tsx
|
|
202
|
-
* const [fieldErrors, setFieldErrors] = useState<FieldErrors>({})
|
|
203
|
-
*
|
|
204
|
-
* const { mutate } = useActorMutation({
|
|
205
|
-
* functionName: "transfer",
|
|
206
|
-
* onError: handleValidationError(setFieldErrors),
|
|
207
|
-
* })
|
|
208
|
-
*
|
|
209
|
-
* // In JSX
|
|
210
|
-
* <input name="to" />
|
|
211
|
-
* {fieldErrors.to && <span className="error">{fieldErrors.to}</span>}
|
|
212
|
-
* ```
|
|
213
|
-
*/
|
|
214
|
-
export function handleValidationError(
|
|
215
|
-
setFieldErrors: (errors: FieldErrors) => void,
|
|
216
|
-
onOtherError?: (error: Error) => void
|
|
217
|
-
): (error: Error) => void {
|
|
218
|
-
return (error: Error) => {
|
|
219
|
-
const fieldErrors = extractValidationErrors(error)
|
|
220
|
-
if (fieldErrors) {
|
|
221
|
-
setFieldErrors(fieldErrors)
|
|
222
|
-
} else if (onOtherError) {
|
|
223
|
-
onOtherError(error)
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|