-
-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow relative urls for fastboot and ember-data #128
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
import Ember from 'ember'; | ||
import Mixin from '@ember/object/mixin'; | ||
import { assign, merge } from '@ember/polyfills' | ||
import { assign, merge } from '@ember/polyfills'; | ||
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 = /\[\]$/; | ||
|
||
/** | ||
|
@@ -87,6 +91,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 +102,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 +139,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 +169,28 @@ export function determineBodyPromise(response, requestData) { | |
} | ||
|
||
export default Mixin.create({ | ||
/** | ||
* @param {String} url | ||
* @param {String} type | ||
* @param {Object} _options | ||
* @returns {Object} | ||
* @override | ||
*/ | ||
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 | ||
* @param {String} type | ||
* @param {Object} _options | ||
* @returns {Object} | ||
* @override | ||
*/ | ||
ajaxOptions(url, type, options = {}) { | ||
options.url = url; | ||
options.type = type; | ||
|
@@ -186,7 +206,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 +246,7 @@ export default Mixin.create({ | |
* @param {Object} options | ||
*/ | ||
_fetchRequest(url, options) { | ||
return fetch(url, options); | ||
return fetch(this.buildServerURL(url), options); | ||
}, | ||
|
||
/** | ||
|
@@ -251,13 +271,38 @@ export default Mixin.create({ | |
} | ||
}, | ||
|
||
/** | ||
* Determine fastboot compliant urls | ||
* @param url | ||
* @returns {*} | ||
*/ | ||
buildServerURL(url) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (!get(this, 'fastboot.isFastBoot')) { | ||
return url; | ||
} | ||
let protocol = get(this, 'protocol'); | ||
let host = get(this, 'fastboot.request.host'); | ||
|
||
/** | ||
* Allows for the error to be selected from either the | ||
* response object, or the response data. | ||
* @param {Object} response | ||
* @param {Object} payload | ||
*/ | ||
if (protocolRelativeRegex.test(url)) { | ||
return `${protocol}${url}`; | ||
} else if (!httpRegex.test(url)) { | ||
try { | ||
return `${protocol}//${host}${url}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When will this throw? |
||
} 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 | ||
* response object, or the response data. | ||
* @param {Object} response | ||
* @param {Object} payload | ||
*/ | ||
parseFetchResponseForError(response, payload) { | ||
return payload || response.statusTest; | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why use CPs with dependent keys? Can these be set in
init
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use Fastboot service here? https://github.com/ember-fastboot/ember-cli-fastboot/tree/master#fastboot-service
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For verifying if Fastboot is used isn't this the preferred way to do it in an addon—to avoid an error if the consumer doesn't have Fastboot installed?
https://ember-fastboot.com/docs/addon-author-guide#accessing-the-fastboot-service
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chrism Yea you're absolutely right, thansk for pointing out!