Skip to content
← Public packages

@kentcdodds/audible

Personal Audible library app — browser re-auth, library, wishlist, NAS archive stub.

src/import-already-exists.ts

37 lines · 1.5 KB · TypeScript
/**
 * Detect home MCP audiobook_import_aaxc "already on disk" failures.
 * Same friendly skip as the archive-to-nas pre-check On NAS path.
 */

function importErrorCode(importResult: unknown): string | undefined {
	if (!importResult || typeof importResult !== 'object' || Array.isArray(importResult)) {
		return undefined
	}
	const record = importResult as Record<string, unknown>
	const nested = record.error
	if (nested && typeof nested === 'object' && !Array.isArray(nested)) {
		const code = (nested as Record<string, unknown>).code
		if (typeof code === 'string') return code
	}
	return typeof record.code === 'string' ? record.code : undefined
}

export function importErrorFilename(importResult: unknown): string | undefined {
	if (!importResult || typeof importResult !== 'object' || Array.isArray(importResult)) {
		return undefined
	}
	const record = importResult as Record<string, unknown>
	const nested = record.error
	if (nested && typeof nested === 'object' && !Array.isArray(nested)) {
		const filename = (nested as Record<string, unknown>).filename
		if (typeof filename === 'string' && filename.trim()) return filename.trim()
	}
	return typeof record.filename === 'string' && record.filename.trim()
		? record.filename.trim()
		: undefined
}

/** Home convert already-on-disk — same friendly skip as the pre-check On NAS path. */
export function isAudiobookAlreadyExistsImportResult(importResult: unknown): boolean {
	return importErrorCode(importResult) === 'audiobook_already_exists'
}