How to use the @loopback/rest.api function in @loopback/rest

To help you get started, we’ve selected a few @loopback/rest 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 / example-codehub / src / controllers / user-controller.ts View on Github external
import {api, HttpErrors} from '@loopback/rest';

// Load OpenAPI specification for this controller
import {def} from './user-controller.api';

// Initially, bajtos was proposing
//   import {api} from '@loopback/controller-decorators';
// in order to allow 3rd party components to provide custom controllers
// while not depending on full loopback core
// After discussion with @ritch, we decided this is preliminary optimization
// that can be left for later

import {inject} from '@loopback/core';

// Notice that the controler is not required to extend any Controller base class
@api(def)
export class UserController {
  // Remote methods are returning a Promise and should be implemented as
  // async functions
  // This is required because most real world methods need to talk to
  // other services, which always takes more than a single tick of event loop
  //
  // This method can be called from other controllers/method the following way:
  //    const user: UserResponse =
  //      await userController.getUserByUsername('bajtos');
  //    console.log(user.email);
  public async getUserByUsername(username: string): Promise {
    return new UserResponse({name: username});
  }

  public async getAuthenticatedUser(
    @inject('userId') userId: number,
github strongloop / loopback4-example-microservices / services / customer / src / controllers / customer.controller.ts View on Github external
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: loopback4-example-microservices
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {api} from '@loopback/rest';
import {def} from './customer.controller.api';
import {repository} from '@loopback/repository';
import {Customer} from '../models';
import {CustomerRepository} from '../repositories/';
import {Filter} from '@loopback/repository/src/query';

@api(def)
export class CustomerController {
  constructor(
    @repository('CustomerRepository')
    private customerRepository: CustomerRepository,
  ) {}

  async getCustomer(id: string): Promise {
    return await this.customerRepository.findById(id);
  }

  async getCustomers(filter?: Filter | string): Promise {
    if (typeof filter === 'string') {
      filter = JSON.parse(filter) as Filter;
    }
    return await this.customerRepository.find(filter);
  }
github strongloop / loopback4-example-microservices / services / account / src / controllers / account.controller.ts View on Github external
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: loopback4-example-microservices
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {api} from '@loopback/rest';
import {def} from './account.controller.api';
import {AccountRepository} from '../repositories';
import {repository} from '@loopback/repository';
import {Account} from '../models';
import {Filter, Where} from '@loopback/repository/src/query';

@api(def)
export class AccountController {
  constructor(
    @repository('AccountRepository')
    private accountRepository: AccountRepository,
  ) {}

  async getAccount(id: string): Promise {
    return await this.accountRepository.findById(id);
  }

  async getAccounts(filter?: Filter | string): Promise {
    if (typeof filter === 'string') {
      filter = JSON.parse(filter) as Filter;
    }
    return await this.accountRepository.find(filter);
  }
github strongloop / loopback4-example-microservices / services / todo-legacy / controllers / todo-controller.ts View on Github external
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
// Node module: loopback4-example-microservices
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {api} from '@loopback/rest';
import {def} from './todo-controller.api';
import {Todo} from '../models/todo';
import {EntityCrudRepository, repository} from '@loopback/repository';

@api(def)
export class TodoController {
  constructor(
    @repository(Todo, 'ds')
    public todoRepository: EntityCrudRepository,
  ) {}

  async get(title?: string): Promise {
    let filter = title ? {where: {title: title}} : {};
    return await this.todoRepository.find(filter);
  }

  async getById(id: number): Promise {
    return await this.todoRepository.find({where: {id: id}});
  }

  async create(body: Object) {
github strongloop / loopback4-example-microservices / services / facade / src / controllers / account.management.controller.ts View on Github external
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: loopback4-example-microservices
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {api} from '@loopback/rest';
import {accountManagementDefinition} from './account.management.controller.api';
import {
  AccountRepository,
  CustomerRepository,
  TransactionRepository,
} from '../repositories';
/* tslint:disable no-any */

@api(accountManagementDefinition)
export class AccountController {
  accountRepository: AccountRepository;
  customerRepository: CustomerRepository;
  transactionRepository: TransactionRepository;

  constructor() {
    this.accountRepository = new AccountRepository();
    this.customerRepository = new CustomerRepository();
    this.transactionRepository = new TransactionRepository();
  }

  async getSummary(accountNumber: string): Promise {
    const account = await this.accountRepository.find(accountNumber);
    const customer = await this.customerRepository.find(account.customerNumber);
    const transaction = await this.transactionRepository.find(accountNumber);
github strongloop / loopback4-example-microservices / services / transaction / src / controllers / transaction.controller.ts View on Github external
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: loopback4-example-microservices
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {api} from '@loopback/rest';
import {def} from './transaction.controller.api';
import {TransactionRepository} from '../repositories';
import {Transaction} from '../models';
import {repository} from '@loopback/repository';
import {Filter} from '@loopback/repository/src/query';

@api(def)
export class TransactionController {
  constructor(
    @repository('TransactionRepository')
    private transactionRepository: TransactionRepository,
  ) {}

  async getTransactions(filter?: Filter | string): Promise {
    if (typeof filter === 'string') {
      filter = JSON.parse(filter) as Filter;
    }

    return await this.transactionRepository.find(filter);
  }
}
github strongloop / loopback4-example-microservices / services / account-without-juggler / controllers / AccountController.ts View on Github external
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
// Node module: loopback4-example-microservices
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {api} from '@loopback/rest';
import {def} from './AccountController.api';
import {AccountRepository} from '../repositories/account';
import {inject} from '@loopback/context';
import {Account} from '../repositories/account/models/Account';

@api(def)
export class AccountController {
  constructor(
    @inject('repositories.account') private repository: AccountRepository,
  ) {}

  //fixme figure out how to use Filter interface
  //fixme filter is string even though swagger spec
  //defines it as object type
  async getAccount(filter: string): Promise {
    return await this.repository.find(JSON.parse(filter));
  }

  async createAccount(accountInstance: Object): Promise {
    return await this.repository.create(accountInstance);
  }
github strongloop / loopback-next / packages / example-codehub / src / controllers / health-controller.ts View on Github external
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
// Node module: @loopback/example-codehub
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {def} from './health-controller.api';
import {api} from '@loopback/rest';
import {inject} from '@loopback/core';

@api(def)
export class HealthController {
  constructor(@inject('app.info') private _info: HealthResponse) {
    this._info = {uptime: 29389384};
  }

  async getHealth(): Promise {
    return Promise.resolve(this._info);
  }
}

export interface HealthResponse {
  uptime: number;
}
github strongloop / loopback-next / packages / rest-crud / src / crud-rest.controller.ts View on Github external
T extends Entity,
  IdType,
  IdName extends keyof T,
  Relations extends object = {}
>(
  modelCtor: typeof Entity & {prototype: T & {[key in IdName]: IdType}},
  options: CrudRestControllerOptions,
): CrudRestControllerCtor {
  const modelName = modelCtor.name;
  const idPathParam: ParameterObject = {
    name: 'id',
    in: 'path',
    schema: getIdSchema(modelCtor),
  };

  @api({basePath: options.basePath, paths: {}})
  class CrudRestControllerImpl
    implements CrudRestController {
    constructor(
      public readonly repository: EntityCrudRepository,
    ) {}

    @post('/', {
      ...response.model(200, `${modelName} instance created`, modelCtor),
    })
    async create(
      @body(modelCtor, {
        title: `New${modelName}`,
        exclude: modelCtor.getIdProperties() as (keyof T)[],
      })
      data: Omit,
    ): Promise {
github strongloop / loopback4-example-microservices / services / account / controllers / AccountController.ts View on Github external
import {api} from '@loopback/rest';
import {def} from './AccountController.api';
import {AccountRepository} from '../repositories/account';

@api(def)
export class AccountController {
  repository: AccountRepository;

  constructor() {
    this.repository = new AccountRepository();
  }

  async getAccount(filter) {
    return await this.repository.find(JSON.parse(filter));
  }

  async createAccount(accountInstance) {
    return await this.repository.create(accountInstance);
  }

  async updateAccount(where, data) {