Skip to content

Commit

Permalink
remove some leftover
Browse files Browse the repository at this point in the history
  • Loading branch information
xg-wang committed Nov 10, 2018
1 parent 28cca9f commit 7ba13f4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions addon/mixins/adapter-fetch.js
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
19 changes: 12 additions & 7 deletions fastboot/instance-initializers/setup-fetch.js
Original file line number Diff line number Diff line change
@@ -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
};
31 changes: 20 additions & 11 deletions public/fastboot-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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`'
);
});

0 comments on commit 7ba13f4

Please sign in to comment.