Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue when passing in string it prevents extra speech marks. #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 49 additions & 49 deletions index.js
Original file line number Diff line number Diff line change
@@ -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); }
}