diff --git a/README.md b/README.md index 7c651626..7dd58056 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Ember-fetch +# ember-fetch [![Build Status](https://travis-ci.org/ember-cli/ember-fetch.svg?branch=master)](https://travis-ci.org/ember-cli/ember-fetch) [![Build status](https://ci.appveyor.com/api/projects/status/u7qcv4pgsvo60sxt?svg=true)](https://ci.appveyor.com/project/embercli/ember-fetch) [![Ember Observer Score](https://emberobserver.com/badges/ember-fetch.svg)](https://emberobserver.com/addons/ember-fetch) @@ -65,7 +65,7 @@ export default { } ``` -For addon authors, if the addon supports Fastboot mode, `ember-fetch` should also be listed as a [peer dependency](https://docs.npmjs.com/files/package.json#peerdependencies). +For addon authors, if the addon supports Fastboot mode, `ember-fetch` should also be listed as a [peer dependency](https://docs.npmjs.com/files/package.json#peerdependencies). This is because Fastboot only invokes top-level addon's `updateFastBootManifest` ([detail](https://github.com/ember-fastboot/ember-cli-fastboot/issues/597)), thus `ember-fetch` has to be a top-level addon installed by the host app. ### Allow native fetch diff --git a/addon/mixins/adapter-fetch.js b/addon/mixins/adapter-fetch.js index cb099a30..cf34b04b 100644 --- a/addon/mixins/adapter-fetch.js +++ b/addon/mixins/adapter-fetch.js @@ -1,14 +1,10 @@ import Mixin from '@ember/object/mixin'; import { assign } from '@ember/polyfills' -import { inject as service } from '@ember/service'; import RSVP from 'rsvp'; import fetch from 'fetch'; import mungOptionsForFetch from '../utils/mung-options-for-fetch'; import determineBodyPromise from '../utils/determine-body-promise'; -const httpRegex = /^https?:\/\//; -const protocolRelativeRegex = /^\/\//; - /** * Helper function to create a plain object from the response's Headers. * Consumed by the adapter's `handleResponse`. diff --git a/fastboot/instance-initializers/setup-fetch.js b/fastboot/instance-initializers/setup-fetch.js index 4ebf0ef7..2516ed85 100644 --- a/fastboot/instance-initializers/setup-fetch.js +++ b/fastboot/instance-initializers/setup-fetch.js @@ -1,12 +1,17 @@ -/* globals define FastBoot */ import setupFetch from 'fetch/setup'; +/** + * To allow relative URLs for Fastboot mode, we need the per request information + * from the fastboot service. Then we re-define the `fetch` amd module. + */ +function patchFetchForRelativeURLs(instance) { + const fastboot = instance.lookup('service:fastboot'); + const request = fastboot.get('request'); + // host is cp + setupFetch(request.protocol, request.get('host'))(); +} + export default { name: 'fetch', - initialize: function patchFetchForRelativeURLs(instance) { - const fastboot = instance.lookup('service:fastboot'); - const request = fastboot.get('request'); - // host is cp - setupFetch(request.protocol, request.get('host'))(); - } + initialize: patchFetchForRelativeURLs }; diff --git a/public/fastboot-fetch.js b/public/fastboot-fetch.js index 23068184..e7943a42 100644 --- a/public/fastboot-fetch.js +++ b/public/fastboot-fetch.js @@ -3,7 +3,9 @@ define('fetch/setup', ['exports'], function(self) { var httpRegex = /^https?:\/\//; var protocolRelativeRegex = /^\/\//; - var AbortControllerPolyfill = FastBoot.require('abortcontroller-polyfill/dist/cjs-ponyfill'); + var AbortControllerPolyfill = FastBoot.require( + 'abortcontroller-polyfill/dist/cjs-ponyfill' + ); var nodeFetch = FastBoot.require('node-fetch'); var abortableFetch = AbortControllerPolyfill.abortableFetch({ fetch: nodeFetch, @@ -13,30 +15,37 @@ define('fetch/setup', ['exports'], function(self) { self['default'] = function(protocol, host) { return function() { define('fetch', ['exports'], function(exports) { - - // Setup the exported fetch for a given origin so it can handle: - // - protocol-relative URL (//can-be-http-or-https.com/) - // - path-relative URL (/file/under/root) - exports['default'] = function(url, options) { + /** + * Setup the exported fetch for a given origin so it can handle: + * - protocol-relative URL (//can-be-http-or-https.com/) + * - path-relative URL (/file/under/root) + * @param {String} url + * @param {Object} [options] + */ + exports['default'] = function fetch(url, options) { if (protocolRelativeRegex.test(url)) { url = host + url; } else if (!httpRegex.test(url)) { if (!host) { - throw new Error('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.'); + throw new Error( + '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.' + ); } url = protocol + '//' + host + url; } return abortableFetch.fetch(url, options); - } + }; exports['Request'] = abortableFetch.Request; exports['Headers'] = nodeFetch.Headers; exports['Response'] = nodeFetch.Response; exports['AbortController'] = AbortControllerPolyfill.AbortController; }); - } - } + }; + }; }); define('fetch/ajax', ['exports'], function() { - throw new Error('You included `fetch/ajax` but it was renamed to `ember-fetch/ajax`'); + throw new Error( + 'You included `fetch/ajax` but it was renamed to `ember-fetch/ajax`' + ); });