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() } check(name, new_value) { return true } _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 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 => { 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 }) } _set(name, new_val) { if (this.check(name, new_val)) { const def = this.definition.props[name] if (!def) { throw new Error("No attribute " + name + " found!") } } } 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 => { this.json = json this.changes = {} this.update('save') return this }) } else { return Api.post('/' + this.__name__, this.json).then(json => { 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 => { this.update('deleted') return this }) } refresh() { return Api.get(`/${this.__name__}/${this.json.uid}`).then(json => { this.json = json this.update('refresh') return this }) } exists() { return !this.deleted && !!this.json.uid } 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} } static by_id(uid) { return Api.get('/' + this.name.toLowerCase() + '/' + uid) .then(x => new this(x)) } static filter(attributes = {}, settings = {}) { Object.keys(settings).forEach(key => { attributes['$' + key] = settings[key] }) return Api.get(`/${this.name.toLowerCase()}?` + Api.queryParams(attributes)) .then(json => json.map(x => new this(x))) } static find(attributes) { return this.filter(attributes, {$limit: 1}) .then(r => r.length === 0 ? null : r[0]) } } StructuredNode.classes = {} class Api { static set_auth(token) { Api.token = token; } static get(url) { return fetch('/api' + url, {headers: Api.headers()}).then(r => { if (r.ok) { return r.json() } 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 => { if (r.ok) { return r.json() } 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 => { if (r.ok) { return r.json() } else { // evaluate response and raise error return ApiError.fromResponse(r) } }) } static delete(url) { return fetch('/api' + url, {headers: Api.headers(), method: 'DELETE'}).then(r => { 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 } } static queryParams(...dicts) { return dicts.map(dict => Object.keys(dict).map(key => encodeURIComponent(key)+'='+encodeURIComponent(dict[key])).join('&') ).join('&') } } 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) }) } } } class PropertyConversion { static serialize(type, value) { if (PropertyConversion.to[type]) { return PropertyConversion.to[type](value) } return value } static deserialize(type, value) { if (PropertyConversion.to[type]) { return PropertyConversion.from[type](value) } return value } } PropertyConversion.from = { DateProperty: (val) => new Date(val), } PropertyConversion.to = { DateProperty: (val) => ('' + val).slice(0,-11), }