Skip to content

Commit

Permalink
Release v0.1.6. See CHANGELOG.md
Browse files Browse the repository at this point in the history
  • Loading branch information
seriema committed May 29, 2014
1 parent 6ecd535 commit 5622247
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 65 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,20 @@
<a name="0.1.6"></a>
### 0.1.6 (2014-05-29)


#### Bug Fixes

* **apimock:**
* fix command 'auto' ([acfc2371](http://github.com/seriema/angular-apimock/commit/acfc2371079be8f428a02e31ece05e1d90bb5c38))
* treat NaN as false for command ([a28544d4](http://github.com/seriema/angular-apimock/commit/a28544d43c5d11f65095b6950fba75bd07553578))
* **nuget:** fix nuget push task ([e77e6e7f](http://github.com/seriema/angular-apimock/commit/e77e6e7f96a8da6510390b3e70ca49b0ab4d4a6a))


#### Features

* **apimock:** add logging through $log ([9d93551f](http://github.com/seriema/angular-apimock/commit/9d93551f3801483a2cd479c972a89a033e88fcab))


<a name="0.1.5"></a>
### 0.1.5 (2014-05-19)

Expand Down
2 changes: 1 addition & 1 deletion bower.json
@@ -1,6 +1,6 @@
{
"name": "angular-apimock",
"version": "0.1.5",
"version": "0.1.6",
"main": "dist/angular-apimock.min.js",
"appPath": "app",
"ignore": [
Expand Down
134 changes: 73 additions & 61 deletions dist/angular-apimock.js
@@ -1,4 +1,4 @@
/*! Angular API Mock v0.1.5
/*! Angular API Mock v0.1.6
* Licensed with MIT
* Made with ♥ from Seriema + Redhorn */
/* Create the main module, `apiMock`. It's the one that needs to be included in
Expand Down Expand Up @@ -39,47 +39,16 @@ angular.module('apiMock', []).config([
*/
// Helper objects
//
var mockDataPath = '/mock_data';
var apiPath = '/api';
var $location;
var $log;
var $q;
var config = {
mockDataPath: '/mock_data',
apiPath: '/api'
};
var fallbacks = [];
// Helper methods
//
function httpStatusResponse(status) {
var response = {
status: status,
headers: {
'Content-Type': 'text/html; charset=utf-8',
'Server': 'Angular ApiMock'
}
};
return $q.reject(response);
}
function getParameter(req) {
var mockValue = localMock(req);
if (mockValue === undefined) {
mockValue = globalMock();
}
return mockValue;
}
function reroute(req) {
if (!isApiPath(req.url)) {
return req;
}
// replace apiPath with mockDataPath.
var path = req.url.substring(config.apiPath.length);
req.url = config.mockDataPath + path;
// strip query strings (like ?search=banana).
var regex = /[a-zA-z0-9/.\-]*/;
req.url = regex.exec(req.url)[0];
// add file endings (method verb and .json).
if (req.url[req.url.length - 1] === '/') {
req.url = req.url.slice(0, -1);
}
req.url += '.' + req.method.toLowerCase() + '.json';
return req;
}
function detectParameter(keys) {
var regex = /apimock/i;
var result;
Expand All @@ -93,24 +62,28 @@ angular.module('apiMock', []).config([
function localMock(req) {
return detectParameter(req);
}
function globalMock() {
return detectParameter($location.search());
function getParameter(req) {
var mockValue = localMock(req);
if (mockValue === undefined) {
mockValue = globalMock();
}
return mockValue;
}
function getCommand(mockValue) {
switch (typeof mockValue) {
case 'number':
if (mockValue !== 0) {
if (mockValue !== 0 && !isNaN(mockValue)) {
return {
type: 'respond',
value: mockValue
};
}
break;
case 'string':
if (mockValue.toLowerCase() === 'auto') {
switch (mockValue.toLowerCase()) {
case 'auto':
return { type: 'recover' };
}
if (mockValue.toLowerCase() === 'true') {
case 'true':
return { type: 'reroute' };
}
break;
Expand All @@ -122,12 +95,29 @@ angular.module('apiMock', []).config([
}
return { type: 'ignore' };
}
var prepareFallback = function (req) {
function globalMock() {
return detectParameter($location.search());
}
function httpStatusResponse(status) {
var response = {
status: status,
headers: {
'Content-Type': 'text/html; charset=utf-8',
'Server': 'Angular ApiMock'
}
};
$log.info('apiMock: mocking HTTP status to ' + status);
return $q.reject(response);
}
function isApiPath(url) {
return url.indexOf(config.apiPath) === 0;
}
function prepareFallback(req) {
if (isApiPath(req.url)) {
fallbacks.push(req);
}
};
var removeFallback = function (res) {
}
function removeFallback(res) {
var found = false;
angular.forEach(fallbacks, function (fallback, index) {
if (fallback.method === res.method && fallback.url === res.url) {
Expand All @@ -136,16 +126,34 @@ angular.module('apiMock', []).config([
}
});
return found;
};
var isApiPath = function (url) {
return url.indexOf(config.apiPath) === 0;
};
function ApiMock(_$location, _$q) {
$location = _$location;
$q = _$q;
}
function reroute(req) {
if (!isApiPath(req.url)) {
return req;
}
// replace apiPath with mockDataPath.
var oldPath = req.url;
var newPath = req.url.substring(config.apiPath.length);
newPath = config.mockDataPath + newPath;
// strip query strings (like ?search=banana).
var regex = /[a-zA-z0-9/.\-]*/;
newPath = regex.exec(newPath)[0];
// add file endings (method verb and .json).
if (newPath[newPath.length - 1] === '/') {
newPath = newPath.slice(0, -1);
}
newPath += '.' + req.method.toLowerCase() + '.json';
req.url = newPath;
$log.info('apiMock: rerouting ' + oldPath + ' to ' + newPath);
return req;
}
// Expose public interface for provider instance
//
function ApiMock(_$location, _$log, _$q) {
$location = _$location;
$log = _$log;
$q = _$q;
}
var p = ApiMock.prototype;
p._countFallbacks = function () {
return fallbacks.length;
Expand Down Expand Up @@ -177,30 +185,29 @@ angular.module('apiMock', []).config([
return false;
}
if (removeFallback(rej.config)) {
$log.info('apiMock: recovering from failure at ' + rej.config.url);
return reroute(rej.config);
}
return false;
};
var config = {
mockDataPath: mockDataPath,
apiPath: apiPath
};
// Expose Provider interface
//
this.config = function (options) {
angular.extend(config, options);
};
this.$get = [
'$location',
'$log',
'$q',
function ($location, $q) {
return new ApiMock($location, $q);
function ($location, $log, $q) {
return new ApiMock($location, $log, $q);
}
];
}).service('httpInterceptor', [
'$injector',
'$q',
'apiMock',
function ($q, apiMock) {
function ($injector, $q, apiMock) {
/* The main service. Is jacked in as a interceptor on `$http` so it gets called
* on every http call. This allows us to do our magic. It uses the provider
* `apiMock` to determine if a mock should be done, then do the actual mocking.
Expand All @@ -215,7 +222,12 @@ angular.module('apiMock', []).config([
return res || $q.when(res);
};
this.responseError = function (rej) {
return apiMock.recover(rej) || $q.reject(rej);
var recover = apiMock.recover(rej);
if (recover) {
var $http = $injector.get('$http');
return $http(recover);
}
return $q.reject(rej);
};
}
]);
4 changes: 2 additions & 2 deletions dist/angular-apimock.min.js

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

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "angular-apiMock",
"version": "0.1.5",
"version": "0.1.6",
"repository": {
"type": "git",
"url": "git://github.com/seriema/angular-apimock.git"
Expand Down

0 comments on commit 5622247

Please sign in to comment.