You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.3 KiB
JavaScript
34 lines
1.3 KiB
JavaScript
"use strict";
|
|
|
|
const defaultValue = require("default-value");
|
|
const createError = require("create-error");
|
|
|
|
let BadStatusCode = createError("BadResponseCode");
|
|
let ParsingFailed = createError("ParsingFailed");
|
|
|
|
module.exports = {
|
|
BadStatusCode,
|
|
ParsingFailed,
|
|
axiosConfiguration: {
|
|
/* We do the status check manually in the response-handling code, so that we can identify it as a specific type of error. */
|
|
validateStatus: () => true,
|
|
/* Likewise, we manually attempt to parse the response body. */
|
|
responseType: "text",
|
|
/* The `responseType` setting above is currently (erroneously?) being ignored by Axios, and it tries to parse JSON anyway, which gives us an inconsistent result. This happens through the default `transformResponse` handler, so we remove that behaviour here by overriding it. */
|
|
transformResponse: (data) => data
|
|
},
|
|
parse: function parseJsonResponse(response, options = {}) {
|
|
let validateStatus = defaultValue(options.validateStatus, (status) => status >= 200 && status < 300);
|
|
|
|
if (validateStatus(response.status)) {
|
|
try {
|
|
return JSON.parse(response.data);
|
|
} catch (error) {
|
|
throw new ParsingFailed("Could not parse response body as JSON");
|
|
}
|
|
} else {
|
|
throw new BadStatusCode(`Got an unexpected HTTP status code (${response.status})`, { statusCode: response.status });
|
|
}
|
|
}
|
|
};
|