import {z} from 'zod' export type GetApiV2StorageFilesArgs = {sinceId?: string | undefined, maxId?: string | undefined, tags?: Array | undefined, q?: string | undefined, runId?: string | undefined, limit?: number | undefined, offset?: number | undefined, showExpired?: boolean | undefined}; export type GetApiV2StorageFilesOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; export const fileResponse = z.object({id: z.number().int(), created: z.string(), isPublic: z.boolean(), isSliced: z.boolean(), isEncrypted: z.boolean(), name: z.string(), fileType: z.string(), url: z.string(), provider: z.enum(["aws", "azure", "gcp"]), region: z.string(), sizeBytes: z.number().int().nullable(), tags: z.array(z.string()), maxAgeDays: z.number().int().nullable(), runId: z.string().nullable(), runIds: z.array(z.string()), creatorToken: z.object({id: z.number().int(), description: z.string()})}); export const getApiV2StorageFilesResponse = z.array(fileResponse); 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 }; /** * List Files * * GET /v2/storage/files * * Also accepts auth: ManageKey, BearerAuth */ export const getApiV2StorageFiles = async (args: GetApiV2StorageFilesArgs, options?: GetApiV2StorageFilesOptions) => { const {baseUrl = "https://connection.keboola.com", fetch: fetchFn = fetch, StorageKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sinceId != null) query.append("sinceId", String(args.sinceId)) if (args.maxId != null) query.append("maxId", String(args.maxId)) for (const item of args.tags ?? []) query.append("tags", String(item)) if (args.q != null) query.append("q", String(args.q)) if (args.runId != null) query.append("runId", String(args.runId)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.showExpired != null) query.append("showExpired", String(args.showExpired)) if (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/files${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(getApiV2StorageFilesResponse, await res.json()) };