You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.6 KiB
56 lines
1.6 KiB
import APIParseError from './APIParseError.js'
|
|
|
|
export default class CRUDBase {
|
|
endpoint = '/api/v1'
|
|
required_fields = []
|
|
|
|
listing_definition = {}
|
|
form_definition = {}
|
|
|
|
item = ''
|
|
plural = ''
|
|
|
|
async list() {
|
|
const results = await axios.get(this._endpoint())
|
|
if ( results && results.data && Array.isArray(results.data.data) ) return results.data.data
|
|
else throw new APIParseError()
|
|
}
|
|
|
|
async get(id) {
|
|
const results = await axios.get(this._endpoint(id))
|
|
if ( results && results.data && results.data.data ) return results.data.data
|
|
else throw new APIParseError()
|
|
}
|
|
|
|
async create(properties) {
|
|
for ( const field of this.required_fields ) {
|
|
if ( !properties[field] ) throw new Error(`Missing required field: ${field}`)
|
|
}
|
|
|
|
const results = await axios.post(this._endpoint(), properties)
|
|
if ( results && results.data && results.data.data ) return results.data.data
|
|
else throw new APIParseError()
|
|
}
|
|
|
|
async update(id, properties) {
|
|
for ( const field of this.required_fields ) {
|
|
if ( !properties[field] ) throw new Error(`Missing required field: ${field}`)
|
|
}
|
|
|
|
await axios.patch(this._endpoint(id), properties)
|
|
}
|
|
|
|
async delete(id) {
|
|
await axios.delete(this._endpoint(id))
|
|
}
|
|
|
|
_endpoint(sub = '/') {
|
|
let first = this.endpoint
|
|
if ( !first.startsWith('/') ) first = `/${first}`
|
|
if ( first.endsWith('/') ) first = first.slice(0, -1)
|
|
|
|
if ( !sub.startsWith('/') ) sub = `/${sub}`
|
|
return `${first}${sub}`
|
|
}
|
|
}
|