import {z} from 'zod' export type V1PortState = 'STATE_UNSPECIFIED' | 'STATE_PENDING' | 'STATE_READY' | 'STATE_FAILED' | 'STATE_DELETING'; export type PatchApiV1PortNameArgs = {'port.name': string, updateMask: string, body: {displayName?: string | undefined, public?: boolean | undefined, internalUrl?: string | undefined, publicUrl?: string | undefined, createTime?: string | undefined, creator?: string | undefined, creatorDisplayName?: string | undefined, updateTime?: (string | null) | undefined, updater?: string | undefined, updaterDisplayName?: string | undefined, state?: V1PortState | undefined, failureReason?: string | undefined}}; export type PatchApiV1PortNameOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const v1PortState = z.enum(["STATE_UNSPECIFIED", "STATE_PENDING", "STATE_READY", "STATE_FAILED", "STATE_DELETING"]); export const v1Port = z.object({name: z.string().optional(), displayName: z.string().optional(), public: z.boolean().optional(), internalUrl: z.string().optional(), publicUrl: z.string().optional(), createTime: z.string().optional(), creator: z.string().optional(), creatorDisplayName: z.string().optional(), updateTime: z.string().nullable().optional(), updater: z.string().optional(), updaterDisplayName: z.string().optional(), state: v1PortState.optional(), failureReason: z.string().optional()}); export const v1UpdatePortResponse = z.object({port: v1Port.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 }; /** * Updates a Port. * * PATCH /v1/{port.name} */ export const patchApiV1PortName = async (args: PatchApiV1PortNameArgs, options: PatchApiV1PortNameOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.updateMask != null) query.append("updateMask", String(args.updateMask)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args['port.name']))}${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(v1UpdatePortResponse, await res.json()) };