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

Function serve

packages/effect/src/http/HttpRouter.ts:1422–1479  ·  view source on GitHub ↗
(
  appLayer: Layer.Layer<A, E, R>,
  options?: {
    readonly routerConfig?: Partial<FindMyWay.RouterConfig> | undefined
    readonly disableLogger?: boolean | undefined
    readonly disableListenLog?: boolean
    /**
     * 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>
  }
)

Source from the content-addressed store, hash-verified

1420 * @since 4.0.0
1421 */
1422export const serve = <A, E, R, HE, HR = Request.Only<"Requires", R> | Request.Only<"GlobalRequires", R>>(
1423 appLayer: Layer.Layer<A, E, R>,
1424 options?: {
1425 readonly routerConfig?: Partial<FindMyWay.RouterConfig> | undefined
1426 readonly disableLogger?: boolean | undefined
1427 readonly disableListenLog?: boolean
1428 /**
1429 * Middleware to apply to the HTTP server.
1430 *
1431 * **Gotchas**
1432 *
1433 * This middleware is applied to the entire HTTP server chain, including the
1434 * sending of the response. Changes to the response are not reflected in the
1435 * final response sent to the client. Use `HttpRouter.middleware` when
1436 * middleware must modify the response.
1437 */
1438 readonly middleware?: (
1439 effect: Effect.Effect<
1440 HttpServerResponse.HttpServerResponse,
1441 Request.Only<"Error", R> | Request.Only<"GlobalError", R> | HttpServerError.HttpServerError,
1442 | Scope.Scope
1443 | HttpServerRequest.HttpServerRequest
1444 | Request.Only<"Requires", R>
1445 | Request.Only<"GlobalRequires", R>
1446 >
1447 ) => Effect.Effect<HttpServerResponse.HttpServerResponse, HE, HR>
1448 }
1449): Layer.Layer<
1450 A,
1451 Request.Without<E>,
1452 HttpServer.HttpServer | Exclude<Request.Without<R> | Exclude<HR, GlobalProvided>, HttpRouter>
1453> => {
1454 let middleware: any = options?.middleware
1455 if (options?.disableLogger !== true) {
1456 middleware = middleware ? compose(middleware, HttpMiddleware.logger) : HttpMiddleware.logger
1457 }
1458 const layerWithRouter = Layer.provideMerge(
1459 appLayer,
1460 options?.routerConfig
1461 ? Layer.provide(layer, Layer.succeed(RouterConfig)(options.routerConfig))
1462 : layer
1463 )
1464 return Effect.contextWith((context) => {
1465 const memoMap = Layer.CurrentMemoMap.forkOrCreate(context, { fresh: [layer] })
1466 return Layer.buildWithMemoMap(layerWithRouter, memoMap, Context.get(context, Scope.Scope))
1467 }).pipe(
1468 Effect.map((context) => {
1469 const router = Context.get(context, HttpRouter)
1470 const handler = router.asHttpEffect()
1471 return Layer.provideMerge(
1472 middleware ? HttpServer.serve(handler, middleware) : HttpServer.serve(handler),
1473 Layer.succeedContext(context)
1474 )
1475 }),
1476 Layer.unwrap,
1477 options?.disableListenLog ? identity : HttpServer.withLogAddress
1478 ) as any
1479}

Callers

nothing calls this directly

Calls 7

composeFunction · 0.90
forkOrCreateMethod · 0.80
pipeMethod · 0.65
getMethod · 0.65
provideMethod · 0.45
succeedMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected