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

Function toWebHandler

packages/effect/src/http/HttpRouter.ts:1535–1599  ·  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

1533 * @since 4.0.0
1534 */
1535export const toWebHandler = <
1536 A,
1537 E,
1538 R extends
1539 | HttpRouter
1540 | Request<"Requires", any>
1541 | Request<"GlobalRequires", any>
1542 | Request<"Error", any>
1543 | Request<"GlobalError", any>,
1544 HE,
1545 HR = Exclude<Request.Only<"Requires", R> | Request.Only<"GlobalRequires", R>, A>,
1546 ReqR = Exclude<HR, A | Scope.Scope | HttpServerRequest.HttpServerRequest>
1547>(
1548 appLayer: Layer.Layer<A, E, R>,
1549 options?: {
1550 readonly memoMap?: Layer.MemoMap | undefined
1551 readonly routerConfig?: Partial<FindMyWay.RouterConfig> | undefined
1552 readonly disableLogger?: boolean | undefined
1553 /**
1554 * Middleware to apply to the HTTP server.
1555 *
1556 * **Gotchas**
1557 *
1558 * This middleware is applied to the entire HTTP server chain, including the
1559 * sending of the response. Changes to the response are not reflected in the
1560 * final response sent to the client. Use `HttpRouter.middleware` when
1561 * middleware must modify the response.
1562 */
1563 readonly middleware?: (
1564 effect: Effect.Effect<
1565 HttpServerResponse.HttpServerResponse,
1566 Request.Only<"Error", R> | Request.Only<"GlobalError", R> | HttpServerError.HttpServerError,
1567 | Scope.Scope
1568 | HttpServerRequest.HttpServerRequest
1569 | Request.Only<"Requires", R>
1570 | Request.Only<"GlobalRequires", R>
1571 >
1572 ) => Effect.Effect<HttpServerResponse.HttpServerResponse, HE, HR | GlobalProvided>
1573 }
1574): {
1575 readonly handler: [ReqR] extends [never]
1576 ? ((request: globalThis.Request, context?: Context.Context<never> | undefined) => Promise<Response>)
1577 : ((
1578 request: globalThis.Request,
1579 context: Context.Context<ReqR>
1580 ) => Promise<Response>)
1581 readonly dispose: () => Promise<void>
1582 readonly ready: () => Promise<void>
1583} => {
1584 let middleware: any = options?.middleware
1585 if (options?.disableLogger !== true) {
1586 middleware = middleware ? compose(middleware, HttpMiddleware.logger) : HttpMiddleware.logger
1587 }
1588 const RouterLayer = options?.routerConfig
1589 ? Layer.provide(layer, Layer.succeed(RouterConfig)(options.routerConfig))
1590 : layer
1591 return HttpEffect.toWebHandlerLayerWith(
1592 Layer.provideMerge(appLayer, RouterLayer) as Layer.Layer<A | HttpRouter, E>,

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