Skip to content

Commit

Permalink
[break retro] added parse option to handle also text responses
Browse files Browse the repository at this point in the history
  • Loading branch information
loris committed Apr 10, 2019
1 parent 051583c commit 3c8dc2d
Show file tree
Hide file tree
Showing 7 changed files with 1,898 additions and 1,539 deletions.
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
| [options.fallback] | <code>Object</code> | | this object will be used as response data when an API endpoint returns error (and no mock option is set). |
| [options.model] | <code>Object</code> | | this object will be used through object-mapper in order to map the API response to our model. If not defined, no mapping will be performed. |
| [options.method] | <code>String</code> | | if not defined, POST will be used if body is present, otherwise GET is used as default. |
| [options.blob] | <code>Boolean</code> | | if defined, data will be extracted as a blob, otherwise, it will default to a json encoding |
| [options.parse] | <code>String</code> | | if 'blob', data will be extracted as a blob, if 'text', data will be extracted as a text, otherwise, it will default to a json encoding |
| [options.params] | <code>Object</code> | | this object is matched against the endpoint expression. All the parameters not present in it, |
| [options.fullResponse] | <code>Boolean</code> | <code>false</code> | it returns the whole response object (not only the data received) will be attached as query string |

6 changes: 3 additions & 3 deletions dist/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function _call(endpoint, options) {
* @param {Object} [options.model] this object will be used through object-mapper in order to map the API response to our model.
* If not defined, no mapping will be performed.
* @param {String} [options.method] if not defined, POST will be used if body is present, otherwise GET is used as default.
* @param {Boolean} [options.blob] if defined, data will be extracted as a blob, otherwise, it will default to a json encoding
* @param {String} [options.parse] if 'blob', data will be extracted as a blob, if 'text', data will be extracted as a text, otherwise, it will default to a json encoding
* @param {Object} [options.params] this object is matched against the endpoint expression. All the parameters not present in it,
* @param {Boolean} [options.fullResponse=false] it returns the whole response object (not only the data received)
* will be attached as query string
Expand Down Expand Up @@ -239,7 +239,7 @@ function sanitizeParams(params) {
*/
function encodeQueryString(obj) {
if (Array.isArray(obj)) {
return obj.map(encodeURI).join(ARRAY_SEPARATOR);
return obj.map(encodeURIComponent).join(ARRAY_SEPARATOR);
}
return encodeURI(obj);
return encodeURIComponent(obj);
}
14 changes: 10 additions & 4 deletions dist/middlewares/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ Object.defineProperty(exports, "__esModule", {
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

exports.default = status;
var PARSE_METHOD = {
blob: 'blob',
text: 'text',
default: 'json'
};

function status(req, res) {
// returns res or a promise that resolves with res
var blob = req.options.blob;
var parse = req.options.parse;

return res.ok ? res.status === 204 ? res : extractJson(res, req.debug, blob) : _extends(res, { errorCode: res.status });
return res.ok ? res.status === 204 ? res : extract(res, req.debug, parse) : _extends(res, { errorCode: res.status });
}

function extractJson(res, debug, blob) {
return res[blob ? 'blob' : 'json']().then(function (data) {
function extract(res, debug, parse) {
return res[PARSE_METHOD[parse || 'default']]().then(function (data) {
return _extends(res, { data: data });
}).catch(function (e) {
debug && console.error('extractJson() api-client error', e);
Expand Down
Loading

0 comments on commit 3c8dc2d

Please sign in to comment.