UNPKG

@proofkit/fmodata

Version:

FileMaker OData API client

81 lines (80 loc) 2.84 kB
import { Database } from './client/database.js'; import { FMServerConnection } from './client/filemaker-odata.js'; import { Logger } from './logger.js'; export interface MockRoute { /** URL pattern to match against. String matches with `includes()`, RegExp tests the full URL. */ urlPattern: string | RegExp; /** HTTP method to match (case-insensitive). If omitted, matches any method. */ method?: string; /** Response data. Arrays are wrapped in OData `{ value: [...] }` format. Objects are sent as-is. */ response: unknown; /** HTTP status code (default: 200) */ status?: number; /** Response headers */ headers?: Record<string, string>; /** If set, the fetch handler rejects with this error (simulates network failure). */ throwError?: Error; } export interface RequestSpy { /** All recorded requests */ readonly calls: ReadonlyArray<{ url: string; method: string; body?: string; headers?: Record<string, string>; }>; /** Clear recorded calls */ clear(): void; /** Get calls matching a URL pattern */ forUrl(pattern: string | RegExp): ReadonlyArray<{ url: string; method: string; body?: string; }>; } /** * A mock FMServerConnection for testing. * * Wraps a real FMServerConnection with a router-style fetch handler, * so the full HTTP parsing pipeline (error classification, OData handling, etc.) * is exercised in tests. * * Routes can be added at construction time or dynamically via `.addRoute()`. */ export declare class MockFMServerConnection { private readonly routes; private readonly connection; private readonly _spy?; constructor(config?: { routes?: MockRoute[]; baseUrl?: string; enableSpy?: boolean; logger?: Logger; }); /** * Add a route to the mock. Routes added after construction are picked up * automatically by subsequent requests. Routes are matched in order, * and the first matching route wins. */ addRoute(route: MockRoute): this; /** * Set multiple routes, replacing any existing routes. */ setRoutes(routes: MockRoute[]): this; /** * Get the request spy (only available if `enableSpy: true` was passed to constructor). */ get spy(): RequestSpy | undefined; /** * Create a Database instance, same API as FMServerConnection.database(). */ database<IncludeSpecialColumns extends boolean = false>(name: string, config?: { normalizeDatabaseName?: boolean; useEntityIds?: boolean; includeSpecialColumns?: IncludeSpecialColumns; }): Database<IncludeSpecialColumns>; /** * Get the underlying FMServerConnection (for cases that need the real type). */ get asConnection(): FMServerConnection; }