Skip to content

Commit e453171

Browse files
committed
Initial commit
1 parent e01c361 commit e453171

File tree

7 files changed

+335
-1
lines changed

7 files changed

+335
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
/bower_components

LICENCE.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# DON'T BE A DICK PUBLIC LICENSE
2+
3+
> Version 1, December 2009
4+
> Copyright (C) 2009 Philip Sturgeon [email protected]
5+
6+
Everyone is permitted to copy and distribute verbatim or modified copies of this license document,
7+
and changing it is allowed as long as the name is changed.
8+
9+
DON'T BE A DICK PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
10+
11+
Do whatever you like with the original work, just don't be a dick.
12+
13+
Being a dick includes - but is not limited to - the following instances:
14+
15+
1. Outright copyright infringement - Don't just copy this and change the name.
16+
2. Selling the unmodified original with no work done what-so-ever, that's REALLY being a dick.
17+
3. Modifying the original work to contain hidden harmful content. That would make you a PROPER dick.
18+
19+
If you become rich through modifications, related works/services, or supporting the original work, share the love.
20+
Only a dick would make loads off this work and not buy the original works creator(s) a pint.
21+
22+
Code is provided with no warranty. Using somebody else's code and bitching when it goes wrong makes you a DONKEY dick.
23+
Fix the problem yourself. A non-dick would submit the fix back.

README.md

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,101 @@
11
# angular-http-status
2-
Angular constant for http status codes
2+
3+
Angular stuff to easily convert status text into its HTTP code and _vice versa_.
4+
5+
I only care about _real_ Http statuses, so I used the list from
6+
http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml where each code is attached to a RFC.
7+
8+
## Installation
9+
10+
### Bower
11+
12+
```
13+
bower install angular-http-status --save
14+
```
15+
16+
Once the lib is downloaded,
17+
18+
1. add a reference in your index.html
19+
20+
`<script type="application/javascript" src="../bower_components/angular-http-status/angular-http-status.js"></script>`
21+
22+
2. add the module in your angular application
23+
24+
```
25+
angular
26+
.module('YOUR-ANGULAR-APP-NAME', [
27+
'ngHttpStatus'
28+
])
29+
.config(...
30+
```
31+
32+
3. enjoy!
33+
34+
## Usage
35+
36+
### The `HttpCodes` constant
37+
38+
It's a _big_ object with status text as keys and status codes as values, like:
39+
40+
```json
41+
{
42+
"CONTINUE": 100, // RFC7231 @6.2.1 : https://tools.ietf.org/html/rfc7231#section-6.2.1
43+
"SWITCHING_PROTOCOLS": 101, // RFC7231 @6.2.2 : https://tools.ietf.org/html/rfc7231#section-6.2.2
44+
"PROCESSING": 102, // RFC2518 : https://tools.ietf.org/html/rfc2518
45+
"..."
46+
```
47+
48+
Once you adds `HttpCodes` as dependency in a controller / service / ..., you cas use it like:
49+
50+
```javascript
51+
if (rejection.status === HttpCodes.UNAUTHORIZED) {
52+
$state.go('login')
53+
}
54+
```
55+
56+
### The `HttpStatus` factory
57+
58+
Defines the following methods:
59+
60+
- `toString({Number} status)` : takes a valid HTTP status code and returns its meaning
61+
(`undefined` if given status isn't in the list)
62+
63+
### Example
64+
65+
Here is the code of the controller used for the demo app:
66+
67+
```javascript
68+
angular
69+
.module('demo-angular-http-status', ['ngHttpStatus'])
70+
.controller('demoCtrl', function(HttpCodes, HttpStatus) {
71+
72+
this.code = 200;
73+
this.text = 'Ok';
74+
75+
this.statusCodes = HttpCodes;
76+
77+
this.toString = function(status) {
78+
return HttpStatus.toString(status);
79+
};
80+
81+
this.toStatus = function(meaning) {
82+
var statusText = meaning.toUpperCase().replace(' ', '_');
83+
84+
return angular.isDefined(HttpCodes[statusText])
85+
? HttpCodes[statusText]
86+
: '<< Invalid key ' + statusText + ' >>'
87+
}
88+
});
89+
```
90+
91+
# License
92+
93+
DBAD. See the [LICENSE](https://github.com/yllieth/angular-http-status/blob/master/LICENSE.md) for more details.
94+
95+
# Contributing
96+
97+
1. Fork it
98+
2. Create your feature branch (`git checkout -b my-new-feature`)
99+
3. Commit your changes (`git commit -am 'Add some feature'`)
100+
4. Push to the branch (`git push origin my-new-feature`)
101+
5. Create new Pull Request

angular-http-status.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
angular
2+
.module('ngHttpStatus', [])
3+
.constant('HttpCodes', {
4+
CONTINUE: 100, // RFC7231 @6.2.1 : https://tools.ietf.org/html/rfc7231#section-6.2.1
5+
SWITCHING_PROTOCOLS: 101, // RFC7231 @6.2.2 : https://tools.ietf.org/html/rfc7231#section-6.2.2
6+
PROCESSING: 102, // RFC2518 : https://tools.ietf.org/html/rfc2518
7+
8+
OK: 200, // RFC7231 @6.3.1 : https://tools.ietf.org/html/rfc7231#section-6.3.1
9+
CREATED: 201, // RFC7231 @6.3.2 : https://tools.ietf.org/html/rfc7231#section-6.3.2
10+
ACCEPTED: 202, // RFC7231 @6.3.3 : https://tools.ietf.org/html/rfc7231#section-6.3.3
11+
NON_AUTHORITATIVE_INFORMATION: 203, // RFC7231 @6.3.4 : https://tools.ietf.org/html/rfc7231#section-6.3.4
12+
NO_CONTENT: 204, // RFC7231 @6.3.5 : https://tools.ietf.org/html/rfc7231#section-6.3.5
13+
RESET_CONTENT: 205, // RFC7231 @6.3.6 : https://tools.ietf.org/html/rfc7231#section-6.3.6
14+
PARTIAL_CONTENT: 206, // RFC7233 @4.1 : https://tools.ietf.org/html/rfc7233#section-4.1
15+
MULTI_STATUS: 207, // RFC4918 : https://tools.ietf.org/html/rfc4918
16+
ALREADY_REPORTED: 208, // RFC5842 : https://tools.ietf.org/html/rfc5842
17+
IM_USED: 226, // RFC3239 : https://tools.ietf.org/html/rfc3239
18+
19+
MULTIPLE_CHOICES: 300, // RFC7231 @6.4.1 : https://tools.ietf.org/html/rfc7231#section-6.4.1
20+
MOVED_PERMANENTLY: 301, // RFC7231 @6.4.2 : https://tools.ietf.org/html/rfc7231#section-6.4.2
21+
FOUND: 302, // RFC7231 @6.4.3 : https://tools.ietf.org/html/rfc7231#section-6.4.3
22+
SEE_OTHER: 303, // RFC7231 @6.4.4 : https://tools.ietf.org/html/rfc7231#section-6.4.4
23+
NOT_MODIFIED: 304, // RFC7232 @4.1 : https://tools.ietf.org/html/rfc7232#section-4.1
24+
USE_PROXY: 305, // RFC7231 @6.4.5 : https://tools.ietf.org/html/rfc7231#section-6.4.5
25+
UNUSED: 306, // RFC7231 @6.4.6 : https://tools.ietf.org/html/rfc7231#section-6.4.6
26+
TEMPORARY_REDIRECT: 307, // RFC7231 @6.4.7 : https://tools.ietf.org/html/rfc7231#section-6.4.7
27+
PERMANENT_REDIRECT: 308, // RFC7538 : https://tools.ietf.org/html/rfc7538
28+
TOO_MANY_REDIRECTS: 310,
29+
30+
BAD_REQUEST: 400, // RFC7231 @6.5.1 : https://tools.ietf.org/html/rfc7231#section-6.5.1
31+
UNAUTHORIZED: 401, // RFC7235 @3.1 : https://tools.ietf.org/html/rfc7235#section-3.1
32+
PAYMENT_REQUIRED: 402, // RFC7231 @6.5.2 : https://tools.ietf.org/html/rfc7231#section-6.5.2
33+
FORBIDDEN: 403, // RFC7231 @6.5.3 : https://tools.ietf.org/html/rfc7231#section-6.5.3
34+
NOT_FOUND: 404, // RFC7231 @6.5.4 : https://tools.ietf.org/html/rfc7231#section-6.5.4
35+
METHOD_NOT_ALLOWED: 405, // RFC7231 @6.5.5 : https://tools.ietf.org/html/rfc7231#section-6.5.5
36+
NOT_ACCEPTABLE: 406, // RFC7231 @6.5.6 : https://tools.ietf.org/html/rfc7231#section-6.5.6
37+
PROXY_AUTHENTICATION_REQUIRED: 407, // RFC7235 @3.2 : https://tools.ietf.org/html/rfc7235#section-3.2
38+
REQUEST_TIMEOUT: 408, // RFC7231 @6.5.7 : https://tools.ietf.org/html/rfc7231#section-6.5.7
39+
CONFLICT: 409, // RFC7231 @6.5.8 : https://tools.ietf.org/html/rfc7231#section-6.5.8
40+
GONE: 410, // RFC7231 @6.5.9 : https://tools.ietf.org/html/rfc7231#section-6.5.9
41+
LENGTH_REQUIRED: 411, // RFC7231 @6.5.10 : https://tools.ietf.org/html/rfc7231#section-6.5.10
42+
PRECONDITION_FAILED: 412, // RFC7232 @4.2 : https://tools.ietf.org/html/rfc7232#section-4.2
43+
PAYLOAD_TOO_LARGE: 413, // RFC7231 @6.5.11 : https://tools.ietf.org/html/rfc7231#section-6.5.11
44+
URI_TOO_LONG: 414, // RFC7231 @6.5.12 : https://tools.ietf.org/html/rfc7231#section-6.5.12
45+
UNSUPPORTED_MEDIA_TYPE: 415, // RFC7231 @6.5.13 : https://tools.ietf.org/html/rfc7231#section-6.5.13
46+
RANGE_NOT_SATISFIABLE: 416, // RFC7233 @4.4 : https://tools.ietf.org/html/rfc7233#section-4.4
47+
EXPECTATION_FAILED: 417, // RFC7231 @6.5.14 : https://tools.ietf.org/html/rfc7231#section-6.5.14
48+
IM_A_TEAPOT: 418, // RFC2324 : https://tools.ietf.org/html/rfc2324
49+
MISDIRECT_REQUEST: 421, // RFC7540 @9.1.2 : https://tools.ietf.org/html/rfc7540#section-9.1.2
50+
UNPROCESSABLE_ENTITY: 422, // RFC4918 : https://tools.ietf.org/html/rfc4918
51+
LOCKED: 423, // RFC4918 : https://tools.ietf.org/html/rfc4918
52+
FAILED_DEPENDENCY: 424, // RFC4918 : https://tools.ietf.org/html/rfc4918
53+
UNORDERED_COLLEcTION: 425, // RFC3648 : https://tools.ietf.org/html/rfc3648
54+
UPGRADE_REQUIRED: 426, // RFC7231 @6.5.15 : https://tools.ietf.org/html/rfc7231#section-6.5.15
55+
PRECONDITION_REQUIRED: 428, // RFC6585 : https://tools.ietf.org/html/rfc6585
56+
TOO_MANY_REQUEST: 429, // RFC6585 : https://tools.ietf.org/html/rfc6585
57+
REQUEST_HEADER_FIELDS_TOO_LARGE: 431, // RFC6585 : https://tools.ietf.org/html/rfc6585
58+
59+
INTERNAL_SERVER_ERROR: 500, // RFC7231 @6.6.1 : https://tools.ietf.org/html/rfc7231#section-6.6.1
60+
NOT_IMPLEMENTED: 501, // RFC7231 @6.6.2 : https://tools.ietf.org/html/rfc7231#section-6.6.2
61+
BAD_GATEWAY: 502, // RFC7231 @6.6.3 : https://tools.ietf.org/html/rfc7231#section-6.6.3
62+
SERVICE_UNAVAILABLE: 503, // RFC7231 @6.6.4 : https://tools.ietf.org/html/rfc7231#section-6.6.4
63+
GATEWAY_TIMEOUT: 504, // RFC7231 @6.6.5 : https://tools.ietf.org/html/rfc7231#section-6.6.5
64+
HTTP_VERSION_NOT_SUPPORTED: 505, // RFC7231 @6.6.6 : https://tools.ietf.org/html/rfc7231#section-6.6.6
65+
VARIANT_ALSO_NEGOTIATES: 506, // RFC2295 : https://tools.ietf.org/html/rfc2295
66+
INSUFFICIENT_STORAGE: 507, // RFC4918 : https://tools.ietf.org/html/rfc4918
67+
LOOP_DETECTED: 508, // RFC5842 : https://tools.ietf.org/html/rfc5842
68+
NOT_EXTENDED: 510, // RFC2774 : https://tools.ietf.org/html/rfc2774
69+
NETWORK_AUTHENTICATION_REQUIRED: 511 // RFC6585 : https://tools.ietf.org/html/rfc6585
70+
})
71+
.factory('HttpStatus', ['HttpCodes', function(HttpCodes) {
72+
return {
73+
toString: function(status) {
74+
if (angular.isNumber(status) === false) {
75+
throw new TypeError(status + ' is not a number and obviously not a valid HTTP status code.')
76+
}
77+
78+
var statusName = '';
79+
var keys = Object.keys(HttpCodes);
80+
81+
for (var i = 0 ; i < keys.length ; i++) {
82+
if (HttpCodes[keys[i]] === status) {
83+
statusName = keys[i].replace('_', ' ').toLowerCase();
84+
break;
85+
}
86+
}
87+
88+
return (statusName.length > 0)
89+
? statusName[0].toUpperCase() + statusName.substr(1)
90+
: undefined;
91+
}
92+
};
93+
}]);

app/app.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
angular
2+
.module('demo-angular-http-status', ['ngHttpStatus'])
3+
.controller('demoCtrl', function(HttpCodes, HttpStatus) {
4+
5+
this.code = 200;
6+
this.text = 'Ok';
7+
8+
this.statusCodes = HttpCodes;
9+
10+
this.toString = function(status) {
11+
return HttpStatus.toString(status);
12+
};
13+
14+
this.toStatus = function(meaning) {
15+
var statusText = meaning.toUpperCase().replace(' ', '_');
16+
17+
return angular.isDefined(HttpCodes[statusText])
18+
? HttpCodes[statusText]
19+
: '<< Invalid key ' + statusText + ' >>'
20+
}
21+
});

app/index.html

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title></title>
6+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
7+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
8+
<script type="application/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
9+
</head>
10+
<body ng-app="demo-angular-http-status">
11+
12+
<div class="container">
13+
<div class="jumbotron">
14+
<h1>Angular HTTP Status</h1>
15+
<p class="lead">There is no revolution here, just a convenience stuff to deal with HTTP Status codes</p>
16+
<p>
17+
<a class="btn btn-lg btn-success" href="https://github.com/yllieth/angular-http-status" role="button">
18+
<i class="fa fa-github"></i>
19+
Installation details on github
20+
</a>
21+
</p>
22+
</div>
23+
24+
<div class="row marketing" ng-controller="demoCtrl as Demo">
25+
<div class="col-lg-8">
26+
<h4 style="margin-bottom: 20px">Interactive conversions</h4>
27+
28+
<div class="row" style="margin-bottom: 20px">
29+
<div class="col-lg-2">Code</div>
30+
<div class="col-lg-6"><input type="number" ng-model="Demo.code" style="width: 100%" /></div>
31+
<div class="col-lg-4">means <code>{{ Demo.toString(Demo.code) }}</code></div>
32+
</div>
33+
34+
<div class="row" style="margin-bottom: 20px">
35+
<div class="col-lg-2">Status text</div>
36+
<div class="col-lg-6"><input type="text" ng-model="Demo.text" style="width: 100%" /></div>
37+
<div class="col-lg-4">is status <code>{{ Demo.toStatus(Demo.text) }}</code></div>
38+
</div>
39+
40+
<div class="row" style="margin-top: 20px; border-top: 1px solid lightgrey ; padding-top: 20px">
41+
<div class="col-lg-12">
42+
This angular module comes with 2 elements:
43+
<ul>
44+
<li>an angular constant (<code>HttpCodes</code>) with the list you can see right here</li>
45+
<li>an angular factory (<code>HttpStatus</code>) with a convenience method <code>toString()</code></li>
46+
</ul>
47+
</div>
48+
</div>
49+
50+
<div class="row" style="margin-top: 20px; border-top: 1px solid lightgrey ; padding-top: 20px">
51+
<div class="col-lg-12">
52+
Codes and status text are from <a href="http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml">http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml</a>.
53+
<br/><br/>
54+
This list only contains HTTP status with an existing RFC (see comments within the code).
55+
</div>
56+
</div>
57+
</div>
58+
59+
<div class="col-lg-4">
60+
<h4>Complete list</h4>
61+
<pre>{{ Demo.statusCodes | json }}</pre>
62+
</div>
63+
</div>
64+
65+
<footer class="footer">
66+
<p>© Yllieth 2015</p>
67+
</footer>
68+
69+
</div>
70+
71+
<script type="application/javascript" src="../angular-http-status.js"></script>
72+
<script type="application/javascript" src="app.js"></script>
73+
</body>
74+
</html>

bower.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "angular-http-status",
3+
"version": "0.1.0",
4+
"authors": [ "Yllieth <[email protected]>" ],
5+
"description": "Convenience constant and factory to deal with HTTP status code",
6+
"keywords": [ "angular", "http", "status", "code" ],
7+
"private": false,
8+
"license": "DBAD",
9+
"dependencies": {
10+
"angular": ">=1.x"
11+
},
12+
"main": "angular-http-status.js",
13+
"ignore": [
14+
".*",
15+
"bower.json",
16+
"package.json",
17+
"app/",
18+
"test/",
19+
"bower_components/",
20+
"node_modules/"
21+
]
22+
}

0 commit comments

Comments
 (0)