Skip to content
← Public packages

@kody/dropbox

Reusable Dropbox OAuth helpers for account, metadata, search, file transfer, sharing, and generic API requests.

src/download.ts

42 lines · 1.3 KB · TypeScript
import { dropboxDownload } from './request.ts'
import type { DropboxRequestResult, JsonRecord } from './types.ts'
import { requireDropboxPath, requireRecord } from './types.ts'

export type DownloadInput = {
	path: string
}

export type DownloadBody = {
	metadata: JsonRecord | null
	bytesBase64: string
	byteLength: number
	contentType: string | null
}

/**
 * Download a Dropbox file and return metadata plus base64-encoded bytes.
 * @param input.path - Absolute Dropbox path starting with /.
 * @returns Download body with metadata and bytesBase64.
 * @example
 * import download from 'kody:@kody/dropbox/download'
 * const file = await download({ path: '/notes/todo.txt' })
 */
export async function download(input: DownloadInput): Promise<DownloadBody> {
	const path = requireDropboxPath(input.path)
	const result: DropboxRequestResult = await dropboxDownload({ path })
	return result.body as DownloadBody
}

/**
 * Download a Dropbox file and return metadata plus base64-encoded bytes.
 * @example
 * import download from 'kody:@kody/dropbox/download'
 * const file = await download({ path: '/notes/todo.txt' })
 */
export default async function downloadEntrypoint(
	params: Partial<DownloadInput> & Record<string, unknown> = {},
) {
	const input = requireRecord(params, 'download')
	requireDropboxPath(input.path)
	return download(input as unknown as DownloadInput)
}