or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

client-management.mderror-handling.mdindex.mdinterceptors.mdmethod-descriptors.mdstreaming.md
tile.json

client-management.mddocs/

Client Management

Core client classes for creating and managing gRPC-Web connections with comprehensive configuration options and support for multiple RPC patterns.

Capabilities

AbstractClientBase

Abstract base interface defining the core client operations that all gRPC-Web clients must implement.

/**
 * Base interface for gRPC-Web clients providing abstract methods for RPC operations
 */
abstract class AbstractClientBase {
  /**
   * Make an RPC call with callback-based response handling
   * @param method - The method URL to invoke
   * @param request - The request message instance
   * @param metadata - Request metadata headers
   * @param methodDescriptor - Method descriptor with type information
   * @param callback - Callback function for response/error handling
   * @returns ClientReadableStream for the call
   */
  abstract rpcCall<REQ, RESP>(
    method: string,
    request: REQ,
    metadata: Metadata,
    methodDescriptor: MethodDescriptor<REQ, RESP>,
    callback: (err: RpcError, response: RESP) => void
  ): ClientReadableStream<RESP>;

  /**
   * Make an RPC call with Promise-based response handling
   * @param method - The method URL to invoke
   * @param request - The request message instance
   * @param metadata - Request metadata headers
   * @param methodDescriptor - Method descriptor with type information
   * @param options - Options including abort signal for cancellation
   * @returns Promise resolving to the response message
   */
  abstract thenableCall<REQ, RESP>(
    method: string,
    request: REQ,
    metadata: Metadata,
    methodDescriptor: MethodDescriptor<REQ, RESP>,
    options?: PromiseCallOptions
  ): Promise<RESP>;

  /**
   * Create a server streaming call
   * @param method - The method URL to invoke
   * @param request - The request message instance
   * @param metadata - Request metadata headers
   * @param methodDescriptor - Method descriptor with type information
   * @returns ClientReadableStream for receiving streaming responses
   */
  abstract serverStreaming<REQ, RESP>(
    method: string,
    request: REQ,
    metadata: Metadata,
    methodDescriptor: MethodDescriptor<REQ, RESP>
  ): ClientReadableStream<RESP>;
}

GrpcWebClientBase

Concrete implementation of the gRPC-Web client using the application/grpc-web wire format.

/**
 * Concrete gRPC-Web client implementation with configurable options
 */
class GrpcWebClientBase extends AbstractClientBase {
  /**
   * Create a new gRPC-Web client
   * @param options - Configuration options for the client
   */
  constructor(options?: GrpcWebClientBaseOptions);

  /**
   * Make a unary RPC call with Promise-based response handling
   * This is an alias for thenableCall() for more semantic naming
   * @param method - The method URL to invoke
   * @param request - The request message instance
   * @param metadata - Request metadata headers
   * @param methodDescriptor - Method descriptor with type information
   * @param options - Options including abort signal for cancellation
   * @returns Promise resolving to the response message
   */
  unaryCall<REQ, RESP>(
    method: string,
    request: REQ,
    metadata: Metadata,
    methodDescriptor: MethodDescriptor<REQ, RESP>,
    options?: PromiseCallOptions
  ): Promise<RESP>;
}

Client Configuration Options

Configuration interface for customizing gRPC-Web client behavior.

/**
 * Configuration options for GrpcWebClientBase
 */
interface GrpcWebClientBaseOptions {
  /** Wire format: 'text' for base64-encoded or 'binary' for binary protobuf */
  format?: string;
  /** Suppress CORS preflight requests by encoding headers in URL parameters */
  suppressCorsPreflight?: boolean;
  /** Include credentials (cookies) in cross-origin requests */
  withCredentials?: boolean;
  /** Array of interceptors for unary RPC calls */
  unaryInterceptors?: UnaryInterceptor<unknown, unknown>[];
  /** Array of interceptors for streaming RPC calls */
  streamInterceptors?: StreamInterceptor<unknown, unknown>[];
}

/**
 * Options for Promise-based RPC calls
 */
interface PromiseCallOptions {
  /** AbortSignal to cancel the call */
  readonly signal?: AbortSignal;
}

Usage Examples:

import { GrpcWebClientBase } from "grpc-web";

// Basic client with default options
const client = new GrpcWebClientBase();

// Client with custom configuration
const client = new GrpcWebClientBase({
  format: 'binary',
  suppressCorsPreflight: true,
  withCredentials: true,
  unaryInterceptors: [authInterceptor, loggingInterceptor],
  streamInterceptors: [streamLoggingInterceptor]
});

// Making calls with different patterns
const response = await client.unaryCall(
  'https://api.example.com/service/Method',
  requestMessage,
  { 'custom-header': 'value' },
  methodDescriptor
);

// With cancellation support
const controller = new AbortController();
const responsePromise = client.unaryCall(
  'https://api.example.com/service/Method',
  requestMessage,
  {},
  methodDescriptor,
  { signal: controller.signal }
);

// Cancel the call after 5 seconds
setTimeout(() => controller.abort(), 5000);

try {
  const response = await responsePromise;
} catch (error) {
  if (error.code === StatusCode.CANCELLED) {
    console.log('Call was cancelled');
  }
}

Wire Format Support

gRPC-Web supports two wire formats:

  • 'text' format: Base64-encoded protobuf with 'application/grpc-web-text' content type
  • 'binary' format: Binary protobuf with 'application/grpc-web+proto' content type

The text format is more compatible with older browsers and proxies but has higher overhead. The binary format is more efficient but requires full binary data support in the network path.

CORS Configuration

When suppressCorsPreflight is enabled, the client encodes request headers as URL parameters to avoid CORS preflight requests. This is useful when:

  • The gRPC-Web proxy doesn't support CORS properly
  • You want to minimize the number of HTTP requests
  • The browser's CORS preflight behavior is causing issues

Credential Handling

The withCredentials option controls whether cookies and authentication headers are included in cross-origin requests. Enable this when:

  • Your gRPC services require authentication cookies
  • You're using session-based authentication
  • The client and server are on different domains but share authentication state