Skip to content

Commit

Permalink
swap type parameters for /platform data types (#2376)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim <hello@timsmart.co>
  • Loading branch information
vecerek and tim-smart committed Apr 16, 2024
1 parent d52ddb7 commit 64d8a87
Show file tree
Hide file tree
Showing 18 changed files with 629 additions and 593 deletions.
15 changes: 15 additions & 0 deletions .changeset/mighty-trees-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"@effect/platform": minor
"@effect/platform-browser": minor
"@effect/platform-bun": minor
"@effect/platform-node": minor
"@effect/platform-node-shared": minor
---

Swap type parameters in /platform data types

A codemod has been released to make migration easier:

```
npx @effect/codemod platform-0.49 src/**/*
```
6 changes: 3 additions & 3 deletions packages/platform-node/src/Http/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ export const make: (
*/
export const makeHandler: {
<R, E>(
httpApp: App.Default<R, E>
httpApp: App.Default<E, R>
): Effect.Effect<
(nodeRequest: Http.IncomingMessage, nodeResponse: Http.ServerResponse<Http.IncomingMessage>) => void,
never,
Exclude<R, Scope.Scope | ServerRequest.ServerRequest>
>
<R, E, App extends App.Default<any, any>>(
httpApp: App.Default<R, E>,
middleware: Middleware.Middleware.Applied<R, E, App>
httpApp: App.Default<E, R>,
middleware: Middleware.Middleware.Applied<App, E, R>
): Effect.Effect<
(nodeRequest: Http.IncomingMessage, nodeResponse: Http.ServerResponse<Http.IncomingMessage>) => void,
never,
Expand Down
10 changes: 5 additions & 5 deletions packages/platform-node/src/internal/http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,20 @@ export const make = (

/** @internal */
export const makeHandler: {
<R, E>(httpApp: App.Default<R, E>): Effect.Effect<
<R, E>(httpApp: App.Default<E, R>): Effect.Effect<
(nodeRequest: Http.IncomingMessage, nodeResponse: Http.ServerResponse) => void,
never,
Exclude<R, ServerRequest.ServerRequest | Scope.Scope>
>
<R, E, App extends App.Default<any, any>>(
httpApp: App.Default<R, E>,
middleware: Middleware.Middleware.Applied<R, E, App>
httpApp: App.Default<E, R>,
middleware: Middleware.Middleware.Applied<App, E, R>
): Effect.Effect<
(nodeRequest: Http.IncomingMessage, nodeResponse: Http.ServerResponse) => void,
never,
Exclude<Effect.Effect.Context<App>, ServerRequest.ServerRequest | Scope.Scope>
>
} = <R, E>(httpApp: App.Default<R, E>, middleware?: Middleware.Middleware) => {
} = <E, R>(httpApp: App.Default<E, R>, middleware?: Middleware.Middleware) => {
const handledApp = App.toHandled(httpApp, (request, exit) => {
if (exit._tag === "Success") {
return Effect.catchAllCause(
Expand Down Expand Up @@ -164,7 +164,7 @@ export const makeHandler: {
/** @internal */
export const makeUpgradeHandler = <R, E>(
lazyWss: Effect.Effect<WS.WebSocketServer>,
httpApp: App.Default<R, E>,
httpApp: App.Default<E, R>,
middleware?: Middleware.Middleware
) => {
const handledApp = App.toHandled(httpApp, (request, exit) => {
Expand Down
34 changes: 19 additions & 15 deletions packages/platform/src/Http/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,28 @@ import * as ServerResponse from "./ServerResponse.js"
* @since 1.0.0
* @category models
*/
export interface HttpApp<R, E, A> extends Effect.Effect<A, E, R | ServerRequest.ServerRequest> {}
export interface HttpApp<A = ServerResponse.ServerResponse, E = never, R = never>
extends Effect.Effect<A, E, R | ServerRequest.ServerRequest>
{}

/**
* @since 1.0.0
* @category models
*/
export type Default<R, E> = HttpApp<R, E, ServerResponse.ServerResponse>
export type Default<E = never, R = never> = HttpApp<ServerResponse.ServerResponse, E, R>

/**
* @since 1.0.0
* @category combinators
*/
export const toHandled = <R, E, _, RH>(
self: Default<R, E>,
export const toHandled = <E, R, _, RH>(
self: Default<E, R>,
handleResponse: (
request: ServerRequest.ServerRequest,
exit: Exit.Exit<ServerResponse.ServerResponse, E | ServerError.ResponseError>
) => Effect.Effect<_, never, RH>,
middleware?: Middleware | undefined
): Default<Exclude<R | RH, Scope.Scope>, E | ServerError.ResponseError> => {
): Default<E | ServerError.ResponseError, Exclude<R | RH, Scope.Scope>> => {
const withTracer = internalMiddleware.tracer(self)
const responded = Effect.withFiberRuntime<
ServerResponse.ServerResponse,
Expand Down Expand Up @@ -110,8 +112,8 @@ export const appendPreResponseHandler: (handler: PreResponseHandler) => Effect.E
* @category fiber refs
*/
export const withPreResponseHandler = dual<
(handler: PreResponseHandler) => <R, E, A>(self: HttpApp<R, E, A>) => HttpApp<R, E, A>,
<R, E, A>(self: HttpApp<R, E, A>, handler: PreResponseHandler) => HttpApp<R, E, A>
(handler: PreResponseHandler) => <R, E, A>(self: HttpApp<A, E, R>) => HttpApp<A, E, R>,
<R, E, A>(self: HttpApp<A, E, R>, handler: PreResponseHandler) => HttpApp<A, E, R>
>(2, (self, handler) =>
Effect.locallyWith(
self,
Expand All @@ -131,23 +133,25 @@ export const withPreResponseHandler = dual<
*/
export const toWebHandlerRuntime = <R>(runtime: Runtime.Runtime<R>) => {
const run = Runtime.runFork(runtime)
return <E>(self: Default<R | Scope.Scope, E>) => {
const resolveSymbol = Symbol()
const rejectSymbol = Symbol()
return <E>(self: Default<E, R | Scope.Scope>) => {
const handled = Effect.scoped(toHandled(self, (request, exit) => {
const webRequest = request.source as Request
if (Exit.isSuccess(exit)) {
;(request as any)._resolve(ServerResponse.toWeb(exit.value, request.method === "HEAD"))
;(request as any)[resolveSymbol](ServerResponse.toWeb(exit.value, request.method === "HEAD"))
} else if (Cause.isInterruptedOnly(exit.cause)) {
;(request as any)._resolve(new Response(null, { status: webRequest.signal.aborted ? 499 : 503 }))
;(request as any)[resolveSymbol](new Response(null, { status: webRequest.signal.aborted ? 499 : 503 }))
} else {
;(request as any)._reject(Cause.pretty(exit.cause))
;(request as any)[rejectSymbol](Cause.pretty(exit.cause))
}
return Effect.unit
}))
return (request: Request): Promise<Response> =>
new Promise((resolve, reject) => {
const req = ServerRequest.fromWeb(request)
;(req as any)._resolve = resolve
;(req as any)._reject = reject
;(req as any)[resolveSymbol] = resolve
;(req as any)[rejectSymbol] = reject
const fiber = run(
Effect.provideService(handled, ServerRequest.ServerRequest, req)
)
Expand All @@ -162,15 +166,15 @@ export const toWebHandlerRuntime = <R>(runtime: Runtime.Runtime<R>) => {
* @since 1.0.0
* @category conversions
*/
export const toWebHandler: <E>(self: Default<Scope.Scope, E>) => (request: Request) => Promise<Response> =
export const toWebHandler: <E>(self: Default<E, Scope.Scope>) => (request: Request) => Promise<Response> =
toWebHandlerRuntime(Runtime.defaultRuntime)

/**
* @since 1.0.0
* @category conversions
*/
export const toWebHandlerLayer = <R, E, RE>(
self: Default<R | Scope.Scope, E>,
self: Default<E, R | Scope.Scope>,
layer: Layer.Layer<R, RE>
): {
readonly close: () => Promise<void>
Expand Down
Loading

0 comments on commit 64d8a87

Please sign in to comment.