import {z} from 'zod' export type GetApiV2StorageBranchBranchIdFilesFileIdArgs = {fileId: number, federationToken?: boolean | undefined, branchId: string}; export type GetApiV2StorageBranchBranchIdFilesFileIdOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; export const fileDetailResponse = 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()}), credentials: z.object({AccessKeyId: z.string(), SecretAccessKey: z.string(), SessionToken: z.string(), Expiration: z.string()}).optional(), s3Path: z.object({bucket: z.string(), key: z.string()}).optional(), absCredentials: z.object({SASConnectionString: z.string(), expiration: z.string()}).optional(), absPath: z.object({container: z.string(), name: z.string()}).optional(), gcsCredentials: z.object({access_token: z.string(), expires_in: z.number().int(), token_type: z.string(), projectId: z.string()}).optional(), gcsPath: z.object({bucket: z.string(), key: z.string()}).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 }; /** * File detail * * GET /v2/storage/branch/{branchId}/files/{fileId} * * Also accepts auth: ManageKey, BearerAuth */ export const getApiV2StorageBranchBranchIdFilesFileId = async (args: GetApiV2StorageBranchBranchIdFilesFileIdArgs, options?: GetApiV2StorageBranchBranchIdFilesFileIdOptions) => { 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.federationToken != null) query.append("federationToken", String(args.federationToken)) if (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/branch/${encodeURIComponent(String(args.branchId))}/files/${encodeURIComponent(String(args.fileId))}${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(fileDetailResponse, await res.json()) };