UNPKG

restfulapi-sdk

Version:
551 lines (462 loc) 18.1 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define([], factory); else if(typeof exports === 'object') exports["APISDK"] = factory(); else root["APISDK"] = factory(); })(this, function() { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ exports: {}, /******/ id: moduleId, /******/ loaded: false /******/ }; /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ // Flag the module as loaded /******/ module.loaded = true; /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ // __webpack_public_path__ /******/ __webpack_require__.p = "/dist/"; /******/ // Load entry module and return exports /******/ return __webpack_require__(0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ function(module, exports, __webpack_require__) { module.exports = __webpack_require__(1); /***/ }, /* 1 */ /***/ function(module, exports, __webpack_require__) { '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"); } } function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } var Cache = __webpack_require__(2); var http = __webpack_require__(4); // 遍历url对象,生成api对象。 var walk = function walk(obj) {}; // 深拷贝对象 var copy = function copy(obj) { var newObj = {}; var walk = function walk(obj, newObj) { for (var key in obj) { if (_typeof(obj[key]) === 'object') { newObj[key] = {}; walk(obj[key], newObj[key]); } else if (typeof obj[key] === 'string') { newObj[key] = obj[key]; } } }; walk(obj, newObj); return newObj; }; // 生成APISDK对象 var APISDK = (function () { function APISDK(url, config) { _classCallCheck(this, APISDK); this.http = new http(config); var api = copy(url); this.walk(api); return api; } _createClass(APISDK, [{ key: 'walk', value: function walk(obj) { var _this = this; for (var key in obj) { if (_typeof(obj[key]) === 'object') { this.walk(obj[key]); } else if (typeof obj[key] === 'string') { var url = obj[key]; obj[key] = (function (url) { return _this.http.getObj(url); })(url); } } } }]); return APISDK; })(); ; APISDK.all = __webpack_require__(5).all; APISDK.httpCode = http.httpCode; APISDK.removeCache = http.removeCache; // 这里是一个坑 // var isWindow = (Function('return this')().constructor + '').match(/ (\w+)|$/)[1] === 'Window'; // if(isWindow) window.APISDK = APISDK; module.exports = APISDK; /***/ }, /* 2 */ /***/ function(module, exports, __webpack_require__) { /* WEBPACK VAR INJECTION */(function(module) {"use strict"; function _typeof2(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } function _typeof(e) { return e && "undefined" != typeof Symbol && e.constructor === Symbol ? "symbol" : typeof e === "undefined" ? "undefined" : _typeof2(e); }var getDate = function getDate(e) { return new Date(e).getDate(); }, Cache = { init: function init(e) { this.limit = e.overdue ? null : e.limit || 3600, this.overdue = e.overdue || 1, this.prefix = (e.prefix || "cache") + "_"; }, deal: function deal(e, t, o) { var a = o ? sessionStorage : localStorage, i = this.limit, r = this.overdue, n = this.prefix, s = a.getItem(n + e), f = Date.now();s ? (s = JSON.parse(s), i ? f - s.createAt > 1e3 * i ? t(!0, s.data) : t(!1, s.data) : r && (getDate(Date.now()) - getDate(s.createAt) > r - 1 ? t(!0, s.data) : t(!1, s.data))) : t(!0, null); }, save: function save(e, t, o) { var a = o ? sessionStorage : localStorage, i = this.prefix, r = { createAt: Date.now(), data: t };a.setItem(i + e, JSON.stringify(r)); }, remove: function remove(e, t) { "string" != typeof e && (t = e, e = null);var o = t ? sessionStorage : localStorage, a = this.prefix;if (e) o.removeItem(a + e);else for (var i in o) { 0 === i.indexOf(a) && o.removeItem(i); } } };"object" === ( false ? "undefined" : _typeof(module)) && (module.exports = Cache); /* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(3)(module))) /***/ }, /* 3 */ /***/ function(module, exports) { "use strict"; module.exports = function (module) { if (!module.webpackPolyfill) { module.deprecate = function () {}; module.paths = []; // module.parent = undefined by default module.children = []; module.webpackPolyfill = 1; } return module; }; /***/ }, /* 4 */ /***/ function(module, exports, __webpack_require__) { '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 Cache = __webpack_require__(2); var Promise = __webpack_require__(5); // 尝试解析json var parse = function parse(str) { try { str = JSON.parse(str); } catch (e) { // this is a normal String } return str; }; // 根据参数对象生成参数 var getParamStr = function getParamStr(params) { var paramStr = ''; for (var key in params) { if (!paramStr) paramStr += key + '=' + encodeURI(params[key]);else paramStr += '\&' + key + '=' + encodeURI(params[key]); } return paramStr; }; // 解析url和参数 var parseUrl = function parseUrl(url, params) { params = JSON.parse(JSON.stringify(params)); var proto = url.match(/:\w+/g); proto && proto.forEach(function (item) { var key = item.replace(':', ''); if (params[key] != null) { url = url.replace(new RegExp(item, 'g'), params[key].toString()); delete params[key]; } else { url = url.replace(new RegExp(item + '/?', 'g'), ''); } }); var paramStr = getParamStr(params); return url + (paramStr ? '?' + paramStr : ''); }; // 生成四种方法 var http = (function () { function http(config) { _classCallCheck(this, http); if (typeof config === 'undefined' || config.cache) { this.config = config || {}; this.config.urlPrefix = config.urlPrefix || ''; Cache.init({ limit: this.config.overdue || 3600, overdue: this.config.overdueDay || null, prefix: this.config.cachePrefix || 'api' }); this.config.isSession = config.isSession || false; this.config.needCache = true; } else { this.config = {}; this.config.urlPrefix = config.urlPrefix || ''; this.config.needCache = false; } } _createClass(http, [{ key: 'getObj', value: function getObj(url) { var _this = this; if (!url) return null; var obj = { get: this.getInitMethod('GET', url), post: this.getInitMethod('POST', url), put: this.getInitMethod('PUT', url), del: this.getInitMethod('DELETE', url), patch: this.getInitMethod('PATCH', url), options: this.getInitMethod('OPTIONS', url) }; if (this.config.needCache) { obj.cache = function () { for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return new Promise(_this.getCacheFunc.bind(_this), url, args); }; } return obj; } }, { key: 'getInitMethod', value: function getInitMethod(method, url) { var _this2 = this; return function () { for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } return new Promise(_this2.getSendFunc.bind(_this2), method, url, args); }; } }, { key: 'getSendFunc', // 生成调用接口的函数 value: function getSendFunc(method, url, args, defer) { var _config = this.config; var needCache = _config.needCache; var isSession = _config.isSession; var urlPrefix = _config.urlPrefix; // 直接调用接口 var req = new XMLHttpRequest(); var realUrl = ''; req.onreadystatechange = function () { if (req.readyState === 4) { if (http.codeCallback) http.codeCallback(req.status); if (req.status >= 200 && req.status < 300) { var res = parse(req.responseText); if (needCache && method === 'get') { // 存入缓存操作 Cache.save(realUrl, res, isSession); } defer(true, res, req.status); } else { defer(false, parse(req.responseText), req.status); } } }; var openReq = function openReq() { req.open(method, realUrl, true); if (method !== 'get') req.setRequestHeader('Content-type', 'application/json'); }; switch (args.length) { case 0: realUrl = urlPrefix + parseUrl(url, {}); openReq(); req.send(); break; case 1: realUrl = urlPrefix + parseUrl(url, args[0]); openReq(); req.send(); break; case 2: realUrl = urlPrefix + parseUrl(url, args[0]); openReq(); req.send(JSON.stringify(args[1])); break; } } }, { key: 'getCacheFunc', // 得到获取缓存数据的函数 value: function getCacheFunc(url, args, defer) { var _this3 = this; var _config2 = this.config; var urlPrefix = _config2.urlPrefix; var isSession = _config2.isSession; var realUrl = ''; var deal = function deal() { Cache.deal(realUrl, function (overdue, data) { if (overdue || !data) { // 请求接口数据 _this3.getSendFunc('get', url, args, defer); } else { setTimeout(function () { if (http.codeCallback) http.codeCallback(200); defer(true, data, 200); }); } }, isSession); }; switch (args.length) { case 0: realUrl = urlPrefix + parseUrl(url, {}); deal(); break; case 1: case 2: realUrl = urlPrefix + parseUrl(url, args[0]); deal(); break; } } }], [{ key: 'httpCode', value: function httpCode(callback) { if (typeof callback === 'function') http.codeCallback = callback; } }, { key: 'removeCache', value: function removeCache(key, isSession) { Cache.remove(key, isSession); } }]); return http; })(); ; module.exports = http; /***/ }, /* 5 */ /***/ function(module, exports) { "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 _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } // 一个简单的实现 // 传入一个函数与它的参数,在函数中声明时可加上最后一个参数defer, 为一个回调,调用方式为: // defer(true, params) // 触发下一轮的success函数 // defer(false, params) // 触发下一轮的error函数 var Promise = (function () { function Promise(todoFunc) { _classCallCheck(this, Promise); this.list = []; this.status = 0; // 当前处理的状态(未处理, 通过与拒绝) this.data = null; // 当期的数据 this.progress = -1; // 当前promise处理到的坐标 this.finish = false; // 本身的then是否已经完成 for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } this.runFunc.apply(this, [todoFunc].concat(args)); } _createClass(Promise, [{ key: "defer", // defer中的操作 value: function defer(resolve) { var list = this.list; var progress = this.progress; for (var _len2 = arguments.length, newArgs = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { newArgs[_key2 - 1] = arguments[_key2]; } if (newArgs) this.data = Array.isArray(newArgs) ? newArgs : [newArgs]; if (resolve) { this.status = 0; } else { this.status = 1; } this.progress++; if (list[this.progress]) { // 如果有的话,处理then的数组 var func = list[this.progress][this.status]; this.runFunc.apply(this, [func].concat(_toConsumableArray(this.data))); } else { this.finish = true; } } }, { key: "runFunc", // 顺序处理then value: function runFunc(todoFunc) { var _this = this; for (var _len3 = arguments.length, args = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { args[_key3 - 1] = arguments[_key3]; } if (todoFunc) todoFunc.apply(undefined, args.concat([function (resolve) { for (var _len4 = arguments.length, newArgs = Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) { newArgs[_key4 - 1] = arguments[_key4]; } _this.defer.apply(_this, [resolve].concat(newArgs)); }])); } }, { key: "then", // 链式push then数组 value: function then() { var _this2 = this; for (var _len5 = arguments.length, item = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) { item[_key5] = arguments[_key5]; } this.list.push(item); return { then: function then() { _this2.then.apply(_this2, arguments); return _this2; } }; } }], [{ key: "all", value: function all(promiseArr) { return new Promise(function (defer) { // 给数组元素加上序列 var done = function done(pass, args, i) { // 删掉refer的一项 args.pop(); if (pass) successArr.push([i].concat(_toConsumableArray(args)));else errorArr.push([i].concat(_toConsumableArray(args))); count++; if (count === promiseArr.length) { defer(true, successArr, errorArr); } }; var successArr = []; var errorArr = []; var count = 0; promiseArr.forEach(function (promise, i) { promise.then(function () { for (var _len6 = arguments.length, args = Array(_len6), _key6 = 0; _key6 < _len6; _key6++) { args[_key6] = arguments[_key6]; } done(true, args, i); }, function () { for (var _len7 = arguments.length, args = Array(_len7), _key7 = 0; _key7 < _len7; _key7++) { args[_key7] = arguments[_key7]; } done(false, args, i); }); }); }); } }]); return Promise; })(); module.exports = Promise; /***/ } /******/ ]) }); ;