How to use @loopback/core - 10 common examples

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 / rest.server / rest.server.unit.ts View on Github external
it('honors expressSettings', () => {
        const app = new Application();
        const server = new TestRestServer(app, {
          expressSettings: {
            'x-powered-by': false,
            env: 'production',
          },
        });
        const expressApp = server.expressApp;
        expect(expressApp.get('x-powered-by')).to.equal(false);
        expect(expressApp.get('env')).to.equal('production');
        // `extended` is the default setting by Express
        expect(expressApp.get('query parser')).to.equal('extended');
        expect(expressApp.get('not set')).to.equal(undefined);
      });
github strongloop / loopback-next / examples / greeter-extension / src / __tests__ / acceptance / greeter-extension.acceptance.ts View on Github external
it('honors a newly added/removed greeter binding', async () => {
    /**
     * A greeter implementation for French
     */
    @bind(asGreeter)
    class FrenchGreeter implements Greeter {
      language = 'fr';

      greet(name: string) {
        return `Bonjour, ${name}!`;
      }
    }
    // Add a new greeter for French
    const binding = createBindingFromClass(FrenchGreeter);
    app.add(binding);

    let msg = await greetingService.greet('fr', 'Raymond');
    expect(msg).to.eql('Bonjour, Raymond!');

    // Remove the French greeter
    app.unbind(binding.key);
github strongloop / loopback-next / packages / boot / src / __tests__ / fixtures / bindable-classes.artifact.ts View on Github external
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/boot
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {bind, BindingScope, Provider} from '@loopback/core';

@bind({
  tags: {serviceType: 'local'},
  scope: BindingScope.SINGLETON,
})
export class BindableGreetingService {
  greet(whom = 'world') {
    return Promise.resolve(`Hello ${whom}`);
  }
}

@bind({tags: {serviceType: 'local', name: 'CurrentDate'}})
export class DateProvider implements Provider {
  value(): Promise {
    return Promise.resolve(new Date());
  }
}
github strongloop / loopback-next / packages / boot / src / __tests__ / fixtures / bindable-classes.artifact.ts View on Github external
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {bind, BindingScope, Provider} from '@loopback/core';

@bind({
  tags: {serviceType: 'local'},
  scope: BindingScope.SINGLETON,
})
export class BindableGreetingService {
  greet(whom = 'world') {
    return Promise.resolve(`Hello ${whom}`);
  }
}

@bind({tags: {serviceType: 'local', name: 'CurrentDate'}})
export class DateProvider implements Provider {
  value(): Promise {
    return Promise.resolve(new Date());
  }
}

export class NotBindableGreetingService {
  greet(whom = 'world') {
    return Promise.resolve(`Hello ${whom}`);
  }
}

export class NotBindableDateProvider implements Provider {
  value(): Promise {
    return Promise.resolve(new Date());
  }
github strongloop / loopback-next / packages / boot / src / __tests__ / fixtures / bindable-classes.artifact.ts View on Github external
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/boot
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {bind, BindingScope, Provider} from '@loopback/core';

@bind({
  tags: {serviceType: 'local'},
  scope: BindingScope.SINGLETON,
})
export class BindableGreetingService {
  greet(whom = 'world') {
    return Promise.resolve(`Hello ${whom}`);
  }
}

@bind({tags: {serviceType: 'local', name: 'CurrentDate'}})
export class DateProvider implements Provider {
  value(): Promise {
    return Promise.resolve(new Date());
  }
}

export class NotBindableGreetingService {
  greet(whom = 'world') {
github strongloop / loopback-next / examples / greeting-app / src / caching-service.ts View on Github external
import debugFactory from 'debug';
import {Message} from './types';
const debug = debugFactory('greeter-extension');

/**
 * Configuration for CachingService
 */
export interface CachingServiceOptions {
  // The time-to-live setting for a cache entry
  ttl: number;
}

/**
 * Message caching service
 */
@bind({scope: BindingScope.SINGLETON})
export class CachingService {
  private timer: NodeJS.Timer;
  private store: Map = new Map();

  constructor(
    @config.view()
    private optionsView: ContextView,
  ) {
    // Use a view so that we can listen on `refresh` events, which are emitted
    // when the configuration binding is updated in the context.
    optionsView.on('refresh', () => {
      debug('Restarting the service as configuration changes...');
      this.restart().catch(err => {
        console.error('Cannot restart the caching service.', err);
        process.exit(1);
      });
github strongloop / loopback-next / packages / v3compat / src / remoting / lb3-model-controller.ts View on Github external
// Node module: @loopback/v3compat
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {inject} from '@loopback/core';
import {OperationArgs, Request, Response, RestBindings} from '@loopback/rest';
import * as debugFactory from 'debug';
import {SharedMethod} from './shared-method';

const debug = debugFactory('loopback:v3compat:rest-adapter');

export class Lb3ModelController {
  @inject(RestBindings.Http.REQUEST)
  protected request: Request;

  @inject(RestBindings.Http.RESPONSE)
  protected response: Response;

  // TODO: a property for strong-remoting's HttpContext

  [key: string]: Function | Request | Response;

  protected buildMethodArguments(
    sharedMethod: SharedMethod,
    inputArgs: OperationArgs,
  ) {
    const finalArgs: OperationArgs = [];
    for (const argSpec of sharedMethod.accepts) {
      const source = argSpec.http && argSpec.http.source;
      switch (source) {
        case 'req':
          finalArgs.push(this.request);
github strongloop / loopback-next / packages / boot / src / booters / service.booter.ts View on Github external
constructor(
    @inject(CoreBindings.APPLICATION_INSTANCE)
    public app: ApplicationWithServices,
    @inject(BootBindings.PROJECT_ROOT) projectRoot: string,
    @config()
    public serviceConfig: ArtifactOptions = {},
  ) {
    super(
      projectRoot,
      // Set Service Booter Options if passed in via bootConfig
      Object.assign({}, ServiceDefaults, serviceConfig),
    );
  }
github strongloop / loopback-next / packages / boot / src / bootstrapper.ts View on Github external
constructor(
    @inject(CoreBindings.APPLICATION_INSTANCE)
    private app: Application & Bootable,
    @inject(BootBindings.PROJECT_ROOT) private projectRoot: string,
    @inject(BootBindings.BOOT_OPTIONS, {optional: true})
    private bootOptions: BootOptions = {},
  ) {
    // Resolve path to projectRoot and re-bind
    this.projectRoot = resolve(this.projectRoot);
    app.bind(BootBindings.PROJECT_ROOT).to(this.projectRoot);

    // This is re-bound for testing reasons where this value may be passed directly
    // and needs to be propagated to the Booters via DI
    app.bind(BootBindings.BOOT_OPTIONS).to(this.bootOptions);
  }
github strongloop / loopback-next / packages / boot / src / booters / lifecyle-observer.booter.ts View on Github external
constructor(
    @inject(CoreBindings.APPLICATION_INSTANCE)
    public app: Application,
    @inject(BootBindings.PROJECT_ROOT) projectRoot: string,
    @config()
    public observerConfig: ArtifactOptions = {},
  ) {
    super(
      projectRoot,
      // Set LifeCycleObserver Booter Options if passed in via bootConfig
      Object.assign({}, LifeCycleObserverDefaults, observerConfig),
    );
  }