From c655a5981a09ca7433428c61bc109eb7d7737384 Mon Sep 17 00:00:00 2001 From: Jack Thomson Date: Thu, 20 Jul 2017 16:47:41 +0100 Subject: [PATCH] Fixed issue when passing in string it prevents extra speech marks. --- index.js | 98 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/index.js b/index.js index 3c0c3f7..59bd3bc 100644 --- a/index.js +++ b/index.js @@ -1,58 +1,58 @@ export default class RestClient { - constructor (baseUrl = '', { headers = {}, devMode = false, simulatedDelay = 0 } = {}) { - if (!baseUrl) throw new Error('missing baseUrl'); - this.headers = { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }; - Object.assign(this.headers, headers); - this.baseUrl = baseUrl; - this.simulatedDelay = simulatedDelay; - this.devMode = devMode; - } + constructor (baseUrl = '', { headers = {}, devMode = false, simulatedDelay = 0 } = {}) { + if (!baseUrl) throw new Error('missing baseUrl'); + this.headers = { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }; + Object.assign(this.headers, headers); + this.baseUrl = baseUrl; + this.simulatedDelay = simulatedDelay; + this.devMode = devMode; + } - _simulateDelay () { - return new Promise(resolve => { - setTimeout(() => { - resolve(); - }, this.simulatedDelay); + _simulateDelay () { + return new Promise(resolve => { + setTimeout(() => { + resolve(); + }, this.simulatedDelay); }); - } - - _fullRoute (url) { - return `${this.baseUrl}${url}`; - } - - _fetch (route, method, body, isQuery = false) { - if (!route) throw new Error('Route is undefined'); - var fullRoute = this._fullRoute(route); - if (isQuery && body) { - var qs = require('qs'); - const query = qs.stringify(body); - fullRoute = `${fullRoute}?${query}`; - body = undefined; } - let opts = { - method, - headers: this.headers - }; - if (body) { - Object.assign(opts, { body: JSON.stringify(body) }); + + _fullRoute (url) { + return `${this.baseUrl}${url}`; } - const fetchPromise = () => fetch(fullRoute, opts); - if (this.devMode && this.simulatedDelay > 0) { - // Simulate an n-second delay in every request - return this._simulateDelay() - .then(() => fetchPromise()) - .then(response => response.json()); - } else { - return fetchPromise() + + _fetch (route, method, body, isQuery = false) { + if (!route) throw new Error('Route is undefined'); + var fullRoute = this._fullRoute(route); + if (isQuery && body) { + let qs = require('qs'); + const query = qs.stringify(body); + fullRoute = `${fullRoute}?${query}`; + body = undefined; + } + let opts = { + method, + headers: this.headers + }; + if (body) { + Object.assign(opts, { body: (typeof(body) !== 'string') ? JSON.stringify(body) : body }); + } + const fetchPromise = () => fetch(fullRoute, opts); + if (this.devMode && this.simulatedDelay > 0) { + // Simulate an n-second delay in every request + return this._simulateDelay() + .then(() => fetchPromise()) .then(response => response.json()); + } else { + return fetchPromise() + .then(response => response.json()); + } } - } - GET (route, query) { return this._fetch(route, 'GET', query, true); } - POST (route, body) { return this._fetch(route, 'POST', body); } - PUT (route, body) { return this._fetch(route, 'PUT', body); } - DELETE (route, query) { return this._fetch(route, 'DELETE', query, true); } + GET (route, query) { return this._fetch(route, 'GET', query, true); } + POST (route, body) { return this._fetch(route, 'POST', body); } + PUT (route, body) { return this._fetch(route, 'PUT', body); } + DELETE (route, query) { return this._fetch(route, 'DELETE', query, true); } }