UNPKG

opinionated-machine

Version:

Very opinionated DI framework for fastify, built on top of awilix

65 lines (64 loc) 3.16 kB
import type { HttpStatusCode, SSEContractDefinition } from '@lokalise/api-contracts'; import type { z } from 'zod'; import type { AnyFastifyInstance } from './AnyFastifyInstance.ts'; import type { InjectPayloadSSEOptions, InjectSSEOptions, InjectSSEResult, SSEResponse } from './sseTestTypes.ts'; /** * Build a `bodyForStatus` accessor bound to one inject call. The closure * captures the contract's schemas map so the resulting helper knows which * schemas to parse against; at the type level the caller is constrained to * status codes the contract actually declares. * * @internal Exported only for unit testing — not part of the public API * (the testing barrel re-exports `injectSSE`/`injectPayloadSSE` by name). */ export declare function bindBodyForStatus<Schemas extends Partial<Record<HttpStatusCode, z.ZodTypeAny>> | undefined>(contract: { responseBodySchemasByStatusCode?: Schemas; }, closed: Promise<SSEResponse>): InjectSSEResult<Schemas>['bodyForStatus']; /** * Inject a GET SSE request using a contract definition. * * Best for testing SSE endpoints that complete (streaming responses). * For long-lived connections, use `connectSSE` with a real HTTP server. * * @param app - Fastify instance * @param contract - SSE route contract * @param options - Request options (params, query, headers) * * @example * ```typescript * const { closed } = injectSSE(app, streamContract, { * query: { userId: 'user-123' }, * }) * const result = await closed * const events = parseSSEEvents(result.body) * ``` */ export declare function injectSSE<Contract extends SSEContractDefinition<'get', z.ZodTypeAny, z.ZodTypeAny, z.ZodTypeAny, undefined, Record<string, z.ZodTypeAny>, Partial<Record<HttpStatusCode, z.ZodTypeAny>> | undefined>>(app: AnyFastifyInstance, contract: Contract, options?: InjectSSEOptions<Contract>): InjectSSEResult<Contract['responseBodySchemasByStatusCode']>; /** * Inject a POST/PUT/PATCH SSE request using a contract definition. * * This helper is designed for testing OpenAI-style streaming APIs where * the request includes a body and the response streams events. * * @param app - Fastify instance * @param contract - SSE route contract with body * @param options - Request options (params, query, headers, body) * * @example * ```typescript * // Fire the SSE request * const { closed } = injectPayloadSSE(app, chatCompletionContract, { * body: { message: 'Hello', stream: true }, * headers: { authorization: 'Bearer token' }, * }) * * // Wait for streaming to complete and get full response * const result = await closed * const events = parseSSEEvents(result.body) * * expect(events).toContainEqual( * expect.objectContaining({ event: 'chunk' }) * ) * ``` */ export declare function injectPayloadSSE<Contract extends SSEContractDefinition<'post' | 'put' | 'patch', z.ZodTypeAny, z.ZodTypeAny, z.ZodTypeAny, z.ZodTypeAny, Record<string, z.ZodTypeAny>, Partial<Record<HttpStatusCode, z.ZodTypeAny>> | undefined>>(app: AnyFastifyInstance, contract: Contract, options: InjectPayloadSSEOptions<Contract>): InjectSSEResult<Contract['responseBodySchemasByStatusCode']>;