import { object, parseSafe, string } from 'remix/data-schema' import { assertFaxId, inputError, onetimefaxRequest } from './client.ts' const removeFileInput = object( { faxId: string(), fileId: string() }, { unknownKeys: 'error' }, ) /** * Remove one attachment from an unsent Onetime Fax draft, for example to * replace the wrong document before sending. Only works before send. * Spends nothing. * * Wraps `DELETE /api/v1/faxes/{faxId}/files/{fileId}` (https://www.onetimefax.com/developers/). * * @param input - `{ faxId, fileId }`. `fileId` comes from `./prepare-fax` or `./fax-status` (`fax.files[].id`). Unknown keys are rejected. * @returns `{ removed: true, faxId, fileId }` * * @example * import removeFile from 'kody:@kody/onetimefax/remove-file' * * await removeFile({ faxId: 'cmr4...', fileId: 'cmr5...' }) */ export default async function removeFile(raw: unknown) { const parsed = parseSafe(removeFileInput, raw) if (!parsed.success) { throw inputError('remove-file', 'faxId, fileId', parsed.issues) } const faxId = assertFaxId(parsed.value.faxId) const fileId = assertFaxId(parsed.value.fileId, 'fileId') await onetimefaxRequest( 'DELETE', `/api/v1/faxes/${encodeURIComponent(faxId)}/files/${encodeURIComponent(fileId)}`, ) return { removed: true as const, faxId, fileId } }