@chuxingpay/hc-faas-base
Version:
FuXun Hotel Connector Base Objects
168 lines (167 loc) • 5.98 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const isEqual = require("fast-deep-equal");
const request_1 = require("../utils/request");
const config_1 = require("../config");
class BaseMapping {
constructor(config) {
if (!config)
throw new Error('小老弟你是不是忘记写构造函数了?');
if (!config.name || !config.version)
throw new Error('invalid config');
this.pkgName = config.name;
this.pkgVersion = config.version;
// TODO: 初始化时形式优化
this._soaHotelRequest = new request_1.default({ host: config_1.soa.hotel.host, token: config_1.soa.key }, { name: this.pkgName, version: this.pkgVersion });
}
/**
* 标识字段, 用于判断是否需要更新
*
* @readonly
* @abstract
* @type {*}
* @memberof BaseMapping
*/
get compareKeys() {
return {};
}
/**
* 根据实例 rules 匹配第三方原始数据
*
* @param {*} originData 第三方原始数据对象
* @param {boolean} withRawData 是否将原始记录写入数据库里面
* @param {*} [parameter] mapping 函数执行参数
* @returns {Promise<T>}
* @memberof BaseMapping
*/
mapping(originData, withRawData, parameter) {
return __awaiter(this, void 0, void 0, function* () {
const result = {};
if (withRawData)
result.rawData = originData;
for (const key of Object.keys(this.rule)) {
const ruleVal = this.rule[key];
if (typeof ruleVal === 'function') {
lodash_1.assign(result, yield ruleVal(originData, parameter));
continue;
}
if (Array.isArray(ruleVal) && ruleVal.length === 2) {
result[key] = lodash_1.get(originData, ruleVal[0], ruleVal[1]);
continue;
}
result[key] = lodash_1.get(originData, ruleVal, null);
}
return result;
});
}
/**
* 对比操作
*
* @param {*} localData 当前本地数据
* @param {*} newDate 获取到的新数据
* @memberof BaseMapping
*/
compare(localData, newData) {
const _keys = this.compareKeys;
if (!_keys || typeof _keys !== 'object')
throw new Error('compareKeys must be defined before calling compare function');
if (localData && localData.toJSON && typeof localData.toJSON === 'function')
localData = localData.toJSON();
const _localData = {};
for (let _key of Object.keys(this.compareKeys)) {
if (typeof this.compareKeys[_key] === 'function') {
_localData[_key] = this.compareKeys[_key](localData);
}
else if (this.compareKeys[_key] || lodash_1.isBoolean(this.compareKeys[_key])) {
_localData[_key] = localData[_key];
}
}
const _newData = lodash_1.pick(newData, Object.keys(this.compareKeys));
return isEqual(_localData, _newData);
}
/**
* 通过队列进行新增
*
* @param {string} channel
* @param {(T | T[])} data
* @returns {Promise<any>}
* @memberof BaseMapping
*/
createToQueue(channel, data) {
return __awaiter(this, void 0, void 0, function* () { });
}
/**
* 通过队列进行更新
*
* @param {string} channel
* @param {(T | T[])} data
* @returns {Promise<any>}
* @memberof BaseMapping
*/
updateToQueue(channel, data) {
return __awaiter(this, void 0, void 0, function* () { });
}
/**
* 通过 API 进行单条数据新增
*
* @param {string} endpoint
* @param {T} payload
* @returns {Promise<any>}
* @memberof BaseMapping
*/
createToApi(endpoint, payload) {
return __awaiter(this, void 0, void 0, function* () {
return this._soaHotelRequest.post(endpoint, { json: payload });
});
}
/**
* 通过 API 进行单条数据更新
*
* @param {string} endpoint
* @param {T} payload
* @returns {Promise<any>}
* @memberof BaseMapping
*/
updateToApi(endpoint, payload) {
return __awaiter(this, void 0, void 0, function* () {
return this._soaHotelRequest.put(endpoint, { json: payload });
});
}
/**
* 通过 API 进行批量新增
*
* @param {string} endpoint
* @param {T} payload
* @returns {Promise<any>}
* @memberof BaseMapping
*/
batchCreateToApi(endpoint, payload) {
return __awaiter(this, void 0, void 0, function* () {
return this._soaHotelRequest.post(endpoint, { json: payload });
});
}
/**
* 通过 API 进行批量更新
*
* @param {string} endpoint
* @param {T} payload
* @returns {Promise<any>}
* @memberof BaseMapping
*/
batchUpdateToApi(endpoint, payload) {
return __awaiter(this, void 0, void 0, function* () {
return this._soaHotelRequest.put(endpoint, { json: payload });
});
}
}
exports.default = BaseMapping;