Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring classes with inheritance
- Loading branch information
1 parent
70faf84
commit 3b098e7
Showing
5 changed files
with
89 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters