@adobe/pdftools-extract-node-sdk
Version:
The Document Services PDF Tools Extract Node.js SDK provides APIs for extracting elements and renditions from PDF
48 lines (43 loc) • 1.46 kB
JavaScript
/*
* Copyright 2019 Adobe
* All Rights Reserved.
*
* NOTICE: Adobe permits you to use, modify, and distribute this file in
* accordance with the terms of the Adobe license agreement accompanying
* it. If you have received this file from a source other than Adobe,
* then your use, modification, or distribution of it requires the prior
* written permission of Adobe.
*/
/* eslint no-param-reassign:0 */
function handleJsonHttp(http) {
const newHttp = Object.create(http);
newHttp.call = function (options, content, multiPartData) {
let self = this,
type = options && self.getHeader(options.headers, 'Content-Type'),
promise1,
promise2;
// Convert to a string if needed.
if (typeof content !== 'string' && type && /json;|json$/.test(type)) {
content = JSON.stringify(content);
}
const handleResult = (result, contentType) => {
// Convert from a string if needed.
if (typeof result.content === 'string' && contentType && /json;|json$/.test(contentType) && result.status !== 204) {
result.content = JSON.parse(result.content);
}
};
promise1 = http.call(options, content, multiPartData);
promise2 = new Promise((resolve, reject) => {
promise1.then(result => {
handleResult(result, self.getHeader(result.headers, 'Content-Type'));
resolve(result);
}, err => reject(err));
});
promise2.request = promise1.request;
return promise2;
};
return newHttp;
}
module.exports = {
handleJsonHttp
};