All files / lib/definitions/helpers ArgResolver.ts

95.23% Statements 20/21
82.35% Branches 14/17
100% Functions 5/5
95% Lines 19/20

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    12x   12x   206x       37x       67x 67x 27x 22x     27x 27x 37x   37x 2x   35x     26x       22x 32x 5x   27x        
import { IResolver } from "../../interfaces/IResolver";
import { IInjectParamMeta } from "../../decorators/Inject";
import { Keys } from "../../Keys";
 
export class ArgResolver {
 
    constructor(private readonly resolver: IResolver) {
    }
 
    paramIsNotRequired(param: IInjectParamMeta): boolean {
        return !this.resolver.hasKeyInDefinition(param.key) && !param?.isRequired;
    }
 
    async resolveArguments(meta: any, context: any, decoratorKey: symbol | string): Promise<any[]> {
        let args: IInjectParamMeta[] = meta[decoratorKey];
        if (!args) return [];
        if (context) {
            args = this.mapContextToArgs(args, context);
        }
 
        const resolvedArgs = [];
        for (const arg of args) {
            Iif (!arg && this.resolver.options.enableAutoCreate) {
                resolvedArgs.push(Keys.OTHER_INJECTION_REQUIRED);
            } else if (this.paramIsNotRequired(arg)) {
                resolvedArgs.push(undefined);
            } else {
                resolvedArgs.push(typeof arg.key === "string" ? await this.resolver.resolve(arg.key, arg.isRequired) : await this.resolver.resolveByType(arg.key));
            }
        }
        return resolvedArgs;
    }
 
    mapContextToArgs(args: IInjectParamMeta[], ctx: any): IInjectParamMeta[] {
        return args.map((arg: IInjectParamMeta) => {
            if (ctx[arg.key]) {
                return {key: ctx[arg.key], isRequired: arg.isRequired, index: arg.index};
            }
            return arg;
        });
    }
}