Skip to content

Commit e0d9ecd

Browse files
committed
fix(component-options): Rename handleCustomRemote to handleCustomRemoteMethods + Fix README for #265
1 parent d7dbca0 commit e0d9ecd

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

README.md

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Example:
119119
"enable": true,
120120
"handleErrors": true,
121121
"errorStackInResponse": false,
122-
"handleCustomRemote": false,
122+
"handleCustomRemoteMethods": false,
123123
"exclude": [
124124
{"model": "comment"},
125125
{"methods": "find"},
@@ -221,19 +221,19 @@ response. It will be stored under the `source.stack` key.
221221
- Type: `boolean`
222222
- Default: `false`
223223

224-
### handleCustomRemote
225-
Allow all (custom) remotes to be serialized by default.
224+
### handleCustomRemoteMethods
225+
Allow all (custom) remote methods to be serialized by default.
226226

227-
This option can be overrided with:
228-
1. `jsonapi` remote option (have highest priority)
229-
2. `exclude` component option
230-
3. `include` component option
227+
This option can be overridden in any of the following ways:
228+
1. Setting a jsonapi property to true or false in a remote method definition.
229+
2. Globally adding the remote method to the component's exclude array.
230+
3. Globally adding the remote method to the component's include array.
231231

232232
#### example
233233
```js
234234
{
235235
...
236-
"handleCustomRemote": true,
236+
"handleCustomRemoteMethods": true,
237237
...
238238
}
239239
```
@@ -409,20 +409,17 @@ Only expose foreign keys for the comment model findById method. eg. `GET /api/co
409409
- Type: `boolean|array`
410410
- Default: `false`
411411

412-
## Custom remote
412+
## Custom remote methods
413413

414-
### `jsonapi` remote options
415-
Sometime you need to control if custom remote should be serialized or not.
416-
**By default a custom remote will NOT be handled by JSONApi.**
414+
### `jsonapi` remote method options
415+
Sometimes you need to be able to control when a custom remote method should be handled by the component. By default, `loopback-component-jsonapi` will not handle (serialize or deserialize) custom remote methods. In order to tell the component to handle a custom remote method, you have the following options (In priority order):
417416

418-
To enabled/disable JSONApi for specific remotes, you have multiple solutions:
417+
1. Set `jsonapi` to `true` when defining a custom remote method.
418+
2. Add the methods name to the component's `exclude` array setting. (see above)
419+
3. Add the methods name to the component's `include` array setting. (see above)
420+
4. Set `handleCustomRemoteMethods` to `true` in the component's settings. (see above)
419421

420-
1. Use `jsonapi` remote option
421-
2. Use `exlude` on component options (see above)
422-
3. Use `include` on component options (see above)
423-
4. Use `handleCustomRemote` on component options (see above)
424-
425-
**This option has precedence** and it forces the remote to BE or to NOT BE deserialized/serialized by JSONApi.
422+
This option takes precedence and sets the component to handle or not handle the custom remote method.
426423

427424
#### examples
428425
```js
@@ -431,21 +428,21 @@ Post.remoteMethod('greet', {
431428
returns: { root: true }
432429
})
433430
```
434-
Ensure the response of `Post.greet` will follow the JSONApi format.
431+
Ensures that the response from Post.greet will follow JSONApi format.
435432

436433
```js
437434
Post.remoteMethod('greet', {
438435
jsonapi: false
439436
returns: { arg: 'greeting', type: 'string' }
440437
})
441438
```
442-
Ensure the response of `Post.greet` will not follow the JSONApi format.
439+
Ensures that the response from Post.greet will never follow JSONApi format.
443440

444441
#### Note
445-
You should always pass `root: true` to the `returns` object when you use JSONApi, especialy when you expect to respond with an array.
442+
You must always pass `root: true` to the `returns` object when using `loopback-component-jsonapi`. This is especialy important when you expect the response to be an array.
446443

447-
### Override serialization type
448-
You may want for a custom method of a given model to return instance(s) from another model. When you do so, you need to enforce the return `type` of this other model in the definition of the remote method.
444+
### Overriding serialization type
445+
When `loopback-component-jsonapi` serializes a custom remote method, by default it will assume that the data being serialized is of the same type as the model the custom remote method is being defined on. Eg. For a remote method on a `Comment` model, it will be assumed that the data being returned from the remote method will be a comment or an array of comments. When this is not the case, you will need to set the type property in the `returns` object in the remote method definition.
449446

450447
*If an unknown type or no type are given, the model name will be used.*
451448

lib/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ function shouldApplyJsonApi (ctx, options) {
235235
}
236236

237237
// a default option can be set in component-config
238-
return !!options.handleCustomRemote
238+
return !!options.handleCustomRemoteMethods
239239
}
240240

241241
function shouldNotApplyJsonApi (ctx, options) {

test/remoteMethods.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ describe('loopback json api remote methods', function () {
121121

122122
describe('when `serializeCustomRemote` is set', function (done) {
123123
beforeEach(function () {
124-
JSONAPIComponent(app, { handleCustomRemote: true })
124+
JSONAPIComponent(app, { handleCustomRemoteMethods: true })
125125
})
126126

127127
testAutocompleteJsonAPI()
@@ -132,7 +132,7 @@ describe('loopback json api remote methods', function () {
132132
describe('when `exclude` is set', function (done) {
133133
beforeEach(function () {
134134
JSONAPIComponent(app, {
135-
handleCustomRemote: true,
135+
handleCustomRemoteMethods: true,
136136
exclude: [{ methods: ['last', 'autocomplete', 'archives'] }],
137137
include: [{ methods: 'last' }]
138138
})
@@ -146,7 +146,7 @@ describe('loopback json api remote methods', function () {
146146
describe('when `include` is set', function (done) {
147147
beforeEach(function () {
148148
JSONAPIComponent(app, {
149-
handleCustomRemote: false,
149+
handleCustomRemoteMethods: false,
150150
include: [{ methods: 'last' }]
151151
})
152152
})
@@ -220,7 +220,7 @@ describe('loopback json api remote methods', function () {
220220
/* Static test */
221221
function testLastRaw () {
222222
it(
223-
'GET /posts/last should return a raw Post (exclude is more important than include and handleCustomRemote)',
223+
'GET /posts/last should return a raw Post (exclude is more important than include and handleCustomRemoteMethods)',
224224
function (done) {
225225
request(app).get('/posts/last').expect(200).end(function (err, res) {
226226
expect(err).to.equal(null)

0 commit comments

Comments
 (0)