|
1 | 1 | /* |
2 | 2 | Promise based wx.request api for Mini Program |
3 | 3 | @Github https://github.com/jonnyshao/wechat-fetch |
4 | | - wefetch beta v1.2.10 |(c) 2018-2019 By Jonny Shao |
| 4 | + wefetch beta v1.3.01 |(c) 2018-2019 By Jonny Shao |
5 | 5 | */ |
6 | 6 | (function (global, factory) { |
7 | 7 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : |
8 | | - typeof define === 'function' && define.amd ? define(factory) : |
9 | | - (global = global || self, global.wefetch = factory()); |
10 | | -}(this, function () { 'use strict'; |
| 8 | + typeof define === 'function' && define.amd ? define(factory) : |
| 9 | + (global = global || self, global.wefetch = factory()); |
| 10 | +}(this, function () { |
| 11 | + 'use strict'; |
11 | 12 |
|
12 | 13 | function Events() { |
13 | 14 | this.listeners = {}; |
|
31 | 32 |
|
32 | 33 | var e = new Events(); |
33 | 34 |
|
34 | | - function promisify (api) { |
35 | | - return function (options) { |
| 35 | + function promisify(api) { |
| 36 | + return function (options) { |
36 | 37 | options = options || {}; |
37 | 38 | options.config = options.config || {}; |
38 | 39 | for (var len = arguments.length, params = Array(len > 1 ? len - 1 : 0), key = 1; key < len; key++) { |
39 | 40 | params[key - 1] = arguments[key]; |
40 | 41 | } |
41 | 42 | return new Promise(function (resolve, reject) { |
42 | | - options.config.eventType ? e.emit(options.config.eventType, api.apply(undefined, [Object.assign({}, options, {success: resolve, fail: reject})].concat(params))) |
43 | | - : api.apply(undefined, [Object.assign({}, options, {success: resolve, fail: reject})].concat(params)); |
| 43 | + options.config.eventType ? e.emit(options.config.eventType, api.apply(undefined, [Object.assign({}, options, { success: resolve, fail: reject })].concat(params))) |
| 44 | + : api.apply(undefined, [Object.assign({}, options, { success: resolve, fail: reject })].concat(params)); |
44 | 45 | }) |
45 | 46 | }; |
46 | 47 | } |
|
57 | 58 | } catch (e) { |
58 | 59 | try { |
59 | 60 | if (my.request) { |
60 | | - this.platform = 'my'; |
61 | | - return promisify(my.request) |
62 | | - } else if (my.httpRequest){ |
63 | | - this.platform = 'my'; |
64 | | - return promisify(my.httpRequest) |
| 61 | + this.platform = 'my'; |
| 62 | + return promisify(my.request) |
| 63 | + } else if (my.httpRequest) { |
| 64 | + this.platform = 'my'; |
| 65 | + return promisify(my.httpRequest) |
65 | 66 | } |
66 | | - }catch (e) { |
67 | | - try{ |
68 | | - if (tt.request) { |
69 | | - this.platform = 'tt'; |
70 | | - return promisify(tt.request) |
71 | | - } |
72 | | - }catch (e) { |
73 | | - if (swan.request) { |
74 | | - this.platform = 'swan'; |
75 | | - return promisify(swan.request) |
76 | | - } |
| 67 | + } catch (e) { |
| 68 | + try { |
| 69 | + if (tt.request) { |
| 70 | + this.platform = 'tt'; |
| 71 | + return promisify(tt.request) |
| 72 | + } |
| 73 | + } catch (e) { |
| 74 | + if (swan.request) { |
| 75 | + this.platform = 'swan'; |
| 76 | + return promisify(swan.request) |
| 77 | + } |
77 | 78 | } |
78 | 79 | } |
79 | 80 | } |
80 | 81 | }; |
81 | 82 | Platform.prototype.getUpload = function () { |
82 | | - return promisify(this.getPlatform().uploadFile); |
| 83 | + return promisify(this.getPlatform().uploadFile); |
83 | 84 | }; |
84 | 85 | Platform.prototype.getDownload = function () { |
85 | | - return promisify(this.getPlatform().downloadFile); |
| 86 | + return promisify(this.getPlatform().downloadFile); |
86 | 87 | }; |
87 | 88 | Platform.prototype.getPlatform = function () { |
88 | | - if (this.platform === 'wx')return wx; |
89 | | - if (this.platform === 'my')return my; |
90 | | - if (this.platform === 'swan')return swan; |
91 | | - if (this.platform === 'tt')return tt; |
| 89 | + if (this.platform === 'wx') return wx; |
| 90 | + if (this.platform === 'my') return my; |
| 91 | + if (this.platform === 'swan') return swan; |
| 92 | + if (this.platform === 'tt') return tt; |
92 | 93 | }; |
93 | 94 | var platform = new Platform(); |
94 | 95 |
|
|
101 | 102 | }; |
102 | 103 |
|
103 | 104 | function bind(fn, context) { |
104 | | - return function wf (){ |
| 105 | + return function wf() { |
105 | 106 | var args = new Array(arguments.length); |
106 | 107 | for (var i = 0, l = args.length; i < l; i++) { |
107 | 108 | args[i] = arguments[i]; |
|
118 | 119 | for (var i = 0, len = typeAry.length; i < len; i++) { |
119 | 120 | (function (name) { |
120 | 121 | type['is' + name] = function (obj) { |
121 | | - return tostring.call(obj) === '[object' +' '+ name + ']'; |
| 122 | + return tostring.call(obj) === '[object' + ' ' + name + ']'; |
122 | 123 | }; |
123 | 124 | })(typeAry[i]); |
124 | 125 | } |
|
157 | 158 | } |
158 | 159 | return result; |
159 | 160 | }, |
160 | | - deepMerge: function() { |
| 161 | + deepMerge: function () { |
161 | 162 | var result = {}; |
162 | | - function assignValue(val, key) { |
163 | | - if (typeof result[key] === 'object' && typeof val === 'object') { |
164 | | - result[key] = utils.deepMerge(result[key], val); |
165 | | - } else if (typeof val === 'object'){ |
166 | | - result[key] = utils.deepMerge({},val); |
167 | | - } else { |
168 | | - result[key] = val; |
169 | | - } |
170 | | - } |
171 | | - for (var i = 0, l = arguments.length; i < l; i++) { |
172 | | - this.forEach(arguments[i], assignValue); |
| 163 | + function assignValue(val, key) { |
| 164 | + if (typeof result[key] === 'object' && typeof val === 'object') { |
| 165 | + result[key] = utils.deepMerge(result[key], val); |
| 166 | + } else if (typeof val === 'object') { |
| 167 | + result[key] = utils.deepMerge({}, val); |
| 168 | + } else { |
| 169 | + result[key] = val; |
173 | 170 | } |
174 | | - return result; |
| 171 | + } |
| 172 | + for (var i = 0, l = arguments.length; i < l; i++) { |
| 173 | + this.forEach(arguments[i], assignValue); |
| 174 | + } |
| 175 | + return result; |
175 | 176 | }, |
176 | | - mergeConfig: function(source,target){ |
177 | | - var c = {}; target = target || {}; |
178 | | - ['url','method', 'data','config'].forEach(function (prop) { |
179 | | - if (target[prop]){ |
| 177 | + mergeConfig: function (source, target) { |
| 178 | + var c = {}; target = target || {}; |
| 179 | + ['url', 'method', 'data', 'config'].forEach(function (prop) { |
| 180 | + if (target[prop]) { |
180 | 181 | c[prop] = target[prop]; |
181 | 182 | } |
182 | 183 | }); |
183 | | - ['header'].forEach(function (prop) { |
184 | | - if (utils.type.isObject(target[prop])){ |
185 | | - c[prop] = utils.deepMerge(source[prop],target[prop]); |
186 | | - } else if(target[prop]){ |
| 184 | + ['header', 'headers'].forEach(function (prop) { |
| 185 | + if (utils.type.isObject(target[prop])) { |
| 186 | + c[prop] = utils.deepMerge(source[prop], target[prop]); |
| 187 | + } else if (target[prop]) { |
187 | 188 | c[prop] = target[prop]; |
188 | | - } else if(utils.type.isObject(source[prop])){ |
| 189 | + } else if (utils.type.isObject(source[prop])) { |
189 | 190 | c[prop] = utils.deepMerge(source[prop]); |
190 | | - } else if (source[prop]){ |
| 191 | + } else if (source[prop]) { |
191 | 192 | c[prop] = source[prop]; |
192 | 193 | } |
193 | 194 | }); |
194 | | - ['baseUrl','timeout','eventType','createRequest'].forEach(function (prop) { |
195 | | - if (target[prop] !== undefined){ |
| 195 | + ['baseUrl', 'timeout', 'eventType', 'createRequest'].forEach(function (prop) { |
| 196 | + if (target[prop] !== undefined) { |
196 | 197 | c[prop] = target[prop]; |
197 | | - } else if (source[prop] !== undefined){ |
| 198 | + } else if (source[prop] !== undefined) { |
198 | 199 | c[prop] = source[prop]; |
199 | 200 | } |
200 | 201 | }); |
201 | | - return c |
| 202 | + return c |
202 | 203 | }, |
203 | 204 | extends: function (extendObj, copyObj, thisArg) { |
204 | 205 | this.forEach(copyObj, function (val, key) { |
|
235 | 236 | }; |
236 | 237 |
|
237 | 238 | function dispatchRequest(config) { |
238 | | - if (platform.platform === 'my' && config.method !== 'download' && config.method !== 'upload') { |
239 | | - config.headers = config.header; |
240 | | - delete config.header; |
241 | | - } |
242 | 239 | if (config.method === 'download') { |
243 | | - config.method = 'get'; |
244 | | - config.createRequest = platform.getDownload(); |
| 240 | + config.method = 'get'; |
| 241 | + config.createRequest = platform.getDownload(); |
245 | 242 | } |
246 | | - if (config.method === 'upload'){ |
247 | | - config.method = 'post'; |
248 | | - config.createRequest = platform.getUpload(); |
| 243 | + if (config.method === 'upload') { |
| 244 | + config.method = 'post'; |
| 245 | + config.createRequest = platform.getUpload(); |
249 | 246 | } |
250 | 247 | var request = config.createRequest; |
251 | 248 | return request(config).then(function (response) { |
|
255 | 252 | }) |
256 | 253 | } |
257 | 254 |
|
258 | | - function request (config) { |
| 255 | + function request(config) { |
259 | 256 | if (typeof config === 'string') { |
260 | | - config = arguments[1] || {}; |
261 | | - config.url = arguments[0]; |
| 257 | + config = arguments[1] || {}; |
| 258 | + config.url = arguments[0]; |
262 | 259 | } |
263 | 260 | config = utils.mergeConfig(this.defaults, config); |
264 | 261 | if (config.url.indexOf('http') === -1) { |
265 | | - if (config.downloadUrl && config.method === 'download') { |
266 | | - config.url = config.downloadUrl + config.url; |
267 | | - } else if (config.uploadUrl && config.method === 'upload') { |
268 | | - config.url = config.uploadUrl + config.url; |
269 | | - } else { //(config.baseUrl) |
270 | | - config.url = config.baseUrl + config.url; |
271 | | - } |
| 262 | + if (config.downloadUrl && config.method === 'download') { |
| 263 | + config.url = config.downloadUrl + config.url; |
| 264 | + } else if (config.uploadUrl && config.method === 'upload') { |
| 265 | + config.url = config.uploadUrl + config.url; |
| 266 | + } else { //(config.baseUrl) |
| 267 | + config.url = config.baseUrl + config.url; |
| 268 | + } |
| 269 | + } |
| 270 | + if (platform.platform === 'my') { |
| 271 | + if (config.method !== 'download' && config.method !== 'upload') { |
| 272 | + config.headers = Object.assign({}, config.header); |
| 273 | + delete config.header; |
| 274 | + } |
272 | 275 | } |
273 | 276 | var chain = [dispatchRequest, undefined]; |
274 | 277 | var promise = Promise.resolve(config); |
|
295 | 298 | WeFetch.prototype.download = function (url, config) { |
296 | 299 | // wf.download({}) support |
297 | 300 | if (utils.type.isObject(url)) { |
298 | | - return this.request(utils.merge(url,{ method: 'download' })) |
| 301 | + return this.request(utils.merge(url, { method: 'download' })) |
299 | 302 | } |
300 | 303 | // default |
301 | | - return this.request(utils.merge(config || {},{ |
| 304 | + return this.request(utils.merge(config || {}, { |
302 | 305 | url: url, |
303 | 306 | method: 'download' |
304 | 307 | })) |
|
307 | 310 | WeFetch.prototype.upload = function (url, config) { |
308 | 311 | // upload({}) support |
309 | 312 | if (utils.type.isObject(url)) { |
310 | | - return this.request(url,{ method: 'upload' }) |
| 313 | + return this.request(url, { method: 'upload' }) |
311 | 314 | } |
312 | 315 | return this.request(utils.merge(config || {}, { |
313 | 316 | url: url, |
|
316 | 319 | }; |
317 | 320 |
|
318 | 321 | function WeFetch(instanceConfig) { |
319 | | - this.defaults = instanceConfig; |
320 | | - this.before = new InterceptorManager(); |
321 | | - this.after = new InterceptorManager(); |
| 322 | + this.defaults = instanceConfig; |
| 323 | + this.before = new InterceptorManager(); |
| 324 | + this.after = new InterceptorManager(); |
322 | 325 | } |
323 | 326 |
|
324 | 327 | WeFetch.prototype.on = function (event, cb) { |
325 | | - e.on(event, cb); |
| 328 | + e.on(event, cb); |
326 | 329 | }; |
327 | 330 | WeFetch.prototype.abort = function (event, cb) { |
328 | | - this.on(event, function (t) { |
329 | | - t.abort(); |
330 | | - cb && cb(); |
331 | | - }); |
| 331 | + this.on(event, function (t) { |
| 332 | + t.abort(); |
| 333 | + cb && cb(); |
| 334 | + }); |
332 | 335 | }; |
333 | 336 | WeFetch.prototype.onProcess = function (event, cb) { |
334 | | - this.on(event, function (t) { |
335 | | - t.onProgressUpdate(cb); |
336 | | - }); |
| 337 | + this.on(event, function (t) { |
| 338 | + t.onProgressUpdate(cb); |
| 339 | + }); |
337 | 340 | }; |
338 | 341 | WeFetch.prototype.promisify = promisify; |
339 | 342 | WeFetch.prototype.request = request; |
340 | 343 |
|
341 | | - function retry(times,request,timeout) { |
342 | | - timeout = timeout || 1000; |
343 | | - if (!times && times !== 0 || !request)throw new Error('request and times params is required'); |
344 | | - if (typeof request !== 'function') throw new Error('request must be a function, but got a\n'+ typeof request); |
345 | | - var p = request(); |
346 | | - if (times > 1) { |
347 | | - times --; |
348 | | - return new Promise(function(resolve, reject) { |
349 | | - p.then(resolve).catch(function () { |
350 | | - setTimeout(function() { |
351 | | - resolve(retry(times, request, timeout)); |
352 | | - }, timeout); |
353 | | - }); |
354 | | - }) |
355 | | - } |
356 | | - return p; |
| 344 | + function retry(times, request, timeout) { |
| 345 | + timeout = timeout || 1000; |
| 346 | + if (!times && times !== 0 || !request) throw new Error('request and times params is required'); |
| 347 | + if (typeof request !== 'function') throw new Error('request must be a function, but got a\n' + typeof request); |
| 348 | + var p = request(); |
| 349 | + if (times > 1) { |
| 350 | + times--; |
| 351 | + return new Promise(function (resolve, reject) { |
| 352 | + p.then(resolve).catch(function () { |
| 353 | + setTimeout(function () { |
| 354 | + resolve(retry(times, request, timeout)); |
| 355 | + }, timeout); |
| 356 | + }); |
| 357 | + }) |
| 358 | + } |
| 359 | + return p; |
357 | 360 | } |
358 | 361 |
|
359 | 362 | Promise.prototype.finally = function (cb) { |
|
369 | 372 | }) |
370 | 373 | }; |
371 | 374 |
|
372 | | - function createInstance (defaultConfig){ |
| 375 | + function createInstance(defaultConfig) { |
373 | 376 | var context = new WeFetch(defaultConfig); |
374 | 377 | var instance = bind(WeFetch.prototype.request, context); |
375 | 378 | utils.extends(instance, WeFetch.prototype, context); |
|
0 commit comments