@opra/common
Version:
Opra common package
57 lines (56 loc) • 2.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMappedClass = createMappedClass;
const index_js_1 = require("../../../helpers/index.js");
const index_js_2 = require("../../../schema/index.js");
const constants_js_1 = require("../../constants.js");
const mapped_type_js_1 = require("../mapped-type.js");
const get_is_inherited_predicate_fn_js_1 = require("./get-is-inherited-predicate-fn.js");
function createMappedClass(source, config, options) {
const isInheritedPredicate = (0, get_is_inherited_predicate_fn_js_1.getIsInheritedPredicateFn)(config.pick, config.omit);
const sourceName = typeof source === 'string'
? source.charAt(0).toUpperCase() + source.substring(1)
: source.name;
const className = options?.name || sourceName + 'Mapped';
const MappedClass = {
[className]: class {
constructor() {
if (typeof source === 'function')
(0, index_js_1.inheritPropertyInitializers)(this, source, isInheritedPredicate);
}
},
}[className];
if (typeof source === 'function')
(0, index_js_1.mergePrototype)(MappedClass.prototype, source.prototype);
if (typeof source === 'function') {
const m = Reflect.getOwnMetadata(constants_js_1.DATATYPE_METADATA, source);
if (!m)
throw new TypeError(`Class "${source}" doesn't have datatype metadata information`);
if (!(m.kind === index_js_2.OpraSchema.ComplexType.Kind ||
m.kind === index_js_2.OpraSchema.MappedType.Kind ||
m.kind === index_js_2.OpraSchema.MixinType.Kind)) {
throw new TypeError(`Class "${source}" is not a ${index_js_2.OpraSchema.ComplexType.Kind}`);
}
}
const metadata = {
...options,
kind: 'MappedType',
base: source,
};
if (config.pick)
metadata.pick = config.pick;
if (config.omit)
metadata.omit = config.omit;
if (config.partial)
metadata.partial = config.partial;
if (config.required)
metadata.required = config.required;
Reflect.defineMetadata(constants_js_1.DATATYPE_METADATA, metadata, MappedClass);
if (typeof source === 'function') {
mapped_type_js_1.MappedType._applyMixin(MappedClass, source, {
...config,
isInheritedPredicate,
});
}
return MappedClass;
}