lemon-core
Version:
Lemon Serverless Micro-Service Platform
129 lines • 5.36 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 });
exports.HttpStorageService = void 0;
/**
* `http-storage-service.js`
* - `storage` service with http api.
*
* @author Steve Jung <steve@lemoncloud.io>
* @date 2019-09-26 initial version
* @date 2019-10-01 moved from ticket-data-service to storage-service.
* @date 2019-12-01 migrated to storage-service.
*
* @copyright (C) 2019 LemonCloud Co Ltd. - All Rights Reserved.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const engine_1 = require("../../engine");
const api_1 = require("../api/");
const NS = engine_1.$U.NS('STRS', 'green'); // NAMESPACE TO BE PRINTED.
/**
* class: `HttpStorageService`
*/
class HttpStorageService {
constructor(endpoint, type, idName) {
/**
* say hello()
* @param name (optional) given name
*/
this.hello = () => `http-storage-service:${this.endpoint}/${this.idName}`;
(0, engine_1._log)(NS, `HttpStorageService(${endpoint || ''})...`);
if (!endpoint)
throw new Error('@endpoint(string) is required!');
this.idName = `${idName || 'id'}`;
const TYPE = type;
this.endpoint = endpoint;
const HEADERS = { 'content-type': 'application/x-www-form-urlencoded' };
this.service = new api_1.APIService(TYPE, endpoint, HEADERS, api_1.APIService.buildClient(TYPE, endpoint, null, ''));
}
read(id) {
return __awaiter(this, void 0, void 0, function* () {
if (!id.trim())
throw new Error('@id (string) is required!');
return this.service.doGet(id);
});
}
readOrCreate(id, model) {
return __awaiter(this, void 0, void 0, function* () {
//return this.service.doGet(id); //TODO
return this.service.doGet(id).catch(e => {
if (`${e.message}`.startsWith('404 NOT FOUND'))
return this.save(id, model);
throw e;
});
});
}
save(id, item) {
return __awaiter(this, void 0, void 0, function* () {
return this.service.doPost(id, undefined, null, item);
});
}
update(id, item, $inc) {
return __awaiter(this, void 0, void 0, function* () {
const $I = yield this.validateIncrement(id, $inc);
const node = Object.assign(Object.assign({}, item), $I);
return this.service.doPut(id, undefined, null, node);
});
}
increment(id, $inc, $upt) {
return __awaiter(this, void 0, void 0, function* () {
if (!id)
throw new Error(`@id is required - http.increment(${this.service.hello})`);
if (!id.trim())
throw new Error(`@id (string) is required - http.increment(${this.service.hello})`);
if (!$inc && !$upt)
throw new Error('@item is required!');
const $I = yield this.validateIncrement(id, $inc);
const node = Object.assign(Object.assign({}, $upt), $I);
return this.service.doPut(id, undefined, null, node);
});
}
delete(id) {
return __awaiter(this, void 0, void 0, function* () {
return this.service.doDelete(id);
});
}
validateIncrement(id, $inc) {
return __awaiter(this, void 0, void 0, function* () {
const $org = yield this.read(id).catch(e => {
if (`${e.message || e}`.startsWith('404 NOT FOUND'))
return { id };
throw e;
});
if (!$inc)
return null;
const $I = Object.entries($inc).reduce((N, cur) => {
const key = cur[0];
const val = cur[1];
if (val !== undefined) {
const org = $org[key];
//! check type matched!
if (org !== undefined && typeof org === 'number' && typeof val !== 'number')
throw new Error(`.${key} (${val}) should be number!`);
//! if not exists, update it.
if (org === undefined) {
N[key] = val;
}
else if (typeof val !== 'number') {
N[key] = val;
}
else {
N[key] = org + val;
}
}
return N;
}, {});
return $I;
});
}
}
exports.HttpStorageService = HttpStorageService;
//# sourceMappingURL=http-storage-service.js.map
;