Skip to content

Commit

Permalink
add convenience client constructors to /rpc-http (Effect-TS#2198)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Feb 22, 2024
1 parent aa6556f commit 0c58b32
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/red-lobsters-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@effect/rpc-http": patch
"@effect/rpc": patch
---

add convenience client constructors to /rpc-http
51 changes: 50 additions & 1 deletion packages/rpc-http/src/HttpResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @since 1.0.0
*/
import * as Body from "@effect/platform/Http/Body"
import type * as Client from "@effect/platform/Http/Client"
import * as Client from "@effect/platform/Http/Client"
import * as ClientRequest from "@effect/platform/Http/ClientRequest"
import * as Resolver from "@effect/rpc/Resolver"
import type * as Router from "@effect/rpc/Router"
Expand All @@ -11,6 +11,7 @@ import type * as Serializable from "@effect/schema/Serializable"
import * as Chunk from "effect/Chunk"
import * as Effect from "effect/Effect"
import type * as RequestResolver from "effect/RequestResolver"
import * as Schedule from "effect/Schedule"
import * as Stream from "effect/Stream"

/**
Expand Down Expand Up @@ -57,3 +58,51 @@ export const makeEffect = <R extends Router.Router<any, any>>(
Effect.scoped
)
)<R>()

/**
* @category constructors
* @since 1.0.0
*/
export const makeClient = <R extends Router.Router<any, any>>(
baseUrl: string
): Serializable.SerializableWithResult.Context<Router.Router.Request<R>> extends never ?
"HttpResolver.makeClient: request context is not `never`"
: Resolver.Client<
RequestResolver.RequestResolver<
Rpc.Request<Router.Router.Request<R>>
>
> =>
Resolver.toClient(make<R>(
Client.fetchOk().pipe(
Client.mapRequest(ClientRequest.prependUrl(baseUrl)),
Client.retry(
Schedule.exponential(50).pipe(
Schedule.intersect(Schedule.recurs(5))
)
)
)
) as any) as any

/**
* @category constructors
* @since 1.0.0
*/
export const makeClientEffect = <R extends Router.Router<any, any>>(
baseUrl: string
): Serializable.SerializableWithResult.Context<Router.Router.Request<R>> extends never ?
"HttpResolver.makeClientEffect: request context is not `never`"
: Resolver.Client<
RequestResolver.RequestResolver<
Rpc.Request<Router.Router.Request<R>>
>
> =>
Resolver.toClient(makeEffect<R>(
Client.fetchOk().pipe(
Client.mapRequest(ClientRequest.prependUrl(baseUrl)),
Client.retry(
Schedule.exponential(50).pipe(
Schedule.intersect(Schedule.recurs(5))
)
)
)
) as any) as any
20 changes: 15 additions & 5 deletions packages/rpc/src/Resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,20 @@ export const annotateHeadersEffect: {
)
))

/**
* @since 1.0.0
* @category models
*/
export type Client<
R extends
| RequestResolver.RequestResolver<Rpc.Request<any>, never>
| Effect.Effect<RequestResolver.RequestResolver<Rpc.Request<any>, never>, never, any>
> = R extends Effect.Effect<RequestResolver.RequestResolver<Rpc.Request<infer RReq>>, infer _E, infer R> ?
(<Req extends RReq>(request: Req) => Rpc.Rpc.Result<Req, R>)
: R extends RequestResolver.RequestResolver<Rpc.Request<infer RReq>, never> ?
(<Req extends RReq>(request: Req) => Rpc.Rpc.Result<Req>)
: never

/**
* @since 1.0.0
* @category combinators
Expand All @@ -259,8 +273,4 @@ export const toClient = <
options?: {
readonly spanPrefix?: string
}
): R extends Effect.Effect<RequestResolver.RequestResolver<Rpc.Request<infer RReq>>, infer _E, infer R> ?
(<Req extends RReq>(request: Req) => Rpc.Rpc.Result<Req, R>)
: R extends RequestResolver.RequestResolver<Rpc.Request<infer RReq>, never> ?
(<Req extends RReq>(request: Req) => Rpc.Rpc.Result<Req>)
: never => ((request: Schema.TaggedRequest.Any) => Rpc.call(request, resolver, options)) as any
): Client<R> => ((request: Schema.TaggedRequest.Any) => Rpc.call(request, resolver, options)) as any

0 comments on commit 0c58b32

Please sign in to comment.