Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
refactor(rest): use aurelia-path instead of qs
Browse files Browse the repository at this point in the history
  • Loading branch information
doktordirk committed Jun 18, 2016
1 parent a20924e commit 65b344c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 40 deletions.
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ System.config({
map: {
"aurelia-dependency-injection": "npm:aurelia-dependency-injection@1.0.0-beta.1.2.3",
"aurelia-fetch-client": "npm:aurelia-fetch-client@1.0.0-beta.1.2.5",
"aurelia-path": "npm:aurelia-path@1.0.0-beta.1.2.2",
"aurelia-polyfills": "npm:aurelia-polyfills@1.0.0-beta.1.1.6",
"extend": "npm:extend@3.0.0",
"fetch": "github:github/fetch@1.0.0",
"qs": "npm:qs@6.2.0",
"npm:aurelia-dependency-injection@1.0.0-beta.1.2.3": {
"aurelia-logging": "npm:aurelia-logging@1.0.0-beta.1.2.1",
"aurelia-metadata": "npm:aurelia-metadata@1.0.0-beta.1.2.1",
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
"dependencies": {
"aurelia-dependency-injection": "^1.0.0-beta.1.2.3",
"aurelia-fetch-client": "^1.0.0-beta.1.2.5",
"extend": "^3.0.0",
"qs": "^6.1.0"
"aurelia-path": "^1.0.0-beta.1.2.2",
"extend": "^3.0.0"
},
"peerDependencies": {
"aurelia-dependency-injection": "^1.0.0-beta.1.2.3",
"aurelia-fetch-client": "^1.0.0-beta.1.2.5",
"extend": "^3.0.0",
"qs": "^6.1.0"
"aurelia-path": "^1.0.0-beta.1.2.2",
"extend": "^3.0.0"
},
"devDependencies": {
"aurelia-polyfills": "^1.0.0-beta.1.1.6",
Expand All @@ -50,8 +50,8 @@
"dependencies": {
"aurelia-dependency-injection": "^1.0.0-beta.1.2.3",
"aurelia-fetch-client": "^1.0.0-beta.1.2.5",
"extend": "^3.0.0",
"qs": "^6.1.0"
"aurelia-path": "^1.0.0-beta.1.2.2",
"extend": "^3.0.0"
},
"devDependencies": {
"aurelia-tools": "^0.1.20",
Expand Down
42 changes: 12 additions & 30 deletions src/rest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import qs from 'qs';
import {buildQueryString} from 'aurelia-path';
import extend from 'extend';

/**
Expand Down Expand Up @@ -42,7 +42,7 @@ export class Rest {
if (typeof body === 'object' && contentType) {
requestOptions.body = contentType.toLowerCase() === 'application/json'
? JSON.stringify(body)
: qs.stringify(body);
: buildQueryString(body);
}

return this.client.fetch(path, requestOptions).then(response => {
Expand All @@ -64,13 +64,7 @@ export class Rest {
* @return {Promise<Object>|Promise<Error>} Server response as Object
*/
find(resource, criteria, options) {
let requestPath = resource;

if (criteria) {
requestPath += typeof criteria !== 'object' ? `/${criteria}` : '?' + qs.stringify(criteria);
}

return this.request('GET', requestPath, undefined, options);
return this.request('GET', getRequestPath(resource, criteria), undefined, options);
}

/**
Expand All @@ -97,13 +91,7 @@ export class Rest {
* @return {Promise<Object>|Promise<Error>} Server response as Object
*/
update(resource, criteria, body, options) {
let requestPath = resource;

if (criteria) {
requestPath += typeof criteria !== 'object' ? `/${criteria}` : '?' + qs.stringify(criteria);
}

return this.request('PUT', requestPath, body, options);
return this.request('PUT', getRequestPath(resource, criteria), body, options);
}

/**
Expand All @@ -117,13 +105,7 @@ export class Rest {
* @return {Promise<Object>|Promise<Error>} Server response as Object
*/
patch(resource, criteria, body, options) {
let requestPath = resource;

if (criteria) {
requestPath += typeof criteria !== 'object' ? `/${criteria}` : '?' + qs.stringify(criteria);
}

return this.request('PATCH', requestPath, body, options);
return this.request('PATCH', getRequestPath(resource, criteria), body, options);
}

/**
Expand All @@ -136,13 +118,7 @@ export class Rest {
* @return {Promise<Object>|Promise<Error>} Server response as Object
*/
destroy(resource, criteria, options) {
let requestPath = resource;

if (criteria) {
requestPath += typeof criteria !== 'object' ? `/${criteria}` : '?' + qs.stringify(criteria);
}

return this.request('DELETE', requestPath, undefined, options);
return this.request('DELETE', getRequestPath(resource, criteria), undefined, options);
}

/**
Expand All @@ -158,3 +134,9 @@ export class Rest {
return this.post(...arguments);
}
}

function getRequestPath(resource, criteria) {
return (criteria !== undefined && criteria !== null
? resource + (typeof criteria !== 'object' ? `/${criteria}` : '?' + buildQueryString(criteria))
: resource);
}
9 changes: 6 additions & 3 deletions test/rest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ describe('Rest', function() {
injectTest.apiEndpoint.find('posts', criteria)
.then(y => {
expect(y.path).toBe('/posts');
expect(JSON.stringify(y.query)).toBe(JSON.stringify(criteria));
expect(y.query.user).toBe(criteria.user);
expect(y.query.comment).toBe(criteria.comment);
}),
injectTest.apiEndpoint.find('posts', undefined, options)
.then(y => {
Expand Down Expand Up @@ -85,7 +86,8 @@ describe('Rest', function() {
.then(y => {
expect(y.method).toBe('PUT');
expect(y.path).toBe('/posts');
expect(JSON.stringify(y.query)).toBe(JSON.stringify(criteria));
expect(y.query.user).toBe(criteria.user);
expect(y.query.comment).toBe(criteria.comment);
expect(y.contentType).toMatch(options.headers['Content-Type']);
expect(y.Authorization).toBe(options.headers['Authorization']);
done();
Expand Down Expand Up @@ -113,7 +115,8 @@ describe('Rest', function() {
.then(y => {
expect(y.method).toBe('PATCH');
expect(y.path).toBe('/post');
expect(JSON.stringify(y.query)).toBe(JSON.stringify(criteria));
expect(y.query.user).toBe(criteria.user);
expect(y.query.comment).toBe(criteria.comment);
expect(y.contentType).toMatch(options.headers['Content-Type']);
expect(y.Authorization).toBe(options.headers['Authorization']);
done();
Expand Down

0 comments on commit 65b344c

Please sign in to comment.