forked from fpapado/micro-anki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (24 loc) · 832 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const {send, createError, json} = require('micro');
const AnkiExport = require('anki-apkg-export').default;
const microCors = require('micro-cors');
const cors = microCors({allowMethods: ['POST']});
async function saveAnki(deckName, cards) {
const apkg = new AnkiExport(deckName);
cards.forEach(card => apkg.addCard(card.front, card.back));
const zip = await apkg.save();
return zip;
}
async function handleRequest(req, res) {
const data = await json(req);
if (!data.cards) {
throw createError(400, 'No cards field found');
}
const deckName = data.deckName ? data.deckName : 'micro-anki-deck';
const zip = await saveAnki(deckName, data.cards);
res.setHeader(
'Content-Disposition',
`attachment; filename="${deckName}.apkg"`
);
send(res, 200, zip);
}
module.exports = cors(handleRequest);