/loopback-decorators

Remote method decorators for loopback

Primary LanguageTypeScript

loopback-decorators

Add remote method decorators to loopback.

Installation

Requirements

Within your node project, install the package from npm using:

npm install loopback-decorators

Example Remote Method

A basic controller:

import {RemoteMethod} from 'loopback-decorators';

@RemoteMethod({
  selector: 'getStuff',
  providers: [
    '$app', '$instance', {
      provide: 'customEntity',
      useFactory: function(modelName) {
        return modelName.customEntityÏ
      },
      deps: ['modelName']
    }
  ],
  meta: {
    accessType: 'EXECUTE',
    isStatic: false,
    description: 'Get some stuff from a models remote method',
    accepts: [{
      arg: 'accessToken',
      type: 'any',
      http: function(ctx) {
        return ctx.req.accessToken;
      }
    }],
    returns: {arg: 'res', type: 'stuff', root: true},
    http: {path: '/get-stuff', verb: 'get'}
  }
})
export class GetStuffRemote {
  constructor(public app, public instance, public customEntity) {}
  async onRemote($accessToken) {
    // This is where you put the remote method logic
  }
}

Example ModelEvent

A basic controller:

import {ModelEvent} from 'loopback-decorators';

@ModelEvent({
  <!-- the loopback event -->
  selector: 'create',
  providers: [
    '$app', '$model', '^User'
    }
  ],
})
export class DoSomethingOnCreate {
  constructor(public app, public Model, public User) {}
  async onEvent(inst) {
    // This is where you put the event method logic
  }
}

Setting up your remote module

import {RemoteMethodModule} from 'loopback-decorators';

@RemoteMethodModule({
  remotes: [GetStuffRemote],
  events: [DoSomethingOnCreate],
  proxyFor: 'ModelInternal',
  proxyMethods: ['find', 'findById']
})
export class ModelAPI {
  constructor(public Model: any) {
  }
}

export = function(Model: any) {
  return new ModelAPI(Model);
};

Validating Remote Inputs

@RemoteMethod({
  // ...
  meta: {
    accessType: 'WRITE',
    isStatic: false,
    description: 'Create a thing',
    accepts: [{
      arg: 'payload',
      type: 'RequestModelType',
      http: { source: 'body' }
    }],
    returns: {arg: 'res', type: 'stuff', root: true},
    http: {path: '/things', verb: 'post'}
  }
})
export class GetStuffRemote {
  constructor(public app, public instance, public customEntity) {}
  async onRemote(@Validate payload) {
    // Error will be thrown before `onRemote` is called if the payload is not valid
  }
}

Returning an instance of another model as a response

export class GetStuffRemote {
  constructor(public app, public instance, public customEntity) {}

  @Response('ModelContructorName')
  async onRemote(payload: any) {
    // Result returned will be an instance of app.models.ModelContructorName
  }
}

or, if you are returning an array

export class GetStuffRemote {
  constructor(public app, public instance, public customEntity) {}

  @Response(['ModelContructorName'])
  async onRemote(payload: any) {
    // Result returned will be an array of app.models.ModelContructorName
  }
}

You can also pass the config directly:

  @Response({ responseClass: 'MyResponseClass' })
  // Or, for an array
  @Response({ responseClass: 'MyResponseClass', isMulti: true })

License

MIT