|
| 1 | +import type { Dependencies, Expressions } from '../../ioc'; |
| 2 | +import type { FieldMetadata } from '../types'; |
| 3 | +import { ComponentConstants } from '../../ioc'; |
| 4 | +import { getTypedMetadata, getOwnTypedMetadata } from '../../utils'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Discovers all fields with @Inject decorator in a component |
| 8 | + * Traverses prototype chain to support inheritance |
| 9 | + * |
| 10 | + * @param instance - Component instance to inspect |
| 11 | + * @returns Array of field metadata containing field names, service names, and expressions |
| 12 | + * |
| 13 | + * @example |
| 14 | + * ```typescript |
| 15 | + * const fields = discoverInjectedFields(new AuthService()); |
| 16 | + * // Returns: [ |
| 17 | + * // { fieldName: 'userService', serviceName: 'UserService', expression: undefined }, |
| 18 | + * // { fieldName: 'loginService', serviceName: 'LoginService', expression: (s) => s.login() } |
| 19 | + * // ] |
| 20 | + * ``` |
| 21 | + */ |
| 22 | +export function discoverInjectedFields(instance: any): FieldMetadata[] { |
| 23 | + const fields: FieldMetadata[] = []; |
| 24 | + const processedFields = new Set<string>(); |
| 25 | + |
| 26 | + // Traverse prototype chain (similar to how Container does it) |
| 27 | + let currentPrototype = Object.getPrototypeOf(instance); |
| 28 | + |
| 29 | + while (currentPrototype && currentPrototype !== Object.prototype) { |
| 30 | + const constructor = currentPrototype.constructor; |
| 31 | + |
| 32 | + // Get dependencies metadata for this level |
| 33 | + const dependencies = getOwnTypedMetadata<Dependencies>(ComponentConstants.DependencyKey, constructor); |
| 34 | + |
| 35 | + // Get expressions metadata for this level |
| 36 | + const expressions = getOwnTypedMetadata<Expressions>(ComponentConstants.ExpressionKey, constructor); |
| 37 | + |
| 38 | + if (dependencies) { |
| 39 | + // Process each dependency field |
| 40 | + for (const [fieldName, serviceName] of Object.entries(dependencies)) { |
| 41 | + // Skip if already processed (child class overrides) |
| 42 | + if (processedFields.has(fieldName)) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + processedFields.add(fieldName); |
| 47 | + |
| 48 | + fields.push({ |
| 49 | + fieldName, |
| 50 | + serviceName, |
| 51 | + expression: expressions?.[fieldName], |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Move up the prototype chain |
| 57 | + currentPrototype = Object.getPrototypeOf(currentPrototype); |
| 58 | + } |
| 59 | + |
| 60 | + return fields; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Checks if a component has any injected dependencies |
| 65 | + * |
| 66 | + * @param ComponentClass - Component class to check |
| 67 | + * @returns True if component has @Inject decorated fields |
| 68 | + * |
| 69 | + * @example |
| 70 | + * ```typescript |
| 71 | + * if (hasInjectedFields(AuthService)) { |
| 72 | + * console.log('AuthService has dependencies'); |
| 73 | + * } |
| 74 | + * ``` |
| 75 | + */ |
| 76 | +export function hasInjectedFields(ComponentClass: new (...args: any[]) => any): boolean { |
| 77 | + const dependencies = getTypedMetadata<Dependencies>(ComponentConstants.DependencyKey, ComponentClass); |
| 78 | + return dependencies !== undefined && Object.keys(dependencies).length > 0; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Gets the service name for a specific field |
| 83 | + * |
| 84 | + * @param ComponentClass - Component class |
| 85 | + * @param fieldName - Name of the field |
| 86 | + * @returns Service name if field is injected, undefined otherwise |
| 87 | + * |
| 88 | + * @internal |
| 89 | + */ |
| 90 | +export function getFieldServiceName( |
| 91 | + ComponentClass: new (...args: any[]) => any, |
| 92 | + fieldName: string, |
| 93 | +): string | undefined { |
| 94 | + const dependencies = getTypedMetadata<Dependencies>(ComponentConstants.DependencyKey, ComponentClass); |
| 95 | + return dependencies?.[fieldName]; |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Gets the expression function for a specific field |
| 100 | + * |
| 101 | + * @param ComponentClass - Component class |
| 102 | + * @param fieldName - Name of the field |
| 103 | + * @returns Expression function if defined, undefined otherwise |
| 104 | + * |
| 105 | + * @internal |
| 106 | + */ |
| 107 | +export function getFieldExpression( |
| 108 | + ComponentClass: new (...args: any[]) => any, |
| 109 | + fieldName: string, |
| 110 | +): ((service: any) => any) | undefined { |
| 111 | + const expressions = getTypedMetadata<Expressions>(ComponentConstants.ExpressionKey, ComponentClass); |
| 112 | + return expressions?.[fieldName]; |
| 113 | +} |
0 commit comments