forked from leancloud/javascript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudfunction.js
More file actions
100 lines (91 loc) · 3.64 KB
/
Copy pathcloudfunction.js
File metadata and controls
100 lines (91 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict';
var _ = require('underscore');
module.exports = function(AV) {
/**
* @namespace Contains functions for calling and declaring
* <a href="/docs/cloud_code_guide#functions">cloud functions</a>.
* <p><strong><em>
* Some functions are only available from Cloud Code.
* </em></strong></p>
*/
AV.Cloud = AV.Cloud || {};
_.extend(AV.Cloud, /** @lends AV.Cloud */ {
/**
* Makes a call to a cloud function.
* @param {String} name The function name.
* @param {Object} data The parameters to send to the cloud function.
* @param {Object} options A Backbone-style options object
* options.success, if set, should be a function to handle a successful
* call to a cloud function. options.error should be a function that
* handles an error running the cloud function. Both functions are
* optional. Both functions take a single argument.
* @return {AV.Promise} A promise that will be resolved with the result
* of the function.
*/
run: function(name, data, options) {
var request = AV._request("functions", name, null, 'POST',
AV._encode(data, null, true));
return request.then(function(resp) {
return AV._decode(null, resp).result;
})._thenRunCallbacks(options);
},
/**
* Make a call to request server date time.
* @param {Object} options A Backbone-style options object
* options.success, if set, should be a function to handle a successful
* call to a cloud function. options.error should be a function that
* handles an error running the cloud function. Both functions are
* optional. Both functions take a single argument.
* @return {AV.Promise} A promise that will be resolved with the result
* of the function.
* @since 0.5.9
*/
getServerDate: function(options) {
var request = AV._request("date", null, null, 'GET');
return request.then(function(resp) {
return AV._decode(null, resp);
})._thenRunCallbacks(options);
},
/**
* Makes a call to request a sms code for operation verification.
* @param {Object} data The mobile phone number string or a JSON
* object that contains mobilePhoneNumber,template,op,ttl,name etc.
* @param {Object} options A Backbone-style options object
* @return {AV.Promise} A promise that will be resolved with the result
* of the function.
*/
requestSmsCode: function(data, options){
if(_.isString(data)) {
data = { mobilePhoneNumber: data };
}
if(!data.mobilePhoneNumber) {
throw "Missing mobilePhoneNumber.";
}
var request = AV._request("requestSmsCode", null, null, 'POST',
data);
return request._thenRunCallbacks(options);
},
/**
* Makes a call to verify sms code that sent by AV.Cloud.requestSmsCode
* @param {String} code The sms code sent by AV.Cloud.requestSmsCode
* @param {phone} phone The mobile phoner number(optional).
* @param {Object} options A Backbone-style options object
* @return {AV.Promise} A promise that will be resolved with the result
* of the function.
*/
verifySmsCode: function(code, phone, options){
if(!code)
throw "Missing sms code.";
var params = {};
if(AV._.isString(phone)) {
params['mobilePhoneNumber'] = phone;
} else {
// To be compatible with old versions.
options = phone;
}
var request = AV._request("verifySmsCode", code, null, 'POST',
params);
return request._thenRunCallbacks(options);
}
});
};