@opra/common
Version:
Opra common package
121 lines (120 loc) • 5.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MappedType = void 0;
require("reflect-metadata");
const objects_1 = require("@jsopen/objects");
const ts_gems_1 = require("ts-gems");
const index_js_1 = require("../../schema/index.js");
const api_field_js_1 = require("./api-field.js");
const complex_type_base_js_1 = require("./complex-type-base.js");
const data_type_js_1 = require("./data-type.js");
const get_is_inherited_predicate_fn_js_1 = require("./utils/get-is-inherited-predicate-fn.js");
/**
* MappedType constructor
*/
exports.MappedType = function (...args) {
if (!this)
throw new TypeError('"this" should be passed to call class constructor');
// Constructor
const [owner, initArgs, context] = args;
complex_type_base_js_1.ComplexTypeBase.call(this, owner, initArgs, context);
const _this = (0, ts_gems_1.asMutable)(this);
_this.kind = index_js_1.OpraSchema.MappedType.Kind;
if (initArgs.base) {
// noinspection SuspiciousTypeOfGuard
if (!(initArgs.base instanceof complex_type_base_js_1.ComplexTypeBase)) {
throw new TypeError(`"${initArgs.base.kind}" can't be set as base for a "${this.kind}"`);
}
_this.base = initArgs.base;
_this.ctor = initArgs.ctor || _this.base.ctor;
if (initArgs.pick)
_this.pick = initArgs.pick.map(f => _this.base.normalizeFieldPath(f));
else if (initArgs.omit)
_this.omit = initArgs.omit.map(f => _this.base.normalizeFieldPath(f));
else if (initArgs.partial) {
_this.partial = Array.isArray(initArgs.partial)
? initArgs.partial.map(f => _this.base.normalizeFieldPath(f))
: initArgs.partial;
}
else if (initArgs.required) {
_this.required = Array.isArray(initArgs.required)
? initArgs.required.map(f => _this.base.normalizeFieldPath(f))
: initArgs.required;
}
/** Copy fields from base */
const isInheritedPredicate = (0, get_is_inherited_predicate_fn_js_1.getIsInheritedPredicateFn)(_this.pick, _this.omit);
const partial = Array.isArray(_this.partial)
? _this.partial.map(x => x.toLowerCase())
: _this.partial;
const required = Array.isArray(_this.required)
? _this.required.map(x => x.toLowerCase())
: _this.required;
for (const [k, v] of _this.base.fieldEntries('*')) {
if (!isInheritedPredicate(k))
continue;
const meta = { ...v };
if (partial === true ||
(Array.isArray(partial) && partial.includes(v.name.toLowerCase()))) {
meta.required = false;
}
else if (required === true ||
(Array.isArray(required) && required.includes(v.name.toLowerCase()))) {
meta.required = true;
}
const field = new api_field_js_1.ApiField(this, meta);
_this._fields.set(field.name, field);
}
if (!_this.pick ||
_this.base.additionalFields === false ||
(Array.isArray(_this.base.additionalFields) &&
_this.base.additionalFields?.[0] === 'error')) {
_this.additionalFields = _this.base.additionalFields;
}
if (initArgs.base.keyField && isInheritedPredicate(initArgs.base.keyField))
_this.keyField = initArgs.base.keyField;
_this.discriminatorField = initArgs.base.discriminatorField;
_this.discriminatorValue = initArgs.base.discriminatorValue;
}
};
/**
*
* @class MappedType
*/
class MappedTypeClass extends complex_type_base_js_1.ComplexTypeBase {
extendsFrom(baseType) {
if (!(baseType instanceof data_type_js_1.DataType))
baseType = this.node.getDataType(baseType);
if (!(baseType instanceof complex_type_base_js_1.ComplexTypeBase))
return false;
if (baseType === this)
return true;
return !!this.base?.extendsFrom(baseType);
}
toJSON(options) {
const superJson = super.toJSON(options);
const baseName = this.base
? this.node.getDataTypeNameWithNs(this.base)
: undefined;
return (0, objects_1.omitUndefined)({
...superJson,
base: baseName ? baseName : this.base.toJSON(options),
kind: this.kind,
pick: this.pick,
omit: this.omit,
partial: this.partial,
required: this.required,
discriminatorField: this.discriminatorField,
discriminatorValue: this.discriminatorValue,
});
}
_locateBase(callback) {
if (!this.base)
return;
if (callback(this.base))
return this.base;
if (this.base._locateBase)
return this.base._locateBase(callback);
}
}
exports.MappedType.prototype = MappedTypeClass.prototype;
exports.MappedType._applyMixin = () => undefined;