Skip to content

Commit 41240cf

Browse files
committed
Merge pull request #24 from digitalsadhu/patch
Basic test for remote method
2 parents 61d5c56 + b65db31 commit 41240cf

File tree

3 files changed

+86
-8
lines changed

3 files changed

+86
-8
lines changed

lib/deserialize.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,30 @@
33
module.exports = function (app) {
44
var remotes = app.remotes();
55

6-
//register a handler to run before all remote methods so that we can
7-
//transform JSON API structured JSON payload into something loopback
8-
//can work with.
9-
remotes.before('**', function (ctx, next) {
6+
function deserializeJSONAPIPayload (ctx, next) {
107

11-
//JSON api doesn't support PUT so we only need to apply our changes to
12-
//POST and PATCH operations.
13-
if (ctx.req.method === 'POST' || ctx.req.method === 'PATCH') {
8+
var regexs = [
9+
/\.create/,
10+
/prototype\.updateAttributes/,
11+
/prototype\.__createRelationships__/,
12+
/prototype\.__updateRelationships__/
13+
];
14+
15+
var matches = regexs.filter(function (regex) {
16+
return ctx.methodString.match(regex);
17+
});
18+
19+
if (matches.length > 0) {
1420

1521
//set the JSON API Content-Type response header
1622
ctx.res.set({'Content-Type': 'application/vnd.api+json'});
1723

1824
//check the incoming payload data to ensure it is valid according to the
1925
//JSON API spec.
2026
var data = ctx.args.data;
27+
28+
if (!data) return next();
29+
2130
if (!data.data) return next(new Error('JSON API resource object must contain `data` property'));
2231
if (!data.data.type) return next(new Error('JSON API resource object must contain `data.type` property'));
2332
if (ctx.req.method === 'PATCH') {
@@ -29,5 +38,10 @@ module.exports = function (app) {
2938
}
3039

3140
next();
32-
});
41+
}
42+
43+
//register a handler to run before all remote methods so that we can
44+
//transform JSON API structured JSON payload into something loopback
45+
//can work with.
46+
remotes.before('**', deserializeJSONAPIPayload);
3347
};

lib/serialize.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,25 @@ module.exports = function (app, options) {
8686
var remotes = app.remotes();
8787

8888
remotes.after('**', function (ctx, next) {
89+
90+
var regexs = [
91+
/\.find/,
92+
/\.create/,
93+
/\.deleteById/,
94+
/\.findById/,
95+
/prototype\.__get__/,
96+
/prototype\.updateAttributes/,
97+
/prototype\.__findRelationships__/,
98+
/prototype\.__createRelationships__/,
99+
/prototype\.__updateRelationships__/
100+
];
101+
102+
var matches = regexs.filter(function (regex) {
103+
return ctx.methodString.match(regex);
104+
});
105+
106+
if (matches.length === 0) return next();
107+
89108
ctx.res.set({'Content-Type': 'application/vnd.api+json'});
90109
//housekeeping, just skip verbs we definitely aren't
91110
//interested in handling.

test/remoteMethods.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var request = require('supertest');
2+
var loopback = require('loopback');
3+
var expect = require('chai').expect;
4+
var JSONAPIComponent = require('../');
5+
var app, Post;
6+
7+
describe('loopback json api remote methods', function () {
8+
beforeEach(function () {
9+
app = loopback();
10+
app.set('legacyExplorer', false);
11+
var ds = loopback.createDataSource('memory');
12+
Post = ds.createModel('post', {
13+
id: {type: Number, id: true},
14+
title: String,
15+
content: String
16+
});
17+
Post.greet = function (msg, cb) {
18+
cb(null, 'Greetings... ' + msg);
19+
};
20+
Post.remoteMethod(
21+
'greet',
22+
{
23+
accepts: {arg: 'msg', type: 'string'},
24+
returns: {arg: 'greeting', type: 'string'}
25+
}
26+
);
27+
app.model(Post);
28+
app.use(loopback.rest());
29+
JSONAPIComponent(app);
30+
});
31+
32+
describe('remote method for application/json', function () {
33+
it('POST /posts/greet should return remote method message', function (done) {
34+
request(app).post('/posts/greet')
35+
.send({'msg': 'John'})
36+
.set('Content-Type', 'application/json')
37+
.expect(200)
38+
.end(function (err, res) {
39+
expect(err).to.equal(null);
40+
expect(res.body.greeting).to.equal('Greetings... John');
41+
done();
42+
});
43+
});
44+
});
45+
});

0 commit comments

Comments
 (0)