import {z} from 'zod' export type MockCreate = {slug: string, name: string, description?: (string | null) | undefined, default_status?: number | undefined, default_response_body?: ({[key: string]: unknown} | Array | null) | undefined, is_enabled?: boolean | undefined}; export type CreateApiV1MocksArgs = {body: MockCreate}; export type CreateApiV1MocksOptions = {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 mockResponse = z.object({id: z.string(), organization_id: z.string(), slug: z.string(), name: z.string(), description: z.string().nullable(), default_status: z.number().int(), default_response_body: z.union([z.record(z.string(), z.unknown()), z.array(z.unknown())]).nullable(), is_enabled: z.boolean(), created_by: z.string().nullable(), created_at: z.string(), updated_at: z.string(), public_url_path: z.string(), scenarios: z.array(mockScenarioResponse).optional()}); 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 }; /** * Create a mock * * POST /v1/mocks */ export const createApiV1Mocks = async (args: CreateApiV1MocksArgs, options: CreateApiV1MocksOptions) => { 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${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(mockResponse, await res.json()) };