import {z} from 'zod' export type ClientCertificate = {id?: string | undefined}; export type Service = {id?: string | undefined, name?: string | undefined, retries?: number | undefined, protocol?: string | undefined, host?: string | undefined, port?: number | undefined, path?: string | undefined, tags?: Array | undefined, enabled?: boolean | undefined, created_at?: number | undefined, updated_at?: number | undefined, connect_timeout?: number | undefined, write_timeout?: number | undefined, read_timeout?: number | undefined, client_certificate?: ClientCertificate | undefined, tls_verify?: boolean | undefined, tls_verify_depth?: Record | undefined, ca_certificates?: Array | undefined}; export type UpdateApiV1KongAdminServicesServiceIdArgs = {serviceId: string, body: Service}; export type UpdateApiV1KongAdminServicesServiceIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const clientCertificate = z.object({id: z.string().optional()}); export const service = z.object({id: z.string().optional(), name: z.string().optional(), retries: z.number().int().optional(), protocol: z.string().optional(), host: z.string().optional(), port: z.number().int().optional(), path: z.string().optional(), tags: z.array(z.string()).optional(), enabled: z.boolean().optional(), created_at: z.number().int().optional(), updated_at: z.number().int().optional(), connect_timeout: z.number().int().optional(), write_timeout: z.number().int().optional(), read_timeout: z.number().int().optional(), client_certificate: clientCertificate.optional(), tls_verify: z.boolean().optional(), tls_verify_depth: z.object({}).optional(), ca_certificates: z.array(z.string()).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 }; /** * Update service * * PUT /v1/kong-admin/services/{serviceId} */ export const updateApiV1KongAdminServicesServiceId = async (args: UpdateApiV1KongAdminServicesServiceIdArgs, options?: UpdateApiV1KongAdminServicesServiceIdOptions) => { const {baseUrl = "http://api.floik.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/kong-admin/services/${encodeURIComponent(String(args.serviceId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(service, await res.json()) };