import {z} from 'zod' export type GatewaySecretReferenceOrLiteral = string; export type GatewaySecret = string; export type SchemaRegistryAuthenticationScheme = {type: 'basic', username: GatewaySecretReferenceOrLiteral, password: GatewaySecret}; export type SchemaRegistryConfluentConfig = {schema_type: 'avro' | 'json' | 'any', endpoint: string, timeout_seconds?: number | undefined, authentication?: SchemaRegistryAuthenticationScheme | undefined}; export type Labels = {[key: string]: string | null}; export type SchemaRegistryCreate = {name: string, description?: string | undefined, type: 'confluent', config: SchemaRegistryConfluentConfig, labels?: Labels | undefined}; export type CreateApiV1EventGatewaysGatewayIdSchemaRegistriesArgs = {body: SchemaRegistryCreate}; export type CreateApiV1EventGatewaysGatewayIdSchemaRegistriesOptions = {baseUrl?: string | undefined, personalAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const labels = z.record(z.string(), z.string().min(1).max(63).nullable()); export const createdAt = z.string(); export const updatedAt = z.string(); export const schemaRegistry = z.object({name: z.string().min(1).max(255), description: z.string().max(512).optional(), type: z.string(), config: z.object({}).nullable().optional(), labels: labels.optional(), id: z.string(), 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 Schema Registry * * POST /v1/event-gateways/{gatewayId}/schema-registries * * Also accepts auth: systemAccountAccessToken, konnectAccessToken, serviceAccessToken */ export const createApiV1EventGatewaysGatewayIdSchemaRegistries = async (args: CreateApiV1EventGatewaysGatewayIdSchemaRegistriesArgs, options?: CreateApiV1EventGatewaysGatewayIdSchemaRegistriesOptions) => { 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/event-gateways/${encodeURIComponent(String(args.gatewayId))}/schema-registries${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(schemaRegistry, await res.json()) };