@@ -44,7 +44,7 @@ import * as Response from "../http/HttpServerResponse.ts"
4444import type { HttpServerResponse } from "../http/HttpServerResponse.ts"
4545import * as Multipart from "../http/Multipart.ts"
4646import * as UrlParams from "../http/UrlParams.ts"
47- import type * as HttpApi from "./HttpApi.ts"
47+ import * as HttpApi from "./HttpApi.ts"
4848import * as HttpApiEndpoint from "./HttpApiEndpoint.ts"
4949import { HttpApiSchemaError } from "./HttpApiError.ts"
5050import type * as HttpApiGroup from "./HttpApiGroup.ts"
@@ -151,7 +151,7 @@ export const group = <
151151 : result
152152 const routes : Array < HttpRouter . Route < any , any > > = [ ]
153153 for ( const item of handlers . handlers . values ( ) ) {
154- routes . push ( handlerToRoute ( group as any , item , services ) )
154+ routes . push ( handlerToRoute ( api , group as any , item , services ) )
155155 }
156156 return Context . makeUnsafe (
157157 new Map ( [ [ group . key , {
@@ -509,6 +509,7 @@ export const endpoint = <
509509 const group = api . groups [ groupIdentifier ] as unknown as HttpApiGroup . Top
510510 const endpoint = group . endpoints [ endpointIdentifier ]
511511 return Effect . succeed ( handlerToHttpEffect (
512+ api ,
512513 group ,
513514 endpoint ,
514515 Context . omit ( Scope . Scope ) ( context ) ,
@@ -721,11 +722,12 @@ type PayloadDecoder =
721722 }
722723
723724function buildPayloadDecoders (
724- payloadMap : HttpApiEndpoint . PayloadMap
725+ payloadMap : HttpApiEndpoint . PayloadMap ,
726+ options : SchemaAST . ParseOptions | undefined
725727) : Map < string , PayloadDecoder > {
726728 const result = new Map < string , PayloadDecoder > ( )
727729 payloadMap . forEach ( ( { encoding, schemas } , contentType ) => {
728- const decode = Schema . decodeUnknownEffect ( Schema . Union ( schemas ) )
730+ const decode = Schema . decodeUnknownEffect ( Schema . Union ( schemas ) , options )
729731 if ( encoding . _tag === "Multipart" ) {
730732 result . set ( contentType , { _tag : "Multipart" , mode : encoding . mode , limits : encoding . limits , decode } )
731733 } else {
@@ -800,25 +802,29 @@ function decodePayload(
800802}
801803
802804function handlerToHttpEffect (
805+ api : Pick < HttpApi . Top , "annotations" > ,
803806 group : HttpApiGroup . Top ,
804807 endpoint : HttpApiEndpoint . Top ,
805808 context : Context . Context < any > ,
806809 handler : HttpApiEndpoint . Handler < HttpApiEndpoint . Constraint , any , any > ,
807810 isRaw : boolean
808811) {
809- const encodeSuccess = Schema . encodeUnknownEffect ( makeSuccessSchema ( endpoint ) )
810- const encodeError = Schema . encodeUnknownEffect ( makeErrorSchema ( endpoint ) )
811- const decodeParams = UndefinedOr . map ( endpoint . params , Schema . decodeUnknownEffect )
812- const decodeHeaders = UndefinedOr . map ( endpoint . headers , Schema . decodeUnknownEffect )
812+ const annotations = Context . merge ( Context . merge ( api . annotations , group . annotations ) , endpoint . annotations )
813+ const options = Context . getOrUndefined ( annotations , HttpApi . ParseOptions )
814+ const decodeUnknownEffect = < S extends Schema . Constraint > ( schema : S ) => Schema . decodeUnknownEffect ( schema , options )
815+ const encodeSuccess = Schema . encodeUnknownEffect ( makeSuccessSchema ( endpoint ) , options )
816+ const encodeError = Schema . encodeUnknownEffect ( makeErrorSchema ( endpoint ) , options )
817+ const decodeParams = UndefinedOr . map ( endpoint . params , decodeUnknownEffect )
818+ const decodeHeaders = UndefinedOr . map ( endpoint . headers , decodeUnknownEffect )
813819 const decodeQuery = UndefinedOr . map (
814820 endpoint . query ,
815- ( schema ) => Schema . decodeUnknownEffect ( Schema . toCodecArrayFromSingle ( schema ) )
821+ ( schema ) => decodeUnknownEffect ( Schema . toCodecArrayFromSingle ( schema ) )
816822 )
817- const encodeStream = makeStreamEncoder ( endpoint )
818- const encodeWithHeaders = makeWithHeadersEncoder ( endpoint )
823+ const encodeStream = makeStreamEncoder ( endpoint , options )
824+ const encodeWithHeaders = makeWithHeadersEncoder ( endpoint , options )
819825
820826 const shouldParsePayload = endpoint . payload . size > 0 && ! isRaw
821- const payloadBy = shouldParsePayload ? buildPayloadDecoders ( endpoint . payload ) : undefined
827+ const payloadBy = shouldParsePayload ? buildPayloadDecoders ( endpoint . payload , options ) : undefined
822828
823829 return applyMiddleware (
824830 group ,
@@ -886,6 +892,7 @@ function handlerToHttpEffect(
886892
887893/** @internal */
888894export function handlerToRoute (
895+ api : Pick < HttpApi . Top , "annotations" > ,
889896 group : HttpApiGroup . Top ,
890897 handler : HandlerRuntime ,
891898 context : Context . Context < any >
@@ -894,7 +901,7 @@ export function handlerToRoute(
894901 return HttpRouter . route (
895902 endpoint . method ,
896903 HttpApiPath . toRouterPath ( endpoint . path , endpoint . params ) as HttpRouter . PathInput ,
897- handlerToHttpEffect ( group , endpoint , context , handler . handler , handler . isRaw ) ,
904+ handlerToHttpEffect ( api , group , endpoint , context , handler . handler , handler . isRaw ) ,
898905 { uninterruptible : handler . uninterruptible }
899906 )
900907}
@@ -992,12 +999,15 @@ interface WithHeadersEncoders {
992999 readonly encodeHeaders : ReadonlyMap < number , WithHeadersEncoder >
9931000}
9941001
995- function makeWithHeadersEncoder ( endpoint : HttpApiEndpoint . Top ) : WithHeadersEncoders | undefined {
1002+ function makeWithHeadersEncoder (
1003+ endpoint : HttpApiEndpoint . Top ,
1004+ options : SchemaAST . ParseOptions | undefined
1005+ ) : WithHeadersEncoders | undefined {
9961006 const encodeHeaders = new Map < number , WithHeadersEncoder > ( )
9971007 const bodySchemas : Array < Schema . ConstraintEncoder < HttpServerResponse , unknown > > = [ ]
9981008 for ( const schema of endpoint . success ) {
9991009 if ( ! HttpApiSchema . isWithHeaders ( schema ) ) continue
1000- encodeHeaders . set ( HttpApiSchema . getStatusSuccessSchema ( schema ) , Schema . encodeUnknownEffect ( schema . headers ) )
1010+ encodeHeaders . set ( HttpApiSchema . getStatusSuccessSchema ( schema ) , Schema . encodeUnknownEffect ( schema . headers , options ) )
10011011 if ( ! HttpApiSchema . isStreamSchema ( schema . schema ) ) {
10021012 bodySchemas . push ( toResponseSuccessSchema ( schema ) )
10031013 }
@@ -1011,12 +1021,15 @@ function makeWithHeadersEncoder(endpoint: HttpApiEndpoint.Top): WithHeadersEncod
10111021 ? bodySchemas [ 0 ]
10121022 : Schema . Union ( bodySchemas )
10131023 return {
1014- encodeBody : Schema . encodeUnknownEffect ( bodySchema ) ,
1024+ encodeBody : Schema . encodeUnknownEffect ( bodySchema , options ) ,
10151025 encodeHeaders
10161026 }
10171027}
10181028
1019- function makeStreamEncoder ( endpoint : HttpApiEndpoint . Top ) : StreamEncoder | undefined {
1029+ function makeStreamEncoder (
1030+ endpoint : HttpApiEndpoint . Top ,
1031+ options : SchemaAST . ParseOptions | undefined
1032+ ) : StreamEncoder | undefined {
10201033 const successSchema = getStreamSuccessSchema ( endpoint )
10211034 if ( successSchema === undefined ) {
10221035 return undefined
@@ -1045,7 +1058,7 @@ function makeStreamEncoder(endpoint: HttpApiEndpoint.Top): StreamEncoder | undef
10451058 }
10461059 }
10471060
1048- const sseEncoder = makeSseEncoder ( streamSchema )
1061+ const sseEncoder = makeSseEncoder ( streamSchema , options )
10491062
10501063 return ( response , context ) => {
10511064 if ( ! Stream . isStream ( response ) ) {
@@ -1090,13 +1103,14 @@ interface SseStreamEncoder {
10901103}
10911104
10921105function makeSseEncoder < Events extends Sse . EventCodec , Error extends Schema . Constraint > (
1093- streamSchema : HttpApiSchema . StreamSse < Events , Error , unknown >
1106+ streamSchema : HttpApiSchema . StreamSse < Events , Error , unknown > ,
1107+ options : SchemaAST . ParseOptions | undefined
10941108) : SseStreamEncoder {
10951109 const CauseSchema = Schema . toCodecJson ( Schema . Cause ( streamSchema . error , Schema . Defect ( ) ) )
10961110 return {
10971111 sseMode : streamSchema . sseMode ,
1098- encodeEvents : Schema . encodeUnknownEffect ( Schema . Array ( streamSchema . events ) ) as any ,
1099- encodeCause : Schema . encodeUnknownEffect ( Schema . fromJsonString ( CauseSchema ) )
1112+ encodeEvents : Schema . encodeUnknownEffect ( Schema . Array ( streamSchema . events ) , options ) as any ,
1113+ encodeCause : Schema . encodeUnknownEffect ( Schema . fromJsonString ( CauseSchema ) , options )
11001114 }
11011115}
11021116
0 commit comments