Skip to content

Commit

Permalink
Add dynamic variables conversion ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
somaromero committed Sep 13, 2024
1 parent e53a814 commit f6f3949
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 31 deletions.
62 changes: 34 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"app": "src/app.js",
"api": "src/api.js"
},
"host": "^10.13.2"
"host": "^11.1.0"
},
"scripts": {
"build": "directus-extension build",
Expand Down
30 changes: 28 additions & 2 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ export default {
pdfMake.vfs = await getBase64Fonts(fonts, assetsService);
pdfMake.fonts = getPdfMakeFonts(fonts);

template['images'] = await addImages(images, assetsService, filesService);
const pdfDoc = convertStringToStructure(template);
pdfDoc['images'] = await addImages(images, assetsService, filesService);

const pdfDocGenerator = pdfMake.createPdf(template);
const pdfDocGenerator = pdfMake.createPdf(pdfDoc);

const buffer = await new Promise((resolve, reject) => {
pdfDocGenerator.getBuffer((buffer) => {
Expand Down Expand Up @@ -233,3 +234,28 @@ async function addImages(images, assetsService, filesService) {

return imageList;
}

function convertStringToStructure(template) {
const regexArray = /^\[(.*?)]$/;
const regexObject = /^\{(.*?)}$/;

if (typeof template === 'string' && regexArray.test(template)) {
return JSON.parse(template);
}

if (typeof template === 'string' && regexObject.test(template)) {
return JSON.parse(template);
}

if (Array.isArray(template)) {
return template.map(item => convertStringToStructure(item));
}

if (typeof template === 'object' && template !== null) {
for (let key in template) {
template[key] = convertStringToStructure(template[key]);
}
}

return template;
}

0 comments on commit f6f3949

Please sign in to comment.