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

Function handlerToHttpEffect

packages/effect/src/http-api/HttpApiBuilder.ts:938–1069  ·  view source on GitHub ↗
(
  api: Pick<HttpApi.Top, "annotations">,
  group: HttpApiGroup.Top,
  endpoint: HttpApiEndpoint.Top,
  context: Context.Context<any>,
  handler: HttpApiEndpoint.Handler<HttpApiEndpoint.Constraint, any, any>,
  isRaw: boolean
)

Source from the content-addressed store, hash-verified

936}
937
938function handlerToHttpEffect(
939 api: Pick<HttpApi.Top, "annotations">,
940 group: HttpApiGroup.Top,
941 endpoint: HttpApiEndpoint.Top,
942 context: Context.Context<any>,
943 handler: HttpApiEndpoint.Handler<HttpApiEndpoint.Constraint, any, any>,
944 isRaw: boolean
945) {
946 const annotations = Context.merge(Context.merge(api.annotations, group.annotations), endpoint.annotations)
947 const options = HttpApi.getSlotParseOptions(annotations)
948 const encodeSuccess = Schema.encodeUnknownEffect(makeSuccessSchema(endpoint, options.headers), options.success)
949 const encodeError = Schema.encodeUnknownEffect(makeErrorSchema(endpoint, options.headers), options.error)
950 const decodeParams = UndefinedOr.map(endpoint.params, (schema) => Schema.decodeUnknownEffect(schema, options.params))
951 const decodeHeaders = UndefinedOr.map(
952 endpoint.headers,
953 (schema) => Schema.decodeUnknownEffect(schema, options.headers)
954 )
955 const decodeQuery = UndefinedOr.map(
956 endpoint.query,
957 (schema) => Schema.decodeUnknownEffect(Schema.toCodecArrayFromSingle(schema), options.query)
958 )
959 const encodeStream = makeStreamEncoder(endpoint, options.success)
960 const encodeWithHeaders = makeWithHeadersEncoder(endpoint, options.success, options.headers)
961 const upgrade = getUpgradeSuccessSchema(endpoint)
962 // The handler of a mixed endpoint may decline the upgrade and return one of the other successes.
963 const upgradeOnly = upgrade !== undefined && endpoint.success.size === 1
964 const encodeUpgradeHeaders = UndefinedOr.map(
965 upgrade?.headers,
966 (schema) => Schema.encodeUnknownEffect(schema, options.headers)
967 )
968
969 const shouldParsePayload = endpoint.payload.size > 0 && !isRaw
970 const payloadBy = shouldParsePayload ? buildPayloadDecoders(endpoint.payload, options.payload) : undefined
971 const redactedNames = HttpApi.getEndpointRedactedNames(api, group, endpoint)
972
973 const effect = applyMiddleware(
974 group,
975 endpoint,
976 context,
977 Effect.gen(function*() {
978 const fiber = Fiber.getCurrent()!
979 const context = fiber.context
980 const httpRequest = Context.getUnsafe(context, HttpServerRequest)
981 const routeContext = Context.getUnsafe(context, HttpRouter.RouteContext)
982 const query = Context.getUnsafe(context, Request.ParsedSearchParams)
983 const request: any = {
984 request: httpRequest,
985 endpoint,
986 group
987 }
988 if (decodeParams) {
989 request.params = yield* HttpApiSchemaError.wrap("Params", decodeParams(routeContext.params))
990 }
991 if (decodeHeaders) {
992 request.headers = yield* HttpApiSchemaError.wrap("Headers", decodeHeaders(httpRequest.headers))
993 }
994 if (decodeQuery) {
995 request.query = yield* HttpApiSchemaError.wrap("Query", decodeQuery(query))

Callers 2

endpointFunction · 0.85
handlerToRouteFunction · 0.85

Calls 15

addRedactedNamesUnsafeFunction · 0.90
makeSuccessSchemaFunction · 0.85
makeStreamEncoderFunction · 0.85
makeWithHeadersEncoderFunction · 0.85
buildPayloadDecodersFunction · 0.85
decodeHeadersFunction · 0.85
isUpgradeRequestFunction · 0.85
upgradeRequiredFunction · 0.85
encodeErrorFunction · 0.85
wrapMethod · 0.80
makeErrorSchemaFunction · 0.70
getUpgradeSuccessSchemaFunction · 0.70

Tested by

no test coverage detected