carbone-sdk
Advanced tools
Comparing version 1.3.1 to 1.4.0
{ | ||
"name": "carbone-sdk", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "Carbone API NodeJS SDK to generate reports easily (PDF, docx, xlsx, ods, odt, ...)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -61,6 +61,7 @@ const templates = require('./templates'); | ||
* @param {Object} data Data to send to carbone render | ||
* @param {Object} [options] optional object to overwrite global options: { "headers" : { "carbone-webhook-url" : "https://" } | ||
*/ | ||
renderPromise: function (pathOrId, data) { | ||
renderPromise: function (pathOrId, data, options) { | ||
return new Promise((resolve, reject) => { | ||
renderFunction.render(pathOrId, data, (err, content, filename) => { | ||
renderFunction.render(pathOrId, data, options, (err, content, filename) => { | ||
if (err) { | ||
@@ -67,0 +68,0 @@ return reject(err); |
@@ -30,3 +30,3 @@ # Carbone Cloud API Node SDK | ||
const options = { | ||
const body = { | ||
data: { | ||
@@ -44,3 +44,3 @@ /** YOUR DATA HERE **/ | ||
/** Generate the document **/ | ||
carboneSDK.render(templateAbsolutePath, options, (err, buffer, filename) => { | ||
carboneSDK.render(templateAbsolutePath, body, (err, buffer, filename) => { | ||
/** | ||
@@ -68,15 +68,17 @@ * ✅ Document generated, returned values: | ||
### Pass headers | ||
### Update default options / headers | ||
Initialise a global header that will be set for each API request. | ||
```js | ||
sdk.setOptions({ | ||
carboneSDK.setOptions({ | ||
// Edit headers for all requests (default) | ||
headers: { | ||
'carbone-template-delete-after': 86400 | ||
'carbone-webhook-url': 'https://...' | ||
} | ||
}, | ||
// Edit the default Carbone URL (https://api.carbone.io/) for On-Premise | ||
// WARNING: Add a trailing slash to the end of your URL | ||
carboneUrl: 'https://your-on-premise-carbone-url:4000/' | ||
}) | ||
``` | ||
### Add a template | ||
@@ -150,3 +152,3 @@ | ||
const options = { | ||
const body = { | ||
data: { /** YOUR DATA HERE **/ }, | ||
@@ -157,3 +159,3 @@ convertTo: "pdf" | ||
carboneSDK.render('templateId', options, (err, buffer, filename) => { | ||
carboneSDK.render('templateId', body, (err, buffer, filename) => { | ||
@@ -163,3 +165,3 @@ }) | ||
Or if you don't want the buffer but juste the link to download it later, you can set the options `isReturningBuffer: false` to the SDK. | ||
Or if you don't want the buffer but just the link to download it later, you can set the options `isReturningBuffer: false` to the SDK. | ||
@@ -169,3 +171,3 @@ ```js | ||
const options = { | ||
const body = { | ||
data: { /** YOUR DATA HERE **/ }, | ||
@@ -180,3 +182,3 @@ convertTo: "pdf" | ||
carboneSDK.render('templateId', options, (err, downloadLink, filename) => { | ||
carboneSDK.render('templateId', body, (err, downloadLink, filename) => { | ||
@@ -194,3 +196,3 @@ }) | ||
const options = { | ||
const body = { | ||
data: { | ||
@@ -205,3 +207,3 @@ /** YOUR DATA HERE **/ | ||
carboneSDK.render('/absolute/path/to/your/template', options, (err, buffer, filename) => { | ||
carboneSDK.render('/absolute/path/to/your/template', body, (err, buffer, filename) => { | ||
@@ -215,3 +217,3 @@ }) | ||
const options = { | ||
const body = { | ||
data: { /** YOUR DATA HERE **/ }, | ||
@@ -223,3 +225,3 @@ convertTo: "pdf" | ||
const writeStream = fs.createWriteStream('result.pdf') | ||
const sdkStream = carboneSDK.render('/absolute/path/to/your/template', options) | ||
const sdkStream = carboneSDK.render('/absolute/path/to/your/template', body) | ||
@@ -238,2 +240,19 @@ sdkStream.on('error', (err) => { | ||
You can also overwrite headers with an optional object. Here is an example to use Carbone webhooks: | ||
```js | ||
const options = { | ||
headers = { | ||
'carbone-webhook-url': 'https://...' | ||
} | ||
} | ||
carboneSDK.render('templateId', body, options, (err, buffer, filename) => { | ||
}) | ||
``` | ||
## API Promise | ||
@@ -290,3 +309,3 @@ | ||
const options = { | ||
const body = { | ||
data: { /** YOUR DATA HERE **/ }, | ||
@@ -297,3 +316,10 @@ convertTo: "pdf" | ||
carboneSDK.renderPromise('/absolute/path/to/your/template', options) | ||
// You can also overwrite headers with an optional object. Here is an example to use Carbone webhooks: | ||
const options = { | ||
headers : { | ||
'carbone-webhook-url': 'https://...' // if you | ||
} | ||
} | ||
carboneSDK.renderPromise('/absolute/path/to/your/template', body [, options]) | ||
.then(result => { | ||
@@ -300,0 +326,0 @@ // result.content contains the rendered file |
@@ -42,5 +42,10 @@ const get = require('simple-get'); | ||
* @param {Object} data Data to send to carbone render | ||
* @param {Object} [options] optional object to overwrite global options: { "headers" : { "carbone-webhook-url" : "https://" } | ||
* @param {Function} callback | ||
*/ | ||
render: function (pathOrId, data, callback) { | ||
render: function (pathOrId, data, options, callback) { | ||
if (options instanceof Function) { | ||
callback = options; | ||
options = {}; | ||
} | ||
// Create stream if no callback is passed in parameter | ||
@@ -55,6 +60,6 @@ let stream = StreamAnswer(); | ||
renderFunctions._renderWithTemplateId(hash, pathOrId, data, stream, callback); | ||
renderFunctions._renderWithTemplateId(hash, pathOrId, data, stream, callback, options); | ||
}); | ||
} else if (pathOrId.length === 64) { | ||
renderFunctions._renderWithTemplateId(pathOrId, null, data, stream, callback); | ||
renderFunctions._renderWithTemplateId(pathOrId, null, data, stream, callback, options); | ||
} else { | ||
@@ -73,5 +78,6 @@ return utils.returnStreamOrCallbackError(new Error('The path must be an absolute path'), stream, callback); | ||
* @param {Function} callback | ||
* @param {Object} options overwrite global options. Example : { headers : { carbone-webhook-url : 'https://' } | ||
* @param {Boolean} _retry Check if the request has already been retried | ||
*/ | ||
_renderWithTemplateId: function (templateId, filePath, data, stream, callback, _retries = 0) { | ||
_renderWithTemplateId: function (templateId, filePath, data, stream, callback, options = {}, _retries = 0) { | ||
get.concat({ | ||
@@ -84,3 +90,4 @@ method: 'POST', | ||
'content-type': 'application/json', | ||
...config.headers | ||
...config.headers, | ||
...options.headers | ||
}, | ||
@@ -95,3 +102,3 @@ json: false, // if true, simple-get tries to Parse the response | ||
return setTimeout(() => { | ||
renderFunctions._renderWithTemplateId(templateId, filePath, data, stream, callback, _retries + 1); | ||
renderFunctions._renderWithTemplateId(templateId, filePath, data, stream, callback, options, _retries + 1); | ||
}, config.retriesIntervalOnError); | ||
@@ -110,3 +117,3 @@ } | ||
renderFunctions._renderWithTemplateId(newTemplateId, filePath, data, stream, callback, _retries); | ||
renderFunctions._renderWithTemplateId(newTemplateId, filePath, data, stream, callback, options, _retries); | ||
}); | ||
@@ -113,0 +120,0 @@ } |
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
38707
571
318