@@ -5,7 +5,17 @@ import { test } from "node:test";
5
5
import { z } from "zod" ;
6
6
import { DEFAULT_SERVER_ERROR_MESSAGE , createSafeActionClient , returnValidationErrors } from "../../typeschema" ;
7
7
8
- const ac = createSafeActionClient ( ) ;
8
+ const ac = createSafeActionClient ( {
9
+ defineMetadataSchema ( ) {
10
+ return z . object ( {
11
+ actionName : z . string ( ) ,
12
+ } ) ;
13
+ } ,
14
+ } )
15
+ . use ( async ( { next } ) => {
16
+ return next ( { ctx : { foo : "bar" } } ) ;
17
+ } )
18
+ . metadata ( { actionName : "test" } ) ;
9
19
10
20
test ( "typeschema - action with no input schema and no server errors calls `onSuccess` and `onSettled` callbacks" , async ( ) => {
11
21
let executed = 0 ;
@@ -45,12 +55,14 @@ test("typeschema - action with input schemas and no errors calls `onSuccess` and
45
55
} ;
46
56
} ,
47
57
{
48
- onSuccess : ( { clientInput, bindArgsClientInputs, parsedInput, bindArgsParsedInputs, data } ) => {
58
+ onSuccess : ( { clientInput, bindArgsClientInputs, parsedInput, bindArgsParsedInputs, data, metadata , ctx } ) => {
49
59
executed ++ ;
50
60
51
61
assert . deepStrictEqual (
52
- { clientInput, bindArgsClientInputs, parsedInput, bindArgsParsedInputs, data } ,
62
+ { clientInput, bindArgsClientInputs, parsedInput, bindArgsParsedInputs, data, metadata , ctx } ,
53
63
{
64
+ metadata : { actionName : "test" } ,
65
+ ctx : { foo : "bar" } ,
54
66
clientInput : inputs [ 2 ] ,
55
67
bindArgsClientInputs : inputs . slice ( 0 , 2 ) ,
56
68
parsedInput : inputs [ 2 ] ,
@@ -64,12 +76,14 @@ test("typeschema - action with input schemas and no errors calls `onSuccess` and
64
76
onError : ( ) => {
65
77
executed ++ ; // should not be called
66
78
} ,
67
- onSettled : ( { clientInput, bindArgsClientInputs, result } ) => {
79
+ onSettled : ( { clientInput, bindArgsClientInputs, result, metadata , ctx } ) => {
68
80
executed ++ ;
69
81
70
82
assert . deepStrictEqual (
71
- { clientInput, bindArgsClientInputs, result } ,
83
+ { clientInput, bindArgsClientInputs, result, metadata , ctx } ,
72
84
{
85
+ metadata : { actionName : "test" } ,
86
+ ctx : { foo : "bar" } ,
73
87
clientInput : inputs [ 2 ] ,
74
88
bindArgsClientInputs : inputs . slice ( 0 , 2 ) ,
75
89
result : {
@@ -102,12 +116,14 @@ test("typeschema - action with input schemas and server error calls `onError` an
102
116
onSuccess : ( ) => {
103
117
executed ++ ; // should not be called
104
118
} ,
105
- onError ( { error, clientInput, bindArgsClientInputs } ) {
119
+ onError ( { error, clientInput, bindArgsClientInputs, metadata , ctx } ) {
106
120
executed ++ ;
107
121
108
122
assert . deepStrictEqual (
109
- { error, clientInput, bindArgsClientInputs } ,
123
+ { error, clientInput, bindArgsClientInputs, metadata , ctx } ,
110
124
{
125
+ metadata : { actionName : "test" } ,
126
+ ctx : { foo : "bar" } ,
111
127
error : {
112
128
serverError : DEFAULT_SERVER_ERROR_MESSAGE ,
113
129
} ,
@@ -116,12 +132,14 @@ test("typeschema - action with input schemas and server error calls `onError` an
116
132
}
117
133
) ;
118
134
} ,
119
- onSettled ( { clientInput, bindArgsClientInputs, result } ) {
135
+ onSettled ( { clientInput, bindArgsClientInputs, result, metadata , ctx } ) {
120
136
executed ++ ;
121
137
122
138
assert . deepStrictEqual (
123
- { result, clientInput, bindArgsClientInputs } ,
139
+ { result, clientInput, bindArgsClientInputs, metadata , ctx } ,
124
140
{
141
+ metadata : { actionName : "test" } ,
142
+ ctx : { foo : "bar" } ,
125
143
result : {
126
144
serverError : DEFAULT_SERVER_ERROR_MESSAGE ,
127
145
} ,
@@ -154,12 +172,14 @@ test("typeschema - action with validation errors calls `onError` and `onSettled`
154
172
onSuccess : ( ) => {
155
173
executed ++ ; // should not be called
156
174
} ,
157
- onError ( { error, clientInput, bindArgsClientInputs } ) {
175
+ onError ( { error, clientInput, bindArgsClientInputs, metadata , ctx } ) {
158
176
executed ++ ;
159
177
160
178
assert . deepStrictEqual (
161
- { error, clientInput, bindArgsClientInputs } ,
179
+ { error, clientInput, bindArgsClientInputs, metadata , ctx } ,
162
180
{
181
+ metadata : { actionName : "test" } ,
182
+ ctx : { foo : "bar" } ,
163
183
error : {
164
184
validationErrors : {
165
185
username : {
@@ -180,12 +200,14 @@ test("typeschema - action with validation errors calls `onError` and `onSettled`
180
200
}
181
201
) ;
182
202
} ,
183
- onSettled ( { clientInput, bindArgsClientInputs, result } ) {
203
+ onSettled ( { clientInput, bindArgsClientInputs, result, metadata , ctx } ) {
184
204
executed ++ ;
185
205
186
206
assert . deepStrictEqual (
187
- { result, clientInput, bindArgsClientInputs } ,
207
+ { result, clientInput, bindArgsClientInputs, metadata , ctx } ,
188
208
{
209
+ metadata : { actionName : "test" } ,
210
+ ctx : { foo : "bar" } ,
189
211
result : {
190
212
validationErrors : {
191
213
username : {
@@ -231,12 +253,14 @@ test("typeschema - action with server validation error calls `onError` and `onSe
231
253
onSuccess : ( ) => {
232
254
executed ++ ; // should not be called
233
255
} ,
234
- onError ( { error, clientInput, bindArgsClientInputs } ) {
256
+ onError ( { error, clientInput, bindArgsClientInputs, metadata , ctx } ) {
235
257
executed ++ ;
236
258
237
259
assert . deepStrictEqual (
238
- { error, clientInput, bindArgsClientInputs } ,
260
+ { error, clientInput, bindArgsClientInputs, metadata , ctx } ,
239
261
{
262
+ metadata : { actionName : "test" } ,
263
+ ctx : { foo : "bar" } ,
240
264
error : {
241
265
validationErrors : {
242
266
username : {
@@ -249,12 +273,14 @@ test("typeschema - action with server validation error calls `onError` and `onSe
249
273
}
250
274
) ;
251
275
} ,
252
- onSettled ( { clientInput, bindArgsClientInputs, result } ) {
276
+ onSettled ( { clientInput, bindArgsClientInputs, result, metadata , ctx } ) {
253
277
executed ++ ;
254
278
255
279
assert . deepStrictEqual (
256
- { result, clientInput, bindArgsClientInputs } ,
280
+ { result, clientInput, bindArgsClientInputs, metadata , ctx } ,
257
281
{
282
+ metadata : { actionName : "test" } ,
283
+ ctx : { foo : "bar" } ,
258
284
result : {
259
285
validationErrors : {
260
286
username : {
0 commit comments