@@ -416,7 +416,82 @@ export type EndpointContext<
416416 ) => APIError ;
417417} ;
418418
419- type EndpointHandler <
419+ export type ExtractBody < E extends EndpointBodyMethodOptions > = E extends {
420+ method : infer M ;
421+ body ?: never ;
422+ }
423+ ? {
424+ method : M ;
425+ }
426+ : E extends {
427+ method : ( "POST" | "PUT" | "DELETE" | "PATCH" | "GET" | "HEAD" ) [ ] ;
428+ body ?: StandardSchemaV1 < infer B > ;
429+ }
430+ ? E extends {
431+ method : infer M ;
432+ body ?: StandardSchemaV1 < B > ;
433+ }
434+ ? { method : M ; body : StandardSchemaV1 < B > }
435+ : never
436+ : E extends {
437+ method :
438+ | "POST"
439+ | "PUT"
440+ | "DELETE"
441+ | "PATCH"
442+ | ( "POST" | "PUT" | "DELETE" | "PATCH" ) [ ] ;
443+ body ?: StandardSchemaV1 < infer B > ;
444+ }
445+ ? E extends {
446+ method : infer M ;
447+ body ?: StandardSchemaV1 < B > ;
448+ }
449+ ? { method : M ; body : StandardSchemaV1 < B > }
450+ : never
451+ : E extends {
452+ method : "*" ;
453+ body ?: StandardSchemaV1 < infer B > ;
454+ }
455+ ? {
456+ method : "*" ;
457+ body ?: StandardSchemaV1 < B > ;
458+ }
459+ : E extends {
460+ method : "GET" | "HEAD" | ( "GET" | "HEAD" ) [ ] ;
461+ body ?: never ;
462+ }
463+ ? E extends { method : infer M }
464+ ? { method : M }
465+ : never
466+ : never ;
467+ export type ExtractError < E extends EndpointOptions > = E extends {
468+ error ?: StandardSchemaV1 < infer Err > ;
469+ }
470+ ? {
471+ error : StandardSchemaV1 < Err > ;
472+ }
473+ : { } ;
474+ export type ExtractQuery < E extends EndpointOptions > = E extends {
475+ query ?: StandardSchemaV1 < infer Q > ;
476+ }
477+ ? {
478+ query : StandardSchemaV1 < Q > ;
479+ }
480+ : { } ;
481+
482+ export type ExtractOthers < E extends EndpointOptions > = Pick <
483+ E ,
484+ Exclude < keyof E , "method" | "body" | "query" | "error" >
485+ > ;
486+
487+ export type ExtractStandSchema < E extends EndpointOptions > = Prettify <
488+ ExtractOthers < E >
489+ > &
490+ ExtractBody < E > &
491+ ExtractQuery < E > &
492+ ExtractError < E > ;
493+
494+ export type EndpointHandler <
420495 Path extends string ,
421496 Options extends EndpointOptions ,
422497 R ,
@@ -430,12 +505,12 @@ export function createEndpoint<
430505 path : Path ,
431506 options : Options ,
432507 handler : EndpointHandler < Path , Options , R > ,
433- ) : StrictEndpoint < Path , Options , R > ;
508+ ) : StrictEndpoint < Path , ExtractStandSchema < Options > , R > ;
434509
435510export function createEndpoint < Options extends EndpointOptions , R > (
436511 options : Options ,
437512 handler : EndpointHandler < never , Options , R > ,
438- ) : StrictEndpoint < never , Options , R > ;
513+ ) : StrictEndpoint < never , ExtractStandSchema < Options > , R > ;
439514
440515export function createEndpoint <
441516 Path extends string ,
@@ -445,7 +520,7 @@ export function createEndpoint<
445520 pathOrOptions : Path | Options ,
446521 handlerOrOptions : EndpointHandler < Path , Options , R > | Options ,
447522 handlerOrNever ?: any ,
448- ) : StrictEndpoint < Path , Options , R > {
523+ ) : StrictEndpoint < Path , ExtractStandSchema < Options > , R > {
449524 const path : string | undefined =
450525 typeof pathOrOptions === "string" ? pathOrOptions : undefined ;
451526 const options : Options =
@@ -574,7 +649,11 @@ export function createEndpoint<
574649 } ;
575650 internalHandler . options = options ;
576651 internalHandler . path = path ;
577- return internalHandler as unknown as StrictEndpoint < Path , Options , R > ;
652+ return internalHandler as unknown as StrictEndpoint <
653+ Path ,
654+ ExtractStandSchema < Options > ,
655+ R
656+ > ;
578657}
579658
580659createEndpoint . create = < E extends { use ?: Middleware [ ] } > ( opts ?: E ) => {
0 commit comments