trooba-http-api
Advanced tools
Comparing version 1.0.1 to 2.0.0
25
index.js
@@ -10,15 +10,14 @@ 'use strict'; | ||
module.exports = function httpfy(transport, Ctor) { | ||
module.exports = function httpfy(pipe, Ctor) { | ||
// allow extending client API | ||
Ctor = Ctor || Client; | ||
transport.api = function api(pipe) { | ||
pipe.set('client:default', function api(pipe) { | ||
return new Ctor(pipe); | ||
}; | ||
return transport; | ||
}); | ||
}; | ||
function Client(pipe) { | ||
function Client(pipe, config) { | ||
this.pipe = pipe; | ||
this.config = config; | ||
} | ||
@@ -139,13 +138,11 @@ | ||
end: function end(callback) { | ||
var requestContext = {}; | ||
requestContext.request = this.request; | ||
requestContext.request.headers = requestContext.request.headers || {}; | ||
var request = this.request; | ||
request = request; | ||
request.headers = request.headers || {}; | ||
// stringify some headers | ||
requestContext.request.headers = | ||
Utils.stringifyHeaders(requestContext.request.headers); | ||
request.headers = | ||
Utils.stringifyHeaders(request.headers); | ||
return this.pipe(requestContext, function onResponseContext(responseContext) { | ||
callback(responseContext.error, responseContext.response); | ||
}); | ||
return this.pipe.create().request(request, callback); | ||
} | ||
@@ -152,0 +149,0 @@ }; |
{ | ||
"name": "trooba-http-api", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "generic API for trooba http and ajax transports", | ||
@@ -26,3 +26,3 @@ "main": "index.js", | ||
"mocha": "^3.1.2", | ||
"trooba": "^0" | ||
"trooba": "^1" | ||
}, | ||
@@ -29,0 +29,0 @@ "dependencies": { |
# trooba-http-api | ||
[![Build Status](https://travis-ci.org/trooba/trooba-http-api.svg?branch=master)](https://travis-ci.org/trooba/trooba-http-api) [![NPM](https://img.shields.io/npm/v/trooba-http-api.svg)](https://www.npmjs.com/package/trooba-http-api) | ||
[![Downloads](https://img.shields.io/npm/dm/trooba-http-api.svg)](http://npm-stat.com/charts.html?package=trooba-http-api) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/trooba/trooba/badge.svg)](https://snyk.io/test/github/trooba/trooba-http-api) | ||
Provides a generic transport API to http/ajax/xhr trooba transports. | ||
@@ -13,2 +17,3 @@ | ||
### Export via transport | ||
```js | ||
@@ -20,20 +25,33 @@ var Wreck = require('wreck'); | ||
module.exports = function httpTransportFactory(config) { | ||
var transport = function transport(requestContext, responseContext) { | ||
requestContext.options = _.merge(requestContext.options || {}, config); | ||
Wreck.request(requestContext.options, function onResponse(err, response) { | ||
responseContext.error = err; | ||
if (response) { | ||
responseContext.statusCode = response.statusCode; | ||
responseContext.response = response; | ||
module.exports = function transport(pipe, config) { | ||
pipe.on('request', function (request) { | ||
request = _.merge(request || {}, config); | ||
Wreck.request(request, function onResponse(err, response) { | ||
if (err) { | ||
pipe.throw(err); | ||
return; | ||
} | ||
responseContext.next(); | ||
pipe.respond(response); | ||
}); | ||
}; | ||
}); | ||
transport = httpfy(transport); | ||
return transport; | ||
// inject api | ||
pipe.set('client:http', httpfy); | ||
}; | ||
``` | ||
### Attach to any pipe | ||
```js | ||
var Trooba = require('trooba'); | ||
var httpfy = require('trooba-http-api'); | ||
var client = Trooba | ||
.use(function someHandler() {}) | ||
.use(function someHandler() {}) | ||
.use(function someHandler() {}) | ||
.use(httpfy) | ||
.build('client:default'); // since it injects default at ''client:default'' | ||
client.delete('path/to/resource').end(console.log); | ||
``` |
@@ -7,9 +7,8 @@ 'use strict'; | ||
var httpfy = require('..'); | ||
var Client = httpfy.Client; | ||
var Request = httpfy.Request; | ||
describe(__filename, function () { | ||
it('should instrument transport object', function () { | ||
var factory = httpfy({}); | ||
Assert.ok(factory.api); | ||
var client = factory.api(); | ||
it('should inject api', function () { | ||
var client = Trooba.use(httpfy).build('client:default'); | ||
Assert.ok(client.get); | ||
@@ -24,17 +23,12 @@ Assert.ok(client.post); | ||
it('should allow extended client contructor', function (done) { | ||
function CustomClient(pipe) { | ||
return pipe(function ctx(requestContext, next) { | ||
Assert.equal('quest', requestContext.req); | ||
next(); | ||
}); | ||
function CustomClient(pipe, config) { | ||
} | ||
var factory = httpfy({}, CustomClient); | ||
var client = factory.api(function pipe(callback) { | ||
callback({ | ||
req: 'quest' | ||
}, done); | ||
}); | ||
client.get(); | ||
CustomClient.prototype.get = function get() { | ||
done(); | ||
}; | ||
var client = Trooba.use(httpfy, CustomClient).build('client:default'); | ||
client.get({foo:'bar'}); | ||
}); | ||
@@ -44,10 +38,10 @@ | ||
function factory() { | ||
return httpfy(function (requestContext, reply) { | ||
function transport(pipe) { | ||
pipe.on('request', function (request) { | ||
Assert.deepEqual({ | ||
foo: 'bar', | ||
headers: {} | ||
}, requestContext.request); | ||
}, request); | ||
reply(null, { | ||
pipe.respond({ | ||
qaz: 'wer' | ||
@@ -58,5 +52,5 @@ }); | ||
var client = Trooba.transport(factory).create(); | ||
var client = Trooba.use(transport).use(httpfy).build('client:default'); | ||
var ctx = client.request({ | ||
client.request({ | ||
foo: 'bar' | ||
@@ -67,15 +61,13 @@ }).end(function (err, res) { | ||
}); | ||
Assert.ok(ctx.request); | ||
}); | ||
it('should allow request override', function (done) { | ||
function factory() { | ||
return httpfy(function (requestContext, reply) { | ||
function transport(pipe) { | ||
pipe.on('request', function (request) { | ||
Assert.deepEqual({ | ||
foo: 'bar', | ||
headers: {} | ||
}, requestContext.request); | ||
}, request); | ||
reply(null, { | ||
pipe.respond({ | ||
qaz: 'wer' | ||
@@ -86,3 +78,3 @@ }); | ||
var client = Trooba.transport(factory).create(); | ||
var client = Trooba.use(transport).use(httpfy).build('client:default'); | ||
@@ -110,4 +102,4 @@ function ExtRequest() { | ||
function factory() { | ||
return httpfy(function (requestContext, reply) { | ||
function transport(pipe) { | ||
pipe.on('request', function (request) { | ||
Assert.deepEqual({ | ||
@@ -117,5 +109,5 @@ method: 'GET', | ||
headers: {} | ||
}, requestContext.request); | ||
}, request); | ||
reply(null, { | ||
pipe.respond( { | ||
qaz: 'wer' | ||
@@ -126,3 +118,3 @@ }); | ||
var client = Trooba.transport(factory).create(); | ||
var client = Trooba.use(transport).use(httpfy).build('client:default'); | ||
@@ -138,4 +130,4 @@ client.get({ | ||
it('should do put', function (done) { | ||
function factory() { | ||
return httpfy(function (requestContext, reply) { | ||
function transport(pipe) { | ||
pipe.on('request', function (request) { | ||
Assert.deepEqual({ | ||
@@ -147,5 +139,5 @@ method: 'PUT', | ||
headers: {} | ||
}, requestContext.request); | ||
}, request); | ||
reply(null, { | ||
pipe.respond({ | ||
qaz: 'wer' | ||
@@ -156,3 +148,3 @@ }); | ||
var client = Trooba.transport(factory).create(); | ||
var client = Trooba.use(transport).use(httpfy).build('client:default'); | ||
@@ -168,4 +160,4 @@ client.put({ | ||
it('should do patch', function (done) { | ||
function factory() { | ||
return httpfy(function (requestContext, reply) { | ||
function transport(pipe) { | ||
pipe.on('request', function (request) { | ||
Assert.deepEqual({ | ||
@@ -177,5 +169,5 @@ method: 'PATCH', | ||
headers: {} | ||
}, requestContext.request); | ||
}, request); | ||
reply(null, { | ||
pipe.respond({ | ||
qaz: 'wer' | ||
@@ -186,3 +178,3 @@ }); | ||
var client = Trooba.transport(factory).create(); | ||
var client = Trooba.use(transport).use(httpfy).build('client:default'); | ||
@@ -198,4 +190,4 @@ client.patch({ | ||
it('should do delete', function (done) { | ||
function factory() { | ||
return httpfy(function (requestContext, reply) { | ||
function transport(pipe) { | ||
pipe.on('request', function (request) { | ||
Assert.deepEqual({ | ||
@@ -205,11 +197,12 @@ method: 'DELETE', | ||
headers: {} | ||
}, requestContext.request); | ||
}, request); | ||
reply(null, { | ||
pipe.respond({ | ||
qaz: 'wer' | ||
}); | ||
}); | ||
} | ||
var client = Trooba.transport(factory).create(); | ||
var client = Trooba.use(transport).use(httpfy).build('client:default'); | ||
@@ -223,4 +216,4 @@ client.delete('/path/to/resource').end(function (err, res) { | ||
it('should do post, headers, path, mixing', function (done) { | ||
function factory() { | ||
return httpfy(function (requestContext, reply) { | ||
function transport(pipe) { | ||
pipe.on('request', function (request) { | ||
Assert.deepEqual({ | ||
@@ -239,11 +232,12 @@ method: 'POST', | ||
} | ||
}, requestContext.request); | ||
}, request); | ||
reply(null, { | ||
pipe.respond({ | ||
qaz: 'wer' | ||
}); | ||
}); | ||
} | ||
var client = Trooba.transport(factory).create(); | ||
var client = Trooba.use(transport).use(httpfy).build('client:default'); | ||
@@ -270,4 +264,4 @@ client.post({ | ||
it('should do get, query object, headers, path, mixing', function (done) { | ||
function factory() { | ||
return httpfy(function (requestContext, reply) { | ||
function transport(pipe) { | ||
pipe.on('request', function (request) { | ||
Assert.deepEqual({ | ||
@@ -284,11 +278,12 @@ method: 'GET', | ||
} | ||
}, requestContext.request); | ||
}, request); | ||
reply(null, { | ||
pipe.respond({ | ||
qaz: 'wer' | ||
}); | ||
}); | ||
} | ||
var client = Trooba.transport(factory).create(); | ||
var client = Trooba.use(transport).use(httpfy).build('client:default'); | ||
@@ -315,4 +310,4 @@ client.get({ | ||
it('should do get, query object, headers, path, mixing', function (done) { | ||
function factory() { | ||
return httpfy(function (requestContext, reply) { | ||
function transport(pipe) { | ||
pipe.on('request', function (request) { | ||
Assert.deepEqual({ | ||
@@ -329,5 +324,5 @@ method: 'GET', | ||
} | ||
}, requestContext.request); | ||
}, request); | ||
reply(null, { | ||
pipe.respond({ | ||
qaz: 'wer' | ||
@@ -338,3 +333,3 @@ }); | ||
var client = Trooba.transport(factory).create(); | ||
var client = Trooba.use(transport).use(httpfy).build('client:default'); | ||
@@ -359,4 +354,4 @@ client.get('b-foo=azs') | ||
it('should do get, query object, headers, path, mixing', function (done) { | ||
function factory() { | ||
return httpfy(function (requestContext, reply) { | ||
function transport(pipe) { | ||
pipe.on('request', function (request) { | ||
Assert.deepEqual({ | ||
@@ -373,5 +368,5 @@ method: 'GET', | ||
} | ||
}, requestContext.request); | ||
}, request); | ||
reply(null, { | ||
pipe.respond({ | ||
qaz: 'wer' | ||
@@ -382,3 +377,3 @@ }); | ||
var client = Trooba.transport(factory).create(); | ||
var client = Trooba.use(transport).use(httpfy).build('client:default'); | ||
@@ -385,0 +380,0 @@ client.request({ |
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
24515
9
56
533