import {z} from 'zod' export type GetApiV1ProjectsProjectIdVolumesIdArgs = {projectId: string, id: string}; export type GetApiV1ProjectsProjectIdVolumesIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const commitmentTerm = z.enum(["COMMITMENT_TERM_NONE", "COMMITMENT_TERM_1_MONTH", "COMMITMENT_TERM_3_MONTHS", "COMMITMENT_TERM_6_MONTHS", "COMMITMENT_TERM_12_MONTHS", "COMMITMENT_TERM_24_MONTHS", "COMMITMENT_TERM_36_MONTHS", "COMMITMENT_TERM_60_MONTHS"]); export const decimal = z.object({value: z.string().optional()}); export const volumeState = z.enum(["VOLUME_STATE_UNSPECIFIED", "CREATING", "ACTIVE", "UPDATING", "DELETING", "FAILED"]); export const volume = z.object({commitmentEndTime: z.string().optional(), commitmentTerm: commitmentTerm.optional(), dataCenterId: z.string(), gibPriceHr: decimal.optional(), id: z.string(), nfsPath: z.string().optional(), nfsServer: z.string().optional(), projectId: z.string(), sizeGib: z.number().int(), sizeGibUsed: z.number().int().optional(), state: volumeState.optional(), totalPriceHr: decimal.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 }; /** * Get a volume * * GET /v1/projects/{projectId}/volumes/{id} */ export const getApiV1ProjectsProjectIdVolumesId = async (args: GetApiV1ProjectsProjectIdVolumesIdArgs, options?: GetApiV1ProjectsProjectIdVolumesIdOptions) => { const {baseUrl = "https://rest.compute.cudo.org", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set("Authorization", bearerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.projectId))}/volumes/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(volume, await res.json()) };