Decorators

@system-inc/base-foundation · 515c140 · 5 symbols

Injects an RpcClient bound to the service declared by the given RpcClientBinding<RpcInterface>. The binding's name matches a client entry in the worker's rpc.client settings (typically a Cloudflare service binding), and its generic threads the remote service's type through so calls are fully typed.

InjectRpcClient(service: RpcClientBinding<RpcInterface>): TypedParameterDecorator<RpcClient<RpcInterface>>
  • service

    The binding identifying the remote service.

export class WorkerBindings {
    static readonly Billing = new RpcClientBinding<BillingRpcService>('BILLING');
}

@Injectable()
export class CheckoutService {
    constructor(
        @InjectRpcClient(WorkerBindings.Billing)
        private billing: RpcClient<BillingRpcService>,
    ) {}

    async charge(input: ChargeInput): Promise<ChargeResult> {
        return await this.billing.call().charge(input);
    }
}

View source ↗

@Rpc

decorator

Marks a method of an @RpcService class as a remote procedure, exposing it to RPC clients. Arguments are described with @RpcArgument so the dispatcher can deserialize and validate them.

Rpc(options: RpcOptions): MethodDecorator
  • options

    Optional per-procedure options (e.g. a `visibility` override).

Rpc(returnType?: TypeFunc, options?: RpcOptions): MethodDecorator
  • returnType

    Optional return type of the RPC, used to serialize the result.

  • options

    Optional per-procedure options (e.g. a `visibility` override).

@Rpc(() => String)
async echo(
    @RpcArgument(() => String) greeting: string,
    @RpcArgument(() => String) name: string,
): Promise<string> {
    return `${greeting} ${name}`;
}
// On an otherwise internal service, expose one procedure publicly.
@Rpc({ visibility: 'public' })
async healthCheck(): Promise<{ ok: boolean }> {
    return { ok: true };
}

View source ↗

@RpcArgument

decorator

Marks an argument of an @Rpc handler so the dispatcher can deserialize it to the given type — and, for class types, validate it — before invoking the handler.

RpcArgument(typeFunc: TypeFunc, options?: RpcArgumentOptions): ParameterDecorator
  • typeFunc

    The type to deserialize the argument to.

  • options

    Argument options such as `name`, `optional`, or `defaultValue`.

@Rpc(() => ChargeResult)
async charge(
    @RpcArgument(() => ChargeInput) input: ChargeInput,
    @RpcArgument(() => Boolean, { optional: true }) dryRun?: boolean,
): Promise<ChargeResult> { ... }

View source ↗

@RpcService

decorator

Marks a class as a remote procedure service, allowing it to expose methods as RPCs via the @Rpc decorator.

List the class in a module's (or the worker's) services. Visibility defaults to 'internal' — callable only from bound workers; use 'public' to expose the service on a public route.

RpcService(options?: RpcServiceOptions): ClassDecorator
  • options

    Service options such as `visibility`.

@RpcService({ visibility: 'internal' })
export class BillingRpcService {
    @Rpc(() => ChargeResult)
    async charge(
        @RpcArgument(() => ChargeInput) input: ChargeInput,
    ): Promise<ChargeResult> { ... }
}

View source ↗

@WebSocketRpc

decorator

Marks a method as a remote procedure callable only over a WebSocket.

WebSocket procedures are fire-and-forget from the caller's perspective — the return type is constrained to Promise<void>.

WebSocketRpc(): TypedMethodDecorator<T>
@WebSocketRpc()
async subscribe(@RpcArgument(() => String) topic: string): Promise<void> { ... }

View source ↗