Skip to content

Commit 6757218

Browse files
committed
提出 _config.apiServerUrl 。
1 parent fc0139c commit 6757218

File tree

5 files changed

+39
-30
lines changed

5 files changed

+39
-30
lines changed

src/utils.js

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ module.exports = function(AV) {
1212
// 挂载一些配置
1313
let AVConfig = AV._config;
1414

15-
_.extend(AVConfig, {
15+
// 服务器请求的节点 host
16+
const API_HOST = {
17+
cn: 'https://api.leancloud.cn',
18+
us: 'https://us-api.leancloud.cn'
19+
};
1620

17-
// 服务器请求的节点 host
18-
apiHost : {
19-
cn: 'https://api.leancloud.cn',
20-
us: 'https://us-api.leancloud.cn'
21-
},
21+
_.extend(AVConfig, {
2222

2323
// 服务器节点地区,默认中国大陆
24-
region: 'cn'
24+
region: 'cn',
25+
26+
// 服务器的 URL,默认初始化时被设置为大陆节点地址
27+
apiServerUrl: AVConfig.apiServerUrl || ''
2528
});
2629

2730
/**
@@ -95,28 +98,29 @@ module.exports = function(AV) {
9598
* @param {String} applicationId Your AV Application ID.
9699
* @param {String} applicationKey Your AV Application Key
97100
*/
98-
const initialize = (applicationId, applicationKey, masterKey) => {
99-
if (AV.applicationId !== undefined &&
100-
applicationId !== AV.applicationId &&
101-
applicationKey !== AV.applicationKey &&
102-
masterKey !== AV.masterKey) {
103-
console.warn('LeanCloud SDK is already initialized, please don\'t reinitialize it.');
104-
}
105-
AV.applicationId = applicationId;
106-
AV.applicationKey = applicationKey;
101+
const initialize = (appId, appKey, masterKey) => {
102+
if (AV.applicationId && appId !== AV.applicationId && appKey !== AV.applicationKey && masterKey !== AV.masterKey) {
103+
console.warn('LeanCloud SDK is already initialized, please do not reinitialize it.');
104+
}
105+
AV.applicationId = appId;
106+
AV.applicationKey = appKey;
107107
AV.masterKey = masterKey;
108108
AV._useMasterKey = false;
109109
};
110110

111-
const setRegion = (region) => {
111+
const setRegionServer = (region) => {
112112
// 服务器地区选项,默认为中国大陆
113113
switch (region) {
114-
case 'cn':
115-
AVConfig.region = 'cn';
116-
break;
117114
case 'us':
118115
AVConfig.region = 'us';
119116
break;
117+
case 'cn':
118+
default:
119+
AVConfig.region = 'cn';
120+
break;
121+
}
122+
if (!AVConfig.apiServerUrl) {
123+
AVConfig.apiServerUrl = API_HOST[AVConfig.region];
120124
}
121125
};
122126

@@ -139,7 +143,7 @@ module.exports = function(AV) {
139143
throw new Error('AV.init(): Master Key is only used in Node.js.');
140144
}
141145
initialize(options.appId, options.appKey, options.masterKey);
142-
setRegion(options.region);
146+
setRegionServer(options.region);
143147
} else {
144148
throw new Error('AV.init(): Parameter is not correct.');
145149
}
@@ -152,6 +156,7 @@ module.exports = function(AV) {
152156
throw new Error('AV.init(): Master Key is only used in Node.js.');
153157
}
154158
initialize(...args);
159+
setRegionServer('cn');
155160
break;
156161
}
157162
};
@@ -194,7 +199,7 @@ module.exports = function(AV) {
194199
**/
195200
// TODO: 后续不再暴露此接口
196201
AV.useAVCloudCN = function(){
197-
AVConfig.region = 'cn';
202+
setRegionServer('cn');
198203
console.warn('Do not use AV.useAVCloudCN. Please use AV.init(), you can set the region of server.');
199204
};
200205

@@ -203,7 +208,7 @@ module.exports = function(AV) {
203208
**/
204209
// TODO: 后续不再暴露此接口
205210
AV.useAVCloudUS = function(){
206-
AVConfig.region = 'us';
211+
setRegionServer('us');
207212
console.warn('Do not use AV.useAVCloudUS. Please use AV.init(), you can set the region of server.');
208213
};
209214

@@ -344,7 +349,11 @@ module.exports = function(AV) {
344349
}
345350

346351
// 兼容 AV.serverURL 旧方式设置 API Host,后续去掉
347-
let apiUrl = AV.serverURL || AVConfig.apiHost[AVConfig.region];
352+
let apiUrl = AV.serverURL || AVConfig.apiServerUrl;
353+
if (AV.serverURL) {
354+
AVConfig.apiServerUrl = AV.serverURL;
355+
console.warn('Please use AV._config.apiServerUrl replace AV.serverURL .');
356+
}
348357
if (apiUrl.charAt(apiUrl.length - 1) !== "/") {
349358
apiUrl += "/";
350359
}

test/cloud.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ describe("AV.Cloud", function() {
44
var originalServerURL, originalAppId, originalAppKey, originalUseMasterKey;
55

66
before(function() {
7-
originalServerURL = AV.serverURL;
7+
originalServerURL = AV._config.apiServerUrl;
88
originalAppId = AV.applicationId;
99
originalAppKey = AV.applicationKey;
1010
originalUseMasterKey = AV._useMasterKey;
1111

12-
AV.serverURL = 'https://leancloud.cn'
12+
AV._config.apiServerUrl = 'https://leancloud.cn'
1313
AV.applicationId = '4h2h4okwiyn8b6cle0oig00vitayum8ephrlsvg7xo8o19ne';
1414
AV.applicationKey = '3xjj1qw91cr3ygjq9lt0g8c3qpet38rrxtwmmp0yffyoy2t4';
1515
AV._useMasterKey = false;
@@ -116,7 +116,7 @@ describe("AV.Cloud", function() {
116116
});
117117

118118
after(function() {
119-
AV.serverURL = originalServerURL;
119+
AV._config.apiServerUrl = originalServerURL;
120120
AV.applicationId = originalAppId;
121121
AV.applicationKey = originalAppKey;
122122
AV._useMasterKey = originalUseMasterKey;

test/file_blob.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
return bb.getBlob(mimeString);
6868
};
6969
function upload (){
70-
AV.serverURL="https://cn-stg1.avoscloud.com";
70+
AV._config.apiServerUrl="https://cn-stg1.avoscloud.com";
7171
AV.init('mxrb5nn3qz7drek0etojy5lh4yrwjnk485lqajnsgjwfxrb5', 'd7sbus0d81mrum4tko4t8gl74b27vl0rh762ff7ngrb6ymmq', 'l0n9wu3kwnrtf2cg1b6w2l87nphzpypgff6240d0lxui2mm4');
7272
AV._useMasterKey = true;
7373
AV.setProduction(true);

test/file_form.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<script src="../dist/av-mini.js"></script>
1717
<script>
1818
function upload (){
19-
AV.serverURL="https://cn.avoscloud.com";
19+
AV._config.apiServerUrl="https://cn.avoscloud.com";
2020
AV.init('u8d277gijfac1ysldfyb92zn9fwhxsiecjs3wnrnj4yi7i00', 'nblwmv06uzbjp170pm1zw3umhrbhs75wp6djgutcglh5sopr', 'c2cbbq9fnrewpvtwm7lm25exs62hk0tnkmbs4q16c4podmdc');
2121
AV._useMasterKey = true;
2222

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ if (typeof require !== 'undefined') {
77
GLOBAL.AV = require('../dist/node/av');
88
}
99

10-
AV.serverURL = 'https://cn-stg1.avoscloud.com';
10+
AV._config.apiServerUrl = 'https://cn-stg1.avoscloud.com';
1111
AV.init({
1212
appId: 'mxrb5nn3qz7drek0etojy5lh4yrwjnk485lqajnsgjwfxrb5',
1313
appKey: 'd7sbus0d81mrum4tko4t8gl74b27vl0rh762ff7ngrb6ymmq',

0 commit comments

Comments
 (0)