All files / metadata/src/utilities/metadataProvider tagModules.ts

71.05% Statements 27/38
52.17% Branches 12/23
100% Functions 4/4
71.05% Lines 27/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80            128x   128x               384x       7680x     7680x       7680x 7680x         7680x 1689x 1689x 1689x 1059x     630x 630x 6165x 6165x 2500x                         2500x     630x     7680x   7680x     128x     384x 7680x 7680x 7680x   7680x      
import { addTypedProvider, type TypedProvider } from '../../metaData';
import { mapModuleTags } from '../Tags';
import { dataLookup, DATA_PRIORITY, instanceLookup } from './dataLookup';
import { makeArrayLike } from './makeArrayLike';
import { metadataLog } from '../logging';
 
const log = metadataLog.getLogger('tagModules');
 
const mapModules = new Map<string, TypedProvider>();
 
/**
 * Clears the tag modules cache. Call after removeAllProviders() so that
 * registerTagModules() can re-register providers (tagModules(module) will
 * return a function instead of undefined).
 */
export function clearTagModules(): void {
  mapModules.clear();
}
 
export function tagModules(module: string, dataLookupName = 'instance') {
  Iif (!mapModuleTags.has(module)) {
    throw new Error(`No module found: ${module}`);
  }
  Iif (mapModules.has(module)) {
    return mapModules.get(module);
  }
 
  if (dataLookupName === 'instance') {
    addTypedProvider(module, instanceLookup, { priority: 25_000 });
  } else Eif (dataLookupName) {
    addTypedProvider(module, dataLookup(dataLookupName), { priority: 25_000 });
  }
 
  const moduleProvider = (next, query, data, options) => {
    const keys = mapModuleTags.get(module);
    const destName = options?.destName || 'lowerName';
    if (!data) {
      return next(query, data, options);
    }
 
    const result = {};
    for (const key of keys) {
      let value = data[key.name];
      if (value !== undefined) {
        Iif (mapModules.has(key.name)) {
          log.debug('Getting nested module', key.name);
          const newValue = [];
          for (const entry of value) {
            if (!entry) {
              continue;
            }
            newValue.push(
              mapModules.get(key.name)(null, query, entry, options?.[key.name])
            );
          }
          value = newValue.length === 1 ? makeArrayLike(newValue[0]) : newValue;
        }
        result[key[destName]] = value;
      }
    }
    return result;
  };
 
  mapModules.set(module, moduleProvider);
 
  return moduleProvider as TypedProvider;
}
 
export const MODULE_PRIORITY = { priority: -1_000 };
 
export function registerTagModules() {
  for (const module of mapModuleTags.keys()) {
    const providerFn = tagModules(module);
    Eif (providerFn) {
      addTypedProvider(module, providerFn, MODULE_PRIORITY);
    }
    addTypedProvider(module, instanceLookup, DATA_PRIORITY);
  }
}