/** * Defines schema-backed contracts for individual RPC procedures. * * An `Rpc` describes one remote procedure by recording its tag, payload schema, * success schema, error schema, defect schema, middleware, and annotations. * Clients and servers read the same declaration, so the procedure contract is * independent of the transport used to call it. This module includes * constructors, type helpers for deriving client and handler shapes, exit * schemas, and handler wrappers for special execution modes. * * @stability unstable * @since 4.0.0 */ import type * as Cause from "../Cause.ts"; import * as Context from "../Context.ts"; import type { Deferred } from "../Deferred.ts"; import type { Effect } from "../Effect.ts"; import type { Exit as Exit_ } from "../Exit.ts"; import type { Headers } from "../http/Headers.ts"; import { type Pipeable } from "../Pipeable.ts"; import type * as Queue from "../Queue.ts"; import * as Schema from "../Schema.ts"; import * as SchemaAST from "../SchemaAST.ts"; import type { Stream } from "../Stream.ts"; import type * as Struct from "../Struct.ts"; import type { NoInfer } from "../Types.ts"; import type { RequestId } from "./RpcMessage.ts"; import type * as RpcMiddleware from "./RpcMiddleware.ts"; import * as RpcSchema from "./RpcSchema.ts"; declare const TypeId = "~effect/rpc/Rpc"; /** * Returns `true` when the value is an `Rpc` definition. * * @stability unstable * @category guards * @since 4.0.0 */ export declare const isRpc: (u: unknown) => u is Rpc; /** * Schema for RPC defects. * * **Details** * * Defect schemas decode and encode without services and can be constructed from * `null`, `undefined`, or an object value. * * @stability unstable * @category schemas * @since 4.0.0 */ export interface DefectSchema extends Schema.Top { make(input: null, options?: Schema.MakeOptions): unknown; make(input: undefined, options?: Schema.MakeOptions): unknown; make(input: {}, options?: Schema.MakeOptions): unknown; readonly DecodingServices: never; readonly EncodingServices: never; } /** * Represents a typed RPC definition. * * **Details** * * An RPC is identified by a tag and carries payload, success, error, defect, * middleware, and annotation metadata used by RPC clients and servers. * * @stability unstable * @category models * @since 4.0.0 */ export interface Rpc extends Pipeable { new (_: never): {}; readonly [TypeId]: typeof TypeId; readonly _tag: Tag; readonly key: string; readonly payloadSchema: Payload; readonly successSchema: Success; readonly errorSchema: Error; readonly defectSchema: Schema.Top; readonly annotations: Context.Context; readonly middlewares: ReadonlySet; readonly "~requires": Requires; /** * Set the schema for the success response of the rpc. */ setSuccess(schema: S): Rpc; /** * Set the schema for the error response of the rpc. */ setError(schema: E): Rpc; /** * Set the schema for the payload of the rpc. */ setPayload

(schema: P): Rpc : P, Success, Error, Middleware, Requires>; /** * Add an `RpcMiddleware` to this procedure. */ middleware(middleware: M): Rpc>; /** * Set the schema for the error response of the rpc. */ prefix(prefix: Prefix): Rpc<`${Prefix}${Tag}`, Payload, Success, Error, Middleware, Requires>; /** * Add an annotation on the rpc. */ annotate(tag: Context.Key, value: NoInfer): Rpc; /** * Merge the annotations of the rpc with the provided annotations. */ annotateMerge(annotations: Context.Context): Rpc; } /** * Represents server-side metadata for the client associated with an RPC request. * * **When to use** * * Use to inspect or annotate the connected client while handling an RPC request * on the server. * * **Details** * * It stores the client id and request annotations that handlers can read or * extend. * * @stability unstable * @category models * @since 4.0.0 */ export declare class ServerClient { readonly id: number; annotations: Context.Context; constructor(id: number); annotate(tag: Context.Key, value: NoInfer): ServerClient; } /** * Represents the server-side implementation of an RPC. * * **Details** * * The handler receives the decoded request plus client, request id, headers, * and RPC metadata, and returns either an effectful result or a stream result. * * @stability unstable * @category models * @since 4.0.0 */ export interface Handler { readonly _: unique symbol; readonly tag: Tag; readonly handler: (request: any, options: { readonly client: ServerClient; readonly requestId: RequestId; readonly headers: Headers; readonly rpc: Any; }) => Effect<{} | Deferred, any> | Stream; readonly context: Context.Context; } /** * An erased RPC definition that preserves the common runtime metadata shared by * all RPCs. * * @stability unstable * @category models * @since 4.0.0 */ export interface Any extends Pipeable { readonly [TypeId]: typeof TypeId; readonly _tag: string; readonly key: string; readonly annotations: Context.Context; } /** * An erased RPC definition with all schema, middleware, annotation, and service * metadata available. * * @stability unstable * @category models * @since 4.0.0 */ export interface AnyWithProps extends Pipeable { readonly [TypeId]: typeof TypeId; readonly _tag: string; readonly key: string; readonly payloadSchema: Schema.Top; readonly successSchema: Schema.Top; readonly errorSchema: Schema.Top; readonly defectSchema: Schema.Top; readonly annotations: Context.Context; readonly middlewares: ReadonlySet; readonly "~requires": any; } /** * Extracts the tag string from an `Rpc`. * * @stability unstable * @category utility types * @since 4.0.0 */ export type Tag = R extends Rpc ? _Tag : never; /** * Extracts the success schema from an `Rpc`. * * @stability unstable * @category utility types * @since 4.0.0 */ export type SuccessSchema = R extends Rpc ? _Success : never; /** * Extracts the decoded success value type from an `Rpc`. * * @stability unstable * @category utility types * @since 4.0.0 */ export type Success = SuccessSchema["Type"]; /** * Extracts the encoded success value type from an `Rpc`. * * @stability unstable * @category utility types * @since 4.0.0 */ export type SuccessEncoded = R extends Rpc ? _Success["Encoded"] : never; /** * Extracts the success schema used in an RPC exit. * * **Details** * * For streaming RPCs, this is the stream element schema; otherwise it is the * RPC success schema. * * @stability unstable * @category utility types * @since 4.0.0 */ export type SuccessExitSchema = SuccessSchema extends RpcSchema.Stream ? _A : SuccessSchema; /** * Extracts the decoded success value carried by an RPC exit. * * **Details** * * For streaming RPCs, the immediate exit success is `void` because stream * elements are delivered separately. * * @stability unstable * @category utility types * @since 4.0.0 */ export type SuccessExit = Success extends infer T ? T extends Stream ? void : T : never; /** * Extracts the decoded stream element type from a streaming RPC, or `never` for * non-streaming RPCs. * * @stability unstable * @category utility types * @since 4.0.0 */ export type SuccessChunk = Success extends Stream ? _A : never; /** * Extracts the RPC error schema, including error schemas contributed by * middleware. * * @stability unstable * @category utility types * @since 4.0.0 */ export type ErrorSchema = R extends Rpc ? _Error | _Middleware["error"] : never; /** * Extracts the decoded error value type from an `Rpc`, including middleware * errors. * * @stability unstable * @category utility types * @since 4.0.0 */ export type Error = Schema.Schema.Type>; /** * Extracts the error schema used in an RPC exit. * * **Details** * * For streaming RPCs, this includes both the stream error schema and the RPC * error schema; otherwise it is the RPC error schema. * * @stability unstable * @category utility types * @since 4.0.0 */ export type ErrorExitSchema = SuccessSchema extends RpcSchema.Stream ? _E | ErrorSchema : ErrorSchema; /** * Extracts the decoded error type used by an RPC exit. * * **Details** * * For streaming RPCs, this includes both stream errors and RPC errors. * * @stability unstable * @category utility types * @since 4.0.0 */ export type ErrorExit = Success extends Stream ? _E | Error : Error; /** * The `Exit` type produced for an RPC, using the RPC's exit success and exit * error types. * * @stability unstable * @category utility types * @since 4.0.0 */ export type Exit = Exit_, ErrorExit>; /** * Extracts the payload constructor input type accepted by the RPC payload * schema. * * @stability unstable * @category utility types * @since 4.0.0 */ export type PayloadConstructor = R extends Rpc ? _Payload["~type.make.in"] : never; /** * Extracts the decoded payload type from an `Rpc`. * * @stability unstable * @category utility types * @since 4.0.0 */ export type Payload = R extends Rpc ? _Payload["Type"] : never; /** * Extracts all schema services required to encode or decode an RPC's payload, * success, error, and middleware error schemas. * * @stability unstable * @category utility types * @since 4.0.0 */ export type Services = R extends Rpc ? _Payload["DecodingServices"] | _Payload["EncodingServices"] | _Success["DecodingServices"] | _Success["EncodingServices"] | _Error["DecodingServices"] | _Error["EncodingServices"] | _Middleware["error"]["DecodingServices"] | _Middleware["error"]["EncodingServices"] : never; /** * Extracts the schema services required on the client side of an RPC. * * **Details** * * This includes payload encoding services and success, error, and middleware * error decoding services. * * @stability unstable * @category utility types * @since 4.0.0 */ export type ServicesClient = R extends Rpc ? _Payload["EncodingServices"] | _Success["DecodingServices"] | _Error["DecodingServices"] | _Middleware["error"]["DecodingServices"] : never; /** * Extracts the schema services required on the server side of an RPC. * * **Details** * * This includes payload decoding services and success, error, and middleware * error encoding services. * * @stability unstable * @category utility types * @since 4.0.0 */ export type ServicesServer = R extends Rpc ? _Payload["DecodingServices"] | _Success["EncodingServices"] | _Error["EncodingServices"] | _Middleware["error"]["EncodingServices"] : never; /** * Extracts the service identifiers for middleware attached to an `Rpc`. * * @stability unstable * @category utility types * @since 4.0.0 */ export type Middleware = R extends Rpc ? Context.Service.Identifier<_Middleware> : never; /** * Extracts client-side middleware service requirements for middleware marked as * required on the client. * * @stability unstable * @category utility types * @since 4.0.0 */ export type MiddlewareClient = R extends Rpc ? _Middleware extends { readonly requiredForClient: true; } ? RpcMiddleware.ForClient<_Middleware["Identifier"]> : never : never; /** * Returns an RPC type with an additional error schema unioned into its error * channel. * * @stability unstable * @category utility types * @since 4.0.0 */ export type AddError = R extends Rpc ? Rpc<_Tag, _Payload, _Success, _Error | Error, _Middleware, _Requires> : never; /** * Returns an RPC type with additional middleware and the corresponding * middleware service requirements applied. * * @stability unstable * @category utility types * @since 4.0.0 */ export type AddMiddleware = R extends Rpc ? Rpc<_Tag, _Payload, _Success, _Error, _Middleware | Middleware, RpcMiddleware.ApplyServices> : never; /** * Converts an RPC definition into the corresponding `Handler` type. * * @stability unstable * @category utility types * @since 4.0.0 */ export type ToHandler = R extends Rpc ? Handler<_Tag> : never; /** * The function signature for implementing an RPC handler. * * **Details** * * The function receives the decoded payload and request metadata, and returns * the RPC result shape, optionally wrapped with `Wrapper` options. * * @stability unstable * @category utility types * @since 4.0.0 */ export type ToHandlerFn = (payload: Payload, options: { readonly client: ServerClient; readonly requestId: RequestId; readonly headers: Headers; readonly rpc: Current; }) => WrapperOr>; /** * Returns `true` when the RPC with the specified tag has a streaming success * schema, or `never` otherwise. * * @stability unstable * @category utility types * @since 4.0.0 */ export type IsStream = R extends Rpc, infer _Error, infer _Middleware, infer _Requires> ? true : never; /** * Extracts RPCs whose tags match the given tag or union of tags. * * @stability unstable * @category utility types * @since 4.0.0 */ export type ExtractTag = R extends Rpc ? R : never; /** * Extracts the services provided by middleware on the RPC with the specified * tag. * * @stability unstable * @category utility types * @since 4.0.0 */ export type ExtractProvides = R extends Rpc ? RpcMiddleware.Provides<_Middleware["Identifier"]> : never; /** * Extracts the service requirements of the RPC with the specified tag. * * @stability unstable * @category utility types * @since 4.0.0 */ export type ExtractRequires = R extends Rpc ? _Requires : never; /** * Removes the services provided by middleware for the specified RPC tag from an * environment type. * * @stability unstable * @category utility types * @since 4.0.0 */ export type ExcludeProvides = Exclude>; /** * Computes the allowed handler result type for an RPC. * * **Details** * * Streaming RPCs may return a stream or an effect that produces a queue. Other * RPCs return an effect that succeeds with the success value or a deferred * success value. * * @stability unstable * @category utility types * @since 4.0.0 */ export type ResultFrom = R extends Rpc ? [_Success] extends [RpcSchema.Stream] ? Stream<_SA["Type"], _SE["Type"] | _Error["Type"], Services> | Effect, _SE["Type"] | Schema.Schema.Type<_Error>, Services> : Effect<_Success["Type"] | Deferred<_Success["Type"], _Error["Type"]>, _Error["Type"], Services> : never; /** * Returns an RPC type with the specified string prefix added to its tag while * preserving its payload, success, error, middleware, and requirements. * * @stability unstable * @category utility types * @since 4.0.0 */ export type Prefixed = Rpcs extends Rpc ? Rpc<`${Prefix}${_Tag}`, _Payload, _Success, _Error, _Middleware, _Requires> : never; /** * Creates an RPC definition with the supplied tag and optional schemas. * * **Details** * * Payload options can be either a schema or struct fields. `stream: true` wraps * the success and error schemas in a stream schema and sets the normal error * schema to `Schema.Never`. `primaryKey` creates a payload class with a * primary key derived from the payload value. * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const make: (tag: Tag, options?: { readonly payload?: Payload; readonly success?: Success; readonly error?: Error; readonly defect?: DefectSchema; readonly stream?: Stream; readonly primaryKey?: [Payload] extends [Schema.Struct.Fields] ? ((payload: Payload extends Schema.Struct.Fields ? Struct.Simplify["Type"]> : Payload["Type"]) => string) : never; }) => Rpc : Payload, Stream extends true ? RpcSchema.Stream : Success, Stream extends true ? typeof Schema.Never : Error>; /** * Creates a custom `Rpc` constructor that can transform the output schemas. * * **Example** (Defining a paginated RPC constructor) * * ```ts import.meta.vitest * import { Schema } from "effect" * import { Rpc } from "effect/rpc" * * // Create a custom Rpc wrapper definition by transforming the success and error * // schemas. * export interface RpcWithPagination extends Rpc.Custom { * readonly out: Rpc.Custom.Out< * Paginated, * this["error"] * > * } * * // The type definition for the transformed success schema. * export interface Paginated extends * Schema.Struct<{ * readonly offset: Schema.Number * readonly total: Schema.Number * readonly results: Schema.$Array * }> * {} * * // You can then implement the schema transformation using `Rpc.custom` * export const makePaginated = Rpc.custom((schemas) => ({ * ...schemas, * success: Schema.Struct({ * offset: Schema.Number, * total: Schema.Number, * results: Schema.Array(schemas.success) * }) * })) * * // You can then use the custom constructor in the same way `Rpc.make` is used. * export const listAllRpc = makePaginated("listAll", { * success: Schema.String * }) * * const result = [listAllRpc._tag, Schema.isSchema(listAllRpc.successSchema)] // => ["listAll", true] * ``` * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const custom: (f: (options: Custom.OutDefault) => (Def & Custom.OutDefault)["out"]) => >(tag: Tag, options?: { readonly payload?: Payload; readonly success?: Success; readonly error?: Error; readonly defect?: DefectSchema; readonly stream?: Stream; readonly primaryKey?: [Payload] extends [Schema.Struct.Fields] ? ((payload: Payload extends Schema.Struct.Fields ? Struct.Simplify["Type"]> : Payload["Type"]) => string) : never; }) => Rpc : Payload, Stream extends true ? RpcSchema.Stream : Out["success"], Stream extends true ? typeof Schema.Never : Out["error"]>; /** * Defines the type-level contract for an RPC custom constructor. * * **Details** * * A custom constructor receives the original success, error, and defect schemas * and returns transformed output schemas through `out`. * * @stability unstable * @category constructors * @since 4.0.0 */ export interface Custom { readonly out: Custom.OutDefault; readonly success: Schema.Top; readonly error: Schema.Top; readonly defect: DefectSchema; } /** * Helper types for defining RPC custom constructors. * * @stability unstable * @since 4.0.0 */ export declare namespace Custom { /** * The transformed schemas produced by a custom RPC constructor. * * @stability unstable * @category constructors * @since 4.0.0 */ interface Out { readonly success: Success; readonly error: Error; readonly defect: DefectSchema; } /** * The default custom-constructor output shape for arbitrary success and error * schemas. * * @stability unstable * @category constructors * @since 4.0.0 */ type OutDefault = Out; /** * Applies a custom constructor definition to concrete success and error * schemas and returns its transformed output schema type. * * @stability unstable * @category constructors * @since 4.0.0 */ type Kind = (Def & { readonly success: Success; readonly error: Error; })["out"]; } /** * Builds the `Schema.Exit` used to encode and decode RPC results. * * **Details** * * The failure side includes the RPC error schema, middleware error schemas, and * stream error schema for streaming RPCs. Streaming RPCs use `Schema.Void` for * the exit success value. The schema is cached per RPC definition. * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const exitSchema: (self: R) => Schema.Exit, ErrorExitSchema, DefectSchema>; /** * Annotation that sets the schema parse options used by `RpcClient` and * `RpcServer` when encoding and decoding an RPC's payload, success values, * stream chunks, and exits. * * **Details** * * The default value is `SchemaAST.defaultParseOptions`. Set `concurrency` to * parse product elements concurrently, which lets decoding transformations that * call `Effect.request` share `RequestResolver` batches. Annotate a single RPC * with `annotate`, or every RPC in a group with `RpcGroup.annotateRpcs`. * * @stability unstable * @category annotations * @since 4.0.0 */ export declare const ParseOptions: Context.Reference; declare const WrapperTypeId = "~effect/rpc/Rpc/Wrapper"; /** * Wraps a handler result with execution options for the RPC server. * * **Details** * * `fork` requests concurrent execution, and `uninterruptible` requests * uninterruptible execution. * * @stability unstable * @category wrapping * @since 4.0.0 */ export interface Wrapper { readonly [WrapperTypeId]: typeof WrapperTypeId; readonly value: A; readonly fork: boolean; readonly uninterruptible: boolean; } /** * A value that may be returned directly or wrapped with RPC server execution * options. * * @stability unstable * @category wrapping * @since 4.0.0 */ export type WrapperOr = A | Wrapper; /** * Returns `true` when the value is an RPC `Wrapper`. * * @stability unstable * @category guards * @since 4.0.0 */ export declare const isWrapper: (u: object) => u is Wrapper; /** * Wraps a handler result with RPC server execution options. * * **Details** * * When the value is already wrapped, unspecified options are inherited from the * existing wrapper. * * @stability unstable * @category wrapping * @since 4.0.0 */ export declare const wrap: (options: { readonly fork?: boolean | undefined; readonly uninterruptible?: boolean | undefined; }) => (value: A) => Wrapper; /** * Returns the wrapped response value when the input is an RPC `Wrapper`, or the * input itself when it is already unwrapped. * * @stability unstable * @category wrapping * @since 4.0.0 */ export declare const unwrap: (value: WrapperOr) => A; /** * Maps the value inside an RPC wrapper, preserving wrapper options such as * `fork` and `uninterruptible`; unwrapped values are mapped directly. * * @stability unstable * @category wrapping * @since 4.0.0 */ export declare const wrapMap: (self: WrapperOr, f: (value: A) => B) => WrapperOr; /** * Wraps a response Effect or Stream so the RPC server executes it concurrently * regardless of the server concurrency setting. * * @stability unstable * @category wrapping * @since 4.0.0 */ export declare const fork: (value: A) => Wrapper; /** * Wraps a response Effect or Stream so the RPC server runs it in an uninterruptible region. * * @stability unstable * @category wrapping * @since 4.0.0 */ export declare const uninterruptible: (value: A) => Wrapper; export {}; //# sourceMappingURL=Rpc.d.ts.map