Extract appendPath function from Api.buildUrl
authorFrank Bessou <frank.bessou@logilab.fr>
Tue, 04 Apr 2017 14:09:22 +0200
changeset 70 2d72f308d3c3
parent 69 29faede0bd68
child 71 407e49c80ec2
Extract appendPath function from Api.buildUrl
src/Api.js
src/utils.js
test/index.js
--- a/src/Api.js	Tue Apr 04 11:50:24 2017 +0200
+++ b/src/Api.js	Tue Apr 04 14:09:22 2017 +0200
@@ -3,6 +3,7 @@
 import 'whatwg-fetch';
 import {isEmpty} from 'lodash/lang';
 import {get, defaultsDeep} from 'lodash/object';
+import {appendPath} from './utils';
 import {wrapEntityData} from './jsonaryutils';
 
 export class Api {
@@ -20,9 +21,7 @@
     }
 
     buildUrl(path) {
-        const uri = path[0] === '/' ? path.slice(1) : path;
-        const prefix = this.baseUrl.slice(-1) === '/' ? this.baseUrl : `${this.baseUrl}/`;
-        return prefix + uri;
+        return appendPath(this.baseUrl, path);
     }
 
     jsonFetchResponse(url, options = {}) {
--- a/src/utils.js	Tue Apr 04 11:50:24 2017 +0200
+++ b/src/utils.js	Tue Apr 04 14:09:22 2017 +0200
@@ -6,6 +6,13 @@
     return get(schema, definitionPath);
 }
 
+export function appendPath(url, path) {
+    path = path[0] === '/' ? path.slice(1) : path;
+    url = url.slice(-1) === '/' ? url : `${url}/`;
+    return url + path;
+}
+
+
 export function buildFormData(wrappedData) {
     // Return a formData object suitable for usage in a
     // react-jsonschema-form's Form with `schema`. This is built by filter out
--- a/test/index.js	Tue Apr 04 11:50:24 2017 +0200
+++ b/test/index.js	Tue Apr 04 14:09:22 2017 +0200
@@ -345,9 +345,8 @@
 
 });
 
-describe('Api', () => {
-
-    describe('buildUrl', () => {
+describe('Utils', () => {
+    describe('appendPath', () => {
         it('should add slash between BASE_URL and path when it is not already there', () => {
             const baseUrl = 'http://example.com';
             const path = 'foo';
@@ -368,6 +367,9 @@
             expect(url).to.be.equal('http://example.com/foo');
         });
     });
+});
+
+describe('Api', () => {
 
     describe('jsonFetch', () => {
         const baseUrl = 'http://example.com';