How to use the @loopback/core.CoreBindings.CONTROLLER_CURRENT function in @loopback/core

To help you get started, we’ve selected a few @loopback/core 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 / __tests__ / unit / router / controller-route.unit.ts View on Github external
it('binds current controller to the request context as singleton', async () => {
      const controller1 = await requestCtx.get(CoreBindings.CONTROLLER_CURRENT);
      expect(controller1).instanceOf(MyController);

      const controller2 = await requestCtx.get(CoreBindings.CONTROLLER_CURRENT);
      expect(controller2).to.be.exactly(controller1);

      const childCtx = new Context(requestCtx);
      const controller3 = await childCtx.get(CoreBindings.CONTROLLER_CURRENT);
      expect(controller3).to.be.exactly(controller1);

      await expect(
        appCtx.get(CoreBindings.CONTROLLER_CURRENT),
      ).to.be.rejectedWith(/The key .+ is not bound to any value/);
    });
github strongloop / loopback-next / packages / rest / src / __tests__ / unit / router / controller-route.unit.ts View on Github external
it('adds bindings to the request context', async () => {
      expect(requestCtx.contains(CoreBindings.CONTROLLER_CURRENT));
      expect(
        requestCtx.getBinding(CoreBindings.CONTROLLER_CURRENT).scope,
      ).to.equal(BindingScope.SINGLETON);
      expect(await requestCtx.get(CoreBindings.CONTROLLER_CLASS)).to.equal(
        MyController,
      );
      expect(
        await requestCtx.get(CoreBindings.CONTROLLER_METHOD_NAME),
      ).to.equal('greet');
      expect(await requestCtx.get(RestBindings.OPERATION_SPEC_CURRENT)).to.eql({
        'x-controller-name': 'MyController',
        'x-operation-name': 'greet',
        tags: ['MyController'],
        responses: {'200': {description: 'An undocumented response body.'}},
      });
    });
github strongloop / loopback-next / packages / rest / src / router / controller-route.ts View on Github external
updateBindings(requestContext: Context) {
    /*
     * Bind current controller to the request context in `SINGLETON` scope.
     * Within the same request, we always get the same instance of the
     * current controller when `requestContext.get(CoreBindings.CONTROLLER_CURRENT)`
     * is invoked.
     *
     * Please note the controller class itself can be bound to other scopes,
     * such as SINGLETON or TRANSIENT (default) in the application or server
     * context.
     *
     * - SINGLETON: all requests share the same instance of a given controller
     * - TRANSIENT: each request has its own instance of a given controller
     */
    requestContext
      .bind(CoreBindings.CONTROLLER_CURRENT)
      .toDynamicValue(() => this._controllerFactory(requestContext))
      .inScope(BindingScope.SINGLETON);
    requestContext.bind(CoreBindings.CONTROLLER_CLASS).to(this._controllerCtor);
    requestContext
      .bind(CoreBindings.CONTROLLER_METHOD_NAME)
      .to(this._methodName);
    requestContext.bind(RestBindings.OPERATION_SPEC_CURRENT).to(this.spec);
  }