@jingbof/rets-client
Version:
RETS (Real Estate Transaction Standards) Client in Typescript
114 lines • 4.46 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetadataParser = void 0;
var xmlr_1 = require("@aequilibrium/xmlr");
var types_1 = require("../types");
var Status;
(function (Status) {
Status["Waiting"] = "";
Status["Resources"] = "resources";
Status["Columns"] = "columns";
Status["Data"] = "data";
})(Status || (Status = {}));
var DATA_SPLIT = '\t';
var MetadataParser = /** @class */ (function (_super) {
__extends(MetadataParser, _super);
function MetadataParser() {
var _this = _super.call(this) || this;
_this.data = [];
_this.resource = '';
_this.columns = [];
_this.status = Status.Waiting;
_this.on('startElement', _this.startElement);
// this.on('endElement', this.endElement)
_this.on('text', _this.text);
return _this;
}
MetadataParser.prototype.startElement = function (name, attrs) {
// console.log('startElement', name, attrs)
switch (name) {
case types_1.RetsKeys.Rets:
if ((attrs === null || attrs === void 0 ? void 0 : attrs.ReplyCode) && (attrs === null || attrs === void 0 ? void 0 : attrs.ReplyCode) !== '0') {
this.emit('error', new Error("Error during login [".concat(JSON.stringify(attrs), "]")));
}
break;
case types_1.RetsKeys.Columns:
this.status = Status.Columns;
break;
case types_1.RetsKeys.Data:
this.status = Status.Data;
break;
default:
if (Object.values(types_1.RetsMetadataType).includes(name)) {
this.resource = name;
this.data[this.resource] = [];
// console.log('startElement is RetsMetadataType', this.data)
}
else
throw new Error("Unexpected Element: [".concat(name, "] [").concat(JSON.stringify(attrs), "]"));
}
};
// endElement(name: string): void {
// console.log('endElement', name)
// // this.status = Status.Waiting
// switch (name) {
// case RetsKeys.Columns:
// if (this.status === Status.CollectingActions) {
// this.status = Status.Waiting
// }
// break
// default:
// break
// }
// }
MetadataParser.prototype.text = function (text) {
var trimmed = text.trim();
if (trimmed.length === 0)
return;
var data = text.split(DATA_SPLIT);
switch (this.status) {
case Status.Columns:
this.columns = data;
// this.status = Status.Waiting
break;
case Status.Data:
this.data[this.resource].push(this.columns.reduce(function (result, key, index) {
var _a;
return (__assign(__assign({}, result), (_a = {}, _a[key] = data[index], _a)));
}, {}));
// this.status = Status.Waiting
break;
default:
break;
}
};
return MetadataParser;
}(xmlr_1.Parser));
exports.MetadataParser = MetadataParser;
//# sourceMappingURL=MetadataParser.js.map