Skip to content

Commit

Permalink
Handle query params. Rename options to data for clarity.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrichar1 committed Oct 11, 2018
1 parent b8ca03c commit 6d67dcc
Showing 1 changed file with 45 additions and 29 deletions.
74 changes: 45 additions & 29 deletions src/jsonapi-vuex.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,59 +33,75 @@ const mutations = (api) => { // eslint-disable-line no-unused-vars

const actions = (api) => {
return {
get: (context, options) => {
get: (context, data) => {
let params = {}
if (Array.isArray(data)) {
[ data, params ] = data
}
let path
if (typeof options === 'string') {
path = options
if (typeof data === 'string') {
path = data
} else {
const { type, id } = options['_jv']
const { type, id } = data['_jv']
path = type + "/" + id
}
return api.get(path)
return api.get(path, {params: params})
.then((results) => {
const data = jsonapiToNorm(results.data.data)
context.commit('update_record', data)
return context.getters.get(options)
const res_data = jsonapiToNorm(results.data.data)
context.commit('update_record', res_data)
return context.getters.get(res_data)
})
.catch((error) => {
return error
})
},
post: (context, options) => {
const { type } = options['_jv']
return api.post(type, normToJsonapi(options))
post: (context, data) => {
let params = {}
if (Array.isArray(data)) {
[ data, params ] = data
}
const { type } = data['_jv']
return api.post(type, normToJsonapi(data), {params: params})
.then(() => {
context.commit('update_record', options)
return context.getters.get(options)
context.commit('update_record', data)
return context.getters.get(data)
})
.catch((error) => {
return error
})
},
patch: (context, options) => {
const { type, id } = options['_jv']
patch: (context, data) => {
let params = {}
if (Array.isArray(data)) {
[ data, params ] = data
}
const { type, id } = data['_jv']
let path = type + "/" + id
return api.patch(path, normToJsonapi(options))
return api.patch(path, normToJsonapi(data), {params:params})
.then(() => {
context.commit('update_record', options)
return context.getters.get(options)
context.commit('update_record', data)
return context.getters.get(data)
})
.catch((error) => {
return error
})
},
delete: (context, options) => {
delete: (context, data) => {
let params = {}
if (Array.isArray(data)) {
[ data, params ] = data
}
let path
if (typeof options === 'string') {
if (typeof data === 'string') {
// Use string as a verbatim path for api request
path = options
path = data
} else {
const { type, id } = options['_jv']
const { type, id } = data['_jv']
path = type + "/" + id
}
return api.delete(path)
return api.delete(path, {params:params})
.then(() => {
context.commit('delete_record', options)
context.commit('delete_record', data)
})
.catch((error) => {
return error
Expand All @@ -99,13 +115,13 @@ const actions = (api) => {

const getters = (api) => { // eslint-disable-line no-unused-vars
return {
get: (state) => (options) => {
get: (state) => (data) => {
let type, id
if (options) {
if (typeof(options) === 'string') {
[type, id] = options.replace(/^\//, "").split("/")
if (data) {
if (typeof(data) === 'string') {
[type, id] = data.replace(/^\//, "").split("/")
} else {
({ type, id } = options['_jv'])
({ type, id } = data['_jv'])
}
}
if (type) {
Expand Down

0 comments on commit 6d67dcc

Please sign in to comment.