import {z} from 'zod' export type AccountId = string; export type UploadId = string; export type UploadPartIndex = number; export type GetApiV2AccountsAccountIdUploadsUploadIdPartsUploadPartIndexArgs = {accountId: AccountId, uploadId: UploadId, uploadPartIndex: UploadPartIndex}; export type GetApiV2AccountsAccountIdUploadsUploadIdPartsUploadPartIndexOptions = {baseUrl?: string | undefined, authorizationHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const uploadPartRange = z.object({inclusiveEnd: z.number().int().gte(-1), inclusiveStart: z.number().int().gte(0)}); export const uploadId = z.string().min(16).max(16); export const uploadPartIndex = z.number().int().gte(0); export const preSignedUploadUrl = z.string().min(10); export const uploadPart = z.object({range: uploadPartRange, uploadId: uploadId, uploadPartIndex: uploadPartIndex, uploadUrl: preSignedUploadUrl}); 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 }; /** * Gets a remaining upload part for a multipart file upload. * * GET /v2/accounts/{accountId}/uploads/{uploadId}/parts/{uploadPartIndex} */ export const getApiV2AccountsAccountIdUploadsUploadIdPartsUploadPartIndex = async (args: GetApiV2AccountsAccountIdUploadsUploadIdPartsUploadPartIndexArgs, options?: GetApiV2AccountsAccountIdUploadsUploadIdPartsUploadPartIndexOptions) => { const {baseUrl = "https://api.bytescale.com", fetch: fetchFn = fetch, authorizationHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(uploadPart, await res.json()) };