From 643caf0da12d4ffb39989a39931cf0769f1246a0 Mon Sep 17 00:00:00 2001 From: Eloy Robillard Date: Tue, 16 Apr 2024 17:05:06 +0200 Subject: Revert to using canAddNotes (#827) Fix #818 --- ext/js/comm/anki-connect.js | 55 +++++---------------------------------------- 1 file changed, 5 insertions(+), 50 deletions(-) (limited to 'ext/js/comm') diff --git a/ext/js/comm/anki-connect.js b/ext/js/comm/anki-connect.js index 72ad58a5..6a008f40 100644 --- a/ext/js/comm/anki-connect.js +++ b/ext/js/comm/anki-connect.js @@ -128,15 +128,16 @@ export class AnkiConnect { return result; } + /** * @param {import('anki').Note[]} notes - * @returns {Promise<({ canAdd: true } | { canAdd: false, error: string })[]>} + * @returns {Promise} */ - async canAddNotesWithErrorDetail(notes) { + async canAddNotes(notes) { if (!this._enabled) { return []; } await this._checkVersion(); - const result = await this._invoke('canAddNotesWithErrorDetail', {notes}); - return this._normalizeCanAddNotesWithErrorDetailArray(result, notes.length); + const result = await this._invoke('canAddNotes', {notes}); + return this._normalizeArray(result, notes.length, 'boolean'); } /** @@ -579,52 +580,6 @@ export class AnkiConnect { return /** @type {T[]} */ (result); } - /** - * @param {unknown} result - * @param {number} expectedCount - * @returns {import('anki-connect.js').CanAddResult[]} - * @throws {Error} - */ - _normalizeCanAddNotesWithErrorDetailArray(result, expectedCount) { - if (!Array.isArray(result)) { - throw this._createUnexpectedResultError('array', result, ''); - } - if (expectedCount !== result.length) { - throw this._createError(`Unexpected result array size: expected ${expectedCount}, received ${result.length}`, result); - } - /** @type {import('anki-connect.js').CanAddResult[]} */ - const result2 = []; - for (let i = 0; i < expectedCount; ++i) { - const item = /** @type {unknown} */ (result[i]); - if (item === null || typeof item !== 'object') { - throw this._createError(`Unexpected result type at index ${i}: expected object, received ${this._getTypeName(item)}`, result); - } - - const {canAdd, error} = /** @type {{[key: string]: unknown}} */ (item); - if (typeof canAdd !== 'boolean') { - throw this._createError(`Unexpected result type at index ${i}, field canAdd: expected boolean, received ${this._getTypeName(canAdd)}`, result); - } - - if (canAdd && typeof error !== 'undefined') { - throw this._createError(`Unexpected result type at index ${i}, field error: expected undefined, received ${this._getTypeName(error)}`, result); - } - if (!canAdd && typeof error !== 'string') { - throw this._createError(`Unexpected result type at index ${i}, field error: expected string, received ${this._getTypeName(error)}`, result); - } - - if (canAdd) { - result2.push({canAdd}); - } else if (typeof error === 'string') { - const item2 = { - canAdd, - error - }; - result2.push(item2); - } - } - return result2; - } - /** * @param {unknown} result * @returns {(?import('anki').NoteInfo)[]} -- cgit v1.2.3