jspurefix
Version:
pure node js fix engine
90 lines • 4.1 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpJsonSampleAdapter = void 0;
const http_transaction_1 = require("./http-transaction");
const tsyringe_1 = require("tsyringe");
const di_tokens_1 = require("../../runtime/di-tokens");
let HttpJsonSampleAdapter = class HttpJsonSampleAdapter {
constructor(config) {
var _a, _b, _c;
this.config = config;
this.queue = [];
this.token = null;
this.routes = new Map();
this.logger = config.logFactory.logger('http.adapter');
const routes = this.routes;
const options = (_c = (_b = (_a = config === null || config === void 0 ? void 0 : config.description) === null || _a === void 0 ? void 0 : _a.application) === null || _b === void 0 ? void 0 : _b.http) === null || _c === void 0 ? void 0 : _c.options;
if (!options) {
return;
}
options.forEach((o) => {
routes.set(o.name, o);
});
this.logger.info(`instance created routes ${routes.size}`);
}
getOptions(data) {
var _a;
const q = this.queue;
if (q.length === 0) {
return null;
}
const next = (_a = q.shift()) !== null && _a !== void 0 ? _a : null;
if (next == null)
return null;
const options = next.options;
options.data = {
fixml: data.toString()
};
this.logger.info(`${next.msgType}: ${next.options.method} ${next.options.url} ${data.length}`);
return options;
}
endMessage(m) {
if (!this.token) {
const headers = m.headers;
this.token = headers.authorization;
this.logger.info(`receive token ${this.token}`);
}
return m.data;
}
beginMessage(msgType) {
var _a;
const routes = this.routes;
const route = (_a = routes.get(msgType)) !== null && _a !== void 0 ? _a : routes.get('default');
const options = {
method: route === null || route === void 0 ? void 0 : route.value.method,
url: route === null || route === void 0 ? void 0 : route.value.url,
json: route === null || route === void 0 ? void 0 : route.value.json,
resolveWithFullResponse: route === null || route === void 0 ? void 0 : route.value.resolveWithFullResponse,
headers: route === null || route === void 0 ? void 0 : route.value.headers
};
const headers = options.headers;
if (headers) {
if (this.token) {
headers.authorization = this.token;
}
else if (msgType === 'UserReq') {
delete headers.authorization;
}
}
this.queue.push(new http_transaction_1.HttpTransaction(msgType, options));
}
};
exports.HttpJsonSampleAdapter = HttpJsonSampleAdapter;
exports.HttpJsonSampleAdapter = HttpJsonSampleAdapter = __decorate([
(0, tsyringe_1.injectable)(),
__param(0, (0, tsyringe_1.inject)(di_tokens_1.DITokens.IJsFixConfig)),
__metadata("design:paramtypes", [Object])
], HttpJsonSampleAdapter);
//# sourceMappingURL=http-json-sample-adapter.js.map