import {z} from 'zod' export type UpdateNetworkConfigRequest = {kms_urls?: (Array | null) | undefined, gateway_urls?: (Array | null) | undefined, restart?: boolean | undefined}; export type PatchApiApiV1CvmsCvmIdNetworkConfigArgs = {cvm_id: string, body: UpdateNetworkConfigRequest}; export type PatchApiApiV1CvmsCvmIdNetworkConfigOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const networkConfigPortMappingEntry = z.object({host_port: z.number().int().gte(1).lte(65535), vm_port: z.number().int().gte(1).lte(65535), protocol: z.string().optional(), host_address: z.string().optional(), source_cidrs: z.array(z.string()).optional()}); export const networkConfigResponse = z.object({kms_urls: z.array(z.string()).optional(), gateway_urls: z.array(z.string()).optional(), ports: z.array(networkConfigPortMappingEntry).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 KMS/Gateway URLs * * PATCH /api/v1/cvms/{cvm_id}/network-config */ export const patchApiApiV1CvmsCvmIdNetworkConfig = async (args: PatchApiApiV1CvmsCvmIdNetworkConfigArgs, options?: PatchApiApiV1CvmsCvmIdNetworkConfigOptions) => { const {baseUrl = "https://cloud-api.phala.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(/\/+$/, '')}/api/v1/cvms/${encodeURIComponent(String(args.cvm_id))}/network-config${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(networkConfigResponse, await res.json()) };