npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

loopback-setup-remote-methods-mixin

v1.2.1

Published

Mixin for Loopback, to easily disable remote methods and setup new ones from the model configuration file.

Downloads

710

Readme

loopback-setup-remote-methods-mixin

Mixins for Loopback, to easily disable or setup new remote methods from the model definition file. It works with both Loopback 2 and 3.

Installation

npm install --save loopback-setup-remote-methods-mixin

Usage

As a mixin (recommended)

First, modify your server/model-config.json to include the path to this module:

For LB3, mixins should be declared in the _meta.mixins property. For LB2, mixins should be declared in the mixins property.

{
 "_meta": {
   "mixins": [
      "loopback/common/mixins",
      "loopback/server/mixins",
      "../common/mixins",
      "./mixins",
      "../node_modules/loopback-setup-remote-methods-mixin"
    ]
  }
}

Then you can use the mixin from your model definition files:

...
  "mixins": {
    "SetupRemoteMethods": {
      "disableAllExcept": ["create", "prototype.updateAttributes"],
      "addFromFile": "./common/models/mymodel-remotes.js"
    }
  }
...

List of default remote methods

http://loopback.io/doc/en/lb3/Exposing-models-over-REST.html#predefined-remote-methods

Options

disable

Disable the defined remote methods. For example, to disable the create and the updateAttributes remote methods:

  "mixins": {
    "SetupRemoteMethods": {
      "disable": ["create", "prototype.updateAttributes"]
    }
  }

Allows wildcards with * (not fully tested though)

disableAllExcept

Disable all the remote methods, except the defined on the options. For example, to disable all except create and updateAttributes remote methods:

  "mixins": {
    "SetupRemoteMethods": {
      "disableAllExcept": ["create", "prototype.updateAttributes"]
    }
  }

Allows wildcards with * (not fully tested though)

relations

Allows to setup some options per relation. Currently only disableAllExcept is supported.

  "mixins": {
    "SetupRemoteMethods": {
      "relations": {
        "customer": {
          "disableAllExcept": ["create", "get"]          
        }
      }
    }
  }

ignoreACL

Default value: false

This option works together with disable and disableAllExcept. If true, it forces to disable the methods, even if they are configured in the ACL to be allowed.

  "mixins": {
    "SetupRemoteMethods": {
      "ignoreACL": true,
      "disableAllExcept": ["create", "prototype.updateAttributes"]
    }
  }

add

It adds new remote methods to the model. This is similar to what's planned for the Methods section. (Which is not yet implemented. This option will be deprecated when that happens.)

Add using JSON

  "mixins": {
    "SetupRemoteMethods": {
      "add": {
        "sayHello": {
          "accepts": {"arg": "msg", "type": "string"},
          "returns": {"arg": "greeting", "type": "string"}
        },
        "sayBye": {
          "accepts": {"arg": "msg", "type": "string"},
          "returns": {"arg": "farewell", "type": "string"}
        }
      }
    }
  }

Then you can have the methods implemented in your model as usual:

const Promise = require('bluebird');

module.exports = function(Employee) {
  Employee.sayHello = msg => {
    return new Promise((resolve) => {
      resolve('Hello ' + msg);
    });
  };
  
  Employee.sayBye = msg => {
    return new Promise((resolve) => {
      resolve('Goodbye ' + msg);
    });
  };
};

Add using JS in the model

Deprecated, use addFromFile instead.

You can define the name of the methods in the model that will provide the remote method definition.

  "mixins": {
    "SetupRemoteMethods": {
      "add": {
        "greet": "remotesDefinitions.greet"
      }
    }
  }

In order to avoid having this definition in the model file, we can have the definition on a different file, let's say we name it remote-methods.js

module.exports = {
  greet
};

function greet() {
  return {
    accepts: {arg: 'msg', type: 'string'},
    returns: {arg: 'greeting', type: 'string'},
  };
}

Then, on your model, you would need to have something like:

module.exports = function(Employee) {
  // Include the definitions in the model for the mixin to be able to get them
  Employee.remotesDefinitions = require('./remote-methods');

  // The implementation of your remote method  
  Employee.greet = msg => {
    return new Promise((resolve) => {
      resolve('Hello ' + msg);
    });
  };
};

addFromFile

There are some cases that you might want to call a method to return the definition. This happens for example if one of the properties should be calculated.

You can add all the methods from the file:

  "mixins": {
    "SetupRemoteMethods": {
      "addFromFile": "./common/models/employee-remotes.js"
    }
  }

Or just some of them:

  "mixins": {
    "SetupRemoteMethods": {
      "addFromFile": {
        "filename": "./common/models/employee-remotes.js",
        "methods": [ "sayHello" ]
      }
    }
  }

The path of the file should be relative to process.cwd().

The file (employee-remotes.js in our example) would contain the remotes definitions:

module.exports = {
  sayHello,
  sayBye
};

function sayHello() {
  return {
    accepts: {arg: 'msg', type: 'string'},
    returns: {arg: 'greeting', type: 'string'},
  };
}

function sayBye() {
  return {
    accepts: {arg: 'msg', type: 'string'},
    returns: {arg: 'farewell', type: 'string'},
  };
}

Then, in the model, you will only need the implementation:

module.exports = function(Employee) {
  Employee.sayHello = msg => {
    return new Promise((resolve) => {
      resolve('Hello ' + msg);
    });
  };
  
  Employee.sayBye = msg => {
    return new Promise((resolve) => {
      resolve('Goodbye ' + msg);
    });
  };
};

Credits

Disabling remote methods feature is based on the discussion at https://github.com/strongloop/loopback/issues/651.

The code for disable, disableAllExcept and ignoreACL options is based on this gist from ebarault, which was based on another gist from drmikecrowe.

Module created by c3s4r for Devsu.