import {z} from 'zod' export type AIGatewayEntityIdentifier = string; export type PublicLabels = {[key: string]: string}; export type ManagedBy = {[key: string]: string}; export type CreateAIGatewayPolicyRequest = {display_name: string, name: AIGatewayEntityIdentifier, type: string, enabled?: boolean | undefined, global?: boolean | undefined, config: {[key: string]: unknown}, labels?: PublicLabels | undefined, managed_by?: ManagedBy | undefined}; export type CreateApiV1AiGatewaysGatewayIdPoliciesArgs = {body: CreateAIGatewayPolicyRequest}; export type CreateApiV1AiGatewaysGatewayIdPoliciesOptions = {baseUrl?: string | undefined, personalAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const aIGatewayEntityIdentifier = z.string().min(1).max(256); export const publicLabels = z.record(z.string(), z.string().min(1).max(63)); export const managedBy = z.record(z.string(), z.string().min(1).max(63)); export const uUID = z.string(); export const createdAt = z.string(); export const updatedAt = z.string(); export const aIGatewayPolicy = z.object({display_name: z.string().min(1).max(256), name: aIGatewayEntityIdentifier, type: z.string(), enabled: z.boolean().optional(), global: z.boolean().optional(), config: z.record(z.string(), z.unknown()), labels: publicLabels.optional(), managed_by: managedBy.optional(), id: uUID, created_at: createdAt, updated_at: updatedAt}); 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 an AI Gateway Policy * * POST /v1/ai-gateways/{gatewayId}/policies * * Also accepts auth: systemAccountAccessToken, konnectAccessToken, serviceAccessToken */ export const createApiV1AiGatewaysGatewayIdPolicies = async (args: CreateApiV1AiGatewaysGatewayIdPoliciesArgs, options?: CreateApiV1AiGatewaysGatewayIdPoliciesOptions) => { const {baseUrl = "https://global.api.konghq.com", fetch: fetchFn = fetch, personalAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (personalAccessToken !== undefined) headers.set('authorization', `Bearer ${personalAccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/ai-gateways/${encodeURIComponent(String(args.gatewayId))}/policies${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(aIGatewayPolicy, await res.json()) };