ModuleRpcCommon

Definitions common to both the client and server sides of RPC services.

license

Copyright (c) Aiden.ai

This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree.

Index

Type aliases

MethodsFor

MethodsFor: Extract<keyof serviceDefinition, string>

Method names for a service definition.

example
const serviceDefinition = {
  foo: { request: {} as {}, response: {} as {} },
  bar: { request: {} as {}, response: {} as {} },
};

// `ModuleRpcCommon.MethodsFor<typeof serviceDefinition>` is equivalent
// to `'foo' | 'bar'`.

RequestFor

RequestFor: serviceDefinition[method]["request"]

The type of a request for the given method of a service.

example
const serviceDefinition = {
  foo: { request: {} as { hello: 'world' }, response: {} as {} },
};

// `ModuleRpcCommon.RequestFor<typeof serviceDefinition, 'foo'>` is equivalent to
// `{ hello: 'world' }`.

ResponseFor

ResponseFor: serviceDefinition[method]["response"]

The type of a response for the given method of a service.

example
const serviceDefinition = {
  foo: { request: {} as {}, response: {} as { hello: 'world' } },
};

// `ModuleRpcCommon.ResponseFor<typeof serviceDefinition, 'foo'>` is equivalent to
// `{ hello: 'world' }`.

ServiceDefinition

ServiceDefinition: object

All service definitions derive from this type.

It associates a method name with request and response types as follows:

const bankingServiceDefinition = {
  getBalance: {
    request: {} as {
      userId: string;
    },
    response: {} as {
      value: number;
      unit: string;
    },
  },
  transfer: {
    request: {} as {
      fromUserId: string;
      toUserId: string;
      amount: {
        value: number;
        unit: string;
      }
    },
    response: {} as {},
  },
};

The {} as allows us to derive proper typing. It is better than defining a service with, e.g., an interface: with the definition above, the methods can be enumerated.

Type declaration

  • [method: string]: object

UnaryMethodsFor

UnaryMethodsFor: Extract<ModuleRpcUtils.ValueOf<object>, string> & MethodsFor<serviceDefinition>

Method names of the unary methods in a service definition (the server streams are filtered out).

example
const serviceDefinition = {
  unary: { request: {} as {}, response: {} as {} },
  serverStream: {
    type: ModuleRpcCommon.ServiceMethodType.serverStream,
    request: {} as {},
    response: {} as {},
  },
};

// `ModuleRpcCommon.UnaryMethodsFor<typeof serviceDefinition>` is equivalent
// to `'unary'` (the method `'serverStream'` is filtered out).

Object literals

Const ServiceMethodType

ServiceMethodType: object

The type of service methods.

serverStream

serverStream: symbol = Symbol('serverStream')

A single request is followed by multiple responses (they can be sent at different points in time).

Server streams can be used, e.g., by a web application to subscribe to push notifications.

unary

unary: symbol = Symbol('unary')

A single request is followed by a single response. Unary RPCs are the most common ones.