@daviapps/react-utils 0.0.1 → 0.0.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/.vscode/settings.json +11 -0
- package/README.md +1 -1
- package/package.json +5 -9
- package/react-hook-form/index.ts +26 -0
- package/zod/v3/index.ts +58 -0
- package/index.ts +0 -3
- package/tsconfig.node.json +0 -26
package/README.md
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
# react-utils
|
|
1
|
+
# react-utils
|
package/package.json
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@daviapps/react-utils",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"repository": {
|
|
6
|
-
"type": "git",
|
|
7
|
-
"url": "git+https://github.com/daviapps/react-utils.git"
|
|
8
|
-
},
|
|
9
|
-
"author": "daviinacio",
|
|
10
|
-
"license": "MIT",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"type": "module",
|
|
11
5
|
"dependencies": {
|
|
12
|
-
"
|
|
6
|
+
"axios": "^1.13.2",
|
|
7
|
+
"react-hook-form": "^7.66.1",
|
|
8
|
+
"zod": "^4.1.12"
|
|
13
9
|
}
|
|
14
10
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AxiosError } from "axios";
|
|
2
|
+
import { UseFormReturn } from "react-hook-form";
|
|
3
|
+
|
|
4
|
+
export interface ServerValidationErrorResponse {
|
|
5
|
+
message: string;
|
|
6
|
+
fieldErrors: Map<string, Array<string>>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function zodServerResolver(e: AxiosError, form: UseFormReturn<any>) {
|
|
10
|
+
const data = e.response?.data as ServerValidationErrorResponse | undefined;
|
|
11
|
+
if (!data) return;
|
|
12
|
+
|
|
13
|
+
if (data.fieldErrors) {
|
|
14
|
+
Object.entries(data.fieldErrors).forEach(([key, value]) => {
|
|
15
|
+
form.setError(key, {
|
|
16
|
+
type: "server",
|
|
17
|
+
message: value[0],
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
} else if (data.message) {
|
|
21
|
+
form.setError("root", {
|
|
22
|
+
type: "server",
|
|
23
|
+
message: data.message,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
package/zod/v3/index.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import z, {
|
|
2
|
+
ZodArray,
|
|
3
|
+
ZodBoolean,
|
|
4
|
+
ZodDefault,
|
|
5
|
+
ZodEffects,
|
|
6
|
+
ZodNullable,
|
|
7
|
+
ZodNumber,
|
|
8
|
+
ZodObject,
|
|
9
|
+
ZodSchema,
|
|
10
|
+
ZodString,
|
|
11
|
+
} from "zod/v3";
|
|
12
|
+
|
|
13
|
+
export function zodSchemaDefaults<S extends ZodObject<any>, T = z.infer<S>>(
|
|
14
|
+
schema: S,
|
|
15
|
+
overrides?: T
|
|
16
|
+
): any {
|
|
17
|
+
return (Object.entries(schema._def.shape()) as [keyof T, ZodSchema][]).reduce(
|
|
18
|
+
(acc, [key, value]) => {
|
|
19
|
+
const defaultValue = overrides && overrides[key];
|
|
20
|
+
|
|
21
|
+
if (value instanceof ZodObject) {
|
|
22
|
+
acc[key] = zodSchemaDefaults(
|
|
23
|
+
value,
|
|
24
|
+
defaultValue
|
|
25
|
+
) as unknown as T[keyof T];
|
|
26
|
+
} else if (
|
|
27
|
+
value instanceof ZodArray ||
|
|
28
|
+
(value instanceof ZodEffects && value._def.schema instanceof ZodArray)
|
|
29
|
+
) {
|
|
30
|
+
acc[key] = [] as unknown as T[keyof T];
|
|
31
|
+
} else if (value instanceof ZodDefault) {
|
|
32
|
+
acc[key] =
|
|
33
|
+
defaultValue ?? (value._def.defaultValue() as unknown as T[keyof T]);
|
|
34
|
+
} else if (
|
|
35
|
+
value instanceof ZodString ||
|
|
36
|
+
(value instanceof ZodEffects && value._def.schema instanceof ZodString)
|
|
37
|
+
) {
|
|
38
|
+
acc[key] = defaultValue ?? ("" as unknown as T[keyof T]);
|
|
39
|
+
} else if (
|
|
40
|
+
value instanceof ZodBoolean ||
|
|
41
|
+
(value instanceof ZodEffects && value._def.schema instanceof ZodBoolean)
|
|
42
|
+
) {
|
|
43
|
+
acc[key] = defaultValue ?? (false as unknown as T[keyof T]);
|
|
44
|
+
} else if (
|
|
45
|
+
value instanceof ZodNumber ||
|
|
46
|
+
(value instanceof ZodEffects && value._def.schema instanceof ZodNumber)
|
|
47
|
+
) {
|
|
48
|
+
acc[key] = ((typeof defaultValue === "string"
|
|
49
|
+
? Number.parseFloat(defaultValue)
|
|
50
|
+
: defaultValue) ?? 0) as T[keyof T];
|
|
51
|
+
} else if (value instanceof ZodNullable) {
|
|
52
|
+
acc[key] = null as T[keyof T];
|
|
53
|
+
}
|
|
54
|
+
return acc;
|
|
55
|
+
},
|
|
56
|
+
{} as T
|
|
57
|
+
);
|
|
58
|
+
}
|
package/index.ts
DELETED
package/tsconfig.node.json
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
|
|
4
|
-
"target": "ES2023",
|
|
5
|
-
"lib": ["ES2023"],
|
|
6
|
-
"module": "ESNext",
|
|
7
|
-
"types": ["node"],
|
|
8
|
-
"skipLibCheck": true,
|
|
9
|
-
|
|
10
|
-
/* Bundler mode */
|
|
11
|
-
"moduleResolution": "bundler",
|
|
12
|
-
"allowImportingTsExtensions": true,
|
|
13
|
-
"verbatimModuleSyntax": true,
|
|
14
|
-
"moduleDetection": "force",
|
|
15
|
-
"noEmit": true,
|
|
16
|
-
|
|
17
|
-
/* Linting */
|
|
18
|
-
"strict": true,
|
|
19
|
-
"noUnusedLocals": true,
|
|
20
|
-
"noUnusedParameters": true,
|
|
21
|
-
"erasableSyntaxOnly": true,
|
|
22
|
-
"noFallthroughCasesInSwitch": true,
|
|
23
|
-
"noUncheckedSideEffectImports": true
|
|
24
|
-
},
|
|
25
|
-
"include": ["vite.config.ts"]
|
|
26
|
-
}
|