Last active
December 6, 2017 16:24
-
-
Save volodymyr-nt/a3401f43614bdded99e7c98dba045913 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| export default class ApiClient { | |
| constructor(params = {}) { | |
| this.baseURL = params.baseURl || ''; | |
| this.defaultData = params.defaultData || {}; | |
| this.defaultData.locale = params.locale || this.defaultLocale; | |
| } | |
| request(method, url, data = {}) { | |
| const requestData = this.createRequestData(data, method); | |
| return $.ajax(this.createRequestObject(method, url, requestData)); | |
| } | |
| createRequestObject(method, url, requestData) { | |
| const request = { | |
| method, | |
| url: this.baseURL + url, | |
| dataType: 'json', | |
| contentType: 'application/json', | |
| data: requestData, | |
| }; | |
| if (this.crossDomain) request.crossDomain = true; | |
| if (this.authorizationParams) { | |
| request.headers = { | |
| Authorization: this.authorizationParams | |
| }; | |
| } | |
| return request; | |
| } | |
| createRequestData(data, method) { | |
| const defaultRequestData = Object.assign({}, this.defaultData); | |
| const requestData = Object.assign({}, defaultRequestData, data); | |
| if (method === 'POST' || method === 'PATCH') { | |
| return JSON.stringify(requestData); | |
| } | |
| return requestData; | |
| } | |
| get(url, params = {}) { | |
| return this.request('GET', url, params); | |
| } | |
| post(url, data = {}) { | |
| return this.request('POST', url, data); | |
| } | |
| patch(url, data = {}) { | |
| return this.request('PATCH', url, data); | |
| } | |
| delete(url) { | |
| return this.request('DELETE', url); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment