import {z} from 'zod' export type Middleware = {order?: number | undefined, resource?: string | undefined, resource_type?: ('function' | 'service') | undefined, resource_version?: string | undefined, type?: ('pre-request' | 'post-request') | undefined}; export type MappingVersionCreateRequest = {resource_type: string, resource: string, resource_version?: string | undefined, active?: boolean | undefined, description?: string | undefined, middleware?: Array | undefined, exact_match?: boolean | undefined, attributes?: Array | undefined, boolean_logic?: string | undefined, invocation_type?: string | undefined}; export type CreateApiGatewayGatewayTopicMappingVersionArgs = {gateway_topic: string, method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD', path: string, body: MappingVersionCreateRequest}; export type CreateApiGatewayGatewayTopicMappingVersionOptions = {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 }; /** * Create a new mapping version * * POST /gateway/{gateway_topic}/mapping/version * * Also accepts auth: apiKeyAuth */ export const createApiGatewayGatewayTopicMappingVersion = async (args: CreateApiGatewayGatewayTopicMappingVersionArgs, options?: CreateApiGatewayGatewayTopicMappingVersionOptions) => { const {baseUrl = "https://api.quiva.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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/${encodeURIComponent(String(args.gateway_topic))}/mapping/version${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(mappingVersionResponseWrapper, await res.json()) };