Commit f1a6a18c authored by Jake Oliversen's avatar Jake Oliversen

Merge branch 'issue/2-promisifying-async-functions' of...

Merge branch 'issue/2-promisifying-async-functions' of gitlab.entropy.cc:blacklight/npm-sling-connector into issue/2-promisifying-async-functions
parents a5701c97 8c167658
......@@ -15,7 +15,6 @@ const DefaultKeepAliveConfig = {
freeSocketTimeout: 30000, // free socket keepalive for 30 seconds
};
var debug=dbg("SLING:debug");
var warn=dbg("SLING:warn")
var errorLog=dbg("SLING:error")
......@@ -152,10 +151,14 @@ SC.get = function(path, depth, callback, options){
//depth selector and JSON ext
path = `${path}${depth?("." + depth):""}.json`;
if(!callback || typeof callback !== "function"){
return new Promise((resolve, reject)=>{
sc_self.getSling(path, options, (err, data)=>{
err ? reject(new Error(err)) : resolve(data)
});
})
}
sc_self.getSling(path,options,callback);
};
......@@ -173,7 +176,14 @@ SC.get = function(path, depth, callback, options){
* @param {SlingConnector.slingConnectorCallback} callback - to call upon completion
*/
SC.getRaw = function(path, callback){
this.getSling(path, {dontParseBody:true, dontUseGetIfExists: true} ,callback);
this.getSling(path, {dontParseBody:true} ,callback);
if(!callback || typeof(callback) !== "function"){
return new Promise((resolve, reject)=>{
this.getSling(path, {dontParseBody:true}, (err, data)=>{
err ? reject(new Error(err)) : resolve(data)
});
})
}
};
......@@ -190,8 +200,8 @@ SC.getSling = async function(path, options = {}, callback){
//setup initial params
var sc_self=this;
if(typeof options === "function"){
callback=options;
options={};
callback=options;
options={};
}
......@@ -325,7 +335,7 @@ SC.getSling = async function(path, options = {}, callback){
gzip: true
};
// console.log("SENDING REQUEST WITH:", reqOptions);
//console.log("SENDING REQUEST WITH:", reqOptions);
request(reqOptions,
......@@ -501,6 +511,15 @@ SC.getSling = async function(path, options = {}, callback){
path = "/" + path;
var sc_self=this;
if(!cb || typeof(cb) !== "function"){
return new Promise((resolve, reject)=>{
this.checkForBackingFile(path, preprocess, (err, data) =>{
err ? reject(new Error(err)) : resolve(data)
});
})
}
if(preprocess.fileMap){
cb(null, {path: preprocess.fileMap, useImmediately: true});
return;
......@@ -645,6 +664,16 @@ SC.getStream = function(path, method, headers){
* @param {function} callback - to call upon completion (err, httpStatusCode, httpResponse, body)
*/
SC.import = function(path, data, options, callback){
if(!callback || typeof(callback) !== "function"){
return new Promise((resolve, reject)=>{
this.import(path, data, options, (err, data) =>{
err ? reject(new Error(err)) : resolve(data)
});
})
}
if(typeof options === "function"){
callback=options;
options={}
......@@ -658,7 +687,6 @@ SC.import = function(path, data, options, callback){
form = _.merge(form, options.extraParams)
}
// if(!options.protectNodes)
// form[':replace']="true"; // This seems to make sling fail when replacing jcr:content.
......@@ -675,7 +703,11 @@ SC.import = function(path, data, options, callback){
//debug("Import form: ", form);
const postUrl = this.buildUri(path);
request.post({
<<<<<<< HEAD
url: postUrl,
=======
url: this.buildUri(path),
>>>>>>> 8c1676586858c555754f7327c3498d5365235d8f
formData: form, // Multi-part mode not necessary, but more efficient for large files than urlencoded
timeout: this.timeout,
followRedirect: false,
......@@ -716,10 +748,23 @@ SC.post = function(path,params,cb){
if(!this.isValidPath(path)){
cb("invalid path provided");
return;
<<<<<<< HEAD
=======
}
if(!cb || typeof(cb) !== "function"){
return new Promise((resolve, reject)=>{
this.post(path, params, (err, data) =>{
err ? reject(new Error(err)) : resolve(data)
});
})
>>>>>>> 8c1676586858c555754f7327c3498d5365235d8f
}
var sc_self=this;
params=params||{};
params._charset_ = params._charset_ || "utf-8";
<<<<<<< HEAD
const postUrl = this.buildUri(path);
var settings={
url: postUrl,
......@@ -730,6 +775,16 @@ SC.post = function(path,params,cb){
agent: sc_self.getRequestAgent(postUrl),
gzip: true
};
=======
var settings={
url: this.buildUri(path),
timeout: this.timeout,
followRedirect: false,
auth: this.makeAuth(),
headers: this.authCookieHeader()
};
>>>>>>> 8c1676586858c555754f7327c3498d5365235d8f
if(params.__url_encode){
delete params.__url_encode; settings.form=params;}
......
{
"res": {
"statusCode": 200,
"headers": "some headers"
},
"data": {
"something": "here",
"jcr:something": "also here",
"another": {
"deeper": "one"
}
}
}
\ No newline at end of file
const SlingConnector = require('../sling-connector');
const sc = new SlingConnector({baseUri: "../__fixtures__"});
test('Call get with callback', done => {
sc.get('test1' , (err, data) => {
expect(data['something']).toBe('here');
done();
})
})
test('Call get without callback', async done => {
try{
let data = await sc.get('test1');
expect(data['something']).toBe("here");
}catch(err){
expect(err).toBeDefined();
}finally{
done();
}
})
\ No newline at end of file
const request = require("request");
const SlingConnector = require('../sling-connector');
const _ = require("lodash");
const sc = new SlingConnector({baseUri: "../__fixtures__"});
beforeEach(() => {
request.mockClear();
});
describe("get-if-exists", ()=> {
test("not applied to standard JSON call", (done) => {
sc.getRaw("/content/raw/simple.1.json", (err, data) => {
expect(err).toBeFalsy();
expect(data).toBeTruthy();
expect(typeof data).toBe('string');
done();
});
});
});
\ No newline at end of file
const SlingConnector = require('../sling-connector');
const sc = new SlingConnector({baseUri: "../__fixtures__"});
test('Call getRaw with callback', done => {
sc.getRaw('test1' , (err, data) => {
expect(data).toBe("{\"something\":\"here\",\"_jcr_something\":\"also here\",\"another\":{\"deeper\":\"one\"}}");
done();
})
})
test('Call get without callback', async done => {
try{
let data = await sc.getRaw('test1');
expect(data).toBe("{\"something\":\"here\",\"_jcr_something\":\"also here\",\"another\":{\"deeper\":\"one\"}}");
}catch(err){
expect(err).toBeDefined();
}finally{
done();
}
})
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment