@vtex/api
Version:
VTEX I/O API client
89 lines (88 loc) • 4.05 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.requestMiddleware = exports.routerCacheMiddleware = exports.defaultsMiddleware = void 0;
const buildFullPath_1 = __importDefault(require("../../../utils/buildFullPath"));
const qs_1 = require("qs");
const ramda_1 = require("ramda");
const Tags_1 = require("../../../tracing/Tags");
const renameBy_1 = require("../../../utils/renameBy");
const setupAxios_1 = require("./setupAxios");
const http = (0, setupAxios_1.getConfiguredAxios)();
const paramsSerializer = (params) => {
return (0, qs_1.stringify)(params, { arrayFormat: 'repeat' });
};
const defaultsMiddleware = ({ baseURL, rawHeaders, params, timeout, retries, verbose, exponentialTimeoutCoefficient, initialBackoffDelay, exponentialBackoffCoefficient, httpsAgent }) => {
const countByMetric = {};
const headers = (0, renameBy_1.renameBy)(ramda_1.toLower, rawHeaders);
return (ctx, next) => {
var _a, _b, _c;
ctx.config = {
baseURL,
exponentialBackoffCoefficient,
exponentialTimeoutCoefficient,
httpsAgent: ctx.config.httpsAgent || httpsAgent,
initialBackoffDelay,
maxRedirects: 0,
retries,
timeout,
validateStatus: status => (status >= 200 && status < 300),
verbose,
...ctx.config,
headers: {
...headers,
...(0, renameBy_1.renameBy)(ramda_1.toLower, ctx.config.headers),
},
params: {
...params,
...ctx.config.params,
},
paramsSerializer,
retryCount: 0,
};
if (ctx.config.verbose && ctx.config.metric) {
const current = countByMetric[ctx.config.metric];
countByMetric[ctx.config.metric] = (current || 0) + 1;
ctx.config.count = countByMetric[ctx.config.metric];
ctx.config.label = `${ctx.config.metric}#${ctx.config.count}`;
}
if ((_a = ctx.tracing) === null || _a === void 0 ? void 0 : _a.isSampled) {
const { config } = ctx;
const fullUrl = (0, buildFullPath_1.default)(config.baseURL, http.getUri(config));
(_c = (_b = ctx.tracing) === null || _b === void 0 ? void 0 : _b.rootSpan) === null || _c === void 0 ? void 0 : _c.addTags({
[Tags_1.OpentracingTags.HTTP_METHOD]: config.method || 'get',
[Tags_1.OpentracingTags.HTTP_URL]: fullUrl,
});
}
return next();
};
};
exports.defaultsMiddleware = defaultsMiddleware;
const ROUTER_CACHE_KEY = 'x-router-cache';
const ROUTER_CACHE_HIT = 'HIT';
const ROUTER_CACHE_REVALIDATED = 'REVALIDATED';
const routerCacheMiddleware = async (ctx, next) => {
var _a, _b, _c, _d, _e;
await next();
const routerCacheHit = (_b = (_a = ctx.response) === null || _a === void 0 ? void 0 : _a.headers) === null || _b === void 0 ? void 0 : _b[ROUTER_CACHE_KEY];
const status = (_c = ctx.response) === null || _c === void 0 ? void 0 : _c.status;
if (routerCacheHit) {
(_e = (_d = ctx.tracing) === null || _d === void 0 ? void 0 : _d.rootSpan) === null || _e === void 0 ? void 0 : _e.setTag("http.cache.router" /* CustomHttpTags.HTTP_ROUTER_CACHE_RESULT */, routerCacheHit);
}
if (routerCacheHit === ROUTER_CACHE_HIT || (routerCacheHit === ROUTER_CACHE_REVALIDATED && status !== 304)) {
ctx.cacheHit = {
memory: 0,
revalidated: 0,
...ctx.cacheHit,
router: 1,
};
}
};
exports.routerCacheMiddleware = routerCacheMiddleware;
const requestMiddleware = (limit) => async (ctx, next) => {
const makeRequest = () => http.request(ctx.config);
ctx.response = await (limit ? limit(makeRequest) : makeRequest());
};
exports.requestMiddleware = requestMiddleware;