Restate Typescript SDK
    Preparing search index...

    Interface ScopedContextExperimental

    A context for making RPC calls within a specific scope.

    interface ScopedContext {
        serviceClient<D>(opts: ServiceDefinitionFrom<D>): Client<Service<D>>;
        serviceSendClient<D>(
            service: ServiceDefinitionFrom<D>,
        ): SendClient<Service<D>>;
        workflowClient<D>(
            opts: WorkflowDefinitionFrom<D>,
            key: string,
        ): Client<Workflow<D>>;
        workflowSendClient<D>(
            opts: WorkflowDefinitionFrom<D>,
            key: string,
        ): SendClient<Workflow<D>>;
    }
    Index

    Methods

    • Experimental

      Makes a type-safe request/response RPC to the specified target service.

      The RPC goes through Restate and is guaranteed to be reliably delivered. The RPC is also journaled for durable execution and will thus not be duplicated when the handler is re-invoked for retries or after suspending.

      This call will return the result produced by the target handler, or the Error, if the target handler finishes with a Terminal Error.

      This call is a suspension point: The handler might suspend while awaiting the response and resume once the response is available.

      Type Parameters

      • D

      Parameters

      Returns Client<Service<D>>

      Service Side:

      const service = restate.service(
      name: "myservice",
      handlers: {
      someAction: async(ctx: restate.Context, req: string) => { ... },
      anotherAction: async(ctx: restate.Context, count: number) => { ... }
      });

      // option 1: export only the type signature
      export type Service = typeof service;


      restate.serve({ services: [service], port: 9080 });

      Client side:

      // option 1: use only types and supply service name separately
      const result1 = await ctx.serviceClient<Service>({name: "myservice"}).someAction("hello!");

      // option 2: use full API spec
      type MyService: Service = { name: "myservice" };
      const result2 = await ctx.serviceClient(Service).anotherAction(1337);
    • Experimental

      Makes a type-safe one-way RPC to the specified target service. This method effectively behaves like enqueuing the message in a message queue.

      The message goes through Restate and is guaranteed to be reliably delivered. The RPC is also journaled for durable execution and will thus not be duplicated when the handler is re-invoked for retries or after suspending.

      This call will return immediately; the message sending happens asynchronously in the background. Despite that, the message is guaranteed to be sent, because the completion of the invocation that triggers the send (calls this function) happens logically after the sending. That means that any failure where the message does not reach Restate also cannot complete this invocation, and will hence recover this handler and (through the durable execution) recover the message to be sent.

      Type Parameters

      • D

      Parameters

      Returns SendClient<Service<D>>

      Service Side:

      const service = restate.service(
      name: "myservice",
      handlers: {
      someAction: async(ctx: restate.Context, req: string) => { ... },
      anotherAction: async(ctx: restate.Context, count: number) => { ... }
      });

      // option 1: export only the type signature of the router
      export type MyApi = typeof service;

      // option 2: export the API definition with type and name (name)
      const MyService: MyApi = { name: "myservice" };

      restate.serve({ services: [service], port: 9080 });

      Client side:

      // option 1: use only types and supply service name separately
      ctx.serviceSendClient<MyApi>({name: "myservice"}).someAction("hello!");

      // option 2: use full API spec
      ctx.serviceSendClient(MyService).anotherAction(1337);