Skip to content

Commit e56fd6b

Browse files
committed
feat: add sources
0 parents  commit e56fd6b

13 files changed

+387
-0
lines changed

.editorconfig

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[Makefile]
12+
charset = utf-8
13+
indent_style = tabs
14+
indent_size = 2
15+
end_of_line = lf
16+
trim_trailing_whitespace = true
17+
insert_final_newline = false

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
npm-debug.log
3+
.DS_Store
4+
bower_components
5+
report

.travis.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
node_js:
3+
- stable
4+
before_install:
5+
- npm install -g bower
6+
- bower install

LICENSE

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
The MIT License
2+
3+
Copyright (c) Tomas Aparicio
4+
5+
Permission is hereby granted, free of charge, to any person
6+
obtaining a copy of this software and associated documentation
7+
files (the "Software"), to deal in the Software without
8+
restriction, including without limitation the rights to use,
9+
copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the
11+
Software is furnished to do so, subject to the following
12+
conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24+
OTHER DEALINGS IN THE SOFTWARE.

Makefile

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
VERSION = 0.1.0
2+
BROWSERIFY = node ./node_modules/browserify/bin/cmd.js
3+
MOCHA = ./node_modules/.bin/mocha
4+
UGLIFYJS = ./node_modules/.bin/uglifyjs
5+
BANNER = "/*! theon-angular-adapter - v$(VERSION) - MIT License - https://github.com/theonjs/theon-angular-adapter */"
6+
MOCHA_PHANTOM = ./node_modules/.bin/mocha-phantomjs
7+
8+
default: all
9+
all: test
10+
browser: uglify
11+
test: browser mocha
12+
13+
uglify:
14+
$(UGLIFYJS) angular-adapter.js --mangle --preamble $(BANNER) --source-map angular-adapter.min.js.map --source-map-url http://cdn.rawgit.com/theonjs/theon-angular-adapter/$(VERSION)/angular-adapter.min.js.map > angular-adapter.min.js
15+
16+
mocha:
17+
$(MOCHA_PHANTOM) --reporter spec --ui bdd test/runner.html
18+
19+
loc:
20+
wc -l angular-adapter.js
21+
22+
gzip:
23+
gzip -c angular-adapter.min.js | wc -c
24+
25+
publish: browser release
26+
git push --tags origin HEAD:master
27+
npm publish

README.md

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# theon-angular-adapter [![Build Status](https://api.travis-ci.org/theonjs/theon-angular-adapter.svg?branch=master)][travis]
2+
3+
AngularJS [$http](https://docs.angularjs.org/api/ng/service/$http) agent adapter for theon based API clients
4+
5+
It works with Angular `+1.0`.
6+
7+
## Installation
8+
9+
Via [npm](http://npmjs.com)
10+
```bash
11+
npm install theon-angular-adapter
12+
```
13+
14+
Via [Bower](http://bower.io)
15+
```bash
16+
bower install theon-angular-adapter
17+
```
18+
19+
Or loading the script remotely
20+
```html
21+
<script src="//cdn.rawgit.com/theonjs/theon-angular-adapter/master/angular-adapter.js"></script>
22+
```
23+
24+
### Environments
25+
26+
- Chrome >= 5
27+
- Firefox >= 3
28+
- Safari >= 5
29+
- Opera >= 10
30+
- IE >= 9
31+
32+
### Setup
33+
34+
Load the module as dependency of your application
35+
```js
36+
var app = angular.module('app', ['theon.adapter'])
37+
```
38+
39+
### Services
40+
41+
#### $resilient
42+
43+
Main service to creating new Resilient HTTP clients
44+
45+
```js
46+
app.config(function ($theonAdapter) {
47+
// Configure theon to use the agent adapter
48+
theon.agents.set($theonAdapter)
49+
})
50+
```
51+
52+
## API
53+
54+
`theon` API [documentation](https://github.com/h2non/theon).
55+
56+
### $theonAdapter(req, res, done)
57+
58+
`theon` HTTP agent adapter.
59+
60+
### Development
61+
62+
Only [node.js](http://nodejs.org) is required for development
63+
64+
Clone the repository
65+
```bash
66+
$ git clone https://github.com/theonjs/theon-angular-adapter.git && cd theon-angular-adapter
67+
```
68+
69+
Install dependencies
70+
```bash
71+
$ npm install
72+
```
73+
```bash
74+
$ bower install
75+
```
76+
77+
Generate browser bundle source
78+
```bash
79+
$ make browser
80+
```
81+
82+
Run tests
83+
```bash
84+
$ make test
85+
```
86+
87+
## License
88+
89+
[MIT](http://opensource.org/licenses/MIT) © Tomas Aparicio
90+
91+
[travis]: http://travis-ci.org/theonjs/theon-angular-adapter

angular-adapter.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*! theon-angular-adapter - v0.1.0 - MIT License - https://github.com/theonjs/theon-angular-adapter */
2+
angular.module('theon.adapter', [])
3+
4+
.factory('$theonAdapter', ['$http', '$theonResponseAdapter', function ($http, adapter) {
5+
var httpAgent = $http
6+
7+
function $httpAdapter (req, res, done) {
8+
var opts = {
9+
url: req.url,
10+
method: req.method,
11+
auth: req.opts.auth,
12+
params: req.query,
13+
headers: req.headers,
14+
data: req.body,
15+
cache: req.agentOpts.cache,
16+
timeout: +req.opts.timeout || +req.agentOpts.timeout,
17+
withCredentials: req.agentOpts.withCredentials,
18+
xsrfHeaderName: req.agentOpts.xsrfHeaderName,
19+
xsrfCookieName: req.agentOpts.xsrfCookieName,
20+
transformRequest: req.agentOpts.transformRequest,
21+
transformResponse: req.agentOpts.transformResponse,
22+
paramSerializer: req.agentOpts.paramSerializer,
23+
responseType: req.agentOpts.responseType
24+
}
25+
26+
httpAgent(opts).then(function (_res) {
27+
done(null, adapter(res, _res))
28+
}, function (err) {
29+
done(adapter(res, err))
30+
})
31+
}
32+
33+
$httpAdapter.setAgent = function (agent) {
34+
httpAgent = agent
35+
}
36+
37+
return $httpAdapter
38+
}])
39+
40+
.factory('$theonResponseAdapter', ['$q', function ($q) {
41+
return function responseAdapter (res, _res) {
42+
if (!_res) return res
43+
44+
// Expose the agent-specific response
45+
res.setOriginalResponse(_res)
46+
47+
// Define recurrent HTTP fields
48+
res.setStatus(_res.status)
49+
res.setStatusText(_res.statusText)
50+
res.setHeaders(_res.headers)
51+
52+
// Define body, if present
53+
if (_res.data) res.setBody(_res.data)
54+
55+
return res
56+
}
57+
}])

angular-adapter.min.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

angular-adapter.min.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bower.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "theon-angular-adapter",
3+
"description": "Angular's $http agent adapter for theon based API clients",
4+
"version": "0.1.0",
5+
"license": "MIT",
6+
"author": "Tomas Aparicio",
7+
"repository": "theonjs/theon-angular-adapter",
8+
"main": "angular-adapter.js",
9+
"keywords": [
10+
"theon",
11+
"adapter",
12+
"http",
13+
"adapter",
14+
"api",
15+
"angular"
16+
],
17+
"ignore": [
18+
"**/.*",
19+
".*",
20+
"*.md",
21+
"src",
22+
"lib",
23+
"test",
24+
"Makefile",
25+
".editorconfig",
26+
".gitignore",
27+
".travis.yml",
28+
".npmignore",
29+
"package.json",
30+
"examples"
31+
],
32+
"devDependencies": {
33+
"theon.js": "~0.1.23"
34+
}
35+
}

package.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "theon-angular-adapter",
3+
"version": "0.3.0",
4+
"description": "Angular's $http agent adapter for theon based API clients",
5+
"license": "MIT",
6+
"author": "Tomas Aparicio",
7+
"repository": "theonjs/theon-angular-adapter",
8+
"main": "angular-adapter.js",
9+
"keywords": [
10+
"theon",
11+
"adapter",
12+
"http",
13+
"adapter",
14+
"api",
15+
"angular"
16+
],
17+
"scripts": {
18+
"test": "make test"
19+
},
20+
"devDependencies": {
21+
"browserify": "^5.9.1",
22+
"chai": "^1.9.1",
23+
"http-server": "^0.6.1",
24+
"mocha-phantomjs": "^3.4.1",
25+
"phantomjs": "^1.9.7-9",
26+
"sinon": "^1.10.3",
27+
"uglify-js": "^2.4.15"
28+
}
29+
}

test/angular-adapter.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
describe('$theonAdapter', function () {
2+
var $theonAdapter
3+
4+
beforeEach(module('theon.adapter'))
5+
6+
beforeEach(inject(function (_$theonAdapter_) {
7+
$theonAdapter = _$theonAdapter_
8+
}))
9+
10+
describe('API', function () {
11+
it('should expose the resilient API', function () {
12+
expect($theonAdapter).to.be.a('function')
13+
})
14+
})
15+
16+
describe('use adapter', function () {
17+
var resStub = null
18+
19+
beforeEach(function () {
20+
resStub = {
21+
setStatus: function (status) {
22+
this.status = status
23+
},
24+
setStatusText: function (text) {
25+
this.statusText = text
26+
},
27+
setHeaders: function (headers) {
28+
this.headers = headers
29+
},
30+
setBody: function (body) {
31+
this.body = body
32+
},
33+
setOriginalResponse: function (res) {
34+
this._res = res
35+
}
36+
}
37+
})
38+
39+
beforeEach(inject(function ($q) {
40+
$theonAdapter.setAgent(function (opts) {
41+
return {
42+
then: function (fn) {
43+
fn({ status: 200, data: 'foo', headers: { foo: 'bar' } })
44+
}
45+
}
46+
})
47+
}))
48+
49+
it('should perform a valid request', function (done) {
50+
$theonAdapter({
51+
url: '/foo',
52+
headers: { foo: 'bar' },
53+
opts: {},
54+
agentOpts: {}
55+
}, resStub, function (err, res) {
56+
expect(err).to.be.null
57+
expect(res).to.be.an('object')
58+
expect(res._res).to.be.an('object')
59+
expect(res.status).to.be.equal(200)
60+
expect(res.headers).to.be.an('object')
61+
expect(res.body).to.be.a('string')
62+
expect(res.body).to.be.equal('foo')
63+
done()
64+
})
65+
})
66+
})
67+
})

0 commit comments

Comments
 (0)