Last active
August 29, 2015 14:01
-
-
Save lovio/466da830818f552a8d5c to your computer and use it in GitHub Desktop.
rewrite the response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ONLY TO MAKE $RESPONSE EASY TO USE | |
* in some case, Backend will respond every request with a 200 http status code | |
* and [error_or_not, data] array. | |
* It's very difficult to use when writing front-end code with angular. | |
* | |
* The purpose of this module is to transform it to legal one | |
*/ | |
/** | |
* typeof response.data === 'string', check point | |
*/ | |
(function(){ | |
'use strict'; | |
angular.module('response-rewriter', []) | |
.config(['$httpProvider', function($httpProvider){ | |
$httpProvider.interceptors.push(['$q', function($q){ | |
return { | |
// we don't care legal response like 401 | |
// we only care [0, xxx] | |
response: function(response){ | |
var data = response.data; | |
if (typeof data === 'string') return response; | |
if (Number(data[0]) === 1) { | |
response.status = 400; | |
} | |
response.data = data[1]; | |
return $q.when(response); | |
} | |
} | |
}]); | |
}]); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment