2020-04-05 00:57:34 +09:00
|
|
|
"use strict";
|
|
|
|
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 (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
|
|
return extendStatics(d, b);
|
|
|
|
};
|
|
|
|
return function (d, b) {
|
|
|
|
extendStatics(d, b);
|
|
|
|
function __() { this.constructor = d; }
|
|
|
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
var FullDate_1 = require("./FullDate");
|
|
|
|
var StrictTypeParser;
|
|
|
|
(function (StrictTypeParser) {
|
|
|
|
var BadValueError = /** @class */ (function (_super) {
|
|
|
|
__extends(BadValueError, _super);
|
2020-04-08 20:17:57 +09:00
|
|
|
function BadValueError(label, message) {
|
|
|
|
var _this = _super.call(this, message) || this;
|
|
|
|
_this.label = label;
|
2020-04-05 00:57:34 +09:00
|
|
|
console.error(_this.message);
|
2020-04-08 20:17:57 +09:00
|
|
|
Object.setPrototypeOf(_this, BadTypeError.prototype);
|
2020-04-05 00:57:34 +09:00
|
|
|
return _this;
|
|
|
|
}
|
|
|
|
return BadValueError;
|
|
|
|
}(Error));
|
|
|
|
StrictTypeParser.BadValueError = BadValueError;
|
2020-04-08 20:17:57 +09:00
|
|
|
var BadTypeError = /** @class */ (function (_super) {
|
|
|
|
__extends(BadTypeError, _super);
|
|
|
|
function BadTypeError(label, type, value) {
|
|
|
|
var _this = _super.call(this, label, label + ": Can not convert `" + (['object', 'array'].includes(typeof value) ?
|
|
|
|
JSON.stringify(value) : "" + value) + "` to type " + type) || this;
|
|
|
|
_this.label = label;
|
|
|
|
_this.type = type;
|
|
|
|
_this.value = value;
|
|
|
|
return _this;
|
|
|
|
}
|
|
|
|
return BadTypeError;
|
|
|
|
}(BadValueError));
|
|
|
|
StrictTypeParser.BadTypeError = BadTypeError;
|
|
|
|
function _int32(x, label) {
|
|
|
|
if (typeof x === 'number' && x === (x | 0))
|
|
|
|
return x;
|
|
|
|
if (typeof x === 'string') { // convert from url
|
|
|
|
var r = +x | 0;
|
|
|
|
if (x === r.toString())
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
throw new BadTypeError(label, 'int32', x);
|
|
|
|
}
|
|
|
|
StrictTypeParser._int32 = _int32;
|
|
|
|
function _number(x, label) {
|
2020-04-05 00:57:34 +09:00
|
|
|
if (typeof x === 'number')
|
|
|
|
return x;
|
2020-04-08 20:17:57 +09:00
|
|
|
if (typeof x === 'string') { // convert from url
|
2020-04-05 00:57:34 +09:00
|
|
|
var r = +x;
|
|
|
|
if (!isNaN(r))
|
|
|
|
return r;
|
|
|
|
}
|
2020-04-08 20:17:57 +09:00
|
|
|
throw new BadTypeError(label, 'number', x);
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
StrictTypeParser._number = _number;
|
2020-04-08 20:17:57 +09:00
|
|
|
function _string(x, label) {
|
2020-04-05 00:57:34 +09:00
|
|
|
if (typeof x === 'string')
|
|
|
|
return x;
|
|
|
|
if (typeof x === 'object')
|
|
|
|
return x.toString();
|
2020-04-08 20:17:57 +09:00
|
|
|
throw new BadTypeError(label, 'string', x);
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
StrictTypeParser._string = _string;
|
2020-04-08 20:17:57 +09:00
|
|
|
function _boolean(x, label) {
|
2020-04-05 00:57:34 +09:00
|
|
|
if (typeof x === 'boolean')
|
|
|
|
return x;
|
|
|
|
if (x === 'true')
|
|
|
|
return true;
|
|
|
|
if (x === 'false')
|
|
|
|
return false;
|
2020-04-08 20:17:57 +09:00
|
|
|
throw new BadTypeError(label, 'boolean', x);
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
StrictTypeParser._boolean = _boolean;
|
2020-04-08 20:17:57 +09:00
|
|
|
function _Date(x, label) {
|
2020-04-05 00:57:34 +09:00
|
|
|
var r = new Date(x);
|
2020-04-08 20:17:57 +09:00
|
|
|
if (x != null && !isNaN(+r))
|
2020-04-05 00:57:34 +09:00
|
|
|
return r;
|
2020-04-08 20:17:57 +09:00
|
|
|
throw new BadTypeError(label, 'Date', x);
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
StrictTypeParser._Date = _Date;
|
2020-04-08 20:17:57 +09:00
|
|
|
function _FullDate(x, label) {
|
2020-04-05 00:57:34 +09:00
|
|
|
var r = new FullDate_1.FullDate(x);
|
2020-04-08 20:17:57 +09:00
|
|
|
if (x != null && !isNaN(+r))
|
2020-04-05 00:57:34 +09:00
|
|
|
return r;
|
2020-04-08 20:17:57 +09:00
|
|
|
throw new BadTypeError(label, 'FullDate', x);
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
StrictTypeParser._FullDate = _FullDate;
|
2020-04-08 20:17:57 +09:00
|
|
|
function _byte(x, label) {
|
2020-04-05 00:57:34 +09:00
|
|
|
if (typeof x === 'string')
|
|
|
|
return x;
|
|
|
|
if (x instanceof Buffer)
|
|
|
|
return x.toString('base64');
|
2020-04-08 20:17:57 +09:00
|
|
|
throw new BadTypeError(label, 'byte', x);
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
StrictTypeParser._byte = _byte;
|
2020-04-08 20:17:57 +09:00
|
|
|
function _binary(x, label) {
|
2020-04-05 00:57:34 +09:00
|
|
|
if (typeof x === 'string')
|
|
|
|
return x;
|
|
|
|
if (x instanceof Buffer)
|
|
|
|
return x.toString('hex');
|
|
|
|
if ((x === null || x === void 0 ? void 0 : x.buffer) instanceof Buffer)
|
|
|
|
return x.toString('hex');
|
2020-04-08 20:17:57 +09:00
|
|
|
throw new BadTypeError(label, 'binary', x);
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
StrictTypeParser._binary = _binary;
|
2020-04-08 20:17:57 +09:00
|
|
|
function _Array(x, label, mapper) {
|
2020-04-05 00:57:34 +09:00
|
|
|
if (x instanceof Array)
|
2020-04-08 20:17:57 +09:00
|
|
|
return x.map(mapper);
|
|
|
|
throw new BadTypeError(label, 'Array', x);
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
StrictTypeParser._Array = _Array;
|
2020-04-08 20:17:57 +09:00
|
|
|
function undefinedCheck(val, label) {
|
|
|
|
if (val === undefined) {
|
|
|
|
throw new BadValueError(label, label + " is required, but got undefined");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function parse(stp, val, label) {
|
|
|
|
// body
|
|
|
|
undefinedCheck(val, label);
|
|
|
|
if (val === null) {
|
|
|
|
throw new BadValueError(label, label + " is not nullable, but got null");
|
|
|
|
}
|
|
|
|
return stp(val, label);
|
|
|
|
}
|
|
|
|
StrictTypeParser.parse = parse;
|
|
|
|
function nullableParse(stp, val, label) {
|
|
|
|
// body
|
|
|
|
undefinedCheck(val, label);
|
|
|
|
return val === null ? null : stp(val, label);
|
|
|
|
}
|
|
|
|
StrictTypeParser.nullableParse = nullableParse;
|
2020-04-05 00:57:34 +09:00
|
|
|
StrictTypeParser.supportTypes = [
|
2020-04-08 20:17:57 +09:00
|
|
|
'int32', 'number', 'string', 'boolean',
|
|
|
|
'Date', 'FullDate', 'byte', 'binary'
|
2020-04-05 00:57:34 +09:00
|
|
|
];
|
|
|
|
})(StrictTypeParser = exports.StrictTypeParser || (exports.StrictTypeParser = {}));
|