@case-contract-testing/case
Version:
Next-generation contract testing suite
48 lines (47 loc) • 1.94 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeAxiosConnector = void 0;
const axios_1 = __importDefault(require("axios"));
const marshaller_1 = require("./marshaller");
// This is the connector for axios.
// It knows how to turn logical requests into axios calls
const withAuthHeaders = (headers, auth) => ({
...headers,
...(typeof auth === 'string' ? { Authorization: `Bearer ${auth}` } : {}),
});
const makeAxiosConnector = (baseurl, auth) => ({
authedGet: (path = '') => axios_1.default
.get(`${baseurl}${path}`, {
...(typeof auth !== 'string' ? { auth } : {}),
headers: withAuthHeaders({
// Accept: 'application/json',
}, auth),
})
.then(marshaller_1.unmarshallSuccess, marshaller_1.unmarshallFailure),
authedPut: (path, content, context) => {
context.logger.maintainerDebug(`PUT: ${baseurl}${path}`, content);
return axios_1.default
.put(`${baseurl}${path}`, content, {
...(typeof auth !== 'string' ? { auth } : {}),
headers: withAuthHeaders({
Accept: 'application/json',
}, auth),
})
.then(marshaller_1.unmarshallSuccess, marshaller_1.unmarshallFailure);
},
authedPost: (path, content, context) => {
context.logger.maintainerDebug(`POST: ${baseurl}${path}`, content);
return axios_1.default
.post(`${baseurl}${path}`, content, {
...(typeof auth !== 'string' ? { auth } : {}),
headers: withAuthHeaders({
Accept: 'application/hal+json',
}, auth),
})
.then(marshaller_1.unmarshallSuccess, marshaller_1.unmarshallFailure);
},
});
exports.makeAxiosConnector = makeAxiosConnector;