{"id":19737963,"url":"https://github.com/devsu/loopback-setup-remote-methods-mixin","last_synced_at":"2025-04-30T05:30:47.658Z","repository":{"id":92793975,"uuid":"75343503","full_name":"devsu/loopback-setup-remote-methods-mixin","owner":"devsu","description":"Mixins for Loopback, to easily disable remote methods and setup new ones from the model definition file.","archived":false,"fork":false,"pushed_at":"2019-04-10T15:13:05.000Z","size":31,"stargazers_count":24,"open_issues_count":2,"forks_count":8,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-22T00:09:34.914Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devsu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-12-01T23:57:43.000Z","updated_at":"2019-03-05T12:05:36.000Z","dependencies_parsed_at":"2023-03-13T17:25:13.123Z","dependency_job_id":null,"html_url":"https://github.com/devsu/loopback-setup-remote-methods-mixin","commit_stats":{"total_commits":43,"total_committers":8,"mean_commits":5.375,"dds":"0.16279069767441856","last_synced_commit":"753a743a5ddef63ee6e4a0321d60417dca0e658b"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devsu%2Floopback-setup-remote-methods-mixin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devsu%2Floopback-setup-remote-methods-mixin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devsu%2Floopback-setup-remote-methods-mixin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devsu%2Floopback-setup-remote-methods-mixin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devsu","download_url":"https://codeload.github.com/devsu/loopback-setup-remote-methods-mixin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251648491,"owners_count":21621350,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T01:12:53.792Z","updated_at":"2025-04-30T05:30:47.377Z","avatar_url":"https://github.com/devsu.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# loopback-setup-remote-methods-mixin\n\nMixins for Loopback, to easily disable or setup new remote methods from the model definition file. It works with both Loopback 2 and 3.\n\n## Installation\n\n```bash\nnpm install --save loopback-setup-remote-methods-mixin\n```\n\n## Usage\n\n### As a mixin (recommended)\n\nFirst, modify your server/model-config.json to include the path to this module:\n\nFor LB3, mixins should be declared in the `_meta.mixins` property. For LB2, mixins should be declared in the `mixins` property.\n\n```json\n{\n \"_meta\": {\n   \"mixins\": [\n      \"loopback/common/mixins\",\n      \"loopback/server/mixins\",\n      \"../common/mixins\",\n      \"./mixins\",\n      \"../node_modules/loopback-setup-remote-methods-mixin\"\n    ]\n  }\n}\n```\n\nThen you can [use the mixin](https://loopback.io/doc/en/lb3/Defining-mixins.html#enable-a-model-with-mixins) from your model definition files:\n\n```json\n...\n  \"mixins\": {\n    \"SetupRemoteMethods\": {\n      \"disableAllExcept\": [\"create\", \"prototype.updateAttributes\"],\n      \"addFromFile\": \"./common/models/mymodel-remotes.js\"\n    }\n  }\n...\n```\nList of default remote methods\n\nhttp://loopback.io/doc/en/lb3/Exposing-models-over-REST.html#predefined-remote-methods\n\n## Options\n\n- [disable](#disable)\n- [disableAllExcept](#disableallexcept)\n- [relations](#relations)\n  - disableAllExcept\n- [ignoreACL](#ignoreacl)\n- [add](#add)\n  - [add using JSON](#add-using-json)\n  - [add using a JS file](#add-using-js-in-the-model) (Deprecated, use **addFromFile** instead)\n- [addFromFile](#addfromfile)\n\n### disable\n\nDisable the defined remote methods. For example, to disable the `create` and the `updateAttributes` remote methods:\n\n```json\n  \"mixins\": {\n    \"SetupRemoteMethods\": {\n      \"disable\": [\"create\", \"prototype.updateAttributes\"]\n    }\n  }\n```\n\nAllows wildcards with `*` (not fully tested though)\n\n### disableAllExcept\n\nDisable all the remote methods, except the defined on the options. For example, to disable all except `create` and `updateAttributes` remote methods: \n\n```json\n  \"mixins\": {\n    \"SetupRemoteMethods\": {\n      \"disableAllExcept\": [\"create\", \"prototype.updateAttributes\"]\n    }\n  }\n```\n\nAllows wildcards with `*` (not fully tested though)\n\n### relations\n\nAllows to setup some options per relation. Currently only `disableAllExcept` is supported.\n\n```json\n  \"mixins\": {\n    \"SetupRemoteMethods\": {\n      \"relations\": {\n        \"customer\": {\n          \"disableAllExcept\": [\"create\", \"get\"]          \n        }\n      }\n    }\n  }\n```\n\n### ignoreACL\n\n**Default value:** false\n\nThis 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. \n\n```json\n  \"mixins\": {\n    \"SetupRemoteMethods\": {\n      \"ignoreACL\": true,\n      \"disableAllExcept\": [\"create\", \"prototype.updateAttributes\"]\n    }\n  }\n```\n\n### add\n\nIt adds new remote methods to the model. This is similar to what's planned for the [Methods](https://loopback.io/doc/en/lb3/Model-definition-JSON-file.html#methods) section. (Which is not yet implemented. This option will be deprecated when that happens.)\n \n#### Add using JSON\n\n```json\n  \"mixins\": {\n    \"SetupRemoteMethods\": {\n      \"add\": {\n        \"sayHello\": {\n          \"accepts\": {\"arg\": \"msg\", \"type\": \"string\"},\n          \"returns\": {\"arg\": \"greeting\", \"type\": \"string\"}\n        },\n        \"sayBye\": {\n          \"accepts\": {\"arg\": \"msg\", \"type\": \"string\"},\n          \"returns\": {\"arg\": \"farewell\", \"type\": \"string\"}\n        }\n      }\n    }\n  }\n```\n\nThen you can have the methods implemented in your model as usual:\n\n```javascript\nconst Promise = require('bluebird');\n\nmodule.exports = function(Employee) {\n  Employee.sayHello = msg =\u003e {\n    return new Promise((resolve) =\u003e {\n      resolve('Hello ' + msg);\n    });\n  };\n  \n  Employee.sayBye = msg =\u003e {\n    return new Promise((resolve) =\u003e {\n      resolve('Goodbye ' + msg);\n    });\n  };\n};\n```\n\n#### Add using JS in the model\n\n**Deprecated**, use [addFromFile](#addfromfile) instead.\n\nYou can define the name of the methods in the model that will provide the remote method definition.\n\n```json\n  \"mixins\": {\n    \"SetupRemoteMethods\": {\n      \"add\": {\n        \"greet\": \"remotesDefinitions.greet\"\n      }\n    }\n  }\n```\n\nIn 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**\n\n```javascript\nmodule.exports = {\n  greet\n};\n\nfunction greet() {\n  return {\n    accepts: {arg: 'msg', type: 'string'},\n    returns: {arg: 'greeting', type: 'string'},\n  };\n}\n\n```\n\nThen, on your model, you would need to have something like:\n\n```javascript\nmodule.exports = function(Employee) {\n  // Include the definitions in the model for the mixin to be able to get them\n  Employee.remotesDefinitions = require('./remote-methods');\n\n  // The implementation of your remote method  \n  Employee.greet = msg =\u003e {\n    return new Promise((resolve) =\u003e {\n      resolve('Hello ' + msg);\n    });\n  };\n};\n```\n\n### addFromFile\n\nThere 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.\n\nYou can add **all** the methods from the file:\n\n```json\n  \"mixins\": {\n    \"SetupRemoteMethods\": {\n      \"addFromFile\": \"./common/models/employee-remotes.js\"\n    }\n  }\n```\n\nOr just some of them:\n\n```json\n  \"mixins\": {\n    \"SetupRemoteMethods\": {\n      \"addFromFile\": {\n        \"filename\": \"./common/models/employee-remotes.js\",\n        \"methods\": [ \"sayHello\" ]\n      }\n    }\n  }\n```\n\nThe path of the file should be relative to `process.cwd()`.\n\nThe file (`employee-remotes.js` in our example) would contain the remotes definitions:\n\n```javascript\nmodule.exports = {\n  sayHello,\n  sayBye\n};\n\nfunction sayHello() {\n  return {\n    accepts: {arg: 'msg', type: 'string'},\n    returns: {arg: 'greeting', type: 'string'},\n  };\n}\n\nfunction sayBye() {\n  return {\n    accepts: {arg: 'msg', type: 'string'},\n    returns: {arg: 'farewell', type: 'string'},\n  };\n}\n```\n\nThen, in the model, you will only need the implementation:\n\n```javascript\nmodule.exports = function(Employee) {\n  Employee.sayHello = msg =\u003e {\n    return new Promise((resolve) =\u003e {\n      resolve('Hello ' + msg);\n    });\n  };\n  \n  Employee.sayBye = msg =\u003e {\n    return new Promise((resolve) =\u003e {\n      resolve('Goodbye ' + msg);\n    });\n  };\n};\n```\n\n## Credits\n\nDisabling remote methods feature is based on the discussion at [https://github.com/strongloop/loopback/issues/651](https://github.com/strongloop/loopback/issues/651).\n\nThe code for `disable`, `disableAllExcept` and `ignoreACL` options is based on this [gist](https://gist.github.com/ebarault/1c3e43e19735f03dee8260471f8d3545) from [ebarault](https://github.com/ebarault), which was based on another [gist](https://gist.github.com/drmikecrowe/7ec75265fda2788e1c08249ece505a44) from [drmikecrowe](https://github.com/drmikecrowe).\n\nModule created by [c3s4r](https://github.com/c3s4r) for [Devsu](http://devsu.com/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevsu%2Floopback-setup-remote-methods-mixin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevsu%2Floopback-setup-remote-methods-mixin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevsu%2Floopback-setup-remote-methods-mixin/lists"}