Skip to content

Instantly share code, notes, and snippets.

@lpj145
Created December 22, 2020 20:19
Show Gist options
  • Save lpj145/d26651d7707092626e89ecd57f9cd682 to your computer and use it in GitHub Desktop.
Save lpj145/d26651d7707092626e89ecd57f9cd682 to your computer and use it in GitHub Desktop.
Rest query js.
import { isUndefined, isArray, isObject } from '@/utils/function'
function capitalize(str) {
return `${str[0].toUpperCase()}${str.slice(1).toLowerCase()}`
}
function methodExist(context, methodName) {
return typeof context[methodName] === 'function'
}
function setSort(context, properties) {
Object.keys(properties).forEach((property) => {
const setDirectionMethod = `sort${capitalize(properties[property])}`
if (methodExist(context, setDirectionMethod)) {
context[setDirectionMethod](property)
}
})
}
function setConditions(context, properties) {
Object.keys(properties).forEach((property) => {
if (!(properties[property] instanceof Object)) {
// eslint-disable-next-line no-throw-literal
throw `property: ${property} cannot is a object.`
}
const conditionMethod = Object.keys(properties[property])[0]
const conditionValue = properties[property][conditionMethod]
if (!methodExist(context, conditionMethod)) {
return
}
if (isArray(conditionValue) || isObject(conditionValue)) {
context[conditionMethod](property, ...conditionValue)
return
}
context[conditionMethod](property, conditionValue)
})
}
export default class QueryRest {
/**
* @param {Rest} apiClient
*/
constructor(apiClient) {
this.httpClient = apiClient
this.urlParam = new URLSearchParams()
}
/**
* @param {Array} fields
*/
select (fields) {
if (!isArray(fields)) {
// eslint-disable-next-line no-throw-literal
throw 'Select need a array argument.'
}
return this.appendCondition('props', fields.join(','))
}
sortDesc (fieldName) {
return this.appendCondition(`sort[${fieldName}]`, 'desc')
}
sortAsc (fieldName) {
return this.appendCondition(`sort[${fieldName}]`, 'asc')
}
page (page = 1) {
return this.appendCondition('page', page)
}
limit (quantity = 10) {
return this.appendCondition('size', quantity)
}
noPage() {
return this.appendCondition('noPage', true)
}
/**
* @param {Array} nested
*/
nestedData (nested) {
if (!isArray(nested)) {
// eslint-disable-next-line no-throw-literal
throw 'nested need to be array.'
}
return this.appendCondition('nested', nested.join(','))
}
notIn (fieldName, arrayValues) {
if (!isArray(arrayValues)) {
// eslint-disable-next-line no-throw-literal
throw `In condition received a non array var to ${fieldName} field and condition`
}
return this.appendCondition(`${fieldName}[not]`, arrayValues.join(','))
}
gt (fieldName, value) {
return this.appendCondition(`${fieldName}[gt]`, value)
}
gte (fieldName, value) {
return this.appendCondition(`${fieldName}[gte]`, value)
}
lt (fieldName, value) {
return this.appendCondition(`${fieldName}[lt]`, value)
}
lte (fieldName, value) {
return this.appendCondition(`${fieldName}[lte]`, value)
}
dto(dtoName = null) {
if (typeof dtoName !== 'string') {
return this
}
return this.appendCondition('dto', dtoName)
}
eq (fieldName, value) {
return this.appendCondition(fieldName, value)
}
in (fieldName, ...arrayValues) {
if (!isArray(arrayValues)) {
// eslint-disable-next-line no-throw-literal
throw `In condition received a non array var to ${fieldName} field and condition`
}
return this.appendCondition(`${fieldName}`, arrayValues.join(','))
}
between (fieldName, from, to) {
if (isUndefined(from) || isUndefined(to)) {
// eslint-disable-next-line no-throw-literal
throw `Shoud pass both values to ${fieldName} condition`
}
return this.appendCondition(fieldName, `${from}..${to}`)
}
like (fieldName, fieldValue) {
return this.appendCondition(fieldName, fieldValue)
}
appendCondition (fieldName, fieldValue) {
if (fieldValue === null || fieldValue === undefined) {
return this
}
this.urlParam.append(fieldName, fieldValue)
return this
}
compileQuery({ conditions, meta, select }) {
if (conditions) {
setConditions(this, conditions)
}
if (select) {
this.select(select)
}
if (!meta) {
return this
}
if (meta.sort) {
setSort(this, meta.sort)
}
if (meta.noPage) {
this.noPage()
}
if (meta.page) {
this.page(meta.page)
}
return this
}
execute (url = '') {
return this.httpClient.find(url, this.urlParam)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment