Skip to content

Commit

Permalink
Merge pull request #24 from chrisiconolly/master
Browse files Browse the repository at this point in the history
Adds the ability to whitelist local requests
  • Loading branch information
douglaseggleton committed Dec 10, 2015
2 parents 2e297bf + e9c6574 commit 9c844bf
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 126 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module.exports = function (grunt) {
grunt.registerTask('workflow:dev', [
'connect:dev',
'build',
'test:dev',
'open:dev',
'watch:dev'
]);
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Add the `ng.httpLoader` module as a dependency in your application:
angular.module('demo', ['ng.httpLoader'])
```

Whitelist the domains that you want the loader to show for:
Whitelist the external domains that you want the loader to show for:

```javascript
.config([
Expand All @@ -54,6 +54,19 @@ Whitelist the domains that you want the loader to show for:
])
```

You can whitelist requests to the local server:

```javascript
.config([
'httpMethodInterceptorProvider',
function (httpMethodInterceptorProvider) {
// ...
httpMethodInterceptorProvider.whitelistLocalRequests();
// ...
}
])
```

Add an HTML element with the `ng-http-loader` directive. This will be displayed
while requests are pending:

Expand Down
16 changes: 15 additions & 1 deletion app/package/js/angular-http-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ angular
.module('ng.httpLoader.httpMethodInterceptor', [])

.provider('httpMethodInterceptor', function () {
var domains = [];
var domains = [],
whitelistLocalRequests = false;

/**
* Add domains to the white list
Expand All @@ -146,6 +147,13 @@ angular
domains.push(domain);
};

/**
* White list requests to the local domain
*/
this.whitelistLocalRequests = function () {
whitelistLocalRequests = true;
};

this.$get = [
'$q',
'$rootScope',
Expand All @@ -160,6 +168,12 @@ angular
* @returns {boolean}
*/
var isUrlOnWhitelist = function (url) {
if (url.substring(0, 2) !== '//' &&
url.indexOf('://') === -1 &&
whitelistLocalRequests) {
return true;
}

for (var i = domains.length; i--;) {
if (url.indexOf(domains[i]) !== -1) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion app/package/js/angular-http-loader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 11 additions & 16 deletions app/src/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,30 @@
function (httpMethodInterceptorProvider) {
httpMethodInterceptorProvider.whitelistDomain('validate.jsontest.com');
httpMethodInterceptorProvider.whitelistDomain('github.com');
httpMethodInterceptorProvider.whitelistLocalRequests();
}
])

.controller('demoCtrl', [
'$scope',
'$http',
function (scope, $http) {
scope.makeRequest = function (index) {
switch (index) {
case 1:
$http.get('http://validate.jsontest.com/?json=%7B%22key%22:%22value%22%7D');
break;
case 2:
$http.get('https://api.github.com/users/fabpot/repos');
break;
case 3:
$http.get('http://www.flickr.com/services/feeds/photos_public.gne?tags=soccer&format=json');
break;
}
scope.buttons = [
{'name': 'jsontest (Whitelisted)', 'link': 'http://validate.jsontest.com/?json=%7B%22key%22:%22value%22%7D'},
{'name': 'Github (Whitelisted)', 'link': '//api.github.com/users/fabpot/repos'},
{'name': 'Local request (Whitelisted)', 'link': '/demo.html'},
{'name': 'Flickr (Not whitelisted - loader won\'t show)', 'link': 'http://www.flickr.com/services/feeds/photos_public.gne?tags=soccer&format=json'},
];

scope.makeRequest = function (link) {
$http.get(link);
}
}
]);
</script>
</head>
<body ng-app="demo" ng-controller="demoCtrl">
<button ng-click="makeRequest(1)">jsontest (Whitelisted)</button>
<button ng-click="makeRequest(2)">Github (Whitelisted)</button>
<button ng-click="makeRequest(3)">Flickr (Not whitelisted - loader won't show)</button>

<button ng-repeat="button in buttons" ng-click="makeRequest(button.link)">{{button.name}}</button>
<!-- Loader template -->
<script type="text/ng-template" id="example-loader.tpl.html">Loading {{title}}...</script>

Expand Down
16 changes: 15 additions & 1 deletion app/src/js/httpMethodInterceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ angular
.module('ng.httpLoader.httpMethodInterceptor', [])

.provider('httpMethodInterceptor', function () {
var domains = [];
var domains = [],
whitelistLocalRequests = false;

/**
* Add domains to the white list
Expand All @@ -19,6 +20,13 @@ angular
domains.push(domain);
};

/**
* White list requests to the local domain
*/
this.whitelistLocalRequests = function () {
whitelistLocalRequests = true;
};

this.$get = [
'$q',
'$rootScope',
Expand All @@ -33,6 +41,12 @@ angular
* @returns {boolean}
*/
var isUrlOnWhitelist = function (url) {
if (url.substring(0, 2) !== '//' &&
url.indexOf('://') === -1 &&
whitelistLocalRequests) {
return true;
}

for (var i = domains.length; i--;) {
if (url.indexOf(domains[i]) !== -1) {
return true;
Expand Down
Loading

0 comments on commit 9c844bf

Please sign in to comment.