UNPKG

@contract-case/case-core

Version:

Core functionality for the ContractCase contract testing suite

66 lines 2.71 kB
"use strict"; 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, params, context) => { const url = `${baseurl}${path}`; context.logger.deepMaintainerDebug(`GET:`, url); return axios_1.default .get(url, { ...(typeof auth !== 'string' ? { auth } : {}), headers: withAuthHeaders({ Accept: 'application/hal+json', }, auth), // include parameters if there were any ...(Object.keys(params).length > 0 ? { params } : {}), }) .then(marshaller_1.unmarshallSuccess, marshaller_1.unmarshallFailure) .then((response) => { context.logger.deepMaintainerDebug(`RESPONSE(GET):`, response); return response; }); }, authedPut: (path, content, context) => { context.logger.deepMaintainerDebug(`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) .then((response) => { context.logger.deepMaintainerDebug(`RESPONSE(PUT):`, response); return response; }); }, authedPost: (path, content, context) => { context.logger.deepMaintainerDebug(`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) .then((response) => { context.logger.deepMaintainerDebug(`RESPONSE(POST):`, response); return response; }); }, }); exports.makeAxiosConnector = makeAxiosConnector; //# sourceMappingURL=axiosConnector.js.map