Created
September 4, 2019 14:10
-
-
Save lpj145/5e643235fcae0d5d2c7e30732bd842ea 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 {isNull, isUndefined, isObject} from 'Utils/functions.js'; | |
import axios from 'axios'; | |
import Query from './Query/Query'; | |
function handleQueries(options) { | |
const mountedParams = {} | |
if (!isUndefined(options.where) && options.where instanceof URLSearchParams) { | |
mountedParams.params = options.where | |
} | |
return mountedParams | |
} | |
function thenResponse(d) { | |
return d = d.data | |
} | |
export default class api{ | |
constructor (baseURL = null, httpClient = null) { | |
this.baseURL = baseURL; | |
if (isNull(httpClient)) { | |
httpClient = axios.create({ | |
baseURL: baseURL, | |
timeout: 10000 | |
}) | |
} | |
this.http = httpClient | |
} | |
get (url, options = {}) { | |
const params = handleQueries(options) | |
return this.http.get(url, { ...params }) | |
.then(thenResponse) | |
} | |
find () { | |
return new Query(this) | |
} | |
create (url) { | |
return this.http.post(url) | |
.then(thenResponse) | |
} | |
update (url) { | |
return this.http.patch(url) | |
.then(thenResponse) | |
} | |
remove (url) { | |
return this.http.delete(url) | |
.then(thenResponse) | |
} | |
} |
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 'Services/http/api.js'; | |
const clientesApi = new api('/api/pessoas'); | |
export default { | |
listarTodos () { | |
return clientesApi | |
.find() | |
.equal("id", 2) | |
.execute() | |
} | |
} |
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 {isArray, isUndefined} from 'Utils/functions.js' | |
import Api from '../api.js' | |
export default class Query { | |
/** | |
* | |
* @param {Api} apiClient | |
*/ | |
constructor (apiClient) { | |
this.apiClient = apiClient | |
this.urlParam = new URLSearchParams() | |
} | |
equal (fieldName, fieldValue) { | |
return this.appendCondition(fieldName, fieldValue, '=') | |
} | |
greatThan (fieldName, fieldValue) { | |
return this.appendCondition(fieldName, '>', fieldValue) | |
} | |
lessThan (fieldName, fieldValue) { | |
return this.appendCondition(fieldName, '<', fieldValue) | |
} | |
greatThanEqual (fieldName, fieldValue) { | |
return this.appendCondition(fieldName, '>=', fieldValue) | |
} | |
lessThanEqual (fieldName, fieldValue) { | |
return this.appendCondition(fieldName, '<=', fieldValue) | |
} | |
in (fieldName, arrayValues) { | |
if (!isArray(arrayValues)) { | |
throw `Argument of value is not a array to ${fieldName} field and condition.` | |
} | |
return this.appendCondition(fieldName, arrayValues) | |
} | |
like (fieldName, fieldValue) { | |
return this.appendCondition(fieldName, 'like', fieldValue) | |
} | |
between (fieldName, value1, value2) { | |
if (isUndefined(value1), isUndefined(value2)) { | |
throw `Shoud pass value1 and value2 to ${fieldName} condition` | |
} | |
return this.appendCondition(fieldName, fieldValue) | |
} | |
/** | |
* Add condition whith UrlParams | |
* @param {String} fieldName | |
* @param {any} fieldValue | |
* @param {String} operator | |
*/ | |
appendCondition (fieldName, fieldValue, operator) { | |
this.urlParam.append(`where[${fieldName}]`, `${operator}~>${fieldValue}`) | |
return this | |
} | |
/** | |
* Execute query on api target | |
* @property {String} url | |
*/ | |
execute (url = '') { | |
return this.apiClient.get(url, { where: this.urlParam }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment