Skip to content

Commit

Permalink
fix HttpMiddleware circular import (#3784)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim <hello@timsmart.co>
  • Loading branch information
patroza and tim-smart authored Oct 14, 2024
1 parent 9e2cc4e commit 2036402
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-mice-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/platform": patch
---

fix HttpMiddleware circular import
43 changes: 7 additions & 36 deletions packages/platform/src/HttpApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import * as Context from "effect/Context"
import * as Effect from "effect/Effect"
import * as Exit from "effect/Exit"
import * as FiberRef from "effect/FiberRef"
import { dual } from "effect/Function"
import { globalValue } from "effect/GlobalValue"
import * as Layer from "effect/Layer"
import * as Option from "effect/Option"
import type * as Option from "effect/Option"
import * as Runtime from "effect/Runtime"
import * as Scope from "effect/Scope"
import type { HttpMiddleware } from "./HttpMiddleware.js"
import * as ServerError from "./HttpServerError.js"
import * as ServerRequest from "./HttpServerRequest.js"
import * as ServerResponse from "./HttpServerResponse.js"
import * as internal from "./internal/httpApp.js"
import * as internalMiddleware from "./internal/httpMiddleware.js"

/**
Expand Down Expand Up @@ -108,48 +107,20 @@ export type PreResponseHandler = (
* @since 1.0.0
* @category fiber refs
*/
export const currentPreResponseHandlers: FiberRef.FiberRef<Option.Option<PreResponseHandler>> = globalValue(
Symbol.for("@effect/platform/HttpApp/preResponseHandlers"),
() => FiberRef.unsafeMake<Option.Option<PreResponseHandler>>(Option.none())
)
export const currentPreResponseHandlers: FiberRef.FiberRef<Option.Option<PreResponseHandler>> =
internal.currentPreResponseHandlers

/**
* @since 1.0.0
* @category fiber refs
*/
export const appendPreResponseHandler: (handler: PreResponseHandler) => Effect.Effect<void> = (
handler: PreResponseHandler
) =>
FiberRef.update(
currentPreResponseHandlers,
Option.match({
onNone: () => Option.some(handler),
onSome: (prev) =>
Option.some((request, response) =>
Effect.flatMap(prev(request, response), (response) => handler(request, response))
)
})
)

export const appendPreResponseHandler: (handler: PreResponseHandler) => Effect.Effect<void> =
internal.appendPreResponseHandler
/**
* @since 1.0.0
* @category fiber refs
*/
export const withPreResponseHandler = dual<
(handler: PreResponseHandler) => <A, E, R>(self: HttpApp<A, E, R>) => HttpApp<A, E, R>,
<A, E, R>(self: HttpApp<A, E, R>, handler: PreResponseHandler) => HttpApp<A, E, R>
>(2, (self, handler) =>
Effect.locallyWith(
self,
currentPreResponseHandlers,
Option.match({
onNone: () => Option.some(handler),
onSome: (prev) =>
Option.some((request, response) =>
Effect.flatMap(prev(request, response), (response) => handler(request, response))
)
})
))
export const withPreResponseHandler = internal.withPreResponseHandler

/**
* @since 1.0.0
Expand Down
44 changes: 44 additions & 0 deletions packages/platform/src/internal/httpApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as Effect from "effect/Effect"
import * as FiberRef from "effect/FiberRef"
import { dual } from "effect/Function"
import { globalValue } from "effect/GlobalValue"
import * as Option from "effect/Option"
import type { HttpApp, PreResponseHandler } from "../HttpApp.js"

/** @internal */
export const currentPreResponseHandlers: FiberRef.FiberRef<Option.Option<PreResponseHandler>> = globalValue(
Symbol.for("@effect/platform/HttpApp/preResponseHandlers"),
() => FiberRef.unsafeMake<Option.Option<PreResponseHandler>>(Option.none())
)

/** @internal */
export const appendPreResponseHandler: (handler: PreResponseHandler) => Effect.Effect<void> = (
handler: PreResponseHandler
) =>
FiberRef.update(
currentPreResponseHandlers,
Option.match({
onNone: () => Option.some(handler),
onSome: (prev) =>
Option.some((request, response) =>
Effect.flatMap(prev(request, response), (response) => handler(request, response))
)
})
)

/** @internal */
export const withPreResponseHandler = dual<
(handler: PreResponseHandler) => <A, E, R>(self: HttpApp<A, E, R>) => HttpApp<A, E, R>,
<A, E, R>(self: HttpApp<A, E, R>, handler: PreResponseHandler) => HttpApp<A, E, R>
>(2, (self, handler) =>
Effect.locallyWith(
self,
currentPreResponseHandlers,
Option.match({
onNone: () => Option.some(handler),
onSome: (prev) =>
Option.some((request, response) =>
Effect.flatMap(prev(request, response), (response) => handler(request, response))
)
})
))
6 changes: 3 additions & 3 deletions packages/platform/src/internal/httpMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { HttpApp } from "@effect/platform"
import * as Context from "effect/Context"
import * as Effect from "effect/Effect"
import * as FiberRef from "effect/FiberRef"
Expand All @@ -16,6 +15,7 @@ import * as ServerRequest from "../HttpServerRequest.js"
import * as ServerResponse from "../HttpServerResponse.js"
import type { HttpServerResponse } from "../HttpServerResponse.js"
import * as TraceContext from "../HttpTraceContext.js"
import * as internalHttpApp from "./httpApp.js"

/** @internal */
export const make = <M extends Middleware.HttpMiddleware>(middleware: M): M => middleware
Expand Down Expand Up @@ -301,7 +301,7 @@ export const cors = (options?: {
const preResponseHandler = (request: ServerRequest.HttpServerRequest, response: HttpServerResponse) =>
Effect.succeed(ServerResponse.setHeaders(response, headersFromRequest(request)))

return <E, R>(httpApp: HttpApp.Default<E, R>): HttpApp.Default<E, R> =>
return <E, R>(httpApp: App.Default<E, R>): App.Default<E, R> =>
Effect.withFiberRuntime((fiber) => {
const context = fiber.getFiberRef(FiberRef.currentContext)
const request = Context.unsafeGet(context, ServerRequest.HttpServerRequest)
Expand All @@ -311,6 +311,6 @@ export const cors = (options?: {
headers: headersFromRequestOptions(request)
}))
}
return Effect.zipRight(HttpApp.appendPreResponseHandler(preResponseHandler), httpApp)
return Effect.zipRight(internalHttpApp.appendPreResponseHandler(preResponseHandler), httpApp)
})
}

0 comments on commit 2036402

Please sign in to comment.