import {z} from 'zod' export type AccountId = string; export type UploadId = string; export type UploadPartIndex = number; export type ETag = string; export type CompleteUploadPartRequest = {etag: ETag}; export type UpdateApiV2AccountsAccountIdUploadsUploadIdPartsUploadPartIndexArgs = {accountId: AccountId, uploadId: UploadId, uploadPartIndex: UploadPartIndex, body: CompleteUploadPartRequest}; export type UpdateApiV2AccountsAccountIdUploadsUploadIdPartsUploadPartIndexOptions = {baseUrl?: string | undefined, authorizationHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const completeMultipartUploadResponsePending = z.object({status: z.literal("Pending")}); export const eTag = z.string().min(1).max(255); export const completeMultipartUploadResponseCompleted = z.object({etag: eTag, status: z.literal("Completed")}); export const completeMultipartUploadResponse = z.union([completeMultipartUploadResponsePending, completeMultipartUploadResponseCompleted]); 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 }; /** * Marks an upload part as uploaded. * * You must call this endpoint after you have successfully issued a `PUT` request to the `uploadUrl` on the corresponding UploadPart. * * PUT /v2/accounts/{accountId}/uploads/{uploadId}/parts/{uploadPartIndex} */ export const updateApiV2AccountsAccountIdUploadsUploadIdPartsUploadPartIndex = async (args: UpdateApiV2AccountsAccountIdUploadsUploadIdPartsUploadPartIndexArgs, options?: UpdateApiV2AccountsAccountIdUploadsUploadIdPartsUploadPartIndexOptions) => { const {baseUrl = "https://api.bytescale.com", fetch: fetchFn = fetch, authorizationHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (authorizationHeader !== undefined) headers.set("Authorization", authorizationHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v2/accounts/${encodeURIComponent(String(args.accountId))}/uploads/${encodeURIComponent(String(args.uploadId))}/parts/${encodeURIComponent(String(args.uploadPartIndex))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(completeMultipartUploadResponse, await res.json()) };