alipay-mini-program-request-options
Version:
210 lines (192 loc) • 5.49 kB
JavaScript
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _require = require('../package.json'),
version = _require.version;
var Methods = {
GET: 'GET',
POST: 'POST',
PUT: 'PUT',
DELETE: 'DELETE'
};
var FileTypes = {
IMAGE: 'image',
VIDEO: 'video',
AUDIO: 'audio'
};
var DataType = ['json', 'text', 'base64', 'arraybuffer'];
var DEFAULT_TIMEOUT = 60000;
var DEFAULT_DATA_TYPE = 'json';
var Options = function () {
function Options(url) {
_classCallCheck(this, Options);
this.url = url;
this.data = {};
this.headers = {};
this.method = Methods.GET;
this.timeout = DEFAULT_TIMEOUT;
this.dataType = DEFAULT_DATA_TYPE;
}
_createClass(Options, [{
key: 'setUrl',
value: function setUrl(url) {
this.url = url;
return this;
}
}, {
key: 'setData',
value: function setData(data) {
this.data = data || {};
return this;
}
}, {
key: 'setMethod',
value: function setMethod(method) {
if (Object.keys(Methods).includes(method)) {
this.method = method || Methods.GET;
}
return this;
}
}, {
key: 'setHeaders',
value: function setHeaders(headers) {
this.headers = headers || {};
return this;
}
}, {
key: 'setTimeout',
value: function setTimeout(timeout) {
this.timeout = timeout || DEFAULT_TIMEOUT;
return this;
}
}, {
key: 'setDataType',
value: function setDataType(dataType) {
if (DataType.includes(dataType)) {
this.dataType = dataType || DEFAULT_DATA_TYPE;
}
return this;
}
}, {
key: 'request',
value: function request(complete) {
var _this = this;
return new Promise(function (resolve, reject) {
var url = _this.url,
method = _this.method,
headers = _this.headers,
data = _this.data,
dataType = _this.dataType;
my.request({
url: url,
method: method,
headers: headers,
data: data,
dataType: dataType,
success: function success(res) {
if (dataType.toLowerCase() === 'json') {
resolve(res.data);
} else {
resolve(res);
}
},
fail: function fail(err) {
reject(err);
},
complete: complete
});
});
}
}]);
return Options;
}();
var UploadOptions = function () {
function UploadOptions(url) {
_classCallCheck(this, UploadOptions);
this.url = url;
this.headers = {};
this.formData = undefined;
this.filePath = '';
this.fileName = '';
this.fileType = FileTypes.IMAGE;
}
_createClass(UploadOptions, [{
key: 'setUrl',
value: function setUrl(url) {
this.url = url;
return this;
}
}, {
key: 'setFormData',
value: function setFormData(formData) {
this.formData = formData || undefined;
return this;
}
}, {
key: 'setFile',
value: function setFile(filePath, fileName, fileType) {
this.filePath = filePath;
this.fileName = fileName;
this.fileType = fileType || FileTypes.IMAGE;
return this;
}
}, {
key: 'setFilePath',
value: function setFilePath(filePath) {
this.filePath = filePath;
return this;
}
}, {
key: 'setFileName',
value: function setFileName(fileName) {
this.fileName = fileName;
return this;
}
}, {
key: 'setFileType',
value: function setFileType(fileType) {
this.fileType = fileType || FileTypes.IMAGE;
return this;
}
}, {
key: 'setHeaders',
value: function setHeaders(headers) {
this.headers = headers || {};
return this;
}
}, {
key: 'upload',
value: function upload(complete) {
var _this2 = this;
return new Promise(function (resolve, reject) {
var url = _this2.url,
headers = _this2.headers,
filePath = _this2.filePath,
fileName = _this2.fileName,
fileType = _this2.fileType,
formData = _this2.formData;
my.uploadFile({
url: url,
header: headers,
filePath: filePath, fileName: fileName, fileType: fileType, formData: formData,
success: function success(res) {
var data = res.data;
resolve(typeof data === 'string' && (data.startsWith('{') || data.startsWith('[')) ? JSON.parse(data) : data);
},
fail: function fail(err) {
reject(err);
},
complete: complete
});
});
}
}]);
return UploadOptions;
}();
module.exports = {
version: version,
Methods: Methods,
Options: Options,
FileTypes: FileTypes,
UploadOptions: UploadOptions
};