Skip to content

Commit

Permalink
支持 Promise 和 async/await 格式
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Sep 21, 2020
1 parent 60c631b commit 4eee462
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 122 deletions.
178 changes: 96 additions & 82 deletions dist/cos-js-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ return /******/ (function(modules) { // webpackBootstrap
/******/ __webpack_require__.p = "/dist/";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 4);
/******/ return __webpack_require__(__webpack_require__.s = 5);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";
/* WEBPACK VAR INJECTION */(function(global) {


var md5 = __webpack_require__(6);
var md5 = __webpack_require__(7);
var CryptoJS = __webpack_require__(10);
var xml2json = __webpack_require__(11);
var json2xml = __webpack_require__(14);
Expand Down Expand Up @@ -486,6 +486,8 @@ var formatParams = function (apiName, params) {
var apiWrapper = function (apiName, apiFn) {
return function (params, callback) {

var self = this;

// 处理参数
if (typeof params === 'function') {
callback = params;
Expand All @@ -507,52 +509,63 @@ var apiWrapper = function (apiName, apiFn) {
callback && callback(formatResult(err), formatResult(data));
};

if (apiName !== 'getService' && apiName !== 'abortUploadTask') {
// 判断参数是否完整
var missingResult = hasMissingParams(apiName, params);
if (missingResult) {
_callback({ error: 'missing param ' + missingResult });
return;
}
// 判断 region 格式
if (params.Region) {
if (params.Region.indexOf('cos.') > -1) {
_callback({ error: 'param Region should not be start with "cos."' });
return;
} else if (!/^([a-z\d-]+)$/.test(params.Region)) {
_callback({ error: 'Region format error.' });
return;
var checkParams = function () {
if (apiName !== 'getService' && apiName !== 'abortUploadTask') {
// 判断参数是否完整
var missingResult = hasMissingParams(apiName, params);
if (missingResult) {
return 'missing param ' + missingResult;
}
// 判断 region 格式
if (!this.options.CompatibilityMode && params.Region.indexOf('-') === -1 && params.Region !== 'yfb' && params.Region !== 'default') {
console.warn('warning: param Region format error, find help here: https://cloud.tencent.com/document/product/436/6224');
if (params.Region) {
if (params.Region.indexOf('cos.') > -1) {
return 'param Region should not be start with "cos."';
} else if (!/^([a-z\d-]+)$/.test(params.Region)) {
return 'Region format error.';
}
// 判断 region 格式
if (!self.options.CompatibilityMode && params.Region.indexOf('-') === -1 && params.Region !== 'yfb' && params.Region !== 'default') {
console.warn('warning: param Region format error, find help here: https://cloud.tencent.com/document/product/436/6224');
}
}
}
// 兼容不带 AppId 的 Bucket
if (params.Bucket) {
if (!/^([a-z\d-]+)-(\d+)$/.test(params.Bucket)) {
// 兼容不带 AppId 的 Bucket
if (params.Bucket) {
if (!/^([a-z\d-]+)-(\d+)$/.test(params.Bucket)) {
if (params.AppId) {
params.Bucket = params.Bucket + '-' + params.AppId;
} else if (self.options.AppId) {
params.Bucket = params.Bucket + '-' + self.options.AppId;
} else {
return 'Bucket should format as "test-1250000000".';
}
}
if (params.AppId) {
params.Bucket = params.Bucket + '-' + params.AppId;
} else if (this.options.AppId) {
params.Bucket = params.Bucket + '-' + this.options.AppId;
} else {
_callback({ error: 'Bucket should format as "test-1250000000".' });
return;
console.warn('warning: AppId has been deprecated, Please put it at the end of parameter Bucket(E.g Bucket:"test-1250000000" ).');
delete params.AppId;
}
}
if (params.AppId) {
console.warn('warning: AppId has been deprecated, Please put it at the end of parameter Bucket(E.g Bucket:"test-1250000000" ).');
delete params.AppId;
// 如果 Key 是 / 开头,强制去掉第一个 /
if (!self.options.UseRawKey && params.Key && params.Key.substr(0, 1) === '/') {
params.Key = params.Key.substr(1);
}
}
// 如果 Key 是 / 开头,强制去掉第一个 /
if (!this.options.UseRawKey && params.Key && params.Key.substr(0, 1) === '/') {
params.Key = params.Key.substr(1);
}
}
var res = apiFn.call(this, params, _callback);
if (apiName === 'getAuth' || apiName === 'getObjectUrl') {
return res;
};

var errMsg = checkParams();
var isSync = apiName === 'getAuth' || apiName === 'getObjectUrl';
var Promise = global.Promise;
if (!isSync && Promise && !callback) {
return new Promise(function (resolve, reject) {
callback = function (err, data) {
err ? reject(err) : resolve(data);
};
if (errMsg) return _callback({ error: errMsg });
apiFn.call(self, params, _callback);
});
} else {
if (errMsg) return _callback({ error: errMsg });
var res = apiFn.call(self, params, _callback);
if (isSync) return res;
}
};
};
Expand Down Expand Up @@ -652,11 +665,39 @@ var util = {
};

module.exports = util;
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(1)))

/***/ }),
/* 1 */
/***/ (function(module, exports) {

var g;

// This works in non-strict mode
g = (function() {
return this;
})();

try {
// This works if eval is allowed (see CSP)
g = g || Function("return this")() || (1,eval)("this");
} catch(e) {
// This works if the window reference is available
if(typeof window === "object")
g = window;
}

// g can still be undefined, but nothing to do about it...
// We return undefined, instead of nothing here, so it's
// easier to handle this case. if(!global) { ...}

module.exports = g;


/***/ }),
/* 2 */
/***/ (function(module, exports) {

/*
* DOM Level 2
* Object DOMException
Expand Down Expand Up @@ -1904,7 +1945,7 @@ try{


/***/ }),
/* 2 */
/* 3 */
/***/ (function(module, exports) {

var initEvent = function (cos) {
Expand Down Expand Up @@ -1943,7 +1984,7 @@ module.exports.init = initEvent;
module.exports.EventProxy = EventProxy;

/***/ }),
/* 3 */
/* 4 */
/***/ (function(module, exports, __webpack_require__) {

var util = __webpack_require__(0);
Expand Down Expand Up @@ -2049,21 +2090,21 @@ var mod = {
module.exports = mod;

/***/ }),
/* 4 */
/* 5 */
/***/ (function(module, exports, __webpack_require__) {

var COS = __webpack_require__(5);
var COS = __webpack_require__(6);
module.exports = COS;

/***/ }),
/* 5 */
/* 6 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";


var util = __webpack_require__(0);
var event = __webpack_require__(2);
var event = __webpack_require__(3);
var task = __webpack_require__(15);
var base = __webpack_require__(16);
var advance = __webpack_require__(18);
Expand Down Expand Up @@ -2126,7 +2167,7 @@ COS.version = '0.6.0';
module.exports = COS;

/***/ }),
/* 6 */
/* 7 */
/***/ (function(module, exports, __webpack_require__) {

/* WEBPACK VAR INJECTION */(function(process, global) {var __WEBPACK_AMD_DEFINE_RESULT__;/**
Expand Down Expand Up @@ -2804,10 +2845,10 @@ module.exports = COS;
}
}
})();
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(7), __webpack_require__(8)))
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(8), __webpack_require__(1)))

/***/ }),
/* 7 */
/* 8 */
/***/ (function(module, exports) {

// shim for using process in browser
Expand Down Expand Up @@ -2996,33 +3037,6 @@ process.chdir = function (dir) {
process.umask = function() { return 0; };


/***/ }),
/* 8 */
/***/ (function(module, exports) {

var g;

// This works in non-strict mode
g = (function() {
return this;
})();

try {
// This works if eval is allowed (see CSP)
g = g || Function("return this")() || (1,eval)("this");
} catch(e) {
// This works if the window reference is available
if(typeof window === "object")
g = window;
}

// g can still be undefined, but nothing to do about it...
// We return undefined, instead of nothing here, so it's
// easier to handle this case. if(!global) { ...}

module.exports = g;


/***/ }),
/* 9 */
/***/ (function(module, exports) {
Expand Down Expand Up @@ -3697,8 +3711,8 @@ function appendElement (hander,node) {

//if(typeof require == 'function'){
var XMLReader = __webpack_require__(13).XMLReader;
var DOMImplementation = exports.DOMImplementation = __webpack_require__(1).DOMImplementation;
exports.XMLSerializer = __webpack_require__(1).XMLSerializer ;
var DOMImplementation = exports.DOMImplementation = __webpack_require__(2).DOMImplementation;
exports.XMLSerializer = __webpack_require__(2).XMLSerializer ;
exports.DOMParser = DOMParser;
//}

Expand Down Expand Up @@ -4505,7 +4519,7 @@ module.exports = function (obj, options) {
/* 15 */
/***/ (function(module, exports, __webpack_require__) {

var session = __webpack_require__(3);
var session = __webpack_require__(4);
var util = __webpack_require__(0);

var originApiMap = {};
Expand Down Expand Up @@ -12390,9 +12404,9 @@ module.exports = request;
/* 18 */
/***/ (function(module, exports, __webpack_require__) {

var session = __webpack_require__(3);
var session = __webpack_require__(4);
var Async = __webpack_require__(19);
var EventProxy = __webpack_require__(2).EventProxy;
var EventProxy = __webpack_require__(3).EventProxy;
var util = __webpack_require__(0);

// 文件分块上传全过程,暴露的分块上传接口
Expand Down
Loading

0 comments on commit 4eee462

Please sign in to comment.