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

Function toWebHandler

packages/effect/src/unstable/http/HttpRouter.ts:1287–1347  ·  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.
     *
     * **Gotchas**
     *
     * This middleware is applied to the entire HTTP server chain, including the
     * sending of the response. Changes to the response are not reflected in the
     * final response sent to the client. Use `HttpRouter.middleware` when
     * middleware must modify the response.
     */
    readonly middleware?: (
      effect: Effect.Effect<
        HttpServerResponse.HttpServerResponse,
        Request.Only<"Error", R> | Request.Only<"GlobalError", R> | HttpServerError.HttpServerError,
        | Scope.Scope
        | HttpServerRequest.HttpServerRequest
        | Request.Only<"Requires", R>
        | Request.Only<"GlobalRequires", R>
      >
    ) => Effect.Effect<HttpServerResponse.HttpServerResponse, HE, HR | GlobalProvided>
  }
)

Source from the content-addressed store, hash-verified

1285 * @since 4.0.0
1286 */
1287export const toWebHandler = <
1288 A,
1289 E,
1290 R extends
1291 | HttpRouter
1292 | Request<"Requires", any>
1293 | Request<"GlobalRequires", any>
1294 | Request<"Error", any>
1295 | Request<"GlobalError", any>,
1296 HE,
1297 HR = Exclude<Request.Only<"Requires", R> | Request.Only<"GlobalRequires", R>, A>,
1298 ReqR = Exclude<HR, A | Scope.Scope | HttpServerRequest.HttpServerRequest>
1299>(
1300 appLayer: Layer.Layer<A, E, R>,
1301 options?: {
1302 readonly memoMap?: Layer.MemoMap | undefined
1303 readonly routerConfig?: Partial<FindMyWay.RouterConfig> | undefined
1304 readonly disableLogger?: boolean | undefined
1305 /**
1306 * Middleware to apply to the HTTP server.
1307 *
1308 * **Gotchas**
1309 *
1310 * This middleware is applied to the entire HTTP server chain, including the
1311 * sending of the response. Changes to the response are not reflected in the
1312 * final response sent to the client. Use `HttpRouter.middleware` when
1313 * middleware must modify the response.
1314 */
1315 readonly middleware?: (
1316 effect: Effect.Effect<
1317 HttpServerResponse.HttpServerResponse,
1318 Request.Only<"Error", R> | Request.Only<"GlobalError", R> | HttpServerError.HttpServerError,
1319 | Scope.Scope
1320 | HttpServerRequest.HttpServerRequest
1321 | Request.Only<"Requires", R>
1322 | Request.Only<"GlobalRequires", R>
1323 >
1324 ) => Effect.Effect<HttpServerResponse.HttpServerResponse, HE, HR | GlobalProvided>
1325 }
1326): {
1327 readonly handler: [ReqR] extends [never]
1328 ? ((request: globalThis.Request, context?: Context.Context<never> | undefined) => Promise<Response>)
1329 : ((
1330 request: globalThis.Request,
1331 context: Context.Context<ReqR>
1332 ) => Promise<Response>)
1333 readonly dispose: () => Promise<void>
1334} => {
1335 let middleware: any = options?.middleware
1336 if (options?.disableLogger !== true) {
1337 middleware = middleware ? compose(middleware, HttpMiddleware.logger) : HttpMiddleware.logger
1338 }
1339 const RouterLayer = options?.routerConfig
1340 ? Layer.provide(layer, Layer.succeed(RouterConfig)(options.routerConfig))
1341 : layer
1342 return HttpEffect.toWebHandlerLayerWith(Layer.provideMerge(appLayer, RouterLayer) as Layer.Layer<A | HttpRouter, E>, {
1343 toHandler: (s) => Effect.succeed(Context.get(s, HttpRouter).asHttpEffect()),
1344 middleware,

Callers

nothing calls this directly

Calls 4

composeFunction · 0.90
getMethod · 0.65
provideMethod · 0.45
succeedMethod · 0.45

Tested by

no test coverage detected