@pact-foundation/pact
Version:
Pact for all things Javascript
60 lines • 2.24 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Request = exports.HTTPMethods = void 0;
const node_https_1 = __importDefault(require("node:https"));
const axios_1 = __importDefault(require("axios"));
const ramda_1 = require("ramda");
const logger_1 = __importDefault(require("./logger"));
var HTTPMethods;
(function (HTTPMethods) {
HTTPMethods["GET"] = "GET";
HTTPMethods["POST"] = "POST";
HTTPMethods["PUT"] = "PUT";
HTTPMethods["PATCH"] = "PATCH";
HTTPMethods["DELETE"] = "DELETE";
HTTPMethods["HEAD"] = "HEAD";
HTTPMethods["OPTIONS"] = "OPTIONS";
HTTPMethods["COPY"] = "COPY";
HTTPMethods["LOCK"] = "LOCK";
HTTPMethods["MKCOL"] = "MKCOL";
HTTPMethods["MOVE"] = "MOVE";
HTTPMethods["PROPFIND"] = "PROPFIND";
HTTPMethods["PROPPATCH"] = "PROPPATCH";
HTTPMethods["UNLOCK"] = "UNLOCK";
HTTPMethods["REPORT"] = "REPORT";
})(HTTPMethods || (exports.HTTPMethods = HTTPMethods = {}));
class Request {
async send(method, url, body) {
try {
const res = await (0, axios_1.default)(url, {
data: body,
headers: {
'Content-Type': 'application/json',
'X-Pact-Mock-Service': 'true',
},
httpsAgent: new node_https_1.default.Agent({
keepAlive: true,
rejectUnauthorized: false,
}),
method,
timeout: 10000,
url,
maxBodyLength: Infinity,
});
if (res.status >= 200 && res.status < 400) {
return res.data;
}
return await Promise.reject(res.data);
}
catch (e) {
const error = e instanceof Error ? e : new Error(String(e));
logger_1.default.error(`error making http request: ${error.message}`);
return Promise.reject((0, ramda_1.pathOr)(error.message, ['response', 'data'], e));
}
}
}
exports.Request = Request;
//# sourceMappingURL=request.js.map