UNPKG

5.55 kBJavaScriptView Raw
1'use strict';
2
3var _promise = require('babel-runtime/core-js/promise');
4
5var _promise2 = _interopRequireDefault(_promise);
6
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
9var _ = require('underscore');
10
11var _require = require('./request'),
12 _request = _require._request,
13 request = _require.request;
14
15module.exports = function (AV) {
16 /**
17 * Contains functions for calling and declaring
18 * <p><strong><em>
19 * Some functions are only available from Cloud Code.
20 * </em></strong></p>
21 *
22 * @namespace
23 * @borrows AV.Captcha.request as requestCaptcha
24 */
25 AV.Cloud = AV.Cloud || {};
26
27 _.extend(AV.Cloud,
28 /** @lends AV.Cloud */{
29 /**
30 * Makes a call to a cloud function.
31 * @param {String} name The function name.
32 * @param {Object} [data] The parameters to send to the cloud function.
33 * @param {AuthOptions} [options]
34 * @return {Promise} A promise that will be resolved with the result
35 * of the function.
36 */
37 run: function run(name, data, options) {
38 return request({
39 service: 'engine',
40 method: 'POST',
41 path: '/functions/' + name,
42 data: AV._encode(data, null, true),
43 authOptions: options
44 }).then(function (resp) {
45 return AV._decode(resp).result;
46 });
47 },
48
49
50 /**
51 * Makes a call to a cloud function, you can send {AV.Object} as param or a field of param; the response
52 * from server will also be parsed as an {AV.Object}, array of {AV.Object}, or object includes {AV.Object}
53 * @param {String} name The function name.
54 * @param {Object} [data] The parameters to send to the cloud function.
55 * @param {AuthOptions} [options]
56 * @return {Promise} A promise that will be resolved with the result of the function.
57 */
58 rpc: function rpc(name, data, options) {
59 if (_.isArray(data)) {
60 return _promise2.default.reject(new Error("Can't pass Array as the param of rpc function in JavaScript SDK."));
61 }
62
63 return request({
64 service: 'engine',
65 method: 'POST',
66 path: '/call/' + name,
67 data: AV._encodeObjectOrArray(data),
68 authOptions: options
69 }).then(function (resp) {
70 return AV._decode(resp).result;
71 });
72 },
73
74
75 /**
76 * Make a call to request server date time.
77 * @return {Promise.<Date>} A promise that will be resolved with the result
78 * of the function.
79 * @since 0.5.9
80 */
81 getServerDate: function getServerDate() {
82 return _request('date', null, null, 'GET').then(function (resp) {
83 return AV._decode(resp);
84 });
85 },
86
87
88 /**
89 * Makes a call to request an sms code for operation verification.
90 * @param {String|Object} data The mobile phone number string or a JSON
91 * object that contains mobilePhoneNumber,template,sign,op,ttl,name etc.
92 * @param {String} data.mobilePhoneNumber
93 * @param {String} [data.template] sms template name
94 * @param {String} [data.sign] sms signature name
95 * @param {String} [data.smsType] sending code by `sms` (default) or `voice` call
96 * @param {SMSAuthOptions} [options]
97 * @return {Promise} A promise that will be resolved if the request succeed
98 */
99 requestSmsCode: function requestSmsCode(data) {
100 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
101
102 if (_.isString(data)) {
103 data = { mobilePhoneNumber: data };
104 }
105 if (!data.mobilePhoneNumber) {
106 throw new Error('Missing mobilePhoneNumber.');
107 }
108 if (options.validateToken) {
109 data = _.extend({}, data, {
110 validate_token: options.validateToken
111 });
112 }
113 return _request('requestSmsCode', null, null, 'POST', data, options);
114 },
115
116
117 /**
118 * Makes a call to verify sms code that sent by AV.Cloud.requestSmsCode
119 * @param {String} code The sms code sent by AV.Cloud.requestSmsCode
120 * @param {phone} phone The mobile phoner number.
121 * @return {Promise} A promise that will be resolved with the result
122 * of the function.
123 */
124 verifySmsCode: function verifySmsCode(code, phone) {
125 if (!code) throw new Error('Missing sms code.');
126 var params = {};
127 if (_.isString(phone)) {
128 params['mobilePhoneNumber'] = phone;
129 }
130
131 return _request('verifySmsCode', code, null, 'POST', params);
132 },
133 _requestCaptcha: function _requestCaptcha(options, authOptions) {
134 return _request('requestCaptcha', null, null, 'GET', options, authOptions).then(function (_ref) {
135 var url = _ref.captcha_url,
136 captchaToken = _ref.captcha_token;
137 return {
138 captchaToken: captchaToken,
139 url: url
140 };
141 });
142 },
143
144
145 /**
146 * Request a captcha.
147 */
148 requestCaptcha: AV.Captcha.request,
149
150 /**
151 * Verify captcha code. This is the low-level API for captcha.
152 * Checkout {@link AV.Captcha} for high abstract APIs.
153 * @param {String} code the code from user input
154 * @param {String} captchaToken captchaToken returned by {@link AV.Cloud.requestCaptcha}
155 * @return {Promise.<String>} validateToken if the code is valid
156 */
157 verifyCaptcha: function verifyCaptcha(code, captchaToken) {
158 return _request('verifyCaptcha', null, null, 'POST', {
159 captcha_code: code,
160 captcha_token: captchaToken
161 }).then(function (_ref2) {
162 var validateToken = _ref2.validate_token;
163 return validateToken;
164 });
165 }
166 });
167};
\No newline at end of file