import {z} from 'zod' export type GetApiGatewayMappingVersionGatewayTopicVersionNumberArgs = {gateway_topic: string, version_number: number, method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD', path: string}; export type GetApiGatewayMappingVersionGatewayTopicVersionNumberOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const middleware = z.object({order: z.number().int().optional(), resource: z.string().optional(), resource_type: z.enum(["function", "service"]).optional(), resource_version: z.string().optional(), type: z.enum(["pre-request", "post-request"]).optional()}); export const mappingVersionResponse = z.object({subject: z.string().optional(), resource_type: z.string().optional(), resource: z.string().optional(), resource_version: z.string().optional(), active: z.boolean().optional(), description: z.string().optional(), middleware: z.array(middleware).optional(), exact_match: z.boolean().optional(), attributes: z.array(z.string()).optional(), boolean_logic: z.string().optional(), invocation_type: z.string().optional(), version: z.number().int().optional(), modified: z.number().int().optional(), type: z.string().optional()}); export const metadata = z.object({subject: z.string().optional()}); export const mappingVersionResponseWrapper = z.object({body: mappingVersionResponse.optional(), metadata: metadata.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 }; /** * Get a specific mapping version * * GET /gateway/mapping-version/{gateway_topic}/{version_number} * * Also accepts auth: apiKeyAuth */ export const getApiGatewayMappingVersionGatewayTopicVersionNumber = async (args: GetApiGatewayMappingVersionGatewayTopicVersionNumberArgs, options?: GetApiGatewayMappingVersionGatewayTopicVersionNumberOptions) => { const {baseUrl = "https://api.quiva.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.method != null) query.append("method", String(args.method)) if (args.path != null) query.append("path", String(args.path)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/gateway/mapping-version/${encodeURIComponent(String(args.gateway_topic))}/${encodeURIComponent(String(args.version_number))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(mappingVersionResponseWrapper, await res.json()) };