import {z} from 'zod' export type AccountId = string; export type FileMetadata = {[key: string]: unknown}; export type MimeType = string; export type OriginalFileName = string; export type RelativeOrAbsoluteFilePath = string; export type FileName = string; export type FileNameVariablesEnabled = boolean; export type RelativeOrAbsoluteFolderPath = string; export type FolderPathVariablesEnabled = boolean; export type DynamicFilePath = {fileName?: FileName | undefined, fileNameFallback?: FileName | undefined, fileNameVariablesEnabled?: FileNameVariablesEnabled | undefined, folderPath?: RelativeOrAbsoluteFolderPath | undefined, folderPathVariablesEnabled?: FolderPathVariablesEnabled | undefined}; export type FilePathDefinition = RelativeOrAbsoluteFilePath | DynamicFilePath; export type FileTagName = string; export type HttpUrlToUpload = string; export type UploadFromUrlRequest = {metadata?: FileMetadata | undefined, mime?: MimeType | undefined, originalFileName?: OriginalFileName | undefined, path?: FilePathDefinition | undefined, tags?: Array | undefined, timeout?: number | undefined, url: HttpUrlToUpload}; export type CreateApiV2AccountsAccountIdUploadsUrlArgs = {accountId: AccountId, body: UploadFromUrlRequest}; export type CreateApiV2AccountsAccountIdUploadsUrlOptions = {baseUrl?: string | undefined, authorizationHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const accountId = z.string().min(7).max(7); export const eTag = z.string().min(1).max(255); export const filePathText = z.string().min(1).max(512); export const rawFileUrlText = z.string().min(1).max(255); export const basicUploadResponse = z.object({accountId: accountId, etag: eTag, filePath: filePathText, fileUrl: rawFileUrlText}); 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 }; /** * Upload from a URL with a single HTTP request: * * POST /v2/accounts/{accountId}/uploads/url */ export const createApiV2AccountsAccountIdUploadsUrl = async (args: CreateApiV2AccountsAccountIdUploadsUrlArgs, options?: CreateApiV2AccountsAccountIdUploadsUrlOptions) => { 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/url${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(basicUploadResponse, await res.json()) };