methodsmodelresponseloopbackjsstrongloop

How to set custom schema for custom remote methods on Strongloop


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'}
});

Solution

  • The way I found to solve this problem is to create a new model in this way, with the helper slc loopback: model

    ? Enter the model name: ArgChallenge
    ? Select the data-source to attach ArgChallenge to: (no data-source)
    ? Select model's base class PersistedModel
    ? Expose ArgChallenge via the REST API? No
    ? Common model or server only? server
    

    And I continue putting properties, then on Challenge.js:

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

    And that works! If anyone knows a better way to do this, please share.