import {z} from 'zod' export type MockScenarioCreate = {name: string, priority?: number | undefined, match_method?: string | undefined, match_path_pattern?: (string | null) | undefined, match_headers?: {[key: string]: unknown} | undefined, match_query?: {[key: string]: unknown} | undefined, match_body?: {[key: string]: unknown} | undefined, response_status?: number | undefined, response_headers?: {[key: string]: unknown} | undefined, response_body?: ({[key: string]: unknown} | Array) | undefined, delay_ms?: number | undefined, is_enabled?: boolean | undefined}; export type BulkScenarioReplaceRequest = {scenarios: Array}; export type UpdateApiV1MocksMockIdScenariosArgs = {mock_id: string, body: BulkScenarioReplaceRequest}; export type UpdateApiV1MocksMockIdScenariosOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const mockScenarioResponse = z.object({id: z.string(), mock_id: z.string(), name: z.string(), priority: z.number().int(), match_method: z.string(), match_path_pattern: z.string().nullable(), match_headers: z.record(z.string(), z.unknown()), match_query: z.record(z.string(), z.unknown()), match_body: z.record(z.string(), z.unknown()), response_status: z.number().int(), response_headers: z.record(z.string(), z.unknown()), response_body: z.union([z.record(z.string(), z.unknown()), z.array(z.unknown())]), delay_ms: z.number().int(), is_enabled: z.boolean(), created_at: z.string(), updated_at: z.string()}); export const updateApiV1MocksMockIdScenariosResponse = z.array(mockScenarioResponse); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Atomically replace all scenarios on a mock * * PUT /v1/mocks/{mock_id}/scenarios */ export const updateApiV1MocksMockIdScenarios = async (args: UpdateApiV1MocksMockIdScenariosArgs, options: UpdateApiV1MocksMockIdScenariosOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/mocks/${encodeURIComponent(String(args.mock_id))}/scenarios${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(updateApiV1MocksMockIdScenariosResponse, await res.json()) };