Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const [ res, id, relationship ] = model.split('/')
let url = this.plural(this.resCase(res))
if (id) url += `/${id}`
if (relationship) url += `/${this.resCase(relationship)}`
/* istanbul ignore next */
const { data } = await this.axios.get(url, {
params,
paramsSerializer: p => query(p),
headers: Object.assign(this.headers, headers)
})
return deserialise(data)
} catch (E) {
throw error(E)
}
}
async get (model, params = {}, headers = {}) {
try {
const [ res, id, relationship ] = model.split('/')
let url = this.plural(this.resCase(res))
if (id) url += `/${id}`
if (relationship) url += `/${this.resCase(relationship)}`
/* istanbul ignore next */
const { data } = await this.axios.get(url, {
params,
paramsSerializer: p => query(p),
headers: Object.assign(this.headers, headers)
})
return deserialise(data)
} catch (E) {
throw error(E)
}
}
async self (params = {}, headers = {}) {
try {
const res = await this.get('users', Object.assign({ filter: { self: true } }, params), headers)
return res.data[0]
} catch (E) {
throw error(E)
}
}
}
async delete (model, id, headers = {}) {
try {
const url = this.plural(this.resCase(model)) + '/' + id
const { data } = await this.axios.delete(url, {
data: serialise.apply(this, [ model, { id }, 'DELETE' ]),
headers: Object.assign(this.headers, headers)
})
return data
} catch (E) {
throw error(E)
}
}
async patch (model, body, headers = {}) {
try {
const serialData = serialise.apply(this, [ model, body, 'PATCH' ])
const url = this.plural(this.resCase(model)) + '/' + body.id
const { data } = await this.axios.patch(
url,
serialData,
{ headers: Object.assign(this.headers, headers) }
)
return data
} catch (E) {
throw error(E)
}
}
async post (model, body, headers = {}) {
try {
const url = this.plural(this.resCase(model))
const { data } = await this.axios.post(
url,
serialise.apply(this, [ model, body ]),
{ headers: Object.assign(this.headers, headers) }
)
return data
} catch (E) {
throw error(E)
}
}
async patch (model, body, headers = {}) {
try {
const serialData = serialise.apply(this, [ model, body, 'PATCH' ])
const url = this.plural(this.resCase(model)) + '/' + body.id
const { data } = await this.axios.patch(
url,
serialData,
{ headers: Object.assign(this.headers, headers) }
)
return data
} catch (E) {
throw error(E)
}
}
async delete (model, id, headers = {}) {
try {
const url = this.plural(this.resCase(model)) + '/' + id
const { data } = await this.axios.delete(url, {
data: serialise.apply(this, [ model, { id }, 'DELETE' ]),
headers: Object.assign(this.headers, headers)
})
return data
} catch (E) {
throw error(E)
}
}
async post (model, body, headers = {}) {
try {
const url = this.plural(this.resCase(model))
const { data } = await this.axios.post(
url,
serialise.apply(this, [ model, body ]),
{ headers: Object.assign(this.headers, headers) }
)
return data
} catch (E) {
throw error(E)
}
}
paramsSerializer: p => query(p),
headers: Object.assign(this.headers, headers)