/** * Middleware services for the unstable RPC runtime. * * A middleware service wraps server handler execution and can also install a * client-side wrapper for generated clients. Its metadata records the services * provided to downstream handlers, the services required by the middleware * implementation, the schema for server-visible failures, the client-only error * type, and whether generated clients must require the matching client layer. * * @stability unstable * @since 4.0.0 */ import * as Context from "../Context.ts"; import * as Effect from "../Effect.ts"; import type { Headers } from "../http/Headers.ts"; import * as Layer from "../Layer.ts"; import * as Schema from "../Schema.ts"; import { Scope } from "../Scope.ts"; import type { unhandled } from "../Types.ts"; import type * as Rpc from "./Rpc.ts"; import type { Request, RequestId } from "./RpcMessage.ts"; /** * The literal type id used to identify RPC middleware service classes. * * @stability unstable * @category type IDs * @since 4.0.0 */ export type TypeId = "~effect/rpc/RpcMiddleware"; /** * The runtime type id used to attach and inspect RPC middleware metadata. * * @stability unstable * @category type IDs * @since 4.0.0 */ export declare const TypeId: TypeId; /** * The server-side RPC middleware function shape, wrapping a handler effect with * access to request metadata and translating provided services into required * services. * * @stability unstable * @category models * @since 4.0.0 */ export interface RpcMiddleware { (effect: Effect.Effect, options: { readonly client: Rpc.ServerClient; readonly requestId: RequestId; readonly rpc: Rpc.AnyWithProps; readonly payload: unknown; readonly headers: Headers; }): Effect.Effect; } /** * Marker success type used by RPC middleware to represent successful completion * without exposing the handler's concrete success value. * * @stability unstable * @category models * @since 4.0.0 */ export interface SuccessValue { readonly _: unique symbol; } /** * The client-side RPC middleware function shape, allowing outgoing requests to * be inspected or modified before calling `next`. * * @stability unstable * @category models * @since 4.0.0 */ export interface RpcMiddlewareClient { (options: { readonly rpc: Rpc.AnyWithProps; readonly request: Request; readonly next: (request: Request) => Effect.Effect; }): Effect.Effect; } /** * Marker service requirement indicating that a middleware has a client-side * implementation available for an RPC client. * * @stability unstable * @category models * @since 4.0.0 */ export interface ForClient { readonly _: unique symbol; readonly id: Id; } /** * An erased server-side RPC middleware function, useful when the concrete * provided services, errors, and requirements are not needed. * * @stability unstable * @category models * @since 4.0.0 */ export interface Any { (effect: Effect.Effect, options: { readonly client: Rpc.ServerClient; readonly requestId: RequestId; readonly rpc: Rpc.AnyWithProps; readonly payload: unknown; readonly headers: Headers; }): Effect.Effect; } /** * A type-level carrier for RPC middleware metadata, including provided * services, required services, error schema, and client error type. * * @stability unstable * @category utility types * @since 4.0.0 */ export interface AnyId { readonly [TypeId]: { readonly provides: any; readonly requires: any; readonly error: Schema.Top; readonly clientError: any; }; } /** * The `Context.Service` class shape created for an RPC middleware, including * its error schema, service metadata, and client-side requirement marker. * * @stability unstable * @category services * @since 4.0.0 */ export interface ServiceClass extends Context.Service> { new (_: never): Context.ServiceClass.Shape> & { readonly [TypeId]: { readonly error: E; readonly provides: Provides; readonly requires: Requires; readonly clientError: ClientError; }; }; readonly [TypeId]: typeof TypeId; readonly error: E; readonly requiredForClient: RequiredForClient; readonly "~ClientError": ClientError; } /** * Extracts the services provided by an RPC middleware. * * @stability unstable * @category utility types * @since 4.0.0 */ export type Provides = A extends { readonly [TypeId]: { readonly provides: infer P; }; } ? P : never; /** * Extracts the services required by an RPC middleware. * * @stability unstable * @category utility types * @since 4.0.0 */ export type Requires = A extends { readonly [TypeId]: { readonly requires: infer R; }; } ? R : never; /** * Applies a middleware's service transformation to an RPC environment by * removing services the middleware provides and adding services it requires. * * @stability unstable * @category utility types * @since 4.0.0 */ export type ApplyServices = Exclude> | Requires; /** * Extracts the error schema associated with an RPC middleware. * * @stability unstable * @category utility types * @since 4.0.0 */ export type ErrorSchema = A extends { readonly [TypeId]: { readonly error: infer E; }; } ? E extends Schema.Constraint ? E : never : never; /** * Extracts the decoded error type produced by an RPC middleware. * * @stability unstable * @category utility types * @since 4.0.0 */ export type Error = ErrorSchema["Type"]; /** * Extracts the encoding services required by a middleware's error schema. * * @stability unstable * @category utility types * @since 4.0.0 */ export type ErrorServicesEncode = ErrorSchema["EncodingServices"]; /** * Extracts the decoding services required by a middleware's error schema. * * @stability unstable * @category utility types * @since 4.0.0 */ export type ErrorServicesDecode = ErrorSchema["DecodingServices"]; /** * An erased RPC middleware context key carrying middleware metadata. * * @stability unstable * @category models * @since 4.0.0 */ export interface AnyService extends Context.Key { readonly [TypeId]: typeof TypeId; readonly error: Schema.Top; readonly requiredForClient: boolean; readonly "~ClientError": any; } /** * An erased RPC middleware context key whose service value is a server-side * middleware function. * * @stability unstable * @category models * @since 4.0.0 */ export interface AnyServiceWithProps extends Context.Key> { readonly [TypeId]: typeof TypeId; readonly error: Schema.Top; readonly requiredForClient: boolean; readonly "~ClientError": any; } /** * Creates a typed RPC middleware service class, with optional service * requirements, provided services, error schema, and client-side requirement * metadata. * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const Service: () => (id: Name, options?: { readonly error?: Error | undefined; readonly requiredForClient?: RequiredForClient | undefined; } | undefined) => ServiceClass; /** * Provides the client-side implementation for an RPC middleware service, * capturing the layer's environment and merging it into each middleware * invocation. * * @stability unstable * @category layers * @since 4.0.0 */ export declare const layerClient: (tag: Context.Key, service: RpcMiddlewareClient | Effect.Effect, EX, RX>) => Layer.Layer, EX, R | Exclude>; //# sourceMappingURL=RpcMiddleware.d.ts.map