From 6f74dd7235bf428bf104acc81c3e2dee5dccff7a Mon Sep 17 00:00:00 2001 From: Robert Wagner Date: Fri, 24 Aug 2018 20:19:18 -0400 Subject: [PATCH 1/2] Use ember-fetch-adapter to allow absolute urls for ember-data --- addon/mixins/adapter-fetch.js | 39 +++++++++++++++++++---------------- package.json | 1 + yarn.lock | 6 ++++++ 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/addon/mixins/adapter-fetch.js b/addon/mixins/adapter-fetch.js index e3c98a52..07bbc097 100644 --- a/addon/mixins/adapter-fetch.js +++ b/addon/mixins/adapter-fetch.js @@ -1,8 +1,8 @@ import Ember from 'ember'; import Mixin from '@ember/object/mixin'; -import { assign, merge } from '@ember/polyfills' +import { assign, merge } from '@ember/polyfills'; +import { inject as service } from '@ember/service'; import RSVP from 'rsvp'; -import fetch from 'fetch'; const { Logger: { warn } @@ -87,6 +87,7 @@ export function headersToObject(headers) { return headersObject; } + /** * Helper function that translates the options passed to `jQuery.ajax` into a format that `fetch` expects. * @param {Object} _options @@ -97,7 +98,7 @@ export function mungOptionsForFetch(_options, adapter) { // This allows this mixin to be backward compatible with Ember < 2.5. const combineObjs = (assign || merge); const options = combineObjs({ - credentials: 'same-origin', + credentials: 'same-origin' }, _options); let adapterHeaders = adapter.get('headers'); @@ -134,6 +135,7 @@ export function mungOptionsForFetch(_options, adapter) { return options; } + /** * Function that always attempts to parse the response as json, and if an error is thrown, * returns an object with 'data' set to null if the response is @@ -163,14 +165,15 @@ export function determineBodyPromise(response, requestData) { } export default Mixin.create({ -/** - * @param {String} url - * @param {String} type - * @param {Object} _options - * @returns {Object} - * @override - */ + adapter: service(), + /** + * @param {String} url + * @param {String} type + * @param {Object} _options + * @returns {Object} + * @override + */ ajaxOptions(url, type, options = {}) { options.url = url; options.type = type; @@ -186,7 +189,7 @@ export default Mixin.create({ ajax(url, type, options) { const requestData = { url, - method: type, + method: type }; const hash = this.ajaxOptions(url, type, options); @@ -226,7 +229,7 @@ export default Mixin.create({ * @param {Object} options */ _fetchRequest(url, options) { - return fetch(url, options); + return this.adapter.fetch(options); }, /** @@ -252,12 +255,12 @@ export default Mixin.create({ }, -/** - * Allows for the error to be selected from either the - * response object, or the response data. - * @param {Object} response - * @param {Object} payload - */ + /** + * Allows for the error to be selected from either the + * response object, or the response data. + * @param {Object} response + * @param {Object} payload + */ parseFetchResponseForError(response, payload) { return payload || response.statusTest; }, diff --git a/package.json b/package.json index a4e11011..9d8c9cbb 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "broccoli-stew": "^2.0.0", "broccoli-templater": "^2.0.1", "ember-cli-babel": "^6.8.2", + "ember-fetch-adapter": "^0.4.3", "node-fetch": "^2.0.0-alpha.9", "rollup-plugin-babel": "^3.0.7" }, diff --git a/yarn.lock b/yarn.lock index b6efaaec..70c9df5c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2320,6 +2320,12 @@ ember-disable-prototype-extensions@^1.1.2: version "1.1.3" resolved "https://registry.yarnpkg.com/ember-disable-prototype-extensions/-/ember-disable-prototype-extensions-1.1.3.tgz#1969135217654b5e278f9fe2d9d4e49b5720329e" +ember-fetch-adapter@^0.4.3: + version "0.4.3" + resolved "https://registry.yarnpkg.com/ember-fetch-adapter/-/ember-fetch-adapter-0.4.3.tgz#c3d2da83ff87f829ab347b841a3201b3cc7e1eb4" + dependencies: + ember-cli-babel "^6.12.0" + ember-inflector@^2.0.0: version "2.3.0" resolved "https://registry.yarnpkg.com/ember-inflector/-/ember-inflector-2.3.0.tgz#94797eba0eea98d902aa1e5da0f0aeef6053317f" From 873ac31171f997bba37e778d18973ddfd06d0676 Mon Sep 17 00:00:00 2001 From: Robert Wagner Date: Sat, 25 Aug 2018 14:15:43 -0400 Subject: [PATCH 2/2] Use buildServerURL instead of ember-fetch-adapter --- addon/mixins/adapter-fetch.js | 48 ++++++++++++++++++++++++++++++++--- package.json | 1 - yarn.lock | 6 ----- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/addon/mixins/adapter-fetch.js b/addon/mixins/adapter-fetch.js index 07bbc097..629f1e95 100644 --- a/addon/mixins/adapter-fetch.js +++ b/addon/mixins/adapter-fetch.js @@ -1,13 +1,17 @@ import Ember from 'ember'; import Mixin from '@ember/object/mixin'; import { assign, merge } from '@ember/polyfills'; -import { inject as service } from '@ember/service'; +import { computed, get } from '@ember/object'; +import { getOwner } from '@ember/application'; import RSVP from 'rsvp'; +import fetch from 'fetch'; const { Logger: { warn } } = Ember; +const httpRegex = /^https?:\/\//; +const protocolRelativeRegex = /^\/\//; const RBRACKET = /\[\]$/; /** @@ -165,7 +169,20 @@ export function determineBodyPromise(response, requestData) { } export default Mixin.create({ - adapter: service(), + fastboot: computed(function() { + let owner = getOwner(this); + return owner && owner.lookup('service:fastboot'); + }), + + protocol: computed(function() { + let protocol = get(this, 'fastboot.request.protocol'); + // In Prember the protocol is the string 'undefined', so we default to HTTP + if (protocol === 'undefined:') { + protocol = 'http:'; + } + + return protocol; + }), /** * @param {String} url @@ -229,7 +246,7 @@ export default Mixin.create({ * @param {Object} options */ _fetchRequest(url, options) { - return this.adapter.fetch(options); + return fetch(this.buildServerURL(url), options); }, /** @@ -254,6 +271,31 @@ export default Mixin.create({ } }, + /** + * Determine fastboot compliant urls + * @param url + * @returns {*} + */ + buildServerURL(url) { + if (!get(this, 'fastboot.isFastBoot')) { + return url; + } + let protocol = get(this, 'protocol'); + let host = get(this, 'fastboot.request.host'); + + if (protocolRelativeRegex.test(url)) { + return `${protocol}${url}`; + } else if (!httpRegex.test(url)) { + try { + return `${protocol}//${host}${url}`; + } catch (fbError) { + throw new Error( + 'You are using Fetch Adapter with no host defined in your adapter. This will attempt to use the host of the FastBoot request, which is not configured for the current host of this request. Please set the hostWhitelist property for in your environment.js. FastBoot Error: ' + + fbError.message + ); + } + } + }, /** * Allows for the error to be selected from either the diff --git a/package.json b/package.json index 9d8c9cbb..a4e11011 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,6 @@ "broccoli-stew": "^2.0.0", "broccoli-templater": "^2.0.1", "ember-cli-babel": "^6.8.2", - "ember-fetch-adapter": "^0.4.3", "node-fetch": "^2.0.0-alpha.9", "rollup-plugin-babel": "^3.0.7" }, diff --git a/yarn.lock b/yarn.lock index 70c9df5c..b6efaaec 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2320,12 +2320,6 @@ ember-disable-prototype-extensions@^1.1.2: version "1.1.3" resolved "https://registry.yarnpkg.com/ember-disable-prototype-extensions/-/ember-disable-prototype-extensions-1.1.3.tgz#1969135217654b5e278f9fe2d9d4e49b5720329e" -ember-fetch-adapter@^0.4.3: - version "0.4.3" - resolved "https://registry.yarnpkg.com/ember-fetch-adapter/-/ember-fetch-adapter-0.4.3.tgz#c3d2da83ff87f829ab347b841a3201b3cc7e1eb4" - dependencies: - ember-cli-babel "^6.12.0" - ember-inflector@^2.0.0: version "2.3.0" resolved "https://registry.yarnpkg.com/ember-inflector/-/ember-inflector-2.3.0.tgz#94797eba0eea98d902aa1e5da0f0aeef6053317f"