import { fillPath, queryString, type ApiShape, type EndpointAt, type EndpointShape, type HeadersLike, type Method, type PathsOf, type Query, type RequestArgs, } from '@avijs/client'; import { createAvi, type AviApp, type AviOptions, type ModuleOptions, type ModuleRef, } from '@avijs/core'; type HeadersArg = ConstructorParameters[0]; export interface TestRequest { /** Objects are sent as JSON; strings, bytes and forms as they are. */ body?: unknown; headers?: HeadersLike; query?: Query; } export interface TestResponse { /** True for a 2xx status. */ readonly ok: boolean; readonly status: number; readonly headers: Headers; /** Parsed JSON for JSON responses, otherwise the text. */ readonly body: unknown; readonly text: string; } export interface TestClient { readonly app: AviApp; /** The base URL the app is listening on, such as `http://127.0.0.1:52341`. */ readonly url: string; request(method: string, path: string, init?: TestRequest): Promise; get(path: string, init?: TestRequest): Promise; post(path: string, body?: unknown, init?: TestRequest): Promise; put(path: string, body?: unknown, init?: TestRequest): Promise; patch(path: string, body?: unknown, init?: TestRequest): Promise; delete(path: string, init?: TestRequest): Promise; /** * Calls typed by the `Api` that `avi client` generates, for `createTestClient(...)`. * Same shape as `@avijs/client`, but a status never throws. */ readonly api: TestApi; /** * The same client, sending these headers on every request, such as a user's token. Headers * passed to a call still win. Calls from either client reach the same app. */ withHeaders(headers: HeadersLike): TestClient; /** Closes the app, which also disposes its providers. */ close(): Promise; } /** A response from `client.api`: `body` is typed when `ok` is true. */ export type TypedTestResponse = | (Omit & { readonly ok: true; readonly body: T }) | (Omit & { readonly ok: false }); type TestCall =

>( path: P, ...args: RequestArgs> ) => Promise>>>; type ResponseOf = E extends EndpointShape ? E['response'] : never; export interface TestApi { readonly get: TestCall; readonly post: TestCall; readonly put: TestCall; readonly patch: TestCall; readonly delete: TestCall; readonly head: TestCall; readonly options: TestCall; } /** * Starts an app on a random local port and returns a small HTTP client for it. Pass a module and * options (including `overrides` to swap providers for fakes), or an app you built yourself. * Pass the generated `Api` type for typed calls: `createTestClient(AppModule)`. */ export async function createTestClient = never>( target: AviApp | ModuleRef | ModuleOptions, options: AviOptions = {}, ): Promise> { const app = isApp(target) ? target : createAvi({ startupSummary: false, ...options }).register(target); const { port } = await app.listen(0, '127.0.0.1'); return client(app, `http://127.0.0.1:${String(port)}`, new Headers()); } function client(app: AviApp, url: string, defaults: Headers): TestClient { const request = async (method: string, path: string, init: TestRequest = {}) => { const headers = new Headers(defaults); for (const [name, value] of new Headers(init.headers as HeadersArg)) headers.set(name, value); let body: string | Uint8Array | FormData | URLSearchParams | Blob | undefined; if (init.body !== undefined) { if (isRawBody(init.body)) { body = init.body; } else { body = JSON.stringify(init.body); if (!headers.has('content-type')) headers.set('content-type', 'application/json'); } } const response = await fetch(`${url}${path}${queryString(init.query)}`, { method, headers, body: body ?? null, }); const text = await response.text(); const type = response.headers.get('content-type') ?? ''; const parsed: unknown = /[/+]json\b/.test(type) && text !== '' ? JSON.parse(text) : text; const { ok, status } = response; return { ok, status, headers: response.headers, body: parsed, text }; }; const typed = (method: Method) => (path: string, init: TypedInit = {}) => request(method, fillPath(path, init.params), init); const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'] as const; const api = Object.fromEntries(methods.map((m) => [m.toLowerCase(), typed(m)])); return { app, url, request, get: (path, init) => request('GET', path, init), post: (path, body, init) => request('POST', path, { ...init, body }), put: (path, body, init) => request('PUT', path, { ...init, body }), patch: (path, body, init) => request('PATCH', path, { ...init, body }), delete: (path, init) => request('DELETE', path, init), api: Object.freeze(api) as unknown as TestApi, withHeaders: (extra) => { const headers = new Headers(defaults); for (const [name, value] of new Headers(extra as HeadersArg)) headers.set(name, value); return client(app, url, headers); }, close: () => app.close(), }; } interface TypedInit extends TestRequest { params?: Readonly>; } function isRawBody(body: unknown): body is string | Uint8Array | FormData | URLSearchParams | Blob { return ( typeof body === 'string' || body instanceof Uint8Array || body instanceof FormData || body instanceof URLSearchParams || body instanceof Blob ); } function isApp(value: unknown): value is AviApp { return ( typeof value === 'object' && value !== null && typeof (value as Partial).listen === 'function' && typeof (value as Partial).register === 'function' ); }