@v2coding/ui 0.1.48 → 0.1.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -128,6 +128,90 @@ const DefaultSetting = {
128
128
  };
129
129
  var DefaultSetting$1 = DefaultSetting;
130
130
 
131
+ const defaultOptions = {
132
+ timeout: 30000,
133
+ jsonpCallback: 'callback',
134
+ jsonpCallbackFunction: null
135
+ };
136
+
137
+ function generateCallbackFunction() {
138
+ return `jsonp_${Date.now()}_${Math.ceil(Math.random() * 100000)}`;
139
+ }
140
+
141
+ function clearFunction(functionName) {
142
+ // IE8 throws an exception when you try to delete a property on window
143
+ // http://stackoverflow.com/a/1824228/751089
144
+ try {
145
+ delete window[functionName];
146
+ } catch (e) {
147
+ window[functionName] = undefined;
148
+ }
149
+ }
150
+
151
+ function removeScript(scriptId) {
152
+ const script = document.getElementById(scriptId);
153
+
154
+ if (script) {
155
+ document.getElementsByTagName('head')[0].removeChild(script);
156
+ }
157
+ }
158
+
159
+ function jsonp(_url) {
160
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
161
+ // to avoid param reassign
162
+ let url = _url;
163
+ const timeout = options.timeout || defaultOptions.timeout;
164
+ const jsonpCallback = options.jsonpCallback || defaultOptions.jsonpCallback;
165
+ let timeoutId;
166
+ return new Promise((resolve, reject) => {
167
+ const callbackFunction = options.jsonpCallbackFunction || generateCallbackFunction();
168
+ const scriptId = `${jsonpCallback}_${callbackFunction}`;
169
+
170
+ window[callbackFunction] = response => {
171
+ resolve({
172
+ ok: true,
173
+ // keep consistent with fetch API
174
+ json: () => Promise.resolve(response)
175
+ });
176
+ if (timeoutId) clearTimeout(timeoutId);
177
+ removeScript(scriptId);
178
+ clearFunction(callbackFunction);
179
+ }; // Check if the user set their own params, and if not add a ? to start a list of params
180
+
181
+
182
+ url += url.indexOf('?') === -1 ? '?' : '&';
183
+ const jsonpScript = document.createElement('script');
184
+ jsonpScript.src = `${url}${jsonpCallback}=${callbackFunction}`;
185
+
186
+ if (options.nonce) {
187
+ jsonpScript.nonce = options.nonce;
188
+ }
189
+
190
+ if (options.referrerPolicy) {
191
+ jsonpScript.referrerPolicy = options.referrerPolicy;
192
+ }
193
+
194
+ jsonpScript.id = scriptId;
195
+ document.head.appendChild(jsonpScript);
196
+ timeoutId = setTimeout(() => {
197
+ reject(new Error(`JSONP request to ${_url} timed out`));
198
+ clearFunction(callbackFunction);
199
+ removeScript(scriptId);
200
+
201
+ window[callbackFunction] = () => {
202
+ clearFunction(callbackFunction);
203
+ };
204
+ }, timeout); // Caught if got 404/500
205
+
206
+ jsonpScript.onerror = () => {
207
+ reject(new Error(`JSONP request to ${_url} failed`));
208
+ clearFunction(callbackFunction);
209
+ removeScript(scriptId);
210
+ if (timeoutId) clearTimeout(timeoutId);
211
+ };
212
+ });
213
+ }
214
+
131
215
  const downloadFile = (data, filename, mime) => {
132
216
  const blob = new Blob([data], {
133
217
  type: mime || data.type || 'application/octet-stream'
@@ -268,6 +352,7 @@ const init$2 = function (Vue) {
268
352
  instance.interceptors.response.use(onResponseSuccess, onResponseError);
269
353
  downloadInstance.interceptors.request.use(onRequestSuccess);
270
354
  downloadInstance.interceptors.response.use(onResponseSuccess, onResponseError);
355
+ instance.jsonp = jsonp;
271
356
  instance.download = initDownload(downloadInstance);
272
357
  instance.downloadServer = downloadInstance;
273
358
  Vue.prototype.$axios = instance;