import {z} from 'zod' export type GetApiV2StorageTablesIdSnapshotsArgs = {id: string, limit?: number | undefined, offset?: number | undefined}; export type GetApiV2StorageTablesIdSnapshotsOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; export const actorResponse = z.object({type: z.enum(["token", "session", "pat", "system"]), id: z.string(), name: z.string(), adminId: z.number().int().nullable()}); export const tableSnapshotResponse = z.object({id: z.string(), description: z.string().nullable(), createdTime: z.string(), type: z.string(), creatorToken: z.object({id: z.number().int(), description: z.string()}), actor: actorResponse.nullable(), uri: z.string(), dataFileId: z.number().int()}); export const getApiV2StorageTablesIdSnapshotsResponse = z.array(tableSnapshotResponse); 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 Table Snapshots * * GET /v2/storage/tables/{id}/snapshots * * Also accepts auth: ManageKey, BearerAuth */ export const getApiV2StorageTablesIdSnapshots = async (args: GetApiV2StorageTablesIdSnapshotsArgs, options?: GetApiV2StorageTablesIdSnapshotsOptions) => { 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.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/tables/${encodeURIComponent(String(args.id))}/snapshots${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(getApiV2StorageTablesIdSnapshotsResponse, await res.json()) };