How to use the @loopback/context.Context 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 / find-bindings.ts View on Github external
export async function main() {
  const ctx = new Context('request');

  // Add EnglishGreeter for now
  ctx.add(createBindingFromClass(EnglishGreeter, {namespace: 'greeters'}));

  // Add ChineseGreeter
  ctx
    .bind('greeters.ChineseGreeter')
    .toClass(ChineseGreeter)
    .tag('greeter');

  const enlishGreeterBinding = ctx.getBinding('greeters.EnglishGreeter');
  console.log(enlishGreeterBinding.key);

  let possibleEnglishGreeters = ctx.find('*.EnglishGreeter');
  console.log(possibleEnglishGreeters.map(b => b.key));
github strongloop / loopback-next / examples / context / src / interceptor-proxy.ts View on Github external
export async function main() {
  const ctx = new Context('request');
  // Bind request context
  ctx.bind(REQUEST_CONTEXT).to(ctx);

  const binding = createBindingFromClass(TracingInterceptor, {
    key: TRACING_INTERCEPTOR,
  });
  ctx.add(binding);

  let count = 1;
  const reqUuidGenerator: RequestIdGenerator = context =>
    `[${context.name}] ${count++}`;
  ctx.bind(REQUEST_ID_GENERATOR).to(reqUuidGenerator);
  ctx.bind(GREETER).toClass(Greeter);
  ctx
    .bind(CONVERTER)
    .toClass(Converter)
github strongloop / loopback-next / examples / context / src / dependency-injection.ts View on Github external
export async function main() {
  const ctx = new Context('request');

  // Bind greeting service
  ctx.bind(GREETING_SERVICE).toClass(GreetingService);

  // Set the current date to a factory function
  ctx.bind(CURRENT_DATE).toDynamicValue(getCurrentDate);

  // Set the current user to `John` (a constant value)
  ctx.bind('currentUser').to('John');

  // Set the current language to `zh`
  ctx.bind('currentLanguage').to('zh');

  // Add EnglishGreeter for now
  ctx
    .bind('greeters.EnglishGreeter')
github strongloop / loopback-next / examples / context / src / context-chain.ts View on Github external
export async function main() {
  const appCtx = new Context('app');

  // Create a context per request, with `appCtx` as the parent
  const requestCtx = new Context(appCtx, 'request');

  const greeterBinding = appCtx
    .bind('services.Greeter')
    .toClass(Greeter)
    .tag('greeter');

  // Set prefix to `app` at app context level
  appCtx.bind('prefix').to(appCtx.name);

  // Get a greeter from request context
  let greeter = await requestCtx.get(greeterBinding.key);

  // Inherit `prefix` from app context
github strongloop / loopback-next / packages / authentication / src / __tests__ / unit / providers / auth-metadata.provider.unit.ts View on Github external
it('returns undefined if no auth metadata is defined', async () => {
        const context: Context = new Context();
        context
          .bind(CoreBindings.CONTROLLER_CLASS)
          .to(ControllerWithNoMetadata);
        context.bind(CoreBindings.CONTROLLER_METHOD_NAME).to('whoAmI');
        context
          .bind(CoreBindings.CONTROLLER_METHOD_META)
          .toProvider(AuthMetadataProvider);
        const authMetadata = await context.get(
          CoreBindings.CONTROLLER_METHOD_META,
        );
        expect(authMetadata).to.be.undefined();
      });
github strongloop / loopback-next / packages / rest / src / __tests__ / unit / rest.server / rest.server.unit.ts View on Github external
async function givenRequestContext() {
    const app = new Application();
    app.component(RestComponent);
    const server = await app.getServer(RestServer);
    const requestContext = new Context(server);
    requestContext.bind(RestBindings.Http.CONTEXT).to(requestContext);
    return requestContext;
  }
});
github strongloop / loopback-next / packages / service-proxy / src / __tests__ / unit / decorators / service-proxy.decorator.unit.ts View on Github external
before(function() {
    ds = new juggler.DataSource({
      name: 'db',
      connector: MockConnector,
    });

    ctx = new Context();
    ctx.bind('datasources.googleMap').to(ds);
    ctx.bind('controllers.MyController').toClass(MyController);
  });
github strongloop / loopback-next / packages / authorization / src / __tests__ / acceptance / authorization.options.acceptance.ts View on Github external
function givenRequestContext() {
    app = new Application();
    reqCtx = new Context(app);
    reqCtx
      .bind(SecurityBindings.USER)
      .to({[securityId]: 'user-01', name: 'user-01'});
    controller = new OrderController();
  }
github strongloop / loopback-next / examples / context / src / custom-inject-decorator.ts View on Github external
export async function main() {
  const ctx = new Context('invocation-context');
  ctx.bind(CURRENT_USER).to('John');
  ctx.bind('greeter').toClass(Greeter);
  const greeter = await ctx.get('greeter');
  console.log(greeter.hello());
}
github strongloop / loopback-next / examples / context / src / parameterized-decoration.ts View on Github external
export async function main() {
  const ctx = new Context();

  ctx.bind('name1').to('John');
  ctx.bind('name2').to('Jane');

  const class1 = createClassWithDecoration('name1', {tags: {prefix: '1'}});
  const binding1 = createBindingFromClass(class1, {key: 'greeter1'});
  ctx.add(binding1);
  console.log('1:', binding1.tagMap);

  const class2 = createClassWithDecoration('name2', {tags: {prefix: '2'}});
  const binding2 = createBindingFromClass(class2, {key: 'greeter2'});
  ctx.add(binding2);
  console.log('2:', binding2.tagMap);

  const greeting1 = await ctx.get('greeter1');
  console.log('1: %s', greeting1.hello());