@serenity-js/rest
Version:
Serenity/JS Screenplay Pattern library for interacting with REST and other HTTP-based services, supporting comprehensive API testing and blended testing scenarios
104 lines • 4.18 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.axiosProxyOverridesFor = axiosProxyOverridesFor;
exports.createGetProxyForUrlFromConfig = createGetProxyForUrlFromConfig;
exports.createGetProxyForUrlFromEnvironmentVariables = createGetProxyForUrlFromEnvironmentVariables;
const process = __importStar(require("node:process"));
const tiny_types_1 = require("tiny-types");
const createUrl_1 = require("./createUrl");
const EnvironmentVariables_1 = require("./EnvironmentVariables");
const ProxyAgent_1 = require("./ProxyAgent");
const ProxyBypass_1 = require("./ProxyBypass");
/**
* @param options
*/
function axiosProxyOverridesFor(options) {
const agent = new ProxyAgent_1.ProxyAgent({
httpAgent: options.httpAgent,
httpsAgent: options.httpsAgent,
// if there's a specific proxy override configured, use it
// if not - detect proxy automatically based on the env variables
getProxyForUrl: options.proxy
? createGetProxyForUrlFromConfig(options.proxy)
: createGetProxyForUrlFromEnvironmentVariables(new EnvironmentVariables_1.EnvironmentVariables(process.env)),
});
return {
proxy: false,
httpAgent: agent,
httpsAgent: agent,
};
}
function createGetProxyForUrlFromConfig(proxyOptions) {
const proxyBypass = ProxyBypass_1.ProxyBypass.create(proxyOptions?.bypass);
return createGetProxyForUrl(proxyBypass, (url) => {
return (0, createUrl_1.createUrl)({
username: proxyOptions.auth?.username,
password: proxyOptions.auth?.password,
protocol: proxyOptions.protocol,
hostname: (0, tiny_types_1.ensure)('proxy.host', proxyOptions?.host, (0, tiny_types_1.isDefined)()),
port: proxyOptions.port ? Number(proxyOptions.port) : undefined,
}).toString();
});
}
function createGetProxyForUrlFromEnvironmentVariables(env) {
const proxyBypass = ProxyBypass_1.ProxyBypass.create(env.findFirst('npm_config_no_proxy', 'no_proxy'));
return createGetProxyForUrl(proxyBypass, (url) => {
const protocolName = url.protocol.replace(/:$/, '');
const proxyUrl = env.findFirst(`npm_config_${protocolName}_proxy`, `${protocolName}_proxy`, 'npm_config_proxy', 'all_proxy');
return proxyUrl || undefined;
});
}
function createGetProxyForUrl(bypass, getProxy) {
return function getProxyForUrl(urlValue) {
if (!isValidUrl(urlValue)) {
return undefined;
}
const url = new URL(urlValue);
return bypass.matches(url)
? undefined
: getProxy(url);
};
}
function isValidUrl(url) {
try {
new URL(url);
return true;
}
catch {
return false;
}
}
//# sourceMappingURL=proxy.js.map
;