MCPcopy Create free account
hub / github.com/Effect-TS/effect / handlerToRoute

Function handlerToRoute

packages/platform/src/HttpApiBuilder.ts:655–717  ·  view source on GitHub ↗
(
  endpoint_: HttpApiEndpoint.HttpApiEndpoint.Any,
  middleware: MiddlewareMap,
  handler: HttpApiEndpoint.HttpApiEndpoint.Handler<any, any, any>,
  isFullRequest: boolean,
  uninterruptible: boolean
)

Source from the content-addressed store, hash-verified

653}
654
655const handlerToRoute = (
656 endpoint_: HttpApiEndpoint.HttpApiEndpoint.Any,
657 middleware: MiddlewareMap,
658 handler: HttpApiEndpoint.HttpApiEndpoint.Handler<any, any, any>,
659 isFullRequest: boolean,
660 uninterruptible: boolean
661): HttpRouter.Route<any, any> => {
662 const endpoint = endpoint_ as HttpApiEndpoint.HttpApiEndpoint.AnyWithProps
663 const isMultipartStream = endpoint.payloadSchema.pipe(
664 Option.map(({ ast }) => HttpApiSchema.getMultipartStream(ast) !== undefined),
665 Option.getOrElse(constFalse)
666 )
667 const multipartLimits = endpoint.payloadSchema.pipe(
668 Option.flatMapNullable(({ ast }) => HttpApiSchema.getMultipart(ast) || HttpApiSchema.getMultipartStream(ast))
669 )
670 const decodePath = Option.map(endpoint.pathSchema, Schema.decodeUnknown)
671 const decodePayload = isFullRequest || isMultipartStream
672 ? Option.none()
673 : Option.map(endpoint.payloadSchema, Schema.decodeUnknown)
674 const decodeHeaders = Option.map(endpoint.headersSchema, Schema.decodeUnknown)
675 const encodeSuccess = Schema.encode(makeSuccessSchema(endpoint.successSchema))
676 return HttpRouter.makeRoute(
677 endpoint.method,
678 endpoint.path,
679 applyMiddleware(
680 middleware,
681 Effect.gen(function*() {
682 const fiber = Option.getOrThrow(Fiber.getCurrentFiber())
683 const context = fiber.currentContext
684 const httpRequest = Context.unsafeGet(context, HttpServerRequest.HttpServerRequest)
685 const routeContext = Context.unsafeGet(context, HttpRouter.RouteContext)
686 const urlParams = Context.unsafeGet(context, HttpServerRequest.ParsedSearchParams)
687 const request: any = { request: httpRequest }
688 if (decodePath._tag === "Some") {
689 request.path = yield* decodePath.value(routeContext.params)
690 }
691 if (decodePayload._tag === "Some") {
692 request.payload = yield* Effect.flatMap(
693 requestPayload(httpRequest, urlParams, multipartLimits),
694 decodePayload.value
695 )
696 } else if (isMultipartStream) {
697 request.payload = Option.match(multipartLimits, {
698 onNone: () => httpRequest.multipartStream,
699 onSome: (limits) => Multipart.withLimitsStream(httpRequest.multipartStream, limits)
700 })
701 }
702 if (decodeHeaders._tag === "Some") {
703 request.headers = yield* decodeHeaders.value(httpRequest.headers)
704 }
705 if (endpoint.urlParamsSchema._tag === "Some") {
706 const schema = endpoint.urlParamsSchema.value
707 request.urlParams = yield* Schema.decodeUnknown(schema)(normalizeUrlParams(urlParams, schema.ast))
708 }
709 const response = yield* handler(request)
710 return HttpServerResponse.isServerResponse(response) ? response : yield* encodeSuccess(response)
711 }).pipe(
712 Effect.catchIf(ParseResult.isParseError, HttpApiDecodeError.refailParseError)

Callers 1

groupFunction · 0.85

Calls 10

makeSuccessSchemaFunction · 0.85
requestPayloadFunction · 0.85
normalizeUrlParamsFunction · 0.85
encodeMethod · 0.80
unsafeGetMethod · 0.80
applyMiddlewareFunction · 0.70
handlerFunction · 0.70
pipeMethod · 0.65
mapMethod · 0.65
valueMethod · 0.45

Tested by

no test coverage detected