Comparing version 1.0.0 to 1.1.0
34
index.js
@@ -375,7 +375,24 @@ 'use strict'; | ||
toObject(Constr, data) { | ||
return this.toFunction((obj) => { | ||
if (obj === null) { | ||
return obj; | ||
} | ||
return new Constr(obj); | ||
}, data); | ||
} | ||
toObjectArray(Constr, data) { | ||
return this.toFunctionArray(obj => new Constr(obj), data); | ||
} | ||
toObjectMap(Constr, data) { | ||
return this.toFunctionMap(obj => new Constr(obj), data); | ||
} | ||
toFunction(fn, data) { | ||
return this.send(data) | ||
.then(res => new Constr(JSON.parse(res.data))) | ||
.then(res => fn(JSON.parse(res.data))) | ||
.catch((err) => { | ||
if (err.statusCode === 404) { | ||
return Promise.resolve(null); | ||
return Promise.resolve(fn(null)); | ||
} | ||
@@ -386,3 +403,3 @@ return Promise.reject(err); | ||
toObjectArray(Constr, data) { | ||
toFunctionArray(fn, data) { | ||
return this.send(data) | ||
@@ -395,3 +412,3 @@ .then((res) => { | ||
for (i = 0; i < json.length; i += 1) { | ||
arr.push(new Constr(json[i])); | ||
arr.push(fn(json[i])); | ||
} | ||
@@ -403,3 +420,3 @@ | ||
toObjectMap(Constr, data) { | ||
toFunctionMap(fn, data) { | ||
return this.send(data) | ||
@@ -412,3 +429,3 @@ .then((res) => { | ||
for (key in json) { | ||
map[key] = new Constr(json[key]); | ||
map[key] = fn(json[key]); | ||
} | ||
@@ -523,2 +540,7 @@ | ||
proxyOptions.headers = options.headers; | ||
if (proxyOptions.auth) { | ||
proxyOptions.headers['Proxy-Authorization'] = `Basic ${Buffer.from(proxyOptions.auth).toString('base64')}`; | ||
proxyOptions.auth = null; | ||
} | ||
proxyOptions.headers.Host = options.host; | ||
@@ -525,0 +547,0 @@ proxyOptions.headers.path = urlStr; |
{ | ||
"name": "oohttp", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "object-oriented http(s) request handler", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -126,2 +126,23 @@ # Description | ||
## Passing results through a function | ||
```javascript | ||
const oohttp = require('oohttp'); | ||
class SomeClass { | ||
constructor(obj) { | ||
if(obj) { | ||
Object.assign(this, obj); | ||
} | ||
} | ||
} | ||
oohttp.Request.get('http://someurl/api/objects') | ||
.toFunctionArray((obj) => { | ||
return new SomeClass(obj); | ||
}) | ||
.then((someObjArray) => { | ||
console.log(someObjArray); | ||
}); | ||
``` | ||
# Defaults | ||
@@ -128,0 +149,0 @@ All requests inherit these default properties at the time the request is done. |
@@ -81,2 +81,8 @@ 'use strict'; | ||
res.setHeader('proxy-test', 'true'); | ||
const authHeader = req.headers['proxy-authorization']; | ||
if (authHeader) { | ||
res.setHeader('proxy-auth', authHeader); | ||
} | ||
proxyServer.web(req, res, { | ||
@@ -374,3 +380,3 @@ target: req.headers.path | ||
describe('proxy', function () { | ||
describe('req.proxyUrl', function() { | ||
describe('req.proxyUrl', function () { | ||
let res; | ||
@@ -388,3 +394,3 @@ before(async function () { | ||
describe('req.proxy()', function() { | ||
describe('req.proxy()', function () { | ||
let req; | ||
@@ -395,8 +401,8 @@ before(function () { | ||
it('should chain', function() { | ||
assert.equal(req.proxy('http://localhost:9802'), req); | ||
it('should chain', function () { | ||
assert.equal(req.proxy('http://testusr:testpwd@localhost:9802'), req); | ||
}); | ||
it('should set proxyUrl', function() { | ||
assert.equal(req.proxyUrl, 'http://localhost:9802'); | ||
it('should set proxyUrl', function () { | ||
assert.equal(req.proxyUrl, 'http://testusr:testpwd@localhost:9802'); | ||
}); | ||
@@ -408,4 +414,9 @@ | ||
}); | ||
it('should have proxy-auth header', async function () { | ||
const res = await req.send(); | ||
assert.equal(res.headers['proxy-auth'], 'Basic dGVzdHVzcjp0ZXN0cHdk'); | ||
}); | ||
}); | ||
}); | ||
}); |
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
45272
9
1234
163