1
1
import type { Infer , Schema } from "@typeschema/main" ;
2
2
import type { } from "zod" ;
3
3
import { actionBuilder } from "./action-builder" ;
4
- import type { MiddlewareFn , SafeActionClientOpts , ServerCodeFn , StateServerCodeFn } from "./index.types" ;
4
+ import type { DVES , MiddlewareFn , SafeActionClientOpts , ServerCodeFn , StateServerCodeFn } from "./index.types" ;
5
5
import type {
6
6
BindArgsValidationErrors ,
7
- FormatBindArgsValidationErrorsFn ,
8
- FormatValidationErrorsFn ,
7
+ FlattenedBindArgsValidationErrors ,
8
+ FlattenedValidationErrors ,
9
+ HandleBindArgsValidationErrorsShapeFn ,
10
+ HandleValidationErrorsShapeFn ,
9
11
ValidationErrors ,
10
12
} from "./validation-errors.types" ;
11
13
12
14
export class SafeActionClient <
13
15
ServerError ,
16
+ ODVES extends DVES | undefined ,
14
17
MetadataSchema extends Schema | undefined = undefined ,
15
18
MD = MetadataSchema extends Schema ? Infer < Schema > : undefined ,
16
19
Ctx = undefined ,
17
20
S extends Schema | undefined = undefined ,
18
21
const BAS extends readonly Schema [ ] = [ ] ,
19
- CVE = ValidationErrors < S > ,
20
- const CBAVE = BindArgsValidationErrors < BAS > ,
22
+ CVE = undefined ,
23
+ const CBAVE = undefined ,
21
24
> {
22
- readonly #handleServerErrorLog: NonNullable < SafeActionClientOpts < ServerError , any > [ "handleServerErrorLog" ] > ;
23
- readonly #handleReturnedServerError: NonNullable < SafeActionClientOpts < ServerError , any > [ "handleReturnedServerError" ] > ;
24
25
readonly #validationStrategy: "typeschema" | "zod" ;
25
-
26
- #middlewareFns: MiddlewareFn < ServerError , any , any , any > [ ] ;
27
- #ctxType = undefined as Ctx ;
28
- #metadataSchema: MetadataSchema ;
29
- #metadata: MD ;
30
- #schema: S ;
31
- #bindArgsSchemas: BAS ;
32
- #formatValidationErrorsFn: FormatValidationErrorsFn < S , CVE > ;
33
- #formatBindArgsValidationErrorsFn: FormatBindArgsValidationErrorsFn < BAS , CBAVE > ;
26
+ readonly #handleServerErrorLog: NonNullable < SafeActionClientOpts < ServerError , any , any > [ "handleServerErrorLog" ] > ;
27
+ readonly #handleReturnedServerError: NonNullable <
28
+ SafeActionClientOpts < ServerError , any , any > [ "handleReturnedServerError" ]
29
+ > ;
30
+ readonly #middlewareFns: MiddlewareFn < ServerError , any , any , any > [ ] ;
31
+ readonly #ctxType = undefined as Ctx ;
32
+ readonly #metadataSchema: MetadataSchema ;
33
+ readonly #metadata: MD ;
34
+ readonly #schema: S ;
35
+ readonly #bindArgsSchemas: BAS ;
36
+ readonly #handleValidationErrorsShape: HandleValidationErrorsShapeFn < S , CVE > ;
37
+ readonly #handleBindArgsValidationErrorsShape: HandleBindArgsValidationErrorsShapeFn < BAS , CBAVE > ;
38
+ readonly #defaultValidationErrorsShape: ODVES ;
34
39
35
40
constructor (
36
41
opts : {
@@ -40,10 +45,15 @@ export class SafeActionClient<
40
45
metadata : MD ;
41
46
schema : S ;
42
47
bindArgsSchemas : BAS ;
43
- formatValidationErrorsFn : FormatValidationErrorsFn < S , CVE > ;
44
- formatBindArgsValidationErrorsFn : FormatBindArgsValidationErrorsFn < BAS , CBAVE > ;
48
+ handleValidationErrorsShape : HandleValidationErrorsShapeFn < S , CVE > ;
49
+ handleBindArgsValidationErrorsShape : HandleBindArgsValidationErrorsShapeFn < BAS , CBAVE > ;
45
50
ctxType : Ctx ;
46
- } & Required < Pick < SafeActionClientOpts < ServerError , any > , "handleReturnedServerError" | "handleServerErrorLog" > >
51
+ } & Required <
52
+ Pick <
53
+ SafeActionClientOpts < ServerError , any , ODVES > ,
54
+ "handleReturnedServerError" | "handleServerErrorLog" | "defaultValidationErrorsShape"
55
+ >
56
+ >
47
57
) {
48
58
this . #middlewareFns = opts . middlewareFns ;
49
59
this . #handleServerErrorLog = opts . handleServerErrorLog ;
@@ -53,8 +63,9 @@ export class SafeActionClient<
53
63
this . #metadata = opts . metadata ;
54
64
this . #schema = ( opts . schema ?? undefined ) as S ;
55
65
this . #bindArgsSchemas = opts . bindArgsSchemas ?? [ ] ;
56
- this . #formatValidationErrorsFn = opts . formatValidationErrorsFn ;
57
- this . #formatBindArgsValidationErrorsFn = opts . formatBindArgsValidationErrorsFn ;
66
+ this . #handleValidationErrorsShape = opts . handleValidationErrorsShape ;
67
+ this . #handleBindArgsValidationErrorsShape = opts . handleBindArgsValidationErrorsShape ;
68
+ this . #defaultValidationErrorsShape = opts . defaultValidationErrorsShape ;
58
69
}
59
70
60
71
/**
@@ -73,9 +84,10 @@ export class SafeActionClient<
73
84
metadata : this . #metadata,
74
85
schema : this . #schema,
75
86
bindArgsSchemas : this . #bindArgsSchemas,
76
- formatValidationErrorsFn : this . #formatValidationErrorsFn ,
77
- formatBindArgsValidationErrorsFn : this . #formatBindArgsValidationErrorsFn ,
87
+ handleValidationErrorsShape : this . #handleValidationErrorsShape ,
88
+ handleBindArgsValidationErrorsShape : this . #handleBindArgsValidationErrorsShape ,
78
89
ctxType : undefined as NextCtx ,
90
+ defaultValidationErrorsShape : this . #defaultValidationErrorsShape,
79
91
} ) ;
80
92
}
81
93
@@ -95,9 +107,10 @@ export class SafeActionClient<
95
107
metadata : data ,
96
108
schema : this . #schema,
97
109
bindArgsSchemas : this . #bindArgsSchemas,
98
- formatValidationErrorsFn : this . #formatValidationErrorsFn ,
99
- formatBindArgsValidationErrorsFn : this . #formatBindArgsValidationErrorsFn ,
110
+ handleValidationErrorsShape : this . #handleValidationErrorsShape ,
111
+ handleBindArgsValidationErrorsShape : this . #handleBindArgsValidationErrorsShape ,
100
112
ctxType : undefined as Ctx ,
113
+ defaultValidationErrorsShape : this . #defaultValidationErrorsShape,
101
114
} ) ;
102
115
}
103
116
@@ -108,10 +121,13 @@ export class SafeActionClient<
108
121
*
109
122
* {@link https://next-safe-action.dev/docs/safe-action-client/instance-methods#schema See docs for more information}
110
123
*/
111
- schema < OS extends Schema , OCVE = ValidationErrors < OS > > (
124
+ schema <
125
+ OS extends Schema ,
126
+ OCVE = ODVES extends "flattened" ? FlattenedValidationErrors < ValidationErrors < OS > > : ValidationErrors < OS > ,
127
+ > (
112
128
schema : OS ,
113
129
utils ?: {
114
- formatValidationErrors ?: FormatValidationErrorsFn < OS , OCVE > ;
130
+ handleValidationErrorsShape ?: HandleValidationErrorsShapeFn < OS , OCVE > ;
115
131
}
116
132
) {
117
133
return new SafeActionClient ( {
@@ -123,10 +139,11 @@ export class SafeActionClient<
123
139
metadata : this . #metadata,
124
140
schema,
125
141
bindArgsSchemas : this . #bindArgsSchemas,
126
- formatValidationErrorsFn : ( utils ?. formatValidationErrors ??
127
- this . #formatValidationErrorsFn ) as FormatValidationErrorsFn < OS , OCVE > ,
128
- formatBindArgsValidationErrorsFn : this . #formatBindArgsValidationErrorsFn ,
142
+ handleValidationErrorsShape : ( utils ?. handleValidationErrorsShape ??
143
+ this . #handleValidationErrorsShape ) as HandleValidationErrorsShapeFn < OS , OCVE > ,
144
+ handleBindArgsValidationErrorsShape : this . #handleBindArgsValidationErrorsShape ,
129
145
ctxType : undefined as Ctx ,
146
+ defaultValidationErrorsShape : this . #defaultValidationErrorsShape,
130
147
} ) ;
131
148
}
132
149
@@ -137,9 +154,14 @@ export class SafeActionClient<
137
154
*
138
155
* {@link https://next-safe-action.dev/docs/safe-action-client/instance-methods#schema See docs for more information}
139
156
*/
140
- bindArgsSchemas < const OBAS extends readonly Schema [ ] , OCBAVE = BindArgsValidationErrors < OBAS > > (
157
+ bindArgsSchemas <
158
+ const OBAS extends readonly Schema [ ] ,
159
+ OCBAVE = ODVES extends "flattened"
160
+ ? FlattenedBindArgsValidationErrors < BindArgsValidationErrors < OBAS > >
161
+ : BindArgsValidationErrors < OBAS > ,
162
+ > (
141
163
bindArgsSchemas : OBAS ,
142
- utils ?: { formatBindArgsValidationErrors ?: FormatBindArgsValidationErrorsFn < OBAS , OCBAVE > }
164
+ utils ?: { handleBindArgsValidationErrorsShape ?: HandleBindArgsValidationErrorsShapeFn < OBAS , OCBAVE > }
143
165
) {
144
166
return new SafeActionClient ( {
145
167
middlewareFns : this . #middlewareFns,
@@ -150,10 +172,11 @@ export class SafeActionClient<
150
172
metadata : this . #metadata,
151
173
schema : this . #schema,
152
174
bindArgsSchemas,
153
- formatValidationErrorsFn : this . #formatValidationErrorsFn ,
154
- formatBindArgsValidationErrorsFn : ( utils ?. formatBindArgsValidationErrors ??
155
- this . #formatBindArgsValidationErrorsFn ) as FormatBindArgsValidationErrorsFn < OBAS , OCBAVE > ,
175
+ handleValidationErrorsShape : this . #handleValidationErrorsShape ,
176
+ handleBindArgsValidationErrorsShape : ( utils ?. handleBindArgsValidationErrorsShape ??
177
+ this . #handleBindArgsValidationErrorsShape ) as HandleBindArgsValidationErrorsShapeFn < OBAS , OCBAVE > ,
156
178
ctxType : undefined as Ctx ,
179
+ defaultValidationErrorsShape : this . #defaultValidationErrorsShape,
157
180
} ) ;
158
181
}
159
182
@@ -174,8 +197,8 @@ export class SafeActionClient<
174
197
metadata : this . #metadata,
175
198
schema : this . #schema,
176
199
bindArgsSchemas : this . #bindArgsSchemas,
177
- formatValidationErrors : this . #formatValidationErrorsFn ,
178
- formatBindArgsValidationErrors : this . #formatBindArgsValidationErrorsFn ,
200
+ handleValidationErrorsShape : this . #handleValidationErrorsShape ,
201
+ handleBindArgsValidationErrorsShape : this . #handleBindArgsValidationErrorsShape ,
179
202
} ) . action ( serverCodeFn ) ;
180
203
}
181
204
@@ -197,8 +220,8 @@ export class SafeActionClient<
197
220
metadata : this . #metadata,
198
221
schema : this . #schema,
199
222
bindArgsSchemas : this . #bindArgsSchemas,
200
- formatValidationErrors : this . #formatValidationErrorsFn ,
201
- formatBindArgsValidationErrors : this . #formatBindArgsValidationErrorsFn ,
223
+ handleValidationErrorsShape : this . #handleValidationErrorsShape ,
224
+ handleBindArgsValidationErrorsShape : this . #handleBindArgsValidationErrorsShape ,
202
225
} ) . stateAction ( serverCodeFn ) ;
203
226
}
204
227
}
0 commit comments