module preferred license /** */ import { BaseError } from 'make-error' ; import { ModuleRpcUtils } from '../utils' ; /**
 * All service definitions derive from this type.
 *
 * It associates a method name with request and response types as follows:
 * ```Typescript
 * 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.
 */ export type ServiceDefinition = { [ method ] : { request : any ; response : any ; type ? : any ; } ; } ; /**
 * The type of service methods.
 */ export const ServiceMethodType = { /**
     * A single request is followed by a single response.
     * Unary RPCs are the most common ones.
     */ unary : Symbol ( 'unary' ) , /**
     * 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.
     */ serverStream : Symbol ( 'serverStream' ) , } ; /** The service definition is misformed or ill-adapted to the transport protocol. */ export class MisspecifiedServiceError extends BaseError { } example export type MethodsFor < serviceDefinition extends ServiceDefinition > = Extract < keyof serviceDefinition , string > ; example export type RequestFor < serviceDefinition extends ServiceDefinition , method extends MethodsFor < serviceDefinition > > = serviceDefinition [ method ] [ 'request' ] ; example export type ResponseFor < serviceDefinition extends ServiceDefinition , method extends MethodsFor < serviceDefinition > > = serviceDefinition [ method ] [ 'response' ] ; example export type UnaryMethodsFor < serviceDefinition extends ServiceDefinition > = Extract < ModuleRpcUtils . ValueOf < { [ method in keyof serviceDefinition ] : serviceDefinition [ method ] extends { type : typeof ServiceMethodType . serverStream ; } ? never : method ; } > , string > & MethodsFor < serviceDefinition > ; /**
 * Error types reported by RPC services.  The error types broadly follow the gRPC
 * status codes as detailed here: https://github.com/grpc/grpc-go/blob/master/codes/codes.go
 */ export enum RpcErrorType { /** Unknown error. */ unknown = 'unknown' , /** The operation was canceled (typically by the caller). */ canceled = 'canceled' , /**
     * The client specified an invalid argument. Note that this differs from FailedPrecondition.
     * It indicates arguments that are problematic regardless of the state of the system
     * (e.g., a malformed file name).
     */ invalidArgument = 'invalidArgument' , /** Some requested entity (e.g., file or directory) was not found. */ notFound = 'notFound' , /** An attempt to create an entity failed because one already exists. */ alreadyExists = 'alreadyExists' , /**
     * Some resource has been exhausted, perhaps a per-user quota, or perhaps the entire
     * file system is out of space.
     */ resourceExhausted = 'resourceExhausted' , /** The caller does not have permission to execute the specified operation. */ permissionDenied = 'permissionDenied' , /**
     * The operation was rejected because the system is not in a state required for the
     * operation's execution. For example, directory to be deleted may be non-empty, an rmdir
     * operation is applied to a non-directory, etc.
     */ failedPrecondition = 'failedPrecondition' , /** Operation is not implemented or not supported/enabled in this service. */ unimplemented = 'unimplemented' , /**
     * Some invariants expected by the underlying system has been broken. If you see one of
     * these errors, something is very broken.
     */ internal = 'internal' , /**
     * The service is currently unavailable.  This is a most likely a transient
     * condition and may be corrected by retrying with a backoff.
     */ unavailable = 'unavailable' , /** the request does not have valid authentication credentials for the operation. */ unauthenticated = 'unauthenticated' } /**
 * Map used to encode both request and response contexts.
 * The maps are produced and decoded by the client/server context connectors.
 */ export interface EncodedContext { [ key ] : string ; }