Skip to content

Commit ecc0014

Browse files
committed
bugfix #7: 修复 hook 函数返回对象的格式
1 parent 6e0c46a commit ecc0014

3 files changed

Lines changed: 144 additions & 20 deletions

File tree

lib/leanengine.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,15 @@ Cloud.use('/__engine/1/ping', function(req, res) {
193193
return respError(res, 'no function or class name');
194194
}
195195

196-
var cb = function(err, data) {
196+
var cb = function(err, data, isBare) {
197197
if (err) {
198198
return respError(res, err);
199199
}
200-
return resp(res, data);
200+
if (isBare) {
201+
return respBare(res, data);
202+
} else {
203+
return resp(res, data);
204+
}
201205
};
202206

203207
var meta = {
@@ -250,6 +254,12 @@ var resp = function(res, data) {
250254
return res.end(JSON.stringify({"result": data}));
251255
};
252256

257+
var respBare = function(res, data) {
258+
res.setHeader('Content-Type', 'application/json; charset=UTF-8');
259+
res.statusCode = 200;
260+
return res.end(JSON.stringify(data));
261+
};
262+
253263
var respOk = function(res) {
254264
resp(res, 'ok');
255265
};
@@ -283,7 +293,9 @@ Cloud.run = function(name, data, options) {
283293

284294
var call = function(funcName, params, user, meta, cb) {
285295
if (!Cloud.__code[funcName]) {
286-
return cb(new Error("Cloud code not find function named '" + funcName + "' for app '" + AV.applicationId + "' on " + NODE_ENV + "."));
296+
var err = new Error("LeanEngine not found function named '" + funcName + "' for app '" + AV.applicationId + "' on " + NODE_ENV + ".");
297+
err.statusCode = 404;
298+
return cb(err);
287299
}
288300
try {
289301
Cloud.__code[funcName]({
@@ -307,7 +319,9 @@ var call = function(funcName, params, user, meta, cb) {
307319

308320
var classHook = function(className, hook, object, user, meta, cb) {
309321
if (!Cloud.__code[hook + className]) {
310-
return cb(new Error("Cloud code not find hook '" + hook + className + "' for app '" + AV.applicationId + "' on " + NODE_ENV + "."));
322+
var err = new Error("LeanEngine not found hook '" + hook + className + "' for app '" + AV.applicationId + "' on " + NODE_ENV + ".");
323+
err.statusCode = 404;
324+
return cb(err);
311325
}
312326
var obj = createAVObject(className);
313327
obj._finishFetch(object, true);
@@ -327,9 +341,9 @@ var classHook = function(className, hook, object, user, meta, cb) {
327341
}, {
328342
success: function() {
329343
if ('__before_delete_for_' === hook) {
330-
return cb(null, {});
344+
return cb(null, {}, true);
331345
} else {
332-
return cb(null, obj);
346+
return cb(null, obj, true);
333347
}
334348
},
335349
error: function(err) {
@@ -393,7 +407,7 @@ var hookNameMapping = {
393407
beforeSave: '__before_save_for_',
394408
afterSave: '__after_save_for_',
395409
afterUpdate: '__after_update_for_',
396-
beforeDelete: '__before_save_for_',
410+
beforeDelete: '__before_delete_for_',
397411
afterDelete: '__after_delete_for_'
398412
};
399413

test/function_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,10 @@ describe('functions', function() {
289289
.post('/1/functions/noThisMethod')
290290
.set('X-AVOSCloud-Application-Id', appId)
291291
.set('X-AVOSCloud-Application-Key', appKey)
292-
.expect(400)
292+
.expect(404)
293293
.expect({
294294
"code": 1,
295-
"error": "Cloud code not find function named 'noThisMethod' for app '" + appId + "' on development."
295+
"error": "LeanEngine not found function named 'noThisMethod' for app '" + appId + "' on development."
296296
}, done);
297297
});
298298

test/hook_test.js

Lines changed: 121 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ AV.Cloud.beforeSave("TestReview", function(request, response){
3333
}
3434
});
3535

36+
AV.Cloud.beforeSave("ErrorObject", function(request, response) {
37+
var a = {};
38+
a.noThisMethod();
39+
response.success();
40+
})
41+
3642
AV.Cloud.afterSave("TestReview", function(request) {
3743
assert.equal(request.object.className, 'TestReview');
3844
assert.equal(request.object.id, '5403e36be4b0b77b5746b292');
@@ -42,6 +48,24 @@ AV.Cloud.afterSave("TestError", function(request) {
4248
noThisMethod();
4349
});
4450

51+
AV.Cloud.afterUpdate("TestClass", function(request) {
52+
var bizTime = new Date();
53+
request.object.set('bizTime', bizTime);
54+
request.object.save(null, {
55+
success: function(obj) {
56+
assert.equal(bizTime, obj.get('bizTime'));
57+
}
58+
});
59+
});
60+
61+
AV.Cloud.beforeDelete("TestClass", function(request, response) {
62+
assert.equal(request.object.className, 'TestClass');
63+
if (request.object.get('foo') === 'important') {
64+
return response.error('important note');
65+
}
66+
response.success();
67+
});
68+
4569
AV.Cloud.onVerified('sms', function(request) {
4670
assert.equal(request.object.id, '54fd6a03e4b06c41e00b1f40');
4771
});
@@ -75,10 +99,8 @@ describe('hook', function() {
7599
})
76100
.expect(200)
77101
.expect({
78-
"result": {
79-
"stars": 1,
80-
"comment": "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567..."
81-
}
102+
"stars": 1,
103+
"comment": "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567..."
82104
}, done);
83105
});
84106

@@ -116,16 +138,52 @@ describe('hook', function() {
116138
})
117139
.expect(200)
118140
.expect({
119-
"result": {
120-
"user": {
121-
"__type": "Pointer",
122-
"className": "_User",
123-
"objectId": "52aebbdee4b0c8b6fa455aa7"
124-
}
141+
"user": {
142+
"__type": "Pointer",
143+
"className": "_User",
144+
"objectId": "52aebbdee4b0c8b6fa455aa7"
125145
}
126146
}, done);
127147
});
128148

149+
it("beforeSave_throw_error", function(done) {
150+
var ori = console.warn;
151+
var warnLogs = [];
152+
console.warn = function() {
153+
warnLogs.push(arguments);
154+
}
155+
request(AV.Cloud)
156+
.post("/1.1/functions/ErrorObject/beforeSave")
157+
.set('X-AVOSCloud-Application-Id', appId)
158+
.set('X-AVOSCloud-Application-Key', appKey)
159+
.send({
160+
object: {
161+
foo: 'bar'
162+
}
163+
})
164+
.expect(500, function(err, res) {
165+
res.body.should.eql({ code: 1, error: 'undefined is not a function' });
166+
console.warn = ori;
167+
warnLogs.length.should.equal(1);
168+
warnLogs[0][0].split('\n')[0].should.equal("Execute \'__before_save_for_ErrorObject\' failed with error: TypeError: undefined is not a function");
169+
done();
170+
});
171+
});
172+
173+
it("beforeSave_not_found", function(done) {
174+
request(AV.Cloud)
175+
.post("/1.1/functions/NoThisObject/beforeSave")
176+
.set('X-AVOSCloud-Application-Id', appId)
177+
.set('X-AVOSCloud-Application-Key', appKey)
178+
.send({
179+
object: {
180+
foo: 'bar'
181+
}
182+
})
183+
.expect(404)
184+
.expect({ code: 1, error: "LeanEngine not found hook '__before_save_for_NoThisObject' for app '" + appId + "' on development." }, done);
185+
});
186+
129187
it('afterSave', function(done) {
130188
request(AV.Cloud)
131189
.post('/1/functions/TestReview/afterSave')
@@ -186,8 +244,60 @@ describe('hook', function() {
186244
}
187245
}
188246
})
247+
.expect(404)
248+
.expect({ code: 1, error: "LeanEngine not found hook \'__after_save_for_NoThisClass\' for app \'" + appId + "\' on development." }, done);
249+
});
250+
251+
it('afterUpdate', function(done) {
252+
request(AV.Cloud)
253+
.post('/1/functions/TestClass/afterUpdate')
254+
.set('X-AVOSCloud-Application-Id', appId)
255+
.set('X-AVOSCloud-Application-Key', appKey)
256+
.set('Content-Type', 'application/json')
257+
.send({
258+
object: {
259+
objectId: '556904d8e4b09419960c14bd',
260+
foo: 'bar'
261+
}
262+
})
263+
.expect(200, function(err, res) {
264+
res.body.should.eql({ result: 'ok' });
265+
setTimeout(function() { // 等待数据更新
266+
done();
267+
}, 1000);
268+
});
269+
});
270+
271+
it('should be deleted', function(done) {
272+
request(AV.Cloud)
273+
.post('/1/functions/TestClass/beforeDelete')
274+
.set('X-AVOSCloud-Application-Id', appId)
275+
.set('X-AVOSCloud-Application-Key', appKey)
276+
.set('Content-Type', 'application/json')
277+
.send({
278+
object: {
279+
objectId: '55690242e4b09419960c01f5',
280+
foo: 'bar'
281+
}
282+
})
283+
.expect(200)
284+
.expect({}, done);
285+
});
286+
287+
it('should not be deleted', function(done) {
288+
request(AV.Cloud)
289+
.post('/1/functions/TestClass/beforeDelete')
290+
.set('X-AVOSCloud-Application-Id', appId)
291+
.set('X-AVOSCloud-Application-Key', appKey)
292+
.set('Content-Type', 'application/json')
293+
.send({
294+
object: {
295+
objectId: '55690242e4b09419960c01f6',
296+
foo: 'important'
297+
}
298+
})
189299
.expect(400)
190-
.expect({ code: 1, error: "Cloud code not find hook \'__after_save_for_NoThisClass\' for app \'" + appId + "\' on development." }, done);
300+
.expect({ code: 1, error: "important note" }, done);
191301
});
192302

193303
it('onVerified', function(done) {

0 commit comments

Comments
 (0)