Skip to content
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

Enable absolute url transform for Request in FastBoot #358

Merged
merged 2 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions public/fetch-fastboot.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ define('fetch', ['exports'], function(exports) {

var FastbootHost, FastbootProtocol;

class FastBootRequest extends nodeFetch.Request {
constructor(input, init) {
if (typeof input === 'string') {
input = buildAbsoluteUrl(input, FastbootProtocol, FastbootHost);
} else if (input && input.href) {
// WHATWG URL or Node.js Url Object
input = buildAbsoluteUrl(input.href, FastbootProtocol, FastbootHost);
}
super(input, init);
}
}

/**
* Isomorphic `fetch` API for both browser and fastboot
*
Expand All @@ -47,9 +59,9 @@ define('fetch', ['exports'], function(exports) {
* @param {Object} [options]
*/
exports.default = function fetch(input, options) {
if (typeof input === 'object') {
input.url = buildAbsoluteUrl(input.url, FastbootProtocol, FastbootHost);
} else {
if (input && input.href) {
input.url = buildAbsoluteUrl(input.href, FastbootProtocol, FastbootHost);
} else if (typeof input === 'string') {
input = buildAbsoluteUrl(input, FastbootProtocol, FastbootHost);
}
return nodeFetch(input, options);
Expand All @@ -62,7 +74,7 @@ define('fetch', ['exports'], function(exports) {
FastbootProtocol = protocol;
FastbootHost = host;
}
exports.Request = nodeFetch.Request;
exports.Request = FastBootRequest;
exports.Headers = nodeFetch.Headers;
exports.Response = nodeFetch.Response;
exports.AbortController = AbortControllerPolyfill.AbortController;
Expand Down
1 change: 1 addition & 0 deletions test/fastboot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe('renders in fastboot build', function() {
}
}).then(function(response) {
expect(response.body).to.contain('Hello World! fetch');
expect(response.body).to.contain('Hello World! fetch (Request)');
});
});
});
5 changes: 4 additions & 1 deletion test/fixtures/dummy/app/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Route from '@ember/routing/route';
import { hash } from 'rsvp';
import fetch from 'fetch';
import fetch, { Request } from 'fetch';
import ajax from 'ember-fetch/ajax';

export default Route.extend({
Expand All @@ -9,6 +9,9 @@ export default Route.extend({
fetch: fetch('/omg.json').then(function(request) {
return request.json();
}),
request: fetch(new Request('/omg.json')).then(function(request) {
return request.json();
}),
ajax: ajax('/omg.json')
});
}
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/dummy/app/templates/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Hello {{model.fetch.name}}! fetch
</div>

<div class="fetch request">
Hello {{model.request.name}}! fetch (Request)
</div>

<div class="ajax">
Hello {{model.ajax.name}}! ajax
</div>