Skip to content
← Public packages

@kody/dropbox

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

src/delete.ts

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

export type DeleteInput = {
	path: string
	parentRev?: string
	confirm?: boolean
	dryRun?: boolean
}

/**
 * Delete a Dropbox file or folder. Requires `confirm: true`; use `dryRun: true` to preview.
 * @param input.path - Absolute Dropbox path starting with /.
 * @param input.confirm - Required true to delete.
 * @returns Delete result or dry-run preview.
 * @example
 * import deleteDropboxPath from 'kody:@kody/dropbox/delete'
 * const result = await deleteDropboxPath({
 *   path: '/notes/old.txt',
 *   confirm: true,
 * })
 */
export async function deleteDropboxPath(
	input: DeleteInput,
): Promise<DropboxRequestResult | JsonRecord> {
	const path = requireDropboxPath(input.path)
	return request({
		endpoint: 'files/delete_v2',
		body: {
			path,
			...(input.parentRev ? { parent_rev: input.parentRev } : {}),
		},
		confirm: input.confirm,
		dryRun: input.dryRun,
	})
}

/**
 * Delete a Dropbox file or folder.
 * Requires `confirm: true`; use `dryRun: true` to preview the request.
 * @example
 * import deleteDropboxPath from 'kody:@kody/dropbox/delete'
 * const result = await deleteDropboxPath({
 *   path: '/notes/old.txt',
 *   confirm: true,
 * })
 */
export default async function deleteDropboxPathEntrypoint(
	params: Partial<DeleteInput> & Record<string, unknown> = {},
) {
	const input = requireRecord(params, 'delete')
	requireDropboxPath(input.path)
	return deleteDropboxPath(input as unknown as DeleteInput)
}