k2hr3-api
Version:
K2HR3 REST API is K2hdkc based Resource and Roles and policy Rules
112 lines (111 loc) • 4.75 kB
JavaScript
;
/*
* K2HR3 REST API
*
* Copyright 2017 Yahoo Japan Corporation.
*
* K2HR3 is K2hdkc based Resource and Roles and policy Rules, gathers
* common management information for the cloud.
* K2HR3 can dynamically manage information as "who", "what", "operate".
* These are stored as roles, resources, policies in K2hdkc, and the
* client system can dynamically read and modify these information.
*
* For the full copyright and license information, please view
* the license file that was distributed with this source code.
*
* AUTHOR: Takeshi Nakatani
* CREATE: Fri Sep 8 2017
* REVISION:
*
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.k2hr3resutil = void 0;
const k2hr3apiutil_1 = __importDefault(require("./k2hr3apiutil"));
//---------------------------------------------------------
// Functions
//---------------------------------------------------------
const rawSendErrorResponse = (req, res, status, msgbody, strType) => {
if (!k2hr3apiutil_1.default.isSafeEntity(res) || !k2hr3apiutil_1.default.isSafeEntity(status) || !k2hr3apiutil_1.default.isSafeNumber(status)) {
if (!k2hr3apiutil_1.default.isSafeEntity(res)) {
// can do nothing
return false;
}
status = 500; // 500: Internal Error
msgbody = 'Wrong parameters are got.';
}
// set response type
if (k2hr3apiutil_1.default.isSafeString(strType)) {
res.type(strType);
}
// set response status
res.status(status ?? 500);
// body message to JSON
let jsonbody = '';
if (null !== msgbody) {
if (k2hr3apiutil_1.default.isString(msgbody)) {
if (k2hr3apiutil_1.default.checkSimpleJSON(msgbody)) {
jsonbody = msgbody;
}
else {
// msgbody is string, but it is not JSON.
jsonbody = JSON.stringify(msgbody);
}
}
else {
jsonbody = JSON.stringify(msgbody);
}
}
// set 'x-k2hr3-error' header
if (k2hr3apiutil_1.default.isPlainObject(req) &&
k2hr3apiutil_1.default.isPlainObject(req.headers)) {
const tmpDbgHead = req.headers['x-k2hr3-debug'];
if (k2hr3apiutil_1.default.isSafeEntity(tmpDbgHead) &&
(k2hr3apiutil_1.default.compareCaseString('ON', k2hr3apiutil_1.default.isArray(tmpDbgHead) ? k2hr3apiutil_1.default.getSafeString(tmpDbgHead[0]) : k2hr3apiutil_1.default.getSafeString(tmpDbgHead)) ||
k2hr3apiutil_1.default.compareCaseString('OK', k2hr3apiutil_1.default.isArray(tmpDbgHead) ? k2hr3apiutil_1.default.getSafeString(tmpDbgHead[0]) : k2hr3apiutil_1.default.getSafeString(tmpDbgHead)) ||
k2hr3apiutil_1.default.compareCaseString('debug', k2hr3apiutil_1.default.isArray(tmpDbgHead) ? k2hr3apiutil_1.default.getSafeString(tmpDbgHead[0]) : k2hr3apiutil_1.default.getSafeString(tmpDbgHead)) ||
k2hr3apiutil_1.default.compareCaseString('dbg', k2hr3apiutil_1.default.isArray(tmpDbgHead) ? k2hr3apiutil_1.default.getSafeString(tmpDbgHead[0]) : k2hr3apiutil_1.default.getSafeString(tmpDbgHead)) ||
k2hr3apiutil_1.default.compareCaseString('1', k2hr3apiutil_1.default.isArray(tmpDbgHead) ? k2hr3apiutil_1.default.getSafeString(tmpDbgHead[0]) : k2hr3apiutil_1.default.getSafeString(tmpDbgHead)) ||
k2hr3apiutil_1.default.compareCaseString('1', k2hr3apiutil_1.default.isArray(tmpDbgHead) ? k2hr3apiutil_1.default.getSafeString(tmpDbgHead[0]) : k2hr3apiutil_1.default.getSafeString(tmpDbgHead)))) {
let errheader;
if (k2hr3apiutil_1.default.isSafeString(jsonbody)) {
errheader = jsonbody;
}
else {
errheader = JSON.stringify({ status: status });
}
res.header('x-k2hr3-error', errheader);
}
}
// send body
res.send(jsonbody);
return true;
};
//---------------------------------------------------------
// Exports
//---------------------------------------------------------
exports.k2hr3resutil = {
//
// <argument> <default> <note>
// req request object
// res response object
// status status code for error, must be number
// msgbody null response error body if need
// strType null response type if need
//
errResponse: rawSendErrorResponse
};
//
// Default
//
exports.default = exports.k2hr3resutil;
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noexpandtab sw=4 ts=4 fdm=marker
* vim<600: noexpandtab sw=4 ts=4
*/