|
1 | 1 | /* globals define FastBoot */ |
2 | | -(function() { |
3 | | - define('fetch', ['exports'], function(self) { |
4 | | - var AbortControllerPolyfill = FastBoot.require('abortcontroller-polyfill/dist/cjs-ponyfill'); |
5 | | - var nodeFetch = FastBoot.require('node-fetch'); |
6 | | - var abortableFetch = AbortControllerPolyfill.abortableFetch({ |
7 | | - fetch: nodeFetch, |
8 | | - Request: nodeFetch.Request |
9 | | - }); |
| 2 | +define('fetch/setup', ['exports'], function(self) { |
| 3 | + var httpRegex = /^https?:\/\//; |
| 4 | + var protocolRelativeRegex = /^\/\//; |
10 | 5 |
|
11 | | - self['default'] = abortableFetch.fetch; |
12 | | - self['Request'] = abortableFetch.Request; |
| 6 | + var AbortControllerPolyfill = FastBoot.require( |
| 7 | + 'abortcontroller-polyfill/dist/cjs-ponyfill' |
| 8 | + ); |
| 9 | + var nodeFetch = FastBoot.require('node-fetch'); |
13 | 10 |
|
14 | | - self['Headers'] = nodeFetch.Headers; |
15 | | - self['Response'] = nodeFetch.Response; |
| 11 | + self['default'] = function(protocol, host) { |
| 12 | + return function() { |
| 13 | + define('fetch', ['exports'], function(exports) { |
| 14 | + function buildAbsoluteUrl(url, protocol, host) { |
| 15 | + if (protocolRelativeRegex.test(url)) { |
| 16 | + url = host + url; |
| 17 | + } else if (!httpRegex.test(url)) { |
| 18 | + if (!host) { |
| 19 | + throw new Error( |
| 20 | + '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.' |
| 21 | + ); |
| 22 | + } |
| 23 | + url = protocol + '//' + host + url; |
| 24 | + } |
| 25 | + return url; |
| 26 | + } |
| 27 | + /** |
| 28 | + * Setup the exported fetch for a given origin so it can handle: |
| 29 | + * - protocol-relative URL (//can-be-http-or-https.com/) |
| 30 | + * - path-relative URL (/file/under/root) |
| 31 | + * @param {String|Object} input |
| 32 | + * @param {Object} [options] |
| 33 | + */ |
| 34 | + exports['default'] = function fetch(input, options) { |
| 35 | + if (typeof input === 'object') { |
| 36 | + input.url = buildAbsoluteUrl(input.url, protocol, host); |
| 37 | + } else { |
| 38 | + input = buildAbsoluteUrl(input, protocol, host); |
| 39 | + } |
| 40 | + return nodeFetch(input, options); |
| 41 | + }; |
| 42 | + exports['Request'] = nodeFetch.Request; |
| 43 | + exports['Headers'] = nodeFetch.Headers; |
| 44 | + exports['Response'] = nodeFetch.Response; |
| 45 | + exports['AbortController'] = AbortControllerPolyfill.AbortController; |
| 46 | + }); |
| 47 | + }; |
| 48 | + }; |
| 49 | +}); |
16 | 50 |
|
17 | | - self['AbortController'] = AbortControllerPolyfill.AbortController; |
18 | | - }); |
19 | | - |
20 | | - define('fetch/ajax', ['exports'], function() { |
21 | | - throw new Error('You included `fetch/ajax` but it was renamed to `ember-fetch/ajax`'); |
22 | | - }); |
23 | | -})(); |
| 51 | +define('fetch/ajax', ['exports'], function() { |
| 52 | + throw new Error( |
| 53 | + 'You included `fetch/ajax` but it was renamed to `ember-fetch/ajax`' |
| 54 | + ); |
| 55 | +}); |
0 commit comments