|
| 1 | +/* globals define FastBoot */ |
| 2 | +define('fetch', ['exports'], function(exports) { |
| 3 | + var httpRegex = /^https?:\/\//; |
| 4 | + var protocolRelativeRegex = /^\/\//; |
| 5 | + |
| 6 | + var AbortControllerPolyfill = FastBoot.require( |
| 7 | + 'abortcontroller-polyfill/dist/cjs-ponyfill' |
| 8 | + ); |
| 9 | + var nodeFetch = FastBoot.require('node-fetch'); |
| 10 | + |
| 11 | + /** |
| 12 | + * Build the absolute url if it's not, can handle: |
| 13 | + * - protocol-relative URL (//can-be-http-or-https.com/) |
| 14 | + * - path-relative URL (/file/under/root) |
| 15 | + * |
| 16 | + * @param {string} url |
| 17 | + * @param {string} protocol |
| 18 | + * @param {string} host |
| 19 | + * @returns {string} |
| 20 | + */ |
| 21 | + function buildAbsoluteUrl(url, protocol, host) { |
| 22 | + if (protocolRelativeRegex.test(url)) { |
| 23 | + url = host + url; |
| 24 | + } else if (!httpRegex.test(url)) { |
| 25 | + if (!host) { |
| 26 | + throw new Error( |
| 27 | + 'You are using using fetch with a path-relative URL, but host is missing from Fastboot request. Please set the hostWhitelist property in your environment.js.' |
| 28 | + ); |
| 29 | + } |
| 30 | + url = protocol + '//' + host + url; |
| 31 | + } |
| 32 | + return url; |
| 33 | + } |
| 34 | + |
| 35 | + var FastbootHost, FastbootProtocol; |
| 36 | + |
| 37 | + /** |
| 38 | + * Isomorphic `fetch` API for both browser and fastboot |
| 39 | + * |
| 40 | + * node-fetch doesn't allow relative URLs, we patch it with Fastboot runtime info. |
| 41 | + * Before instance-initializers Absolute URL is still not allowed, in this case |
| 42 | + * node-fetch will throw error. |
| 43 | + * `FastbootProtocol` and `FastbootHost` are re-set for every instance during its |
| 44 | + * initializers through calling `setupFastboot`. |
| 45 | + * |
| 46 | + * @param {String|Object} input |
| 47 | + * @param {Object} [options] |
| 48 | + */ |
| 49 | + exports.default = function fetch(input, options) { |
| 50 | + if (typeof input === 'object') { |
| 51 | + input.url = buildAbsoluteUrl(input.url, FastbootProtocol, FastbootHost); |
| 52 | + } else { |
| 53 | + input = buildAbsoluteUrl(input, FastbootProtocol, FastbootHost); |
| 54 | + } |
| 55 | + return nodeFetch(input, options); |
| 56 | + }; |
| 57 | + /** |
| 58 | + * Assign the local protocol and host being used for building absolute URLs |
| 59 | + * @private |
| 60 | + */ |
| 61 | + exports.setupFastboot = function setupFastboot(protocol, host) { |
| 62 | + FastbootProtocol = protocol; |
| 63 | + FastbootHost = host; |
| 64 | + } |
| 65 | + exports.Request = nodeFetch.Request; |
| 66 | + exports.Headers = nodeFetch.Headers; |
| 67 | + exports.Response = nodeFetch.Response; |
| 68 | + exports.AbortController = AbortControllerPolyfill.AbortController; |
| 69 | +}); |
| 70 | + |
| 71 | +define('fetch/ajax', ['exports'], function() { |
| 72 | + throw new Error( |
| 73 | + 'You included `fetch/ajax` but it was renamed to `ember-fetch/ajax`' |
| 74 | + ); |
| 75 | +}); |
0 commit comments