abap-adt-api
Version:
Interface to Abap Developer Tools webservice
101 lines (100 loc) • 4.04 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.logResponse = exports.logError = void 0;
const axios_1 = __importDefault(require("axios"));
const _1 = require(".");
const utilities_1 = require("./utilities");
const getLoggingData = (config) => {
if (!(0, utilities_1.isObject)(config))
return { id: -1, startTime: new Date(), duration: 0 };
const id = (0, utilities_1.isNumber)(config === null || config === void 0 ? void 0 : config.adtRequestNumber) ? config.adtRequestNumber : -1;
const startTime = (config === null || config === void 0 ? void 0 : config.adtStartTime) instanceof Date ? config.adtStartTime : new Date();
return {
id,
startTime,
duration: new Date().getTime() - startTime.getTime()
};
};
const createLogData = (request, response, clientId, config, error) => {
var _a;
const { id, duration, startTime } = getLoggingData(config);
const stateful = ((_a = request.headers) === null || _a === void 0 ? void 0 : _a["X-sap-adt-sessiontype"]) === _1.session_types.stateful;
return { id, request, response, startTime, duration, stateful, clientId };
};
const convertRequest = (original) => {
if (!(0, utilities_1.isObject)(original))
return { headers: {}, method: "", uri: "", params: {} };
const { headers, data, method, uri, params } = original;
return {
method: method || "GET",
uri: (0, utilities_1.isString)(uri) ? uri : "",
params: (0, utilities_1.isObject)(params) ? { ...params } : {},
headers: (0, utilities_1.isObject)(headers) ? { ...headers } : {},
body: (0, utilities_1.isString)(data) || (0, utilities_1.isUndefined)(data) ? data : JSON.stringify(data)
};
};
const convertAxiosResponse = (original) => {
if (!original)
return { headers: {}, statusCode: 0, statusMessage: "" };
const { headers, data, status, statusText } = original;
return {
headers: headers ? { ...headers } : {},
statusCode: status,
statusMessage: statusText,
body: (0, utilities_1.isString)(data) ? data : JSON.stringify(data)
};
};
const convertResponse = (original) => {
if (!original)
return { headers: {}, statusCode: 0, statusMessage: "" };
if ((0, _1.isAdtException)(original)) {
const resp = {
headers: {},
statusCode: (0, _1.isAdtError)(original) ? original.err : 501,
statusMessage: original.message
};
return resp;
}
else {
const { headers, body, status, statusText } = original;
return {
headers: headers ? { ...headers } : {},
statusCode: status,
statusMessage: statusText,
body: (0, utilities_1.isString)(body) ? body : JSON.stringify(body)
};
}
};
const logError = (clientId, error, callback, config) => {
try {
if (!callback)
return;
if (axios_1.default.isAxiosError(error)) {
const request = convertRequest(error.config);
const response = convertAxiosResponse(error.response);
callback(createLogData(request, response, clientId, error.config, error));
}
else {
const resp = (0, _1.isAdtException)(error)
? convertResponse(error)
: convertResponse(undefined);
callback(createLogData(convertRequest(config), resp, clientId, config, error));
}
}
catch (error) { }
};
exports.logError = logError;
const logResponse = (clientId, original, config, callback) => {
try {
if (!callback)
return;
const request = convertRequest(config);
const response = convertResponse(original);
callback(createLogData(request, response, clientId, config));
}
catch (error) { }
};
exports.logResponse = logResponse;