Far scegliere un template agli utenti
Mostra i template di OpenTemplates nella tua app e genera quello che sceglie un utente.
Quando la tua app offre più documenti, leggi l'elenco dei template da OpenTemplates invece di scrivere gli ID nel tuo codice. I nuovi template compaiono così nella tua app senza nessuna modifica.
1. Dai al token della tua app Allow read list of profiles. Leggi Token API.
2. Leggi l'elenco e tieni il nome e l'ID di ogni template. Il codice gira dentro una funzione async:
const URL = 'https://templates.example.com';
const headers = { 'x-token': process.env.OPENTEMPLATES_TOKEN };
const profiles = await (await fetch(URL + '/profiles/', { headers })).json();
const choices = [];
for (const profile of profiles)
for (const template of profile.templates)
choices.push({ label: profile.name + ' / ' + template.name, id: template.reference, model: template.model });Con i dati della demo questo dà:
Demo project / Invoice -> demo/invoice
Demo project / Welcome letter -> demo/welcome
Demo project / Receipt -> demo/receipt
Demo project / Credit note -> demo/creditnote
Letters / Welcome letter -> letters/welcome3. Mostra label all'utente. Quando l'utente ne sceglie uno, generalo con il suo id:
const html = await (await fetch(URL + '/', {
method: 'POST',
headers: { ...headers, 'content-type': 'application/json' },
body: JSON.stringify({ id: choice.id, data: yourData })
})).text();model contiene il modello di esempio del template. Usalo per vedere quali campi si aspetta il template, o per compilare un'anteprima.
Leggi Elencare profili e template.