Reputation: 681
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.
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'}
});
Upvotes: 12
Views: 6788
Reputation: 63
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'},
},
);
Upvotes: 0
Reputation: 181
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 :)
Upvotes: 5
Reputation: 181
@jrltt, Instead of using default, use object structure pointing to type under accepts and it should work. Note, http source:body is needed.
With random object:
Challenge.remoteMethod('score', {
accepts: {
arg: 'data',
type: {
"id": "string",
"userId": "string",
"user": {},
"totalScore": 0,
"tags": []
},
http: {
source: 'body'
}
},
returns: {
arg: 'scores',
type: 'Challenge'
},
http: {
path: '/score',
verb: 'post'
}
});
With a defined model which is available in model-config or create using loopback model generator, then that model name can be used to point type. So lets use User model to show in accepts parameter,
Challenge.remoteMethod('score', {
accepts: {
arg: 'data',
type: 'User',
http: {
source: 'body'
}
},
returns: {
arg: 'scores',
type: 'Challenge'
},
http: {
path: '/score',
verb: 'post'
}
});
Upvotes: 3
Reputation: 681
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.
Upvotes: 2
Reputation: 91
I believe you might have gone through the official docs of strongloop. If not, here is the link that explains the remote methods and their accepted data types. https://docs.strongloop.com/display/public/LB/Remote+methods
Assuming your custom object is Challenge, to show the object in response you have to specify the type( the type can be one of the loopback's data type or you custom model). So to return Challenge you have to add following code :
Challenge.remoteMethod('score', {
accepts: { arg: 'data', type: 'object', http: { source: 'body' } },
returns: {arg: 'scores', type: 'Challenge', root: true},
http: {path: '/score', verb: 'post'},
});
The second arrow that you have specified is the default values that you want to try out with your API call. You can pass any custom string with default as the key. For example, if you want to pass some object :
Challenge.remoteMethod('score', {
accepts: {
arg: 'data',
type: 'object',
default: '{
"id": "string",
"userId": "string",
"user": {},
"totalScore": 0,
"tags": []
}',
http: {
source: 'body'
}
},
returns: {
arg: 'scores',
type: 'Challenge'
},
http: {
path: '/score',
verb: 'post'
}
});
So, for response you can not customize the model. But to pass default values you can put anything in the string format.
Upvotes: 8