import {z} from 'zod' export type GetApiKosV1BucketsArgs = {page?: number | undefined, page_size?: number | undefined, name_filter?: string | undefined, refresh?: boolean | undefined}; export type GetApiKosV1BucketsOptions = {baseUrl: string, KCustomerID?: string | undefined, fetch?: typeof fetch | undefined}; export const bucketResponse = z.object({name: z.string().nullable().optional(), krnid: z.string().nullable().optional(), owner: z.string().optional(), account_id: z.string().optional(), createdAt: z.string().nullable().optional(), usedCapacity: z.number().nullable().optional(), allocatedCapacity: z.number().int().nullable().optional(), region: z.string().nullable().optional(), url: z.string().nullable().optional(), status: z.string().nullable().optional(), path: z.string().nullable().optional(), protocol: z.string().nullable().optional(), tags: z.record(z.string(), z.unknown()).nullable().optional(), versioning: z.boolean().nullable().optional(), anonymous_access: z.boolean().nullable().optional(), tier: z.string().nullable().optional()}); export const bucketsResponse = z.object({totalItems: z.number().int().optional(), totalPages: z.number().int().optional(), currentPage: z.number().int().optional(), nextPage: z.number().int().optional(), items: z.array(bucketResponse).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 all buckets * * GET /kos/v1/buckets * * Also accepts auth: Authorization */ export const getApiKosV1Buckets = async (args: GetApiKosV1BucketsArgs, options: GetApiKosV1BucketsOptions) => { const {baseUrl, fetch: fetchFn = fetch, KCustomerID} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.name_filter != null) query.append("name_filter", String(args.name_filter)) if (args.refresh != null) query.append("refresh", String(args.refresh)) if (KCustomerID !== undefined) headers.set("K-Customer-ID", KCustomerID) const url = `${baseUrl.replace(/\/+$/, '')}/kos/v1/buckets${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(bucketsResponse, await res.json()) };