# loopback-component-remote-ctx

> add the remote context to the specified remote methods of the loopback application.

Latest version **0.3.3** (published 2017-12-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install loopback-component-remote-ctx
pnpm add loopback-component-remote-ctx
yarn add loopback-component-remote-ctx
bun add loopback-component-remote-ctx
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.3 |
| Published | 2017-12-14 |
| First published | 2016-05-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Riceball LEE |
| Maintainers | riceball |
| Keywords | loopback, strongloop, remote method, remote context |

## Links

- npm: https://www.npmjs.com/package/loopback-component-remote-ctx
- Repository: https://github.com/snowyu/loopback-component-remote-ctx.js
- Issues: https://github.com/snowyu/loopback-component-remote-ctx.js/issues
- npm.io page: https://npm.io/package/loopback-component-remote-ctx

## Dependencies (3)

- [debug](https://npm.io/package/debug.md) ^2.2.0
- [util-ex](https://npm.io/package/util-ex.md) ^0.3.15
- [bluebird](https://npm.io/package/bluebird.md) ^3.4.0

## Recent versions

- 0.3.3 (latest) — 2017-12-14
- 0.3.2 — 2017-07-11
- 0.3.1 — 2017-07-11
- 0.3.0 — 2017-03-23
- 0.2.4 — 2016-11-16
- 0.2.3 — 2016-11-04
- 0.2.2 — 2016-07-26
- 0.2.1 — 2016-06-27
- 0.2.0 — 2016-06-27
- 0.1.2 — 2016-06-08
- 0.1.1 — 2016-05-31
- 0.1.0 — 2016-05-31

## README

# Loopback Component Remote context

This loopback component enables you to add the remote context to the specified remote methods of the loopback application.


Note: this hack would add a new argument to the remote method.


### Installation

0. Require: loopback 2.37.0 or newer
1. Install in you loopback project:

  `npm install --save loopback-component-remote-ctx`

2. Create a component-config.json file in your server folder (if you don't already have one)

3. Configure options inside `component-config.json`:

  ```json
  {
    "loopback-component-remote-ctx": {
      "enabled": true,
      "whiteList": ["YourModel", "OrYourModel.remoteMethod"],
      "blackList": ["YourModel", "OrYourModel.remoteMethod"],
      "argName": "remoteCtx"
    }
  }
  ```
  - `enabled` *[Boolean]*: whether enable this component. *defaults: true*
  - `whiteList` *[Array of String]* : Only add the remote context to these methods
  - `blackList` *[Array of String]* : Don't add the remote context to these methods.
  - `argName` *[String]*: the new argument name added to remote method. *defaults: remoteCtx*


Note: the `options` argument of remote method is always be inject. DO NOT USE `options` as the argument name.

### Usage

You should enable the model-level setting `injectOptionsFromRemoteContext` if you use the loopback v2.x

```js
// common/models/customer.json
{
  "name": "Customer",
  "base": "User",
  "injectOptionsFromRemoteContext": true,
  "properties": {
    // ...
  }
}
```

And you can NEVER USE the `options` name. Check your remote method arguments before used it.

```js

Model.observe('access', function(ctx, next){
  console.log(ctx.options.remoteCtx) //the remoteCtx
})

Model.beforeRemote('*', function(ctx, next){
  Model.findById('id', null, ctx, function(err, result){
    if (err) return next(err);
    next(result);
  })
})

Model.yourRemoteMethod = function(msg, ctx){
  //if your write this before injected via the component:
  return Model.findById('id', null, ctx)
  //else should be this, your controller::
  // return Model.findById('id', null, ctx)
}

Model.remoteMethod(
  'yourRemoteMethod',
  {
    accepts: [
      {arg: 'msg', type: String},
      {arg: 'options', type: Object, http:'optionsFromRequest' }
    ],
    returns: {arg: 'greeting', type: 'string'}
  }
);

```






### History

* v0.3.0 (loopback >=2.37.0)
  * use the `optionsFromRequest` object.
* v0.2.0
  * **broken**:  put the remote context to the options.remoteCtx instead of options.
  * [bug] the original options of the model method is lost.


### Refs

* https://github.com/strongloop/loopback/issues/1495

---
_Source: https://npm.io/package/loopback-component-remote-ctx · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
