11'use strict' ;
2-
3- var _ = require ( 'underscore' ) ;
4-
5- /*global _: false, $: false, localStorage: false, process: true,
6- XMLHttpRequest: false, XDomainRequest: false, exports: false,
7- require: false */
82module . exports = function ( AV ) {
93
4+ const _ = AV . _ ;
5+
106 // 挂载一些配置
11- _ . extend ( AV . _config , {
12- cnApiUrl : 'https://api.leancloud.cn' ,
13- usApiUrl : 'https://us-api.leancloud.cn'
7+ let avConfig = AV . _config ;
8+
9+ _ . extend ( avConfig , {
10+ // 服务器请求的节点 host
11+ apiHost : {
12+ cn : 'https://api.leancloud.cn' ,
13+ us : 'https://us-api.leancloud.cn'
14+ } ,
15+ // 服务器节点地区,默认中国大陆
16+ region : 'cn'
1417 } ) ;
1518
1619 /**
@@ -23,7 +26,7 @@ module.exports = function(AV) {
2326
2427 // Check whether we are running in Node.js.
2528 if ( typeof ( process ) !== 'undefined' && process . versions && process . versions . node ) {
26- AV . _config . isNode = true ;
29+ avConfig . isNode = true ;
2730 }
2831
2932 // Helpers
@@ -89,7 +92,7 @@ module.exports = function(AV) {
8992 applicationId !== AV . applicationId &&
9093 applicationKey !== AV . applicationKey &&
9194 masterKey !== AV . masterKey ) {
92- console . warn ( 'AVOSCloud SDK is already initialized, please don\'t reinitialize it.' ) ;
95+ console . warn ( 'LeanCloud SDK is already initialized, please don\'t reinitialize it.' ) ;
9396 }
9497 AV . applicationId = applicationId ;
9598 AV . applicationKey = applicationKey ;
@@ -112,18 +115,25 @@ module.exports = function(AV) {
112115 case 1 :
113116 const options = args [ 0 ] ;
114117 if ( typeof options === 'object' ) {
115- if ( ! AV . _config . isNode && options . masterKey ) {
118+ if ( ! avConfig . isNode && options . masterKey ) {
116119 throw new Error ( 'AV.init(): Master Key is only used in Node.js.' ) ;
117120 }
118121 initialize ( options . appId , options . appKey , options . masterKey ) ;
122+
123+ // 服务器地区选项,默认为中国大陆
124+ switch ( options . region ) {
125+ case 'us' :
126+ avConfig . region = 'us' ;
127+ break ;
128+ }
119129 } else {
120130 throw new Error ( 'AV.init(): Parameter is not correct.' ) ;
121131 }
122132 break ;
123133 // 兼容旧版本的初始化方法
124134 case 2 :
125135 case 3 :
126- if ( ! AV . _config . isNode && args . length === 3 ) {
136+ if ( ! avConfig . isNode && args . length === 3 ) {
127137 throw new Error ( 'AV.init(): Master Key is only used in Node.js.' ) ;
128138 }
129139 initialize ( ...args ) ;
@@ -132,11 +142,11 @@ module.exports = function(AV) {
132142 } ;
133143
134144 // If we're running in node.js, allow using the master key.
135- if ( AV . _config . isNode ) {
145+ if ( avConfig . isNode ) {
136146 AV . Cloud = AV . Cloud || { } ;
137147 /**
138- * Switches the AVOSCloud SDK to using the Master key. The Master key grants
139- * priveleged access to the data in AVOSCloud and can be used to bypass ACLs and
148+ * Switches the LeanCloud SDK to using the Master key. The Master key grants
149+ * priveleged access to the data in LeanCloud and can be used to bypass ACLs and
140150 * other restrictions that are applied to the client SDKs.
141151 * <p><strong><em>Available in Cloud Code and Node.js only.</em></strong>
142152 * </p>
@@ -164,23 +174,16 @@ module.exports = function(AV) {
164174 AV . applicationProduction = AV . _isNullOrUndefined ( production ) ? 1 : production ;
165175 } ;
166176
167- /**
168- *Use china avoscloud API service
169- */
177+ // TODO: 后续不再暴露此接口
170178 AV . useAVCloudCN = function ( ) {
171- AV . serverURL = AV . _config . cnApiUrl ;
179+ avConfig . region = 'cn' ;
172180 } ;
173181
174- /**
175- *Use USA avoscloud API service
176- */
182+ // TODO: 后续不再暴露此接口
177183 AV . useAVCloudUS = function ( ) {
178- AV . serverURL = AV . _config . usApiUrl ;
184+ avConfig . region = 'us' ;
179185 } ;
180186
181- // 默认使用国内节点
182- AV . useAVCloudCN ( ) ;
183-
184187 /**
185188 * Returns prefix for localStorage keys used by this instance of AV.
186189 * @param {String } path The relative suffix to append to it.
@@ -317,20 +320,21 @@ module.exports = function(AV) {
317320 throw "Bad route: '" + route + "'." ;
318321 }
319322
320- var url = AV . serverURL ;
321- if ( url . charAt ( url . length - 1 ) !== "/" ) {
322- url += "/" ;
323+ // 兼容 AV.serverURL 旧方式设置 API Host,后续去掉
324+ let apiUrl = AV . serverURL || avConfig . apiHost [ avConfig . region ] ;
325+ if ( apiUrl . charAt ( apiUrl . length - 1 ) !== "/" ) {
326+ apiUrl += "/" ;
323327 }
324- url += "1.1/" + route ;
328+ apiUrl += "1.1/" + route ;
325329 if ( className ) {
326- url += "/" + className ;
330+ apiUrl += "/" + className ;
327331 }
328332 if ( objectId ) {
329- url += "/" + objectId ;
333+ apiUrl += "/" + objectId ;
330334 }
331335 if ( ( route === 'users' || route === 'classes' ) && dataObject && dataObject . _fetchWhenSave ) {
332336 delete dataObject . _fetchWhenSave ;
333- url += '?new=true' ;
337+ apiUrl += '?new=true' ;
334338 }
335339
336340 dataObject = AV . _ . clone ( dataObject || { } ) ;
@@ -357,7 +361,7 @@ module.exports = function(AV) {
357361 dataObject . _InstallationId = _InstallationId ;
358362
359363 var data = JSON . stringify ( dataObject ) ;
360- return AV . _ajax ( method , url , data ) . then ( null , function ( response ) {
364+ return AV . _ajax ( method , apiUrl , data ) . then ( null , function ( response ) {
361365 // Transform the error into an instance of AV.Error by trying to parse
362366 // the error string as JSON.
363367 var error ;
@@ -398,7 +402,6 @@ module.exports = function(AV) {
398402 * is set, then none of the AV Objects that are serialized can be dirty.
399403 */
400404 AV . _encode = function ( value , seenObjects , disallowObjects ) {
401- var _ = AV . _ ;
402405 if ( value instanceof AV . Object ) {
403406 if ( disallowObjects ) {
404407 throw "AV.Objects not allowed here" ;
@@ -463,7 +466,6 @@ module.exports = function(AV) {
463466 * TODO: make decode not mutate value.
464467 */
465468 AV . _decode = function ( key , value ) {
466- var _ = AV . _ ;
467469 if ( ! _ . isObject ( value ) ) {
468470 return value ;
469471 }
@@ -613,7 +615,6 @@ module.exports = function(AV) {
613615 * * it does work for dictionaries with a "length" attribute.
614616 */
615617 AV . _objectEach = AV . _each = function ( obj , callback ) {
616- var _ = AV . _ ;
617618 if ( _ . isObject ( obj ) ) {
618619 _ . each ( _ . keys ( obj ) , function ( key ) {
619620 callback ( obj [ key ] , key ) ;
0 commit comments