umpirix/sdk/js/test/helpers.ts
caelum0x 4847e35451 OpenJev: open-source typed decision service
Typed decision/classification service with an in-process gte-small ONNX
embedding engine and lexical fallback, JS/Python SDKs, adapters, and the
otsone model family. Apache-2.0 (sub-packages keep their MIT licenses).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 00:12:36 +03:00

28 lines
870 B
TypeScript

import type { Fetch } from "../src";
export interface RecordedRequest {
url: string;
init: RequestInit | undefined;
body: unknown;
}
/** Create a fetch mock that records requests and returns supplied responses. */
export const mockFetch = (
respond: (req: RecordedRequest) => Response | Promise<Response>,
): { fetch: Fetch; requests: RecordedRequest[] } => {
const requests: RecordedRequest[] = [];
const fetch: Fetch = async (url, init) => {
const body = typeof init?.body === "string" ? JSON.parse(init.body) : undefined;
const req = { url, init, body };
requests.push(req);
return respond(req);
};
return { fetch, requests };
};
export const json = (data: unknown, init: ResponseInit = {}): Response =>
new Response(JSON.stringify(data), {
...init,
headers: { "content-type": "application/json", ...init.headers },
});