UNPKG

@essential-projects/metadata

Version:

the core metadata service for using the metadata from inside the domain

116 lines (114 loc) 4.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const loggerhythm_1 = require("loggerhythm"); const merge = require("deepmerge"); require("reflect-metadata"); const logger = new loggerhythm_1.Logger('MetadataProvider'); const GLOBAL_METADATA_STORE_KEY = '$metadata'; let globalVariable; if (typeof global !== 'undefined') { globalVariable = global; } else { globalVariable = window; } function getGlobalMetadataStore() { if (!globalVariable[GLOBAL_METADATA_STORE_KEY]) { globalVariable[GLOBAL_METADATA_STORE_KEY] = {}; } return globalVariable[GLOBAL_METADATA_STORE_KEY]; } if (typeof globalVariable.existingMetadataProvider === 'undefined') { globalVariable.existingMetadataProvider = { getForInstance(metadataKey, namespace, target, targetKey) { if (!isObject(target)) { return undefined; } let result = exports.MetadataProvider.getForOwnInstance(metadataKey, namespace, target, targetKey); if (result === undefined) { result = exports.MetadataProvider.getForInstance(metadataKey, Object.getPrototypeOf(target), targetKey); } return result; }, getForOwnInstance(metadataKey, namespace, target, targetKey) { if (!isObject(target)) { return undefined; } const type = target.constructor.name; let metadataForType = {}; if (type !== 'Object') { metadataForType = exports.MetadataProvider.getForType(metadataKey, namespace, type, targetKey); } const ownMetadata = Reflect.getOwnMetadata(metadataKey, target, targetKey); let result; if (metadataForType === undefined) { result = ownMetadata; } else if (ownMetadata === undefined) { result = metadataForType; } else { result = merge(metadataForType, ownMetadata); } return result; }, getForType(metadataKey, namespace, type, targetKey) { const allTypeMetadata = getGlobalMetadataStore(); const namespaceMetadata = allTypeMetadata[namespace]; if (!namespaceMetadata) { return undefined; } const typeMetadata = namespaceMetadata[type]; if (!typeMetadata) { return undefined; } const typeMetadataForKey = typeMetadata[metadataKey]; if (!typeMetadataForKey) { return undefined; } const typeMetadataForProperty = typeMetadataForKey[targetKey]; return typeMetadataForProperty; }, getAllForType(metadataKey, namespace, type) { const allTypeMetadata = getGlobalMetadataStore(); const namespaceMetadata = allTypeMetadata[namespace]; if (!namespaceMetadata) { return undefined; } const typeMetadata = namespaceMetadata[type]; if (!typeMetadata) { return undefined; } const typeMetadataForKey = typeMetadata[metadataKey]; return typeMetadataForKey; }, setForInstance(metadataKey, metadataValue, target, targetKey) { Reflect.defineMetadata(metadataKey, metadataValue, target, targetKey); }, hasType(namespace, type) { const allTypeMetadata = getGlobalMetadataStore(); return allTypeMetadata[namespace][type] !== undefined; }, setForType(metadataKey, metadataValue, namespace, type, targetKey) { const allTypeMetadata = getGlobalMetadataStore(); if (!allTypeMetadata[namespace]) { allTypeMetadata[namespace] = {}; } if (!allTypeMetadata[namespace][type]) { allTypeMetadata[namespace][type] = {}; } if (!allTypeMetadata[namespace][type][metadataKey]) { allTypeMetadata[namespace][type][metadataKey] = {}; } if (metadataValue && !allTypeMetadata[namespace][type][metadataKey][targetKey]) { allTypeMetadata[namespace][type][metadataKey][targetKey] = {}; } allTypeMetadata[namespace][type][metadataKey][targetKey] = merge(metadataValue, allTypeMetadata[namespace][type][metadataKey][targetKey]); }, }; } function isObject(value) { return value && (typeof value === 'function' || typeof value === 'object'); } exports.MetadataProvider = globalVariable.existingMetadataProvider; //# sourceMappingURL=provider.js.map