Skip to content

Commit

Permalink
Refactoring classes with inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
sonufrienko committed Aug 26, 2018
1 parent 70faf84 commit 3b098e7
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 191 deletions.
41 changes: 2 additions & 39 deletions src/ajaxClient.js
@@ -1,11 +1,6 @@
import fetch from 'isomorphic-fetch';
import queryString from 'query-string';

export default class AjaxClient {
constructor(options) {
this.baseUrl = options.baseUrl;
}
import RestClient from './restClient';

export default class AjaxClient extends RestClient {
getConfig(method, data, cookie) {
const config = {
credentials: this.getCredentialsConfig(this.baseUrl),
Expand All @@ -31,36 +26,4 @@ export default class AjaxClient {
baseUrl.includes('http://') || baseUrl.includes('https://');
return includePrefix ? 'include' : 'same-origin';
}

returnStatusAndJson(response) {
return response
.json()
.then(json => ({ status: response.status, json }))
.catch(() => ({ status: response.status, json: null }));
}

get(endpoint, filter, cookie) {
return fetch(
`${this.baseUrl}${endpoint}?${queryString.stringify(filter)}`,
this.getConfig('get', null, cookie)
).then(this.returnStatusAndJson);
}

post(endpoint, data) {
return fetch(this.baseUrl + endpoint, this.getConfig('post', data)).then(
this.returnStatusAndJson
);
}

put(endpoint, data) {
return fetch(this.baseUrl + endpoint, this.getConfig('put', data)).then(
this.returnStatusAndJson
);
}

delete(endpoint) {
return fetch(this.baseUrl + endpoint, this.getConfig('delete')).then(
this.returnStatusAndJson
);
}
}
84 changes: 5 additions & 79 deletions src/apiClient.js
@@ -1,92 +1,18 @@
import fetch from 'isomorphic-fetch';
import queryString from 'query-string';
import RestClient from './restClient';

export default class ApiClient {
constructor(options) {
this.baseUrl = options.baseUrl;
this.token = options.token;
}

static authorize = (baseUrl, endpoint, email) => {
export default class ApiClient extends RestClient {
static authorize = (baseUrl, email) => {
const config = {
credentials: 'same-origin',
method: 'post',
headers: {
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, deflate'
},
body: JSON.stringify({ email })
};
return fetch(baseUrl + endpoint, config).then(
ApiClient.returnStatusAndJson
return fetch(`${baseUrl}/authorize`, config).then(
RestClient.returnStatusAndJson
);
};

static returnStatusAndJson = response =>
response
.json()
.then(json => ({ status: response.status, json }))
.catch(() => ({ status: response.status, json: null }));

getConfig(method, data) {
const config = {
credentials: 'same-origin',
method,
headers: {
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, deflate',
Authorization: `Bearer ${this.token}`
}
};

if (data) {
config.body = JSON.stringify(data);
}
return config;
}

postFormDataConfig(formData) {
const config = {
credentials: 'same-origin',
method: 'post',
body: formData,
headers: {
Authorization: `Bearer ${this.token}`
}
};

return config;
}

get(endpoint, filter) {
return fetch(
`${this.baseUrl}${endpoint}?${queryString.stringify(filter)}`,
this.getConfig('get')
).then(ApiClient.returnStatusAndJson);
}

post(endpoint, data) {
return fetch(this.baseUrl + endpoint, this.getConfig('post', data)).then(
ApiClient.returnStatusAndJson
);
}

put(endpoint, data) {
return fetch(this.baseUrl + endpoint, this.getConfig('put', data)).then(
ApiClient.returnStatusAndJson
);
}

delete(endpoint) {
return fetch(this.baseUrl + endpoint, this.getConfig('delete')).then(
ApiClient.returnStatusAndJson
);
}

postFormData(endpoint, formData) {
return fetch(
this.baseUrl + endpoint,
this.postFormDataConfig(formData)
).then(ApiClient.returnStatusAndJson);
}
}
3 changes: 1 addition & 2 deletions src/index.js
Expand Up @@ -112,8 +112,7 @@ export default class Client {
this.webstore.services.logs = new WebStoreServiceLogs(webstoreClient);
}

static authorize = (baseUrl, email) =>
ApiClient.authorize(baseUrl, '/authorize', email);
static authorize = (baseUrl, email) => ApiClient.authorize(baseUrl, email);

static authorizeInWebStore = (email, adminUrl) =>
WebStoreClient.authorize(email, adminUrl);
Expand Down
77 changes: 77 additions & 0 deletions src/restClient.js
@@ -0,0 +1,77 @@
import fetch from 'isomorphic-fetch';
import queryString from 'query-string';

export default class RestClient {
constructor({ baseUrl, token }) {
this.baseUrl = baseUrl;
this.token = token;
}

getConfig(method, data) {
const config = {
method,
headers: {
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, deflate',
Authorization: `Bearer ${this.token}`
}
};

if (data) {
config.body = JSON.stringify(data);
}
return config;
}

postFormDataConfig = formData => ({
method: 'post',
body: formData,
headers: {
Authorization: `Bearer ${this.token}`
}
});

returnStatusAndJson = response =>
response
.json()
.then(json => ({ status: response.status, json }))
.catch(() => ({ status: response.status, json: null }));

static returnStatusAndJsonStatic = response =>
response
.json()
.then(json => ({ status: response.status, json }))
.catch(() => ({ status: response.status, json: null }));

get(endpoint, filter, cookie) {
return fetch(
`${this.baseUrl}${endpoint}?${queryString.stringify(filter)}`,
this.getConfig('get', null, cookie)
).then(this.returnStatusAndJson);
}

post(endpoint, data) {
return fetch(this.baseUrl + endpoint, this.getConfig('post', data)).then(
this.returnStatusAndJson
);
}

postFormData(endpoint, formData) {
return fetch(
this.baseUrl + endpoint,
this.postFormDataConfig(formData)
).then(this.returnStatusAndJson);
}

put(endpoint, data) {
return fetch(this.baseUrl + endpoint, this.getConfig('put', data)).then(
this.returnStatusAndJson
);
}

delete(endpoint) {
return fetch(this.baseUrl + endpoint, this.getConfig('delete')).then(
this.returnStatusAndJson
);
}
}
75 changes: 4 additions & 71 deletions src/webstoreClient.js
@@ -1,10 +1,9 @@
import fetch from 'isomorphic-fetch';
import queryString from 'query-string';
import RestClient from './restClient';

export default class WebStoreClient {
export default class WebStoreClient extends RestClient {
constructor(options) {
this.baseUrl = 'https://api.cezerin.com/v1';
this.token = options.token;
super({ baseUrl: 'https://api.cezerin.com/v1', token: options.token });
}

static authorize = (email, adminUrl) => {
Expand All @@ -18,73 +17,7 @@ export default class WebStoreClient {
};

return fetch('https://api.cezerin.com/v1/account/authorize', config).then(
WebStoreClient.returnStatusAndJson
RestClient.returnStatusAndJson
);
};

static returnStatusAndJson = response =>
response
.json()
.then(json => ({ status: response.status, json }))
.catch(() => ({ status: response.status, json: null }));

getConfig(method, data) {
const config = {
method,
headers: {
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, deflate',
Authorization: `Bearer ${this.token}`
}
};

if (data) {
config.body = JSON.stringify(data);
}
return config;
}

postFormDataConfig(formData) {
const config = {
method: 'post',
body: formData,
headers: {
Authorization: `Bearer ${this.token}`
}
};

return config;
}

get(endpoint, filter) {
return fetch(
`${this.baseUrl}${endpoint}?${queryString.stringify(filter)}`,
this.getConfig('get')
).then(WebStoreClient.returnStatusAndJson);
}

post(endpoint, data) {
return fetch(this.baseUrl + endpoint, this.getConfig('post', data)).then(
WebStoreClient.returnStatusAndJson
);
}

put(endpoint, data) {
return fetch(this.baseUrl + endpoint, this.getConfig('put', data)).then(
WebStoreClient.returnStatusAndJson
);
}

delete(endpoint) {
return fetch(this.baseUrl + endpoint, this.getConfig('delete')).then(
WebStoreClient.returnStatusAndJson
);
}

postFormData(endpoint, formData) {
return fetch(
this.baseUrl + endpoint,
this.postFormDataConfig(formData)
).then(WebStoreClient.returnStatusAndJson);
}
}

0 comments on commit 3b098e7

Please sign in to comment.