@daviapps/react-utils 0.0.2 → 0.0.3

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.
@@ -0,0 +1,32 @@
1
+ module.exports = {
2
+ root: true,
3
+ env: { browser: true, es2020: true },
4
+ extends: [
5
+ 'eslint:recommended',
6
+ 'plugin:@typescript-eslint/recommended',
7
+ 'plugin:react-hooks/recommended',
8
+ ],
9
+ ignorePatterns: ['dist', '.eslintrc.cjs', 'tailwind.config.js', 'tailwind.config.js'],
10
+ parser: '@typescript-eslint/parser',
11
+ plugins: ['react-refresh'],
12
+ rules: {
13
+ 'react-refresh/only-export-components': [
14
+ 'warn',
15
+ { allowConstantExport: true },
16
+ ],
17
+ "@typescript-eslint/ban-ts-ignore": "off",
18
+ "@typescript-eslint/ban-ts-comment": "off",
19
+ "@typescript-eslint/no-unused-vars": [
20
+ "warn",
21
+ {
22
+ "args": "all",
23
+ "argsIgnorePattern": "^_",
24
+ "caughtErrors": "all",
25
+ "caughtErrorsIgnorePattern": "^_",
26
+ "destructuredArrayIgnorePattern": "^_",
27
+ "varsIgnorePattern": "^_",
28
+ "ignoreRestSiblings": true
29
+ }
30
+ ],
31
+ }
32
+ }
@@ -1,3 +1,5 @@
1
+ import { AxiosError } from "axios";
2
+ // import type { FieldValues, UseFormReturn } from "react-hook-form";
1
3
  import z, {
2
4
  ZodArray,
3
5
  ZodBoolean,
@@ -56,3 +58,30 @@ export function zodSchemaDefaults<S extends ZodObject<any>, T = z.infer<S>>(
56
58
  {} as T
57
59
  );
58
60
  }
61
+
62
+ export interface ServerValidationErrorResponse {
63
+ message: string;
64
+ fieldErrors: Map<string, Array<string>>;
65
+ }
66
+
67
+ export function zodServerResolver(
68
+ e: AxiosError,
69
+ form: any /*UseFormReturn<any>*/
70
+ ) {
71
+ const data = e.response?.data as ServerValidationErrorResponse | undefined;
72
+ if (!data) return;
73
+
74
+ if (data.fieldErrors) {
75
+ Object.entries(data.fieldErrors).forEach(([key, value]) => {
76
+ form.setError(key, {
77
+ type: "server",
78
+ message: value[0],
79
+ });
80
+ });
81
+ } else if (data.message) {
82
+ form.setError("root", {
83
+ type: "server",
84
+ message: data.message,
85
+ });
86
+ }
87
+ }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@daviapps/react-utils",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "axios": "^1.13.2",
7
- "react-hook-form": "^7.66.1",
7
+ "react-hook-form": "7.51.5",
8
8
  "zod": "^4.1.12"
9
9
  }
10
10
  }
package/tsconfig.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "compilerOptions": {
3
+ "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.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": false,
22
+ "noFallthroughCasesInSwitch": true,
23
+ "noUncheckedSideEffectImports": true,
24
+
25
+ "baseUrl": "."
26
+ }
27
+ }
@@ -1,26 +0,0 @@
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
- }