import {z} from 'zod' export type Address = {ip?: string | undefined, ipv6?: string | undefined, port?: number | undefined, fqdn?: string | undefined}; export type RemoteConnectionToken = {access_token?: string | undefined, client_secret?: string | undefined, refresh_token_endpoint?: string | undefined, refresh_token?: string | undefined, client_id?: string | undefined}; export type RemoteConnectionInfo = {node_address_list?: Array
| undefined, token?: RemoteConnectionToken | undefined, peer_remote_connection_uuid?: unknown | undefined, cluster_uuid?: unknown | undefined, cluster_function?: string | undefined, additional_capabilities?: Array | undefined}; export type BasicCredential = {username?: string | undefined, password?: string | undefined}; export type RemoteConnectionCredential = {auth_type?: string | undefined, bearer_token?: string | undefined, basic_credential?: BasicCredential | undefined}; export type RemoteConnectionResources = {local_connection_info?: RemoteConnectionInfo | undefined, role?: string | undefined, remote_address?: Address | undefined, remote_connection_info?: RemoteConnectionInfo | undefined, remote_credential?: RemoteConnectionCredential | undefined}; export type RemoteConnection = {name: string, resources: RemoteConnectionResources, description: string}; export type UserReference = {kind: string, name?: string | undefined, uuid: unknown}; export type RemoteConnectionMetadata = {last_update_time?: string | undefined, kind: string, uuid?: unknown | undefined, spec_version?: number | undefined, creation_time?: string | undefined, spec_hash?: string | undefined, should_force_translate?: boolean | undefined, owner_reference?: UserReference | undefined, categories?: {[key: string]: string} | undefined, name?: string | undefined}; export type RemoteConnectionIntentInput = {spec: RemoteConnection, api_version?: string | undefined, metadata: RemoteConnectionMetadata}; export type CreateApiRemoteConnectionsArgs = {body: RemoteConnectionIntentInput}; export type CreateApiRemoteConnectionsOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const messageResource = z.object({message: z.string(), reason: z.string(), details: z.record(z.string(), z.string()).optional()}); export const address = z.object({ip: z.string().optional(), ipv6: z.string().optional(), port: z.number().int().optional(), fqdn: z.string().optional()}); export const remoteConnectionToken = z.object({access_token: z.string().optional(), client_secret: z.string().optional(), refresh_token_endpoint: z.string().optional(), refresh_token: z.string().optional(), client_id: z.string().optional()}); export const remoteConnectionInfo = z.object({node_address_list: z.array(address).optional(), token: remoteConnectionToken.optional(), peer_remote_connection_uuid: z.string().optional(), cluster_uuid: z.string().optional(), cluster_function: z.string().optional(), additional_capabilities: z.array(z.string()).optional()}); export const basicCredential = z.object({username: z.string().optional(), password: z.string().optional()}); export const remoteConnectionCredential = z.object({auth_type: z.string().optional(), bearer_token: z.string().optional(), basic_credential: basicCredential.optional()}); export const remoteConnectionResources = z.object({local_connection_info: remoteConnectionInfo.optional(), role: z.string().optional(), remote_address: address.optional(), remote_connection_info: remoteConnectionInfo.optional(), remote_credential: remoteConnectionCredential.optional()}); export const remoteConnectionDefStatus = z.object({state: z.string().optional(), message_list: z.array(messageResource).optional(), name: z.string(), resources: remoteConnectionResources, description: z.string()}); export const remoteConnection = z.object({name: z.string().max(64), resources: remoteConnectionResources, description: z.string().max(1000)}); export const userReference = z.object({kind: z.string(), name: z.string().optional(), uuid: z.string()}); export const remoteConnectionMetadata = z.object({last_update_time: z.string().optional(), kind: z.string(), uuid: z.string().optional(), spec_version: z.number().int().optional(), creation_time: z.string().optional(), spec_hash: z.string().optional(), should_force_translate: z.boolean().optional(), owner_reference: userReference.optional(), categories: z.record(z.string(), z.string()).optional(), name: z.string().max(64).optional()}); export const remoteConnectionIntentResponse = z.object({status: remoteConnectionDefStatus.optional(), spec: remoteConnection.optional(), api_version: z.string(), metadata: remoteConnectionMetadata}); 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 }; /** * Creates a remote connection * * POST /remote_connections */ export const createApiRemoteConnections = async (args: CreateApiRemoteConnectionsArgs, options: CreateApiRemoteConnectionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/remote_connections${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(remoteConnectionIntentResponse, await res.json()) };