|
| 1 | +/* |
| 2 | + * copied and rewritten as ESM (just a simple rewrite as ESM to avoid loading a CJS package) |
| 3 | + * https://github.com/camsong/fetch-jsonp/blob/master/src/fetch-jsonp.js |
| 4 | + */ |
| 5 | + |
| 6 | +interface JsonpOptions { |
| 7 | + timeout: number; |
| 8 | + jsonpCallback: string; |
| 9 | + jsonpCallbackFunction: string; |
| 10 | + charset: string; |
| 11 | + nonce: string; |
| 12 | + referrerPolicy: string; |
| 13 | + crossorigin: boolean; |
| 14 | +}; |
| 15 | + |
| 16 | +const defaultOptions = { |
| 17 | + timeout: 5000, |
| 18 | + jsonpCallback: 'callback', |
| 19 | + jsonpCallbackFunction: null, |
| 20 | +}; |
| 21 | +const generateCallbackFunction = () => `jsonp_${Date.now()}_${Math.ceil(Math.random() * 100000)}`; |
| 22 | +const clearFunction = (functionName: string) => delete (window as any)[functionName]; |
| 23 | +const removeScript = (scriptId: string) => { |
| 24 | + const script = document.getElementById(scriptId); |
| 25 | + if (script) { |
| 26 | + document.getElementsByTagName('head')[0].removeChild(script); |
| 27 | + } |
| 28 | +}; |
| 29 | + |
| 30 | +function fetchJsonp<T = any>(_url: string, options: Partial<JsonpOptions> = {}): Promise<{ ok: boolean; json: () => Promise<T>; }> { |
| 31 | + // to avoid param reassign |
| 32 | + let url = _url; |
| 33 | + const timeout = options.timeout || defaultOptions.timeout; |
| 34 | + const jsonpCallback = options.jsonpCallback || defaultOptions.jsonpCallback; |
| 35 | + let timeoutId: any; |
| 36 | + |
| 37 | + return new Promise((resolve, reject) => { |
| 38 | + const callbackFunction = options.jsonpCallbackFunction || generateCallbackFunction(); |
| 39 | + const scriptId = `${jsonpCallback}_${callbackFunction}`; |
| 40 | + |
| 41 | + (window as any)[callbackFunction] = (response: T) => { |
| 42 | + // keep consistent with fetch API |
| 43 | + resolve({ ok: true, json: () => Promise.resolve(response) }); |
| 44 | + if (timeoutId) clearTimeout(timeoutId); |
| 45 | + removeScript(scriptId); |
| 46 | + clearFunction(callbackFunction); |
| 47 | + }; |
| 48 | + |
| 49 | + // Check if the user set their own params, and if not add a ? to start a list of params |
| 50 | + url += (url.indexOf('?') === -1) ? '?' : '&'; |
| 51 | + |
| 52 | + const jsonpScript = document.createElement('script'); |
| 53 | + jsonpScript.setAttribute('src', `${url}${jsonpCallback}=${callbackFunction}`); |
| 54 | + if (options.charset) { |
| 55 | + jsonpScript.setAttribute('charset', options.charset); |
| 56 | + } |
| 57 | + if (options.nonce) { |
| 58 | + jsonpScript.setAttribute('nonce', options.nonce); |
| 59 | + } |
| 60 | + if (options.referrerPolicy) { |
| 61 | + jsonpScript.setAttribute('referrerPolicy', options.referrerPolicy); |
| 62 | + } |
| 63 | + if (options.crossorigin) { |
| 64 | + jsonpScript.setAttribute('crossorigin', 'true'); |
| 65 | + } |
| 66 | + jsonpScript.id = scriptId; |
| 67 | + document.getElementsByTagName('head')[0].appendChild(jsonpScript); |
| 68 | + |
| 69 | + timeoutId = setTimeout(() => { |
| 70 | + reject(new Error(`JSONP request to ${_url} timed out`)); |
| 71 | + |
| 72 | + clearFunction(callbackFunction); |
| 73 | + removeScript(scriptId); |
| 74 | + (window as any)[callbackFunction] = () => { |
| 75 | + clearFunction(callbackFunction); |
| 76 | + }; |
| 77 | + }, timeout); |
| 78 | + |
| 79 | + // Caught if got 404/500 |
| 80 | + jsonpScript.onerror = () => { |
| 81 | + reject(new Error(`JSONP request to ${_url} failed`)); |
| 82 | + clearFunction(callbackFunction); |
| 83 | + removeScript(scriptId); |
| 84 | + if (timeoutId) clearTimeout(timeoutId); |
| 85 | + }; |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +export default fetchJsonp; |
0 commit comments