Created
February 25, 2022 13:10
-
-
Save farnetani/040b8d587cc347e5ecc49cfbb908798a 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
import api from "./api"; | |
class Http { | |
constructor() { | |
this.http = api; | |
} | |
/** | |
* Refresh auth on http | |
*/ | |
refreshAuth() { | |
const token = localStorage.getItem("token"); | |
delete api.defaults.headers.Authorization; | |
if (token) { | |
api.defaults.headers.Authorization = "Bearer " + token; | |
} | |
} | |
/** | |
* Parser error on axios request | |
* @param err | |
* @private | |
*/ | |
async _error(err) { | |
const { response = null } = err; | |
// Check is network error | |
if (!response) { | |
throw new Error(err.message); | |
} | |
// const { status } = response; | |
// if (status === 401) { | |
// localStorage.removeItem("token"); | |
// this.refreshAuth(); | |
// } | |
// const checkStatus = status === 400 || status === 404 || status === 500; | |
// if (checkStatus) { | |
// const { data } = response; | |
// Object.keys(data).forEach((m) => { | |
// if (Array.isArray(data[m])) { | |
// data[m].forEach((k) => { | |
// throw new Error(`${m}: ${k}`); | |
// }); | |
// } else { | |
// throw new Error(`${m}: ${data[m]}`); | |
// } | |
// }); | |
// } | |
} | |
/** | |
* Should be call get on url passing params to filter | |
* @param url | |
* @param params | |
* @returns {Promise<*>} | |
*/ | |
async load(url, params = {}) { | |
const { data } = await this.http | |
.get(url, { params }) | |
.catch((err) => this._error(err)); | |
return data; | |
} | |
/*** | |
* Should be save data. If has pk, call post method on api, else call patch for update data | |
* @param url | |
* @param data | |
* @param pk | |
* @returns {Promise<*>} | |
*/ | |
save(url, data = {}, pk = null) { | |
url = pk && url.endsWith("/") ? url : `${url}`; | |
if (!pk) { | |
return this.http.post(url, data).then((response) => response.data); | |
// .catch((err) => this._error(err)); | |
} | |
url = pk && url.endsWith("/") ? url : `${url}/`; | |
data.id = pk; | |
return this.http | |
.put(`${url}${pk}`, data) | |
.then((response) => response.data) | |
.catch((err) => this._error(err)); | |
} | |
/*** | |
* Should be remove data. | |
* @param url | |
* @param pk | |
* @returns {Promise<*>} | |
*/ | |
remove(url, pk) { | |
url = url.endsWith("/") ? url : `${url}/`; | |
return this.http.delete(`${url}${pk}`).catch((err) => this._error(err)); | |
} | |
} | |
const HttpSingleton = (function () { | |
const instance = null; | |
return { | |
getInstance: () => (!instance ? new Http() : instance), | |
}; | |
})(); | |
export default HttpSingleton.getInstance(); |
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
Skip to content | |
Search… | |
All gists | |
Back to GitHub | |
@farnetani | |
@wgalleti | |
wgalleti/http.js Secret | |
Created 16 hours ago • Report abuse | |
0 | |
0 | |
Code | |
Revisions 1 | |
<script src="https://gist.github.com/wgalleti/d5a0335d4fefb201c90ae248786ec91f.js"></script> | |
http.js | |
import api from "./api"; | |
class Http { | |
constructor() { | |
this.http = api; | |
} | |
/** | |
* Refresh auth on http | |
*/ | |
refreshAuth() { | |
const token = localStorage.getItem("token"); | |
delete api.defaults.headers.Authorization; | |
if (token) { | |
api.defaults.headers.Authorization = "Bearer " + token; | |
} | |
} | |
/** | |
* Parser error on axios request | |
* @param err | |
* @private | |
*/ | |
async _error(err) { | |
const { response = null } = err; | |
// Check is network error | |
if (!response) { | |
throw new Error(err.message); | |
} | |
// const { status } = response; | |
// if (status === 401) { | |
// localStorage.removeItem("token"); | |
// this.refreshAuth(); | |
// } | |
// const checkStatus = status === 400 || status === 404 || status === 500; | |
// if (checkStatus) { | |
// const { data } = response; | |
// Object.keys(data).forEach((m) => { | |
// if (Array.isArray(data[m])) { | |
// data[m].forEach((k) => { | |
// throw new Error(`${m}: ${k}`); | |
// }); | |
// } else { | |
// throw new Error(`${m}: ${data[m]}`); | |
// } | |
// }); | |
// } | |
} | |
/** | |
* Should be call get on url passing params to filter | |
* @param url | |
* @param params | |
* @returns {Promise<*>} | |
*/ | |
async load(url, params = {}) { | |
const { data } = await this.http | |
.get(url, { params }) | |
.catch((err) => this._error(err)); | |
return data; | |
} | |
/*** | |
* Should be save data. If has pk, call post method on api, else call patch for update data | |
* @param url | |
* @param data | |
* @param pk | |
* @returns {Promise<*>} | |
*/ | |
save(url, data = {}, pk = null) { | |
url = pk && url.endsWith("/") ? url : `${url}`; | |
if (!pk) { | |
return this.http.post(url, data).then((response) => response.data); | |
// .catch((err) => this._error(err)); | |
} | |
url = pk && url.endsWith("/") ? url : `${url}/`; | |
data.id = pk; | |
return this.http | |
.put(`${url}${pk}`, data) | |
.then((response) => response.data) | |
.catch((err) => this._error(err)); | |
} | |
/*** | |
* Should be remove data. | |
* @param url | |
* @param pk | |
* @returns {Promise<*>} | |
*/ | |
remove(url, pk) { | |
url = url.endsWith("/") ? url : `${url}/`; | |
return this.http.delete(`${url}${pk}`).catch((err) => this._error(err)); | |
} | |
} | |
const HttpSingleton = (function () { | |
const instance = null; | |
return { | |
getInstance: () => (!instance ? new Http() : instance), | |
}; | |
})(); | |
export default HttpSingleton.getInstance(); | |
model.js | |
import Http from "./http"; | |
import CustomStore from "devextreme/data/custom_store"; | |
export default class Model { | |
/** | |
* @param url (address for endpoint resource) | |
* @param columns (devExpress columns Grid) | |
* @param form (devExpress form Config) | |
* @param primaryKey (resource primary key) | |
*/ | |
constructor(url, columns = null, form = {}, primaryKey = "id") { | |
this.url = url; | |
this.primaryKey = primaryKey; | |
this.columns = columns; | |
this.form = form; | |
this.http = Http; | |
this.pageSize = 50; | |
} | |
/** | |
* List all resource on endpoint | |
* @returns {Promise<Array>} | |
*/ | |
all() { | |
return this.http.load(`${this.url}-all`); | |
} | |
/*** | |
* List a instance of resource on endpoint | |
* @param pk | |
* @returns {Promise<Object>} | |
*/ | |
find(pk, url = "") { | |
const newURL = this.url.endsWith("/") ? this.url : `${this.url}/`; | |
if (url !== "") { | |
return this.http.load(`${newURL}${pk}/${url}`); | |
} | |
return this.http.load(`${newURL}${pk}`); | |
} | |
/*** | |
* Return a list of filtered resource on endpoint | |
* @param params | |
* @param url | |
* @returns {Promise<Array>} | |
*/ | |
filter(params = {}, url = "") { | |
return this.http.load(`${this.url}${url}`, params); | |
} | |
/*** | |
* Return a list of search on endpoint | |
* @param term | |
* @param url | |
* @returns {*} | |
*/ | |
search(term = "", url = "") { | |
return this.http.get(`${this.url}${url}`, { search: term }); | |
} | |
/** | |
* Sometimes we need a custom load | |
* @param url | |
* @param params | |
* @returns {*} | |
*/ | |
static load(url, params) { | |
return this.http.load(url, params); | |
} | |
/** | |
* Save resource on endpoint | |
* @param data | |
* @param pk | |
* @param url | |
* @returns {*} | |
*/ | |
save(data = {}, pk = null, url = "") { | |
return this.http.save(`${this.url}${url}`, data, pk); | |
} | |
/** | |
* Remove resource on endpoint | |
* @param pk | |
* @returns {Q.Promise<any> | Promise<postcss.Result> | undefined} | |
*/ | |
remove(pk) { | |
return this.http.remove(this.url, pk); | |
} | |
} | |
@farnetani | |
Leave a comment | |
Nenhum arquivo selecionado | |
Attach files by dragging & dropping, selecting or pasting them. | |
© 2022 GitHub, Inc. | |
Terms | |
Privacy | |
Security | |
Status | |
Docs | |
Contact GitHub | |
Pricing | |
API | |
Training | |
Blog | |
About |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment