import {z} from 'zod' export type UpdateApiV2StorageFilesFileIdRefreshArgs = {fileId: number}; export type UpdateApiV2StorageFilesFileIdRefreshOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; export const filePrepareResponse = 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()}), uploadParams: z.object({key: z.string(), bucket: z.string(), acl: z.enum(["private", "public-read"]), credentials: z.object({AccessKeyId: z.string(), SecretAccessKey: z.string(), SessionToken: z.string(), Expiration: z.string()}), "x-amz-server-side-encryption": z.string().optional()}).optional(), absUploadParams: z.object({blobName: z.string(), accountName: z.string(), container: z.string(), absCredentials: z.object({SASConnectionString: z.string(), expiration: z.string()})}).optional(), gcsUploadParams: z.object({projectId: z.string(), bucket: z.string(), key: z.string(), access_token: z.string(), expires_in: z.number().int(), token_type: 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 }; /** * Refresh File Credentials * * PUT /v2/storage/files/{fileId}/refresh * * Also accepts auth: ManageKey, BearerAuth */ export const updateApiV2StorageFilesFileIdRefresh = async (args: UpdateApiV2StorageFilesFileIdRefreshArgs, options?: UpdateApiV2StorageFilesFileIdRefreshOptions) => { 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 (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/files/${encodeURIComponent(String(args.fileId))}/refresh${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(filePrepareResponse, await res.json()) };