Created
November 11, 2018 22:47
-
-
Save diegoazh/56c56c29aee8958b34fb6f83ffa226ab to your computer and use it in GitHub Desktop.
Api client how a proxy between vue and loopback
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
// Creé esta clase para no tener que hacer un objeto `api-client` donde repitamos código de axios, ya que los verbos | |
// http siempre son los mismos, y lo único que cambia son los endpoints y los métodos utilizados después. | |
/* eslint-disable no-underscore-dangle */ | |
import axios from 'axios'; | |
import base from '../utils/uri-base'; | |
class Api { | |
constructor() { | |
this._base = base; | |
this._http = axios; | |
this._endpoint = ''; | |
this._id = ''; | |
this._verb = ''; | |
this._apiMethod = ''; | |
this._data = null; | |
this._options = null; | |
} | |
uriConstructor() { | |
let uri = `${this._base}/${this._endpoint}`; | |
if (this._id) { | |
uri += `/${this._id}`; | |
} | |
return `${uri}/${this._apiMethod}`; | |
} | |
endpoint(endpoint) { | |
this._endpoint = endpoint; | |
return this; | |
} | |
id(id) { | |
this._id = id; | |
return this; | |
} | |
data(data) { | |
this._data = data; | |
return this; | |
} | |
options(options) { | |
this._options = options; | |
return this; | |
} | |
get() { | |
this._verb = 'get'; | |
return this; | |
} | |
post() { | |
this._verb = 'post'; | |
return this; | |
} | |
put() { | |
this._verb = 'put'; | |
return this; | |
} | |
delete() { | |
this._verb = 'delete'; | |
return this; | |
} | |
apiMethod(method) { | |
this._apiMethod = method; | |
return this; | |
} | |
async exec() { | |
if (!this._endpoint || !this._verb) throw new Error('Please define the endpoint and the http verb to use'); | |
try { | |
const res = (this._verb === 'post' || this._verb === 'put') | |
? await this._http[this._verb](this.uriConstructor(), this._data, this._options) | |
: await this._http[this._verb](this.uriConstructor(), this._options); | |
this._apiMethod = ''; | |
this._verb = ''; | |
this._id = ''; | |
this._data = null; | |
this._options = null; | |
return res.data ? res.data : res; | |
} catch (error) { | |
throw error; | |
} | |
} | |
} | |
// Required for quasar plugins | |
export default ({ Vue }) => { | |
Vue.prototype.$api = new Api(); | |
}; | |
// ........................................................................................................ | |
// Forma de uso en un componente vue | |
async mounted() { | |
try { | |
this.sellers = await this.$api.endpoint('sellers').get().exec(); | |
this.sales = await this.$api.endpoint('sellers').id(this.sellers[0].id).get().apiMethod('sales').exec(); | |
// La linea 105 podria reescribirse asi | |
this.sales = await this.$api.id(this.sellers[0].id).get().apiMethod('sales').exec(); | |
// Como el endpoint sellers ya fue utilizado este queda almacenado para futuras consultas | |
// lo unico que se limpia al realizar el exec son los datos que frecuentemente se limpian | |
} catch (error) { | |
throw error; | |
} | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment