Skip to content

Commit

Permalink
Fix response format from /import
Browse files Browse the repository at this point in the history
The result should be API JSON, not translator JSON.

If we want to use translator tests, we'll need to convert the test JSON
to API JSON for the comparison.

Follow-up to zotero#89
  • Loading branch information
dstillman committed Mar 10, 2019
1 parent 72d2312 commit d7e575b
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions src/importEndpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,17 @@ class ImportEndpoint {
return;
}
translate.setTranslator(translators[0]);
await translate.translate({ libraryID: 1 });

var items = await translate.translate({ libraryID: 1 });
ctx.set('Content-Type', 'application/json');
for (const item of translate._itemSaver.items) {
this.normalizeItem(item);
}

ctx.response.body = JSON.stringify(translate._itemSaver.items, null, 2);
}

normalizeItem(item) {
delete item.id;
delete item.itemID;

this.normalizeTags(item);

for (const note of (item.notes || [])) {
this.normalizeTags(note);
}
}

normalizeTags(item) {
if (item.tags) item.tags = item.tags.map(tag => (typeof tag === 'string') ? { tag } : tag);
if (item.tags && item.tags.length === 0) delete item.tags;
if (item.tags) item.tags.sort((a, b) => tagValue(a).localeCompare(tagValue(b)));

// Convert translator JSON to API JSON
var newItems = [];
items.forEach(item => {
newItems.push(...Zotero.Utilities.itemToAPIJSON(item));
});

ctx.response.body = JSON.stringify(newItems, null, 2);
}
}

Expand Down

0 comments on commit d7e575b

Please sign in to comment.