@@ -51,11 +51,13 @@ export type Mod<
51
51
ModArgsValidator extends PropertyValidators ,
52
52
ModCtx extends Record < string , any > ,
53
53
ModMadeArgs extends Record < string , any > ,
54
+ ExtraArgs extends Record < string , any > = Record < string , any > ,
54
55
> = {
55
56
args : ModArgsValidator ;
56
57
input : (
57
58
ctx : Ctx ,
58
59
args : ObjectType < ModArgsValidator > ,
60
+ extra : ExtraArgs ,
59
61
) =>
60
62
| Promise < { ctx : ModCtx ; args : ModMadeArgs } >
61
63
| { ctx : ModCtx ; args : ModMadeArgs } ;
@@ -70,12 +72,13 @@ export type Mod<
70
72
export function customCtx <
71
73
InCtx extends Record < string , any > ,
72
74
OutCtx extends Record < string , any > ,
75
+ ExtraArgs extends Record < string , any > = Record < string , any > ,
73
76
> (
74
- mod : ( original : InCtx ) => Promise < OutCtx > | OutCtx ,
75
- ) : Mod < InCtx , Record < string , never > , OutCtx , Record < string , never > > {
77
+ mod : ( original : InCtx , extra : ExtraArgs ) => Promise < OutCtx > | OutCtx ,
78
+ ) : Mod < InCtx , Record < string , never > , OutCtx , Record < string , never > , ExtraArgs > {
76
79
return {
77
80
args : { } ,
78
- input : async ( ctx ) => ( { ctx : await mod ( ctx ) , args : { } } ) ,
81
+ input : async ( ctx , _ , extra ) => ( { ctx : await mod ( ctx , extra ) , args : { } } ) ,
79
82
} ;
80
83
}
81
84
@@ -147,17 +150,25 @@ export function customQuery<
147
150
ModMadeArgs extends Record < string , any > ,
148
151
Visibility extends FunctionVisibility ,
149
152
DataModel extends GenericDataModel ,
153
+ ExtraArgs extends Record < string , any > = Record < string , any > ,
150
154
> (
151
155
query : QueryBuilder < DataModel , Visibility > ,
152
- mod : Mod < GenericQueryCtx < DataModel > , ModArgsValidator , ModCtx , ModMadeArgs > ,
156
+ mod : Mod <
157
+ GenericQueryCtx < DataModel > ,
158
+ ModArgsValidator ,
159
+ ModCtx ,
160
+ ModMadeArgs ,
161
+ ExtraArgs
162
+ > ,
153
163
) {
154
164
return customFnBuilder ( query , mod ) as CustomBuilder <
155
165
"query" ,
156
166
ModArgsValidator ,
157
167
ModCtx ,
158
168
ModMadeArgs ,
159
169
GenericQueryCtx < DataModel > ,
160
- Visibility
170
+ Visibility ,
171
+ ExtraArgs
161
172
> ;
162
173
}
163
174
@@ -219,13 +230,15 @@ export function customMutation<
219
230
ModMadeArgs extends Record < string , any > ,
220
231
Visibility extends FunctionVisibility ,
221
232
DataModel extends GenericDataModel ,
233
+ ExtraArgs extends Record < string , any > = Record < string , any > ,
222
234
> (
223
235
mutation : MutationBuilder < DataModel , Visibility > ,
224
236
mod : Mod <
225
237
GenericMutationCtx < DataModel > ,
226
238
ModArgsValidator ,
227
239
ModCtx ,
228
- ModMadeArgs
240
+ ModMadeArgs ,
241
+ ExtraArgs
229
242
> ,
230
243
) {
231
244
return customFnBuilder ( mutation , mod ) as CustomBuilder <
@@ -234,7 +247,8 @@ export function customMutation<
234
247
ModCtx ,
235
248
ModMadeArgs ,
236
249
GenericMutationCtx < DataModel > ,
237
- Visibility
250
+ Visibility ,
251
+ ExtraArgs
238
252
> ;
239
253
}
240
254
@@ -298,44 +312,55 @@ export function customAction<
298
312
ModMadeArgs extends Record < string , any > ,
299
313
Visibility extends FunctionVisibility ,
300
314
DataModel extends GenericDataModel ,
315
+ ExtraArgs extends Record < string , any > = Record < string , any > ,
301
316
> (
302
317
action : ActionBuilder < DataModel , Visibility > ,
303
- mod : Mod < GenericActionCtx < DataModel > , ModArgsValidator , ModCtx , ModMadeArgs > ,
318
+ mod : Mod <
319
+ GenericActionCtx < DataModel > ,
320
+ ModArgsValidator ,
321
+ ModCtx ,
322
+ ModMadeArgs ,
323
+ ExtraArgs
324
+ > ,
304
325
) : CustomBuilder <
305
326
"action" ,
306
327
ModArgsValidator ,
307
328
ModCtx ,
308
329
ModMadeArgs ,
309
330
GenericActionCtx < DataModel > ,
310
- Visibility
331
+ Visibility ,
332
+ ExtraArgs
311
333
> {
312
334
return customFnBuilder ( action , mod ) as CustomBuilder <
313
335
"action" ,
314
336
ModArgsValidator ,
315
337
ModCtx ,
316
338
ModMadeArgs ,
317
339
GenericActionCtx < DataModel > ,
318
- Visibility
340
+ Visibility ,
341
+ ExtraArgs
319
342
> ;
320
343
}
321
344
322
345
function customFnBuilder (
323
346
builder : ( args : any ) => any ,
324
- mod : Mod < any , any , any , any > ,
347
+ mod : Mod < any , any , any , any , any > ,
325
348
) {
326
349
// Looking forward to when input / args / ... are optional
327
350
const inputMod = mod . input ?? NoOp . input ;
328
351
const inputArgs = mod . args ?? NoOp . args ;
329
352
return function customBuilder ( fn : any ) : any {
330
- const handler = fn . handler ?? fn ;
331
- if ( "args" in fn ) {
353
+ // N.B.: This is fine if it's a function
354
+ const { args, handler = fn , returns, ...extra } = fn ;
355
+ if ( args ) {
332
356
return builder ( {
333
- args : addArgs ( fn . args , inputArgs ) ,
334
- returns : fn . returns ,
357
+ args : addArgs ( args , inputArgs ) ,
358
+ returns,
335
359
handler : async ( ctx : any , allArgs : any ) => {
336
360
const added = await inputMod (
337
361
ctx ,
338
362
pick ( allArgs , Object . keys ( inputArgs ) ) as any ,
363
+ extra ,
339
364
) ;
340
365
const args = omit ( allArgs , Object . keys ( inputArgs ) ) ;
341
366
return handler ( { ...ctx , ...added . ctx } , { ...args , ...added . args } ) ;
@@ -351,7 +376,7 @@ function customFnBuilder(
351
376
return builder ( {
352
377
returns : fn . returns ,
353
378
handler : async ( ctx : any , args : any ) => {
354
- const added = await inputMod ( ctx , args ) ;
379
+ const added = await inputMod ( ctx , args , extra ) ;
355
380
return handler ( { ...ctx , ...added . ctx } , { ...args , ...added . args } ) ;
356
381
} ,
357
382
} ) ;
@@ -430,6 +455,7 @@ export type CustomBuilder<
430
455
ModMadeArgs extends Record < string , any > ,
431
456
InputCtx ,
432
457
Visibility extends FunctionVisibility ,
458
+ ExtraArgs extends Record < string , any > ,
433
459
> = {
434
460
<
435
461
ArgsValidator extends PropertyValidators | void | Validator < any , any , any > ,
@@ -439,14 +465,18 @@ export type CustomBuilder<
439
465
ArgsArrayForOptionalValidator < ArgsValidator > = DefaultArgsForOptionalValidator < ArgsValidator > ,
440
466
> (
441
467
func :
442
- | {
468
+ | ( {
443
469
args ?: ArgsValidator ;
444
470
returns ?: ReturnsValidator ;
445
471
handler : (
446
472
ctx : Overwrite < InputCtx , ModCtx > ,
447
473
...args : ArgsForHandlerType < OneOrZeroArgs , ModMadeArgs >
448
474
) => ReturnValue ;
449
- }
475
+ } & {
476
+ [ key in keyof ExtraArgs as key extends "args" | "returns" | "handler"
477
+ ? never
478
+ : key ] : ExtraArgs [ key ] ;
479
+ } )
450
480
| {
451
481
(
452
482
ctx : Overwrite < InputCtx , ModCtx > ,
@@ -474,9 +504,10 @@ export type CustomCtx<Builder> =
474
504
infer ModCtx ,
475
505
any ,
476
506
infer InputCtx ,
507
+ any ,
477
508
any
478
509
>
479
510
? Overwrite < InputCtx , ModCtx >
480
511
: never ;
481
512
482
- type Overwrite < T , U > = Omit < T , keyof U > & U ;
513
+ type Overwrite < T , U > = keyof U extends never ? T : Omit < T , keyof U > & U ;
0 commit comments