Permalink
Browse files

fix(rest): maintain trailing slash to resource parameter

  • Loading branch information...
doktordirk committed Jul 31, 2016
1 parent c23db95 commit 4ec39a3620dc3d0522aa2293265ad554aea1f5b4
Showing with 21 additions and 3 deletions.
  1. +1 −0 doc/api_rest.md
  2. +5 −1 src/rest.js
  3. +15 −2 test/rest.spec.js
@@ -26,6 +26,7 @@ import {Rest} from 'aurelia-api';

All methods will:

* maintain trailing slashes of the resource parameter
* stringify the body, if it is an object and the `Content-Type` is set to `application/json` (the default).
* convert the body to querystring format, if the body is an object and the `Content-Type` is set to any other value.
* leave the body unchanged, if the `Content-Type` is not set or when the body is not an object.
@@ -136,7 +136,11 @@ export class Rest {
}

function getRequestPath(resource, criteria) {
let [, path, trailingSlash] = resource.match(/(.+[^\/])(\/?)$/);

return (criteria !== undefined && criteria !== null
? resource + (typeof criteria !== 'object' ? `/${criteria}` : '?' + buildQueryString(criteria))
? path + (typeof criteria !== 'object'
? `/${criteria}${trailingSlash}`
: `${trailingSlash}?${buildQueryString(criteria)}`)
: resource);
}
@@ -39,22 +39,35 @@ describe('Rest', function() {
injectTest.apiEndpoint.find('posts')
.then(y => {
expect(y.method).toBe('GET');
expect(y.path).toBe('/posts');
}),
injectTest.apiEndpoint.find('posts')
injectTest.apiEndpoint.find('posts/')
.then(y => {
expect(y.path).toBe('/posts');
expect(y.method).toBe('GET');
expect(y.path).toBe('/posts/');
}),
injectTest.apiEndpoint.find('posts', 'id')
.then(y => {
expect(y.path).toBe('/posts/id');
expect(JSON.stringify(y.query)).toBe('{}');
}),
injectTest.apiEndpoint.find('posts/', 'id')
.then(y => {
expect(y.path).toBe('/posts/id/');
expect(JSON.stringify(y.query)).toBe('{}');
}),
injectTest.apiEndpoint.find('posts', criteria)
.then(y => {
expect(y.path).toBe('/posts');
expect(y.query.user).toBe(criteria.user);
expect(y.query.comment).toBe(criteria.comment);
}),
injectTest.apiEndpoint.find('posts/', criteria)
.then(y => {
expect(y.path).toBe('/posts/');
expect(y.query.user).toBe(criteria.user);
expect(y.query.comment).toBe(criteria.comment);
}),
injectTest.apiEndpoint.find('posts', undefined, options)
.then(y => {
expect(y.path).toBe('/posts');

0 comments on commit 4ec39a3

Please sign in to comment.