This commit is contained in:
Magnus Åhall 2024-11-28 18:11:14 +01:00
parent 515c748e14
commit bd4a475923
23 changed files with 1217 additions and 192 deletions

67
static/js/api.mjs Normal file
View file

@ -0,0 +1,67 @@
export class API {
// query resolves into the JSON data produced by the application, or an exception with 'type' and 'error' properties.
static async query(method, path, request) {
return new Promise((resolve, reject) => {
const body = JSON.stringify(request)
const headers = {}
// Authentication is done with a bearer token.
// Here provided to the backend if set.
const token = localStorage.getItem('token')
if (token) {
headers.Authorization = `Bearer ${token}`
}
fetch(path, { method, headers, body })
.then(response => {
// An HTTP communication level error occured.
if (!response.ok || response.status != 200)
return reject({
type: 'http',
error: response,
})
return response.json()
})
.then(json => {
// Application level response are handled here.
if (!json.OK)
return reject({
type: 'application',
error: json.Error,
application: json,
})
resolve(json)
})
.catch(err =>
// Catch any other errors from fetch.
reject({
type: 'http',
error: err,
}))
})
}
static hasAuthenticationToken() {//{{{
const token = localStorage.getItem('token')
return token !== null && token !== ''
}//}}}
static authenticate(username, password) {//{{{
return new Promise((resolve, reject) => {
const req = { username, password }
API.query('POST', '/user/authenticate', req)
.then(response => {
localStorage.setItem('token', response.Token)
localStorage.setItem('user', JSON.stringify(response.User))
resolve(response.User)
})
.catch(e => {
console.log(e.type, e.error)
reject(e.error)
})
})
}//}}}
static logout() {//{{{
localStorage.removeItem('token')
location.href = '/'
}//}}}
}

View file

@ -1 +1,23 @@
console.log('app.mjs')
import { h, Component, createRef } from 'preact'
import htm from 'htm'
import { API } from 'api'
const html = htm.bind(h)
export class Notes2 {
constructor() {//{{{
}//}}}
render() {//{{{
return html`
<button onclick=${()=>API.logout()}>Log out</button>
`
}//}}}
treeGet() {
const req = {}
API.query('POST', '/tree/get', req)
.then(response => {
console.log(response)
})
.catch(e => console.log(e.type, e.error))
}
}