Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kentcdodds/dropbox

src/download.ts

34 lines · 1.0 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
}

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:@kentcdodds/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)
}