Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
fix: skip instead of returning url when namespace is present in url
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmosgenius authored and alexlafroscia committed Oct 9, 2018
1 parent 975724d commit fe518dc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
12 changes: 6 additions & 6 deletions addon/mixins/ajax-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,18 +482,18 @@ export default Mixin.create({

let namespace = options.namespace || get(this, 'namespace');
if (namespace) {
// If the URL has already been constructed (presumably, by Ember Data), then we should just leave it alone
const hasNamespaceRegex = new RegExp(`^(/)?${stripSlashes(namespace)}/`);
if (hasNamespaceRegex.test(url)) {
return url;
}
// If host is given then we need to strip leading slash too( as it will be added through join)
if (host) {
namespace = stripSlashes(namespace);
} else if (endsWithSlash(namespace)) {
namespace = removeTrailingSlash(namespace);
}
urlParts.push(namespace);

// If the URL has already been constructed (presumably, by Ember Data), then we should just leave it alone
const hasNamespaceRegex = new RegExp(`^(/)?${stripSlashes(namespace)}/`);
if (!hasNamespaceRegex.test(url)) {
urlParts.push(namespace);
}
}

// *Only* remove a leading slash when there is host or namespace -- we need to maintain a trailing slash for
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/mixins/ajax-request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,20 @@ describe('Unit | Mixin | ajax request', function() {
);
});

it('is set on the url containing namespace', function() {
const RequestWithHostAndNamespace = AjaxRequest.extend({
host: 'https://discuss.emberjs.com',
namespace: '/api/v1'
});
const service = new RequestWithHostAndNamespace();
const url = '/api/v1/users/me';
const ajaxoptions = service.options(url);

expect(ajaxoptions.url).to.equal(
'https://discuss.emberjs.com/api/v1/users/me'
);
});

it('is set with the host address as `//` and url not starting with `/`', function() {
const RequestWithHostAndNamespace = AjaxRequest.extend({
host: '//'
Expand Down

0 comments on commit fe518dc

Please sign in to comment.