How to use the @loopback/context.instantiateClass function in @loopback/context

To help you get started, we’ve selected a few @loopback/context examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github strongloop / loopback-next / packages / rest / src / router / controller-route.ts View on Github external
return async ctx => {
    // By default, we get an instance of the controller from the context
    // using `controllers.` as the key
    let inst = await ctx.get(`controllers.${controllerCtor.name}`, {
      optional: true,
    });
    if (inst === undefined) {
      inst = await instantiateClass(controllerCtor, ctx);
    }
    return inst;
  };
}
github strongloop / loopback-next / examples / context / src / injection-without-binding.ts View on Github external
async function sayHello(ctx: Context) {
  const greeter = await instantiateClass(Greeter, ctx);
  console.log(greeter.hello());
}
github strongloop / loopback-next / packages / rest / src / body-parsers / body-parser.ts View on Github external
request: Request,
  ) {
    if (typeof customParser === 'string') {
      const parser = this.parsers.find(
        p =>
          p.name === customParser ||
          p.name === builtinParsers.mapping[customParser],
      );
      if (parser) {
        debug('Using custom parser %s', customParser);
        return parser.parse(request);
      }
    } else if (typeof customParser === 'function') {
      if (isBodyParserClass(customParser)) {
        debug('Using custom parser class %s', customParser.name);
        const parser = await instantiateClass(
          customParser as Constructor,
          this.ctx!,
        );
        return parser.parse(request);
      } else {
        debug('Using custom parser function %s', customParser.name);
        return customParser(request);
      }
    }
    throw new Error('Custom parser not found: ' + customParser);
  }
}