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

Function serve

packages/effect/src/unstable/http/HttpRouter.ts:1223–1272  ·  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

1221 * @since 4.0.0
1222 */
1223export const serve = <A, E, R, HE, HR = Request.Only<"Requires", R> | Request.Only<"GlobalRequires", R>>(
1224 appLayer: Layer.Layer<A, E, R>,
1225 options?: {
1226 readonly routerConfig?: Partial<FindMyWay.RouterConfig> | undefined
1227 readonly disableLogger?: boolean | undefined
1228 readonly disableListenLog?: boolean
1229 /**
1230 * Middleware to apply to the HTTP server.
1231 *
1232 * **Gotchas**
1233 *
1234 * This middleware is applied to the entire HTTP server chain, including the
1235 * sending of the response. Changes to the response are not reflected in the
1236 * final response sent to the client. Use `HttpRouter.middleware` when
1237 * middleware must modify the response.
1238 */
1239 readonly middleware?: (
1240 effect: Effect.Effect<
1241 HttpServerResponse.HttpServerResponse,
1242 Request.Only<"Error", R> | Request.Only<"GlobalError", R> | HttpServerError.HttpServerError,
1243 | Scope.Scope
1244 | HttpServerRequest.HttpServerRequest
1245 | Request.Only<"Requires", R>
1246 | Request.Only<"GlobalRequires", R>
1247 >
1248 ) => Effect.Effect<HttpServerResponse.HttpServerResponse, HE, HR>
1249 }
1250): Layer.Layer<
1251 A,
1252 Request.Without<E>,
1253 HttpServer.HttpServer | Exclude<Request.Without<R> | Exclude<HR, GlobalProvided>, HttpRouter>
1254> => {
1255 let middleware: any = options?.middleware
1256 if (options?.disableLogger !== true) {
1257 middleware = middleware ? compose(middleware, HttpMiddleware.logger) : HttpMiddleware.logger
1258 }
1259 const RouterLayer = options?.routerConfig
1260 ? Layer.provide(layer, Layer.succeed(RouterConfig)(options.routerConfig))
1261 : layer
1262 return Effect.gen(function*() {
1263 const router = yield* HttpRouter
1264 const handler = router.asHttpEffect()
1265 return middleware ? HttpServer.serve(handler, middleware) : HttpServer.serve(handler)
1266 }).pipe(
1267 Layer.unwrap,
1268 Layer.provideMerge(appLayer),
1269 Layer.provide(RouterLayer),
1270 options?.disableListenLog ? identity : HttpServer.withLogAddress
1271 ) as any
1272}
1273
1274/**
1275 * Builds a Fetch-compatible request handler from an HTTP router application

Callers

nothing calls this directly

Calls 4

composeFunction · 0.90
pipeMethod · 0.65
provideMethod · 0.45
succeedMethod · 0.45

Tested by

no test coverage detected