MCPcopy
hub / github.com/Effect-TS/effect / handlerToRoute

Function handlerToRoute

packages/platform/src/HttpApiBuilder.ts:644–706  ·  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

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