Created
December 21, 2019 15:07
-
-
Save olehmell/24830107ec64231dc64ae6a307d07b0a to your computer and use it in GitHub Desktop.
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
class Api { | |
_res; | |
constructor (_baseUrl) { | |
this._baseUrl = _baseUrl; | |
this._xhr = new XMLHttpRequest(); | |
this._xhr.onload = this._onload; | |
this._xhr.addEventListener( 'load', this._onSuccess ); | |
} | |
_onload () { | |
const response = JSON.parse(this.response) | |
if (request.status >= 200 && request.status < 400) { | |
response.forEach(movie => { | |
console.log(movie.title) | |
}) | |
this._res = response; | |
} else { | |
new Error(`Error with status: ${response.status}! Message: ${response.error}`); | |
} | |
} | |
_onSuccess () { | |
this._res = this.response; | |
console.log( res ); | |
} | |
async get (url) { | |
await this._xhr.open( 'GET', `http://${this.baseUrl}${url}` ); | |
await this._xhr.send(); | |
return this._res; | |
} | |
async post (url, data) { | |
await this._xhr.open( 'POST', `http://${this.baseUrl}${url}` ); | |
const formattedJsonData = JSON.stringify( data ); | |
await this._xhr.send( formattedJsonData ); | |
return this._res; | |
} | |
async delete (url) { | |
await this._xhr.open( 'DELETE', `http://${this.baseUrl}${url}` ); | |
await this._xhr.send(); | |
return this._res; | |
} | |
} | |
class ClientApi extends Api { | |
constructor () { | |
super('testnet'); | |
} | |
async createUser (data) { | |
const res = await this.post('/user/create', data); | |
console.log(res); | |
} | |
async getUser (id) { | |
const res = await this.get(`/user/get${id}`); | |
console.log(res); | |
} | |
async deleteUser (id) { | |
const res = await this.delete(`/user/delete${id}`); | |
console.log(res); | |
} | |
} | |
export const api = new Api(); | |
export const clientApi = new ClientApi(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment