vue-api-query
Advanced tools
Comparing version
{ | ||
"name": "vue-api-query", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"description": "Companion vue package for spatie/laravel-query-builder", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -1,2 +0,1 @@ | ||
import axios from 'axios' | ||
import Parser from './Parser'; | ||
@@ -7,4 +6,5 @@ | ||
constructor(model) { | ||
this.model = new (model) | ||
this.model = model | ||
this.includes = [] | ||
this.appends = [] | ||
this.sorts = [] | ||
@@ -16,33 +16,8 @@ this.filters = { | ||
this.parser = new Parser(this) | ||
if (this.model.baseURL === undefined) { | ||
throw new Error('You must declare baseURL() method (ex: http://site.com/api)') | ||
} | ||
if (this.model.resource === undefined) { | ||
throw new Error('You must declare resource() method .') | ||
} | ||
} | ||
find (id) { | ||
if (id === undefined) { | ||
throw new Error('The "id" is required on find() method') | ||
} | ||
let url = `${this.model.baseURL()}/${this.model.resource()}/${id}${this.parser.uri()}` | ||
return axios.get(`${url}`).then(response => { | ||
this.model = Object.assign(this.model, response.data) | ||
return this.model | ||
}) | ||
query () { | ||
return this.parser.query() | ||
} | ||
get () { | ||
let url = `${this.model.baseURL()}/${this.model.resource()}${this.parser.uri()}` | ||
return axios.get(`${url}`).then(response => { | ||
return response.data | ||
}) | ||
} | ||
with (...args) { | ||
@@ -75,2 +50,8 @@ this.includes = args | ||
append (...args) { | ||
this.appends = args | ||
return this | ||
} | ||
orderBy (...args) { | ||
@@ -77,0 +58,0 @@ this.sorts = args |
161
src/Model.js
@@ -1,50 +0,153 @@ | ||
import axios from 'axios' | ||
import Builder from './Builder'; | ||
import StaticModel from './StaticModel'; | ||
export default class Model { | ||
export default class Model extends StaticModel { | ||
static get () { | ||
return new Builder(this).get() | ||
constructor(...atributtes) { | ||
super() | ||
this.builder = new Builder() | ||
this.from = null | ||
Object.assign(this, ...atributtes) | ||
if (this.baseURL === undefined) { | ||
throw new Error('You must declare baseURL() method (ex: http://site.com/api)') | ||
} | ||
if (this.resource === undefined) { | ||
throw new Error('You must declare resource() method .') | ||
} | ||
} | ||
request (config) { | ||
// to be implemented on base model | ||
} | ||
static find (id) { | ||
return new Builder(this).find(id) | ||
hasMany (model) { | ||
let instance = new model | ||
let url = `${this.baseURL()}/${this.resource()}/${this.id}/${instance.resource()}` | ||
instance._from(url) | ||
return instance | ||
} | ||
static with (...args) { | ||
return new Builder(this).with(args) | ||
_from (url) { | ||
this.from = url | ||
} | ||
static where (field, value) { | ||
return new Builder(this).where(field, value) | ||
with (...args) { | ||
this.builder.with(args) | ||
return this | ||
} | ||
static whereIn (field, array) { | ||
return new Builder(this).whereIn(field, array) | ||
where (field, value) { | ||
this.builder.where(field, value) | ||
return this | ||
} | ||
static orderBy (...args) { | ||
return new Builder(this).orderBy(args) | ||
whereIn (field, array) { | ||
this.builder.whereIn(field, array) | ||
return this | ||
} | ||
endpoint () { | ||
if (this.id === undefined || this.id === 0 || this.id === '') { | ||
return `${this.baseURL()}/${this.resource()}` | ||
} else { | ||
return `${this.baseURL()}/${this.resource()}/${this.id}` | ||
append (...args) { | ||
this.builder.append(args) | ||
return this | ||
} | ||
orderBy (...args) { | ||
this.builder.orderBy(args) | ||
return this | ||
} | ||
find (id) { | ||
return (id === undefined) ? this._first() : this._exact(id) | ||
} | ||
_exact (id) { | ||
if (!Number.isInteger(id)) { | ||
throw new Error('The "id" must be a integer') | ||
} | ||
let url = `${this.baseURL()}/${this.resource()}/${id}${this.builder.query()}` | ||
return this.request({ | ||
url, | ||
method: 'GET' | ||
}).then(response => { | ||
let item = new this.constructor(response.data) | ||
delete item.builder | ||
delete item.from | ||
return item | ||
}) | ||
} | ||
save () { | ||
if (this.id === undefined || this.id === 0 || this.id === '') { | ||
return this.create() | ||
_first () { | ||
return this.get().then(response => { | ||
let item | ||
if (response.data.data) { | ||
item = response.data.data[0] | ||
} else { | ||
item = response.data[0] | ||
} | ||
if (item) | ||
return item | ||
else | ||
throw new Error('No item found for specified params') | ||
}) | ||
} | ||
get () { | ||
let base = this.from || `${this.baseURL()}/${this.resource()}` | ||
let url = `${base}${this.builder.query()}` | ||
return this.request({ | ||
url, | ||
method: 'GET' | ||
}).then(response => { | ||
let collection = response.data.data || response.data | ||
collection = collection.map(c => { | ||
let item = new this.constructor(c) | ||
delete item.builder | ||
delete item.from | ||
return item | ||
}) | ||
if (response.data.data !== undefined) { | ||
response.data.data = collection | ||
} else { | ||
response.data = collection | ||
} | ||
return response.data | ||
}) | ||
} | ||
hasId () { | ||
return this.id !== undefined && this.id !== 0 && this.id !== '' | ||
} | ||
endpoint () { | ||
if (this.hasId()) { | ||
return `${this.baseURL()}/${this.resource()}/${this.id}` | ||
} else { | ||
return this.update() | ||
return `${this.baseURL()}/${this.resource()}` | ||
} | ||
} | ||
create () { | ||
return axios.post(this.endpoint(), this).then(response => { | ||
save () { | ||
return this.hasId() ? this._update() : this._create() | ||
} | ||
_create () { | ||
return this.request({ | ||
method: 'POST', | ||
url: this.endpoint(), | ||
data: this | ||
}).then(response => { | ||
let self = Object.assign(this, response.data) | ||
@@ -55,4 +158,8 @@ return self | ||
update () { | ||
return axios.put(this.endpoint(), this).then(response => { | ||
_update () { | ||
return this.request({ | ||
method: 'PUT', | ||
url: this.endpoint(), | ||
data: this | ||
}).then(response => { | ||
let self = Object.assign(this, response.data) | ||
@@ -59,0 +166,0 @@ return self |
@@ -7,39 +7,66 @@ import qs from 'qs' | ||
this.builder = builder | ||
this.uri = '' | ||
} | ||
uri () { | ||
let includes = this.includes() | ||
let filters = this.filters() | ||
let sorts = this.sorts() | ||
query () { | ||
this.includes() | ||
this.filters() | ||
this.appends() | ||
this.sorts() | ||
return `${includes}${filters}${sorts}` | ||
return this.uri | ||
} | ||
hasFilters () { | ||
return Object.keys(this.builder.filters.filter).length > 0 | ||
} | ||
hasIncludes () { | ||
return this.builder.includes.length > 0 | ||
} | ||
hasAppends () { | ||
return this.builder.appends.length > 0 | ||
} | ||
hasSorts () { | ||
return this.builder.sorts.length > 0 | ||
} | ||
prepend () { | ||
return (this.builder.includes.length) ? '&' : '?' | ||
return (this.uri === '') ? '?' : '&' | ||
} | ||
includes () { | ||
if (this.builder.includes.length === 0) { | ||
return '' | ||
if (!this.hasIncludes()) { | ||
return | ||
} | ||
return '?include=' + this.builder.includes | ||
this.uri += this.prepend() + 'include=' + this.builder.includes | ||
} | ||
appends () { | ||
if (!this.hasAppends()) { | ||
return | ||
} | ||
this.uri += this.prepend() + 'append=' + this.builder.appends | ||
} | ||
sorts () { | ||
if (this.builder.sorts.length === 0) { | ||
return '' | ||
if (!this.hasSorts()) { | ||
return | ||
} | ||
return this.prepend() + 'sort=' + this.builder.sorts | ||
this.uri += this.prepend() + 'sort=' + this.builder.sorts | ||
} | ||
filters () { | ||
if (Object.keys(this.builder.filters.filter).length === 0) { | ||
return '' | ||
if (!this.hasFilters()) { | ||
return | ||
} | ||
return this.prepend() + qs.stringify(this.builder.filters) | ||
this.uri += this.prepend() + qs.stringify(this.builder.filters) | ||
} | ||
} |
76398
3.97%13
8.33%513
35.71%