|
1 | 1 | /* eslint-disable */
|
2 |
| -(function (global, factory) { |
3 |
| - if (typeof define === 'function' && define.amd) { |
4 |
| - define(['exports', 'module'], factory); |
5 |
| - } else if (typeof exports !== 'undefined' && typeof module !== 'undefined') { |
6 |
| - factory(exports, module); |
7 |
| - } else { |
8 |
| - var mod = { |
9 |
| - exports: {} |
10 |
| - }; |
11 |
| - factory(mod.exports, mod); |
12 |
| - global.fetchJsonp = mod.exports; |
13 |
| - } |
14 |
| -})(window, function (exports, module) { |
15 |
| - 'use strict'; |
16 |
| - |
17 |
| - var defaultOptions = { |
18 |
| - timeout: 5000, |
19 |
| - jsonpCallback: 'callback', |
20 |
| - jsonpCallbackFunction: null |
21 |
| - }; |
22 |
| - |
23 |
| - function generateCallbackFunction() { |
24 |
| - return 'jsonp_' + Date.now() + '_' + Math.ceil(Math.random() * 100000); |
25 |
| - } |
26 |
| - |
27 |
| - function clearFunction(functionName) { |
28 |
| - // IE8 throws an exception when you try to delete a property on window |
29 |
| - // http://stackoverflow.com/a/1824228/751089 |
30 |
| - try { |
31 |
| - delete window[functionName]; |
32 |
| - } catch (e) { |
33 |
| - window[functionName] = undefined; |
34 |
| - } |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +var defaultOptions = { |
| 5 | + timeout: 5000, |
| 6 | + jsonpCallback: 'callback', |
| 7 | + jsonpCallbackFunction: null |
| 8 | +}; |
| 9 | + |
| 10 | +function generateCallbackFunction() { |
| 11 | + return 'jsonp_' + Date.now() + '_' + Math.ceil(Math.random() * 100000); |
| 12 | +} |
| 13 | + |
| 14 | +function clearFunction(functionName) { |
| 15 | + // IE8 throws an exception when you try to delete a property on window |
| 16 | + // http://stackoverflow.com/a/1824228/751089 |
| 17 | + try { |
| 18 | + delete window[functionName]; |
| 19 | + } catch (e) { |
| 20 | + window[functionName] = undefined; |
35 | 21 | }
|
| 22 | +} |
36 | 23 |
|
37 |
| - function removeScript(scriptId) { |
38 |
| - var script = document.getElementById(scriptId); |
39 |
| - if (script) { |
40 |
| - document.getElementsByTagName('head')[0].removeChild(script); |
41 |
| - } |
| 24 | +function removeScript(scriptId) { |
| 25 | + var script = document.getElementById(scriptId); |
| 26 | + if (script) { |
| 27 | + document.getElementsByTagName('head')[0].removeChild(script); |
42 | 28 | }
|
| 29 | +} |
43 | 30 |
|
44 |
| - function fetchJsonp(_url) { |
45 |
| - var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; |
46 |
| - |
47 |
| - // to avoid param reassign |
48 |
| - var url = _url; |
49 |
| - var timeout = options.timeout || defaultOptions.timeout; |
50 |
| - var jsonpCallback = options.jsonpCallback || defaultOptions.jsonpCallback; |
51 |
| - |
52 |
| - var timeoutId = undefined; |
| 31 | +function fetchJsonp(_url) { |
| 32 | + var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; |
53 | 33 |
|
54 |
| - return new Promise(function (resolve, reject) { |
55 |
| - var callbackFunction = options.jsonpCallbackFunction || generateCallbackFunction(); |
56 |
| - var scriptId = jsonpCallback + '_' + callbackFunction; |
| 34 | + // to avoid param reassign |
| 35 | + var url = _url; |
| 36 | + var timeout = options.timeout || defaultOptions.timeout; |
| 37 | + var jsonpCallback = options.jsonpCallback || defaultOptions.jsonpCallback; |
57 | 38 |
|
58 |
| - window[callbackFunction] = function (response) { |
59 |
| - resolve({ |
60 |
| - ok: true, |
61 |
| - // keep consistent with fetch API |
62 |
| - json: function json() { |
63 |
| - return Promise.resolve(response); |
64 |
| - } |
65 |
| - }); |
| 39 | + var timeoutId = undefined; |
66 | 40 |
|
67 |
| - if (timeoutId) clearTimeout(timeoutId); |
| 41 | + return new Promise(function (resolve, reject) { |
| 42 | + var callbackFunction = options.jsonpCallbackFunction || generateCallbackFunction(); |
| 43 | + var scriptId = jsonpCallback + '_' + callbackFunction; |
68 | 44 |
|
69 |
| - removeScript(scriptId); |
| 45 | + window[callbackFunction] = function (response) { |
| 46 | + resolve({ |
| 47 | + ok: true, |
| 48 | + // keep consistent with fetch API |
| 49 | + json: function json() { |
| 50 | + return Promise.resolve(response); |
| 51 | + } |
| 52 | + }); |
70 | 53 |
|
71 |
| - clearFunction(callbackFunction); |
72 |
| - }; |
| 54 | + if (timeoutId) clearTimeout(timeoutId); |
73 | 55 |
|
74 |
| - // Check if the user set their own params, and if not add a ? to start a list of params |
75 |
| - url += url.indexOf('?') === -1 ? '?' : '&'; |
| 56 | + removeScript(scriptId); |
76 | 57 |
|
77 |
| - var jsonpScript = document.createElement('script'); |
78 |
| - jsonpScript.setAttribute('src', '' + url + jsonpCallback + '=' + callbackFunction); |
79 |
| - if (options.charset) { |
80 |
| - jsonpScript.setAttribute('charset', options.charset); |
81 |
| - } |
82 |
| - jsonpScript.id = scriptId; |
83 |
| - document.getElementsByTagName('head')[0].appendChild(jsonpScript); |
| 58 | + clearFunction(callbackFunction); |
| 59 | + }; |
84 | 60 |
|
85 |
| - timeoutId = setTimeout(function () { |
86 |
| - reject(new Error('JSONP request to ' + _url + ' timed out')); |
| 61 | + // Check if the user set their own params, and if not add a ? to start a list of params |
| 62 | + url += url.indexOf('?') === -1 ? '?' : '&'; |
87 | 63 |
|
88 |
| - clearFunction(callbackFunction); |
89 |
| - removeScript(scriptId); |
90 |
| - window[callbackFunction] = function () { |
91 |
| - clearFunction(callbackFunction); |
92 |
| - }; |
93 |
| - }, timeout); |
| 64 | + var jsonpScript = document.createElement('script'); |
| 65 | + jsonpScript.setAttribute('src', '' + url + jsonpCallback + '=' + callbackFunction); |
| 66 | + if (options.charset) { |
| 67 | + jsonpScript.setAttribute('charset', options.charset); |
| 68 | + } |
| 69 | + jsonpScript.id = scriptId; |
| 70 | + document.getElementsByTagName('head')[0].appendChild(jsonpScript); |
94 | 71 |
|
95 |
| - // Caught if got 404/500 |
96 |
| - jsonpScript.onerror = function () { |
97 |
| - reject(new Error('JSONP request to ' + _url + ' failed')); |
| 72 | + timeoutId = setTimeout(function () { |
| 73 | + reject(new Error('JSONP request to ' + _url + ' timed out')); |
98 | 74 |
|
| 75 | + clearFunction(callbackFunction); |
| 76 | + removeScript(scriptId); |
| 77 | + window[callbackFunction] = function () { |
99 | 78 | clearFunction(callbackFunction);
|
100 |
| - removeScript(scriptId); |
101 |
| - if (timeoutId) clearTimeout(timeoutId); |
102 | 79 | };
|
103 |
| - }); |
104 |
| - } |
| 80 | + }, timeout); |
105 | 81 |
|
106 |
| - // export as global function |
107 |
| - /* |
108 |
| - let local; |
109 |
| - if (typeof global !== 'undefined') { |
110 |
| - local = global; |
111 |
| - } else if (typeof self !== 'undefined') { |
112 |
| - local = self; |
113 |
| - } else { |
114 |
| - try { |
115 |
| - local = Function('return this')(); |
116 |
| - } catch (e) { |
117 |
| - throw new Error('polyfill failed because global object is unavailable in this environment'); |
118 |
| - } |
119 |
| - } |
120 |
| - local.fetchJsonp = fetchJsonp; |
121 |
| - */ |
| 82 | + // Caught if got 404/500 |
| 83 | + jsonpScript.onerror = function () { |
| 84 | + reject(new Error('JSONP request to ' + _url + ' failed')); |
| 85 | + |
| 86 | + clearFunction(callbackFunction); |
| 87 | + removeScript(scriptId); |
| 88 | + if (timeoutId) clearTimeout(timeoutId); |
| 89 | + }; |
| 90 | + }); |
| 91 | +} |
122 | 92 |
|
123 |
| - module.exports = fetchJsonp; |
124 |
| -}); |
| 93 | +window.fetchJsonp = fetchJsonp |
0 commit comments