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

Function toWebHandler

packages/platform/src/HttpLayerRouter.ts:1134–1194  ·  view source on GitHub ↗
(
  appLayer: Layer.Layer<A, E, R>,
  options?: {
    readonly memoMap?: Layer.MemoMap | undefined
    readonly routerConfig?: Partial<FindMyWay.RouterConfig> | undefined
    readonly disableLogger?: boolean | undefined
    /**
     * Middleware to apply to the HTTP server.
     *
     * NOTE: This middleware is applied to the entire HTTP server chain,
     * including the sending of the response. This means that modifications
     * to the response **WILL NOT** be reflected in the final response sent to the
     * client.
     *
     * Use HttpLayerRouter.middleware to create middleware that can modify the
     * response.
     */
    readonly middleware?: (
      effect: Effect.Effect<
        HttpServerResponse.HttpServerResponse,
        Request.Only<"Error", R> | Request.Only<"GlobalError", R> | HttpServerError.RouteNotFound,
        | Scope.Scope
        | HttpServerRequest.HttpServerRequest
        | Request.Only<"Requires", R>
        | Request.Only<"GlobalRequires", R>
      >
    ) => Effect.Effect<HttpServerResponse.HttpServerResponse, HE, HR>
  }
)

Source from the content-addressed store, hash-verified

1132 * @category Server
1133 */
1134export const toWebHandler = <
1135 A,
1136 E,
1137 R extends
1138 | HttpRouter
1139 | Request<"Requires", any>
1140 | Request<"GlobalRequires", any>
1141 | Request<"Error", any>
1142 | Request<"GlobalError", any>,
1143 HE,
1144 HR = Exclude<Request.Only<"Requires", R> | Request.Only<"GlobalRequires", R>, A>
1145>(
1146 appLayer: Layer.Layer<A, E, R>,
1147 options?: {
1148 readonly memoMap?: Layer.MemoMap | undefined
1149 readonly routerConfig?: Partial<FindMyWay.RouterConfig> | undefined
1150 readonly disableLogger?: boolean | undefined
1151 /**
1152 * Middleware to apply to the HTTP server.
1153 *
1154 * NOTE: This middleware is applied to the entire HTTP server chain,
1155 * including the sending of the response. This means that modifications
1156 * to the response **WILL NOT** be reflected in the final response sent to the
1157 * client.
1158 *
1159 * Use HttpLayerRouter.middleware to create middleware that can modify the
1160 * response.
1161 */
1162 readonly middleware?: (
1163 effect: Effect.Effect<
1164 HttpServerResponse.HttpServerResponse,
1165 Request.Only<"Error", R> | Request.Only<"GlobalError", R> | HttpServerError.RouteNotFound,
1166 | Scope.Scope
1167 | HttpServerRequest.HttpServerRequest
1168 | Request.Only<"Requires", R>
1169 | Request.Only<"GlobalRequires", R>
1170 >
1171 ) => Effect.Effect<HttpServerResponse.HttpServerResponse, HE, HR>
1172 }
1173): {
1174 readonly handler: [HR] extends [never]
1175 ? ((request: globalThis.Request, context?: Context.Context<never> | undefined) => Promise<Response>)
1176 : ((request: globalThis.Request, context: Context.Context<HR>) => Promise<Response>)
1177 readonly dispose: () => Promise<void>
1178} => {
1179 let middleware: any = options?.middleware
1180 if (options?.disableLogger !== true) {
1181 middleware = middleware ? compose(middleware, HttpMiddleware.logger) : HttpMiddleware.logger
1182 }
1183 const RouterLayer: Layer.Layer<HttpRouter | A, E> = Layer.provideMerge(
1184 appLayer,
1185 options?.routerConfig
1186 ? Layer.provide(layer, Layer.succeed(RouterConfig, options.routerConfig))
1187 : layer
1188 ) as any
1189 return HttpApp.toWebHandlerLayerWith(RouterLayer, {
1190 toHandler: (r) => Effect.succeed(Context.get(r.context, HttpRouter).asHttpEffect()),
1191 middleware,

Callers

nothing calls this directly

Calls 3

provideMethod · 0.65
getMethod · 0.65
composeFunction · 0.50

Tested by

no test coverage detected