@63pokupki/nodejs-common
Version:
Common nodejs functionality
72 lines • 2.08 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.faApiRequest = void 0;
const axios_1 = __importDefault(require("axios"));
const https_1 = __importDefault(require("https"));
const CryptoH_1 = require("../Helpers/CryptoH");
const JwtH_1 = require("../Helpers/JwtH");
// для https соединения
const httpsAgent = new https_1.default.Agent({
rejectUnauthorized: false, // (NOTE: this will disable client verification)
});
/** Ключ для межсерверного запроса */
let sKeySrv = '';
/** Получить srvkey */
function fSrvKey(ctx) {
if (!sKeySrv) {
sKeySrv = (0, CryptoH_1.mEncrypt)(ctx.srv.cry.algorithm, ctx.srv.cry.key, (0, JwtH_1.mJwtEncode)({
data: ctx.srv.keyPool,
secret: ctx.srv.jwt.jwtKey,
algorithm: ctx.srv.jwt.algorithm,
exp: ctx.srv.jwt.exp,
deviation: 15
}));
}
return sKeySrv;
}
/**
* Заголовки для запроса к API
* @param req
*/
const fApiHeaders = (ctx) => ({
headers: {
srvkey: fSrvKey(ctx),
apikey: ctx.sys.apikey,
},
timeout: 20000,
httpsAgent
});
/**
* Формирование запроса к API
* @param sUrl - относительный url
* @param ctx -
* @param data - данные для запроса
*/
async function faApiRequest(ctx, // Контекст
sUrl, // Путь
data) {
let resp = {
ok: false,
data: null,
e: true,
msg: '',
errors: [],
};
let respData = null;
try {
resp = (await axios_1.default.post(sUrl, data, fApiHeaders(ctx))).data;
if (resp === null || resp === void 0 ? void 0 : resp.ok) {
respData = resp.data;
}
}
catch (e) {
respData = e.response.data;
}
return respData;
}
exports.faApiRequest = faApiRequest;
;
//# sourceMappingURL=ApiRequest.js.map