Skip to content

src/delete.ts

78 lines · 2.5 KB · TypeScript
Raw
/**
 * Delete a Dropbox file or folder. Pick this for a confirmed `files/delete_v2`
 * instead of assembling a raw `./request`. Requires `confirm: true`.
 *
 * @param params.path - Dropbox path starting with `/`.
 * @param params.parentRev - Optional revision to delete (conflict check).
 * @param params.confirm - Required `true` to perform the mutating call.
 * @param params.dryRun - When true, return the planned request without calling Dropbox.
 * @returns Dropbox delete result, or a dry-run preview.
 *
 * @example
 * import deleteDropboxPath from 'kody:@kentcdodds/dropbox/delete'
 * const result = await deleteDropboxPath({
 *   path: '/notes/old.txt',
 *   confirm: true,
 * })
 */

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. Pick this for a confirmed `files/delete_v2`
 * instead of assembling a raw `./request`. Requires `confirm: true`.
 *
 * @param params.path - Dropbox path starting with `/`.
 * @param params.parentRev - Optional revision to delete (conflict check).
 * @param params.confirm - Required `true` to perform the mutating call.
 * @param params.dryRun - When true, return the planned request without calling Dropbox.
 * @returns Dropbox delete result, or a dry-run preview.
 *
 * @example
 * import deleteDropboxPath from 'kody:@kentcdodds/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:@kentcdodds/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)
}