How to use the @loopback/context.invokeMethod 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 / examples / context / src / dependency-injection.ts View on Github external
// Now add ChineseGreeter
  ctx
    .bind('greeters.ChineseGreeter')
    .toClass(ChineseGreeter)
    .tag('greeter');

  greeting = await invokeMethod(greetingService, 'greet', ctx);
  console.log(greeting);

  // Change the current language to `en`
  ctx.bind('currentLanguage').to('en');

  // Change the current user to `Jane`
  ctx.bind('currentUser').to('Jane');

  greeting = await invokeMethod(greetingService, 'greet', ctx);
  console.log(greeting);
}
github strongloop / loopback-next / packages / rest / src / router / controller-route.ts View on Github external
async invokeHandler(
    requestContext: Context,
    args: OperationArgs,
  ): Promise {
    const controller = await requestContext.get(
      'controller.current',
    );
    if (typeof controller[this._methodName] !== 'function') {
      throw new HttpErrors.NotFound(
        `Controller method not found: ${this.describe()}`,
      );
    }
    // Invoke the method with dependency injection
    return invokeMethod(controller, this._methodName, requestContext, args, {
      source: new RouteSource(this),
    });
  }
}
github strongloop / loopback-next / packages / authorization / src / __tests__ / acceptance / authorization-casbin.acceptance.ts View on Github external
it('denies cancelOrder for bob', async () => {
    givenRequestContext({[securityId]: 'bob', name: 'bob'});
    const orderId = await controller.placeOrder({
      customerId: 'bob',
      productId: 'product-a',
      quantity: 10,
      price: 320,
    });
    const result = invokeMethod(controller, 'cancelOrder', reqCtx, [orderId]);
    await expect(result).to.be.rejectedWith('Access denied');
    expect(events).to.containEql('OrderController.prototype.cancelOrder');
  });
github strongloop / loopback-next / packages / authorization / src / __tests__ / acceptance / authorization-casbin.acceptance.ts View on Github external
it('allows cancelOrder for customer_service', async () => {
    givenRequestContext({
      [securityId]: 'customer_service',
      name: 'customer_service',
    });
    const orderId = await controller.placeOrder({
      customerId: 'bob',
      productId: 'product-a',
      quantity: 10,
      price: 320,
    });
    const result = await invokeMethod(controller, 'cancelOrder', reqCtx, [
      orderId,
    ]);
    expect(result).to.be.true();
    expect(events).to.containEql('OrderController.prototype.cancelOrder');
  });
github strongloop / loopback-next / packages / authorization / src / __tests__ / acceptance / authorization.acceptance.ts View on Github external
it('denies cancelOrder for regular user', async () => {
    const result = invokeMethod(controller, 'cancelOrder', reqCtx, [
      'order-01',
    ]);
    await expect(result).to.be.rejectedWith('Access denied');
    expect(events).to.containEql('OrderController.prototype.cancelOrder');
  });
github strongloop / loopback-next / packages / authorization / src / __tests__ / acceptance / authorization.acceptance.ts View on Github external
it('allows placeOrder for everyone', async () => {
    const orderId = await invokeMethod(controller, 'placeOrder', reqCtx, [
      {
        customerId: 'customer-01',
        productId: 'product-a',
        quantity: 10,
        price: 320,
      },
    ]);
    expect(orderId).to.eql('order-1');
    expect(events).to.containEql('OrderController.prototype.placeOrder');
  });
github strongloop / loopback-next / packages / authorization / src / __tests__ / acceptance / authorization.options.acceptance.ts View on Github external
async function placeOrder() {
    let finalDecision = Deny;
    try {
      await invokeMethod(controller, 'placeOrder', reqCtx, ['prod-01', 10]);
      finalDecision = Allow;
    } catch (err) {
      finalDecision = Deny;
    }
    return finalDecision;
  }
github strongloop / loopback-next / packages / authorization / src / __tests__ / acceptance / authorization.options.acceptance.ts View on Github external
async function cancelOrder() {
    let finalDecision = Deny;
    try {
      await invokeMethod(controller, 'cancelOrder', reqCtx, ['order-01']);
      finalDecision = Allow;
    } catch (err) {
      finalDecision = Deny;
    }
    return finalDecision;
  }
github raymondfeng / loopback4-example-websocket / src / websocket-controller-factory.ts View on Github external
regexpEventHandlers.set(regexps, async (...args: unknown[]) => {
            await invokeMethod(this.controller, m, this.ctx, args);
          });
        }