0.2.0 • Published 5 years ago

loopback-decorators v0.2.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

loopback-decorators

Add remote method decorators to loopback.

Installation

Requirements

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

npm install loopback-decorators

Special Providers

Loopback decorators provides several special provider tokens for common remote method dependencies. These are:

TokenProvides
$appThe loopback application (similar to require('./server'))
$instanceThe model instance for non-static remote methods
$modelThe model being operated on (for both static and non-static methods)
$ctxThe loopback http remoting context
$reqThe loopback http request
$resThe loopback http response
$headersThe incoming http request headers
$ctx.req.headers.to.somethingPull arbitrary value off ctx
$optionsFromRequestSee using options from request
$optionsAlias for $optionsFromRequest
^SomeModelNameAny model registered in your loopback model registry

You can also setup your own providers (even with their own dependencies) as follows (see examples for further info):

{
  provide: 'tokenName',
  useFactory: (list, of, deps) => someValue,
  deps: ['$app', 'someDep', '^MyModel']
}

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 })

Proxy requests from one model to another

In some cases - you want to have your public API use strict ACLs but keep your internal models private yet still open to Admin type applications. In these cases, proxyFor allows you to proxy certain remote methods onto another model:

import { RemoteMethodModule } from 'loopback-decorators';

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

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

The above example will allow your ModelAPI model to use find and findById as API endpoints which will talk to ModelInternal under the hood.

IMPORTANT NOTE The api model (ModelAPI in this case) MUST extend PersistedModel for the above to work as find and findById are methods of PersistedModel and not Model.

License

MIT

0.2.0

5 years ago

0.1.0

5 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago