Rayfin

@microsoft/rayfin-lib

The shared ApiClient, error classes, and client-composition factory types every other Rayfin SDK package builds on — mostly an internal dependency.

@microsoft/rayfin-lib is the shared HTTP client and utility layer underneath every other @microsoft/rayfin-* SDK package — rayfin-client, rayfin-core, rayfin-data, rayfin-auth, rayfin-auth-provider-fabric, rayfin-functions, and the connector and storage packages all depend on it. In 2.0 it is the only runtime dependency of @microsoft/rayfin-client. Most Rayfin applications never import it directly — the client re-exports ApiClientConfig, the error classes, and the factory types — but it is useful to know what lives here when you read a stack trace or write a custom service factory.

Installation

npm install @microsoft/rayfin-lib

ApiClient

The isomorphic HTTP client every higher-level SDK class wraps. It works in both browsers and Node.js using native fetch, with automatic 401 retry when a refresh callback is configured. In 2.0 one ApiClient is the shared transport of a composed RayfinClient, and it is the first argument every service and auth factory receives.

class ApiClient {
  constructor(config: ApiClientConfig);
  setAccessTokenCallback(callback: () => string | null | Promise<string | null>): void;
  setRefreshCallback(callback: () => Promise<void>): void;
  setAuthExhaustedCallback(callback: () => void): void;
  resolveUrl(path: string): string;
  getFunctionsBaseUrl(): string | undefined;
  get<T>(path: string, options?: RequestOptions): Promise<T>;
  post<T>(path: string, data?: any, options?: RequestOptions): Promise<T>;
  put<T>(path: string, data?: any, options?: RequestOptions): Promise<T>;
  delete<T>(path: string, options?: RequestOptions): Promise<T>;
  requestRaw(
    path: string,
    options?: {
      method?: string;
      headers?: Record<string, string>;
      body?: BodyInit | null;
      signal?: AbortSignal;
      timeout?: number;
    },
  ): Promise<Response>;
}

interface ApiClientConfig {
  baseUrl: string;
  publishableKey: string;
  functionsBaseUrl?: string;
  headers?: Record<string, string>;
  moniker?: string;
  timeout?: number;
  fetch?: typeof globalThis.fetch;
  getAccessToken?: () => string | null | Promise<string | null>;
  useProxy?: boolean;
  onAuthExhausted?: () => void;
  onRefreshNeeded?: () => Promise<void>;
}

See @microsoft/rayfin-client for the option table. RayfinClientConfig extends ApiClientConfig without the token hooks and useProxy, which belong to authentication adapters in 2.0. setAccessTokenCallback and setRefreshCallback are how Auth attaches itself to an ApiClient (Auth.attachToClient()), which is also how @microsoft/rayfin-auth-provider-fabric gets its token-refresh behavior.

The access-token callback and getAccessToken may return a promise, so a token provider can refresh asynchronously before a request is sent. Changed in 2.0

useProxy has no effect; set baseUrl: '' for same-origin routing. RayfinClient.create rejects it.

New in 1.36 ApiClientConfig.fetch lets callers inject a custom fetch implementation, for example one configured with a network proxy, and resolveUrl() is exported for cross-package wiring. getFunctionsBaseUrl() is also an exported method; application code normally builds a RayfinClient instead of calling them directly.

Client composition types

New in 2.0

These types define the contract between RayfinClient.create and the packages that plug into it. They are re-exported by @microsoft/rayfin-client.

interface ClientLifecycle {
  onDispose(cleanup: () => unknown): void;
}

type ClientServiceFactory<TService extends object = object> = (
  http: ApiClient,
  lifecycle: ClientLifecycle,
) => TService;

type ClientAuthFactory<TAuth extends object | undefined = object | undefined> = (
  http: ApiClient,
  lifecycle: ClientLifecycle,
) => TAuth;

A service factory returns its API synchronously — never a promise — and registers any cleanup with lifecycle.onDispose() as soon as it acquires a resource. An auth factory attaches authentication to the transport and returns the session API to expose as client.auth, or undefined. dataService(), functionsService(), connectorsService(), storageService(), browserSession(), and suppliedToken() are all factories of these shapes. See Write a custom service.

Errors

Every SDK package's thrown errors extend this hierarchy:

class SdkError extends Error {
  name: string;
  code?: string;
  constructor(message: string, code?: string);
}

class AuthError extends SdkError {}

interface WorkloadFailure {
  readonly errorCode?: string;
  readonly subErrorCode?: string;
  readonly functionName?: string;
  readonly invocationId?: string;
  readonly status?: string;
}

class NetworkError extends SdkError {
  status?: number;
  workload?: WorkloadFailure;
  serverErrorCode?: string;
  consentUrl?: string;
  retryAfterSeconds?: number;
  constructor(message: string, status?: number, code?: string);
}

NetworkError.workload carries structured Fabric workload failure detail when a response includes that envelope, so callers can branch on fields such as errorCode, subErrorCode, functionName, invocationId, and status instead of parsing message. New in 1.36

serverErrorCode is the machine-readable code from the response body (error, error.code, or errorCode), separate from code so existing branches on code keep working. retryAfterSeconds is the wait the service asked for, from the body or the Retry-After header. consentUrl is set when the service answers CONNECTOR_CONSENT_REQUIRED. New in 2.0

@microsoft/rayfin-client re-exports these same classes, so one instanceof AuthError check matches an error from any Rayfin package. In 1.x the client declared its own, separate AuthError. Changed in 2.0 See Errors on the client reference page.

EntityNameResolver

A narrowly scoped helper exported from the package root, used internally by the data client:

class EntityNameResolver {
  static getPlural(entityName: string): string;
  static getSingular(pluralName: string): string;
  static isPlural(word: string): boolean;
  static setCustomPlural(singular: string, plural: string): void;
  static clearCustomPlurals(): void;
}

EntityNameResolver backs the automatic singular/plural table-name inference @microsoft/rayfin-core performs for @entity() classes (a Todo class maps to a todos table), and lets you register irregular plurals it would not guess correctly on its own.

What's intentionally not documented here

ServicePlugin (an abstract base class for building custom service plugins) is exported from the package but marked @alpha and @hidden in its own source comments — it is not considered part of the public API yet, so this page does not document its shape.

Something wrong on this page?Report an issueEdit this page

On this page