carbone-sdk
Advanced tools
Comparing version 1.4.0 to 1.5.0
{ | ||
"name": "carbone-sdk", | ||
"version": "1.4.0", | ||
"description": "Carbone API NodeJS SDK to generate reports easily (PDF, docx, xlsx, ods, odt, ...)", | ||
"version": "1.5.0", | ||
"description": "Carbone API NodeJS SDK to generate documents: PDF, DOCX, XLSX, PPTX, ODT, ODP, ODS, CSV, JPG, PNG, invoices, reports, financial, contracts, financial statements and more.", | ||
"main": "index.js", | ||
@@ -30,3 +30,3 @@ "directories": { | ||
"engines": { | ||
"node": ">=14.0.0" | ||
"node": ">=18.0.0" | ||
}, | ||
@@ -33,0 +33,0 @@ "author": "", |
@@ -40,3 +40,3 @@ const get = require('simple-get'); | ||
* Render a template | ||
* @param {String} pathOrId Template path or template ID | ||
* @param {String} template Template ID, or Path, or URL, or Buffer | ||
* @param {Object} data Data to send to carbone render | ||
@@ -46,3 +46,3 @@ * @param {Object} [options] optional object to overwrite global options: { "headers" : { "carbone-webhook-url" : "https://" } | ||
*/ | ||
render: function (pathOrId, data, options, callback) { | ||
render: function (template, data, options, callback) { | ||
if (options instanceof Function) { | ||
@@ -54,20 +54,34 @@ callback = options; | ||
let stream = StreamAnswer(); | ||
utils.getTemplateAsBuffer(template, options, function(err, template) { | ||
if (err) { | ||
return utils.returnStreamOrCallbackError(err, stream, callback); | ||
} | ||
if (Buffer.isBuffer(template) === true && options?.headers?.['carbone-template-delete-after'] + '' === '0') { | ||
data.template = utils.bufferToBase64(template); | ||
renderFunctions._renderWithTemplateId('template', null, data, stream, callback, options); | ||
} else if (Buffer.isBuffer(template) === true) { | ||
_templatesFunction.addTemplate(template, data.payload, (err, templateID) => { | ||
if (err) { | ||
return utils.returnStreamOrCallbackError(err, stream, callback); | ||
} | ||
renderFunctions._renderWithTemplateId(templateID, null, data, stream, callback, options); | ||
}); | ||
} else if (typeof template === 'string' && utils.checkPathIsAbsolute(template)) { | ||
renderFunctions._calculateHash(template, data.payload, (err, hash) => { | ||
if (err) { | ||
return utils.returnStreamOrCallbackError(err, stream, callback); | ||
} | ||
if (utils.checkPathIsAbsolute(pathOrId)) { | ||
renderFunctions._calculateHash(pathOrId, data.payload, (err, hash) => { | ||
if (err) { | ||
return utils.returnStreamOrCallbackError(err, stream, callback); | ||
} | ||
renderFunctions._renderWithTemplateId(hash, template, data, stream, callback, options); | ||
}); | ||
} else if (typeof template === 'string' && template.length === 64) { | ||
renderFunctions._renderWithTemplateId(template, null, data, stream, callback, options); | ||
} else { | ||
return utils.returnStreamOrCallbackError(new Error('The template must be: a template ID, or template URL, or template absolute path, or a template as Buffer'), stream, callback); | ||
} | ||
renderFunctions._renderWithTemplateId(hash, pathOrId, data, stream, callback, options); | ||
}); | ||
} else if (pathOrId.length === 64) { | ||
renderFunctions._renderWithTemplateId(pathOrId, null, data, stream, callback, options); | ||
} else { | ||
return utils.returnStreamOrCallbackError(new Error('The path must be an absolute path'), stream, callback); | ||
} | ||
}) | ||
return stream; | ||
}, | ||
/** | ||
@@ -74,0 +88,0 @@ * Render a template with the template ID |
@@ -14,3 +14,3 @@ const get = require('simple-get'); | ||
* Upload user template | ||
* @param {String} localPath User filepath | ||
* @param {String} template User filepath | ||
* @param {String} payload User payload | ||
@@ -20,3 +20,3 @@ * @param {Function} callback | ||
*/ | ||
addTemplate: function (localPath, payload, callback, _retry = false) { | ||
addTemplate: function (template, payload, callback, _retry = false) { | ||
if (callback === undefined) { | ||
@@ -31,29 +31,37 @@ callback = payload; | ||
if (!utils.checkPathIsAbsolute(localPath)) { | ||
return callback(new Error('The path must be an absolute path')); | ||
} | ||
utils.getTemplateAsBuffer(template, null, (err, template) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (Buffer.isBuffer(template) === false && typeof template === 'string' && !utils.checkPathIsAbsolute(template)) { | ||
return callback(new Error('The template must be either: An absolute path, URL or a Buffer')); | ||
} | ||
const form = new FormData() | ||
form.append('payload', payload) | ||
form.append('template', fs.createReadStream(localPath)) | ||
const _readStream = Buffer.isBuffer(template) === true ? utils.bufferToReadStream(template) : fs.createReadStream(template); | ||
get.concat({ | ||
method: 'POST', | ||
url: `${config.carboneUrl}template`, | ||
body: form, | ||
headers: { | ||
authorization: `Bearer ${_apiKey}`, | ||
"content-type": form.getHeaders()['content-type'], | ||
'carbone-version': sdkConfig.getVersion(), | ||
...config.headers | ||
} | ||
}, (err, response, body) => { | ||
if (err) { | ||
if (err.code === 'ECONNRESET' && !_retry) { | ||
return this.addTemplate(localPath, payload, callback, true); | ||
const form = new FormData() | ||
form.append('payload', payload) | ||
form.append('template', _readStream) | ||
return get.concat({ | ||
method: 'POST', | ||
url: `${config.carboneUrl}template`, | ||
body: form, | ||
headers: { | ||
authorization: `Bearer ${_apiKey}`, | ||
"content-type": form.getHeaders()['content-type'], | ||
'carbone-version': sdkConfig.getVersion(), | ||
...config.headers | ||
} | ||
return callback(err); | ||
} | ||
return utils.parseResponse(response, body, 'templateId', true, callback); | ||
}); | ||
}, function (err, response, body) { | ||
if (err) { | ||
if (err.code === 'ECONNRESET' && !_retry) { | ||
return templateFunctions.addTemplate(template, payload, callback, true); | ||
} | ||
return callback(err); | ||
} | ||
return utils.parseResponse(response, body, 'templateId', true, callback); | ||
}) | ||
}) | ||
}, | ||
@@ -83,3 +91,3 @@ | ||
if (err.code === 'ECONNRESET' && !_retry) { | ||
return this.delTemplate(templateId, callback, true); | ||
return templateFunctions.delTemplate(templateId, callback, true); | ||
} | ||
@@ -142,3 +150,3 @@ return callback(err); | ||
} | ||
}; | ||
} | ||
@@ -145,0 +153,0 @@ module.exports = (apiKey) => { |
65
utils.js
const path = require('path'); | ||
const URL = require("url").URL; | ||
const get = require('simple-get'); | ||
const stream = require('stream'); | ||
const fs = require('fs'); | ||
module.exports = { | ||
const _utils = { | ||
/** | ||
@@ -77,5 +81,62 @@ * Return the absolute path of the file | ||
} | ||
return stream.emit('error', error); | ||
}, | ||
/** | ||
* Verify if an URL is valid. | ||
* @param {string} URL | ||
* @returns | ||
*/ | ||
validURL: (url) => { | ||
try { | ||
new URL(url); | ||
return true; | ||
} catch (err) { | ||
return false; | ||
} | ||
}, | ||
downloadFile: (url, callback) => { | ||
if (_utils.validURL(url) === false) { | ||
return callback(new Error('The template URL is not valid')); | ||
} | ||
return get.concat({ | ||
url: url, | ||
method: 'GET', | ||
timeout : 10000 | ||
}, function (err, res, buffer) { | ||
if (err) { | ||
return callback(err); | ||
} else if (res.statusCode < 200 || res.statusCode >= 400) { | ||
return callback(new Error(`Downloading a template URL returned a ${res.statusCode} status code`)); | ||
} | ||
return callback(null, buffer); | ||
}) | ||
}, | ||
bufferToBase64: function (buffer) { | ||
return Buffer.from(buffer).toString('base64') | ||
}, | ||
bufferToReadStream: function (buffer) { | ||
return new stream.Readable({ | ||
read() { | ||
this.push(buffer); | ||
this.push(null); | ||
} | ||
}); | ||
}, | ||
getTemplateAsBuffer: function (template, options, callback) { | ||
if (typeof template === 'string' && (template?.startsWith('https://') || template?.startsWith('http://'))) { | ||
return _utils.downloadFile(template, callback); | ||
} | ||
if (typeof template === 'string' && _utils.checkPathIsAbsolute(template) === true && | ||
options && options?.headers && options?.headers?.['carbone-template-delete-after'] + '' === '0') { | ||
try { | ||
template = fs.readFileSync(template); | ||
} catch(err) { | ||
return callback(err); | ||
} | ||
return callback(null, template); | ||
} | ||
return callback(null, template); | ||
} | ||
} | ||
module.exports = _utils; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42018
652
3