How to set custom schema for custom remote methods on Strongloop

Viewed 6728

I'm newbie on Strongloop and I can't find information for how to customize my response class (model schema for a object I built) and I don't know how to show on the API explorer the object with custom data.

Capture for strongloop api explorer

For example, I have a custom remote method called score

POST /Challenges/score

I want to show for the parameter data a custom model schema instead of single parameters, not the Model Schema for Challenge, the data on the body have all the parameters and show to the user on the Data Type: Model Schema, is this possible?

{
  "id": "string",
  "limit": 0,
  "order": "string",
  "userId": "string"
}

On the other side, in Response Class I want to show the schema for the response object. Something like this:

{
  "id":"string",
  "userId":"string",
  "user": {},
  "totalScore":0,
  "tags": []
}

I looked different questions (this and this), but can not find something to solve this issues.

Update

Here is the definition of the remote method

Challenge.remoteMethod('score', {
    accepts: { arg: 'data', type: 'object', http: { source: 'body' } },
    returns: {arg: 'scores', type: 'array'},
    http: {path: '/score', verb: 'post'}
});
5 Answers

In loopback, remote arguments can identify data models which have been defined using ds.define('YourCustomModelName', dataFormat);

so for you case, write a function in a Challenge.js file which will have a remote method (in ur case score) defined.

const loopback = require('loopback');
const ds = loopback.createDataSource('memory'); 
module.exports = function(Challenge) {
  defineChallengeArgFormat() ;
 // remote methods (score) defined
};

let defineChallengeArgFormat = function() {
  let dataFormat = {
            "id": String,
            "userId": String,
            "user": {},
            "totalScore": Number,
            "tags": []
        };
  ds.define('YourCustomModelName', dataFormat);
};

Under remote arguments type use 'type': 'YourCustomModelName'

    Challenge.remoteMethod('score', {
        accepts: {
            arg: 'data',
            type: 'YourCustomModelName',
            http: {
                source: 'body'
            }
        },
        returns: {
            arg: 'scores',
            type: 'Challenge'
        },
        http: {
            path: '/score',
            verb: 'post'
        }
    });

You should see it reflecting on explorer after restarting server and refreshing :)

I found a way to solve this problem by changing the type parameter in the accepts array. when we create remoteMethod; we provide accepts array. there is arg, type, required, http. then we can give our request object into type parameter.

Example code

UserModel.remoteMethod(
 'login',
  {
    description: 'Login a user with username/email and password.',
    accepts: [
      {
        arg: 'credentials',
        type: {'email': 'string', 'password': 'string'},
        required: true,
        http: {source: 'body'},
      },
      {
        arg: 'include', type: ['string'], http: {source: 'query'},
        description: 'Related objects to include in the response. ' +
          'See the description of return value for more details.',
      },
    ],
    returns: {
      arg: 'accessToken', type: 'object', root: true,
      description:
        g.f('The response body contains properties of the {{AccessToken}} created on login.\n' +
          'Depending on the value of `include` parameter, the body may contain ' +
          'additional properties:\n\n' +
          '  - `user` - `U+007BUserU+007D` - Data of the currently logged in user. ' +
          '{{(`include=user`)}}\n\n'),
    },
    http: {verb: 'post'},
  },
);
Related