You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
5.6 KiB
JavaScript

4 years ago
class StructuredNode {
constructor(json) {
if (this.constructor === StructuredNode) {
throw new TypeError('Cannot instantiate object of abstract type StructuredNode!')
}
this.json = json
this.changes = {}
this.definition = StructuredNode.classes[this.constructor.name]
this.hooks = []
this.deleted = false
this.__name__ = this.constructor.name.toLowerCase()
4 years ago
}
check(name, type, new_value) {
return true
4 years ago
}
_link_property(name, elm) {
const relation = this.definition.rels[name]
if (!relation) {
throw new ApiError(`Cannot find realation ${name} of object ${this.constructor.name}!`, 404, {obj: this, elm: elm})
}
const type = StructuredNode.classes[relation.target].klass
4 years ago
if (typeof elm == "string") {
elm = type.by_id(elm)
}
if (elm instanceof type) {
if (!elm.exists() || !this.exists()) {
throw new ApiError("Cannot connect object before it exists on the backend!")
}
// make results available immediately
if (relation.cardinality.endsWith('OrMore')) {
if (!this.json[name].contains(elm)) {
this.json[name].push(elm)
}
} else {
this.json[name] = elm
}
return Api.post(`/${this.__name__}/${this.json.uid}/${name}/${elm.json.uid}`).then(json => {
4 years ago
this.json = json
this.update('link')
return this
})
}
}
_unlink_property(name, type, elm) {
return Api.delete(`/${this.__name__}/${this.json.uid}/${name}/${elm.json.uid}`).then(json => {
this.json = json
this.update('link')
return this
})
4 years ago
}
static by_id(uid) {
return Api.get('/' + this.name.toLowerCase() + '/' + uid).then(x => new this(x))
4 years ago
}
static find(attributes, settings) {
}
save() {
if (this.deleted) {
throw new ApiError("Cannot save deleted object!", {obj: this})
}
if (this.json.uid) {
return Api.put(`/${this.__name__}/${this.json.uid}`, this.changes).then(json => {
4 years ago
this.json = json
this.changes = {}
this.update('save')
return this
})
} else {
return Api.post('/' + this.__name__, this.json).then(json => {
4 years ago
this.json = json
this.changes = {}
this.update('created')
return this
})
}
}
delete() {
if (!this.exists()) {
throw new ApiError("Cannot delete object that does not exist yet!", {obj: this})
}
this.deleted = true
Api.delete(`/${this.__name__}/${this.json.uid}`).then(r => {
4 years ago
this.update('deleted')
return this
})
}
exists() {
return !this.deleted && !!this.json.uid
4 years ago
}
onupdate(callback) {
this.hooks.push(callback)
}
update(event) {
this.hooks.forEach(cb => {
try {
cb(this, event)
} catch (e) {
console.error(`Error while running update on ${this.constructor.name} update function ${cb}: ${e}`)
console.error(cb, event, e)
}
})
}
static registerClassDefinition(klass, props, rels) {
console.log(klass, props, rels)
StructuredNode.classes[klass.name] = {klass, props, rels}
4 years ago
}
}
StructuredNode.classes = {}
class Api {
static set_auth(token) {
Api.token = token;
}
static get(url) {
return fetch('/api' + url, {headers: Api.headers()}).then(r => {
4 years ago
if (r.ok) {
return r.json()
4 years ago
} else {
// evaluate response and raise error
return ApiError.fromResponse(r)
}
})
}
static post(url, body="") {
return fetch('/api' + url, {headers: Api.headers(), body: JSON.stringify(body), method: 'POST'}).then(r => {
4 years ago
if (r.ok) {
return r.json()
4 years ago
} else {
// evaluate response and raise error
return ApiError.fromResponse(r)
}
})
}
static put(url, body) {
return fetch('/api' + url, {headers: Api.headers(), body: JSON.stringify(body), method: 'PUT'}).then(r => {
4 years ago
if (r.ok) {
return r.json()
4 years ago
} else {
// evaluate response and raise error
return ApiError.fromResponse(r)
}
})
}
static delete(url) {
return fetch('/api' + url, {headers: Api.headers(), method: 'DELETE'}).then(r => {
4 years ago
if (r.ok) {
return r
} else {
// evaluate response and raise error
return ApiError.fromResponse(r)
}
})
}
static headers() {
return {
'Content-Type': 'application/json',
// 'Authorization': 'Bearer ' + Api.token
4 years ago
}
}
}
class ApiError {
constructor(msg, code, data) {
this.msg = msg
this.code = code
this.data = data
}
static fromResponse(r) {
if (r.headers.get('Content-Type') === 'application/json') {
return r.json().then(json => {
throw new ApiError(json.msg, r.status, json.data)
})
} else {
return r.text().then(text => {
throw new ApiError(text, r.status)
})
}
}
}