typed-reflector
Version:
Metadata reflector with typing support.
168 lines (162 loc) • 4.84 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// index.ts
var index_exports = {};
__export(index_exports, {
MetadataSetter: () => MetadataSetter,
Reflector: () => Reflector
});
module.exports = __toCommonJS(index_exports);
// src/reflector.ts
var import_reflect_metadata = require("reflect-metadata");
// src/utility/utility.ts
function isClass(target) {
if (typeof target !== "function") {
return false;
}
const proto = target.constructor;
return proto === Function;
}
function getClass(target) {
return isClass(target) ? target : getClass(target.constructor);
}
// src/reflector.ts
var Reflector = class {
get(metadataKey, instance, key) {
const instanceClass = getClass(instance);
if (key) {
return Reflect.getMetadata(metadataKey, instanceClass, key);
} else {
return Reflect.getMetadata(metadataKey, instanceClass);
}
}
getArray(metadataKey, instance, key) {
return this.get(metadataKey, instance, key) || [];
}
getProperty(metadataKey, instance, key, alternate) {
const valueFromClass = this.getArray(metadataKey, instance);
const valueFromAlternate = alternate ? this.getArray(metadataKey, alternate) : [];
const valueFromProperty = this.getArray(metadataKey, instance, key);
return [
...valueFromClass,
...valueFromAlternate,
...valueFromProperty
];
}
getMetadataFromDecorator(dec, metadataKey) {
const _dec = dec;
class TempClass {
}
const propertyKey = "_temp_property_key_" + Math.random().toString(16).slice(2);
_dec(TempClass.prototype, propertyKey);
return this.get(metadataKey, TempClass, propertyKey);
}
};
// src/setter.ts
var import_reflect_metadata2 = require("reflect-metadata");
var MetadataSetter = class {
getMetadataInDecorator(metaKey, target, key) {
if (key) {
return Reflect.getMetadata(metaKey, target, key);
} else {
return Reflect.getMetadata(metaKey, target);
}
}
setMetadataInDecorator(metaKey, value, target, key) {
if (key) {
return Reflect.defineMetadata(metaKey, value, target, key);
} else {
return Reflect.defineMetadata(metaKey, value, target);
}
}
transform(metadataKey, metadataValueFun, keysIndexMeta) {
return (target, key) => {
const targetClass = getClass(target);
const oldValue = this.getMetadataInDecorator(
metadataKey,
targetClass,
key
);
const newValue = metadataValueFun(oldValue);
this.setMetadataInDecorator(metadataKey, newValue, targetClass, key);
if (keysIndexMeta) {
const keysDec = this.appendUnique(keysIndexMeta, key);
keysDec(targetClass);
}
};
}
set(metadataKey, metadataValue, keysIndexMeta) {
return this.transform(
metadataKey,
() => metadataValue,
keysIndexMeta
);
}
append(metadataKey, metadataValue, keysIndexMeta) {
return this.transform(
metadataKey,
(arr) => {
const newArr = arr || [];
newArr.push(metadataValue);
return newArr;
},
keysIndexMeta
);
}
appendUnique(metadataKey, metadataValue, keysIndexMeta) {
return this.transform(
metadataKey,
(arr) => {
const newArr = arr || [];
if (newArr.includes(metadataValue)) {
return newArr;
}
newArr.push(metadataValue);
return newArr;
},
keysIndexMeta
);
}
concat(metadataKey, metadataValue, keysIndexMeta) {
return this.transform(
metadataKey,
(arr) => (arr || []).concat(metadataValue),
keysIndexMeta
);
}
param(metadataKey, metadataValue, keysIndexMeta) {
return (obj, key, i) => {
const dec = this.transform(
metadataKey,
(arr) => {
const newArr = arr || [];
newArr[i] = metadataValue;
return newArr;
},
keysIndexMeta
);
dec(obj, key);
};
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
MetadataSetter,
Reflector
});
//# sourceMappingURL=index.cjs.map