ui: align js with api
This commit is contained in:
+5
-7
@@ -36,6 +36,7 @@
|
||||
</div>
|
||||
<script type="module">
|
||||
import Alpine from 'alpinejs'
|
||||
import { requireAuth } from '/src/auth.js'
|
||||
import { logout, getchannels, getmessages } from '/src/api.js'
|
||||
|
||||
window.app = () => ({
|
||||
@@ -45,12 +46,9 @@
|
||||
selectedChannel: null,
|
||||
messages: [],
|
||||
async init() {
|
||||
const res = await fetch('/api/whoami')
|
||||
if (res.status === 401) {
|
||||
window.location.href = '/login.html'
|
||||
return
|
||||
}
|
||||
this.username = await res.text()
|
||||
const username = await requireAuth()
|
||||
if (!username) return
|
||||
this.username = username
|
||||
try {
|
||||
this.channels = await getchannels()
|
||||
} catch (e) {
|
||||
@@ -62,7 +60,7 @@
|
||||
this.messages = []
|
||||
this.error = ''
|
||||
try {
|
||||
const from = new Date(Date.now() - 24 * 60 * 60 * 1000)
|
||||
const from = new Date(Date.now() - 96 * 60 * 60 * 1000)
|
||||
const to = new Date(Date.now())
|
||||
this.messages = await getmessages(ch.ID, { from, to })
|
||||
} catch (e) {
|
||||
|
||||
+62
-6
@@ -1,28 +1,84 @@
|
||||
const BASE = __API_URL__
|
||||
const opts = { credentials: 'include' }
|
||||
|
||||
export async function login(username, password) {
|
||||
const form = new FormData()
|
||||
form.append('username', username)
|
||||
form.append('password', password)
|
||||
const res = await fetch('/api/login', { method: 'POST', body: form })
|
||||
const res = await fetch(`${BASE}/login`, { ...opts, method: 'POST', body: form })
|
||||
if (!res.ok) throw new Error(await res.text())
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
const res = await fetch('/api/logout', { method: 'POST' })
|
||||
const res = await fetch(`${BASE}/logout`, { ...opts, method: 'POST' })
|
||||
if (!res.ok) throw new Error(await res.text())
|
||||
}
|
||||
|
||||
export async function getchannels() {
|
||||
const res = await fetch('/api/channels')
|
||||
export async function register(name, password) {
|
||||
const form = new FormData()
|
||||
form.append('name', name)
|
||||
form.append('password', password)
|
||||
const res = await fetch(`${BASE}/register`, { ...opts, method: 'POST', body: form })
|
||||
if (!res.ok) throw new Error(await res.text())
|
||||
return res.json()
|
||||
}
|
||||
|
||||
export async function getchannels() {
|
||||
const res = await fetch(`${BASE}/channels`, opts)
|
||||
if (!res.ok) throw new Error(await res.text())
|
||||
return res.json()
|
||||
}
|
||||
|
||||
export async function getchannel(channelid) {
|
||||
const res = await fetch(`${BASE}/channels/${channelid}`, opts)
|
||||
if (!res.ok) throw new Error(await res.text())
|
||||
return res.json()
|
||||
}
|
||||
|
||||
export async function createchannel({ name, type = '', location = '', notes = '' }) {
|
||||
const form = new FormData()
|
||||
form.append('name', name)
|
||||
form.append('type', type)
|
||||
form.append('location', location)
|
||||
form.append('notes', notes)
|
||||
const res = await fetch(`${BASE}/channels`, { ...opts, method: 'POST', body: form })
|
||||
if (!res.ok) throw new Error(await res.text())
|
||||
return res.json()
|
||||
}
|
||||
|
||||
export async function deletechannel(channelid) {
|
||||
const res = await fetch(`${BASE}/channels/${channelid}`, { ...opts, method: 'DELETE' })
|
||||
if (!res.ok) throw new Error(await res.text())
|
||||
}
|
||||
|
||||
export async function getmessages(channelid, { from, to } = {}) {
|
||||
const params = new URLSearchParams()
|
||||
if (from) params.set('from', from.toISOString())
|
||||
if (to) params.set('to', to.toISOString())
|
||||
const query = params.size ? `?${params}` : ''
|
||||
const res = await fetch(`/api/channels/${channelid}/messages${query}`)
|
||||
const res = await fetch(`${BASE}/channels/${channelid}/messages${query}`, opts)
|
||||
if (!res.ok) throw new Error(await res.text())
|
||||
return res.json()
|
||||
}
|
||||
}
|
||||
|
||||
export async function getmessage(channelid, messageid) {
|
||||
const res = await fetch(`${BASE}/channels/${channelid}/messages/${messageid}`, opts)
|
||||
if (!res.ok) throw new Error(await res.text())
|
||||
return res.json()
|
||||
}
|
||||
|
||||
export function getfileurl(fileid) {
|
||||
return `${BASE}/files/${fileid}`
|
||||
}
|
||||
|
||||
export async function getusers() {
|
||||
const res = await fetch(`${BASE}/users`, opts)
|
||||
if (!res.ok) throw new Error(await res.text())
|
||||
return res.json()
|
||||
}
|
||||
|
||||
export async function getuser(userid) {
|
||||
const res = await fetch(`${BASE}/users/${userid}`, opts)
|
||||
if (!res.ok) throw new Error(await res.text())
|
||||
return res.json()
|
||||
}
|
||||
|
||||
+6
-2
@@ -1,12 +1,16 @@
|
||||
const BASE = __API_URL__
|
||||
|
||||
export async function requireAuth() {
|
||||
const res = await fetch('/api/whoami')
|
||||
const res = await fetch(`${BASE}/whoami`, { credentials: 'include' })
|
||||
if (res.status === 401) {
|
||||
window.location.href = '/login.html'
|
||||
return null
|
||||
}
|
||||
return res.text()
|
||||
}
|
||||
|
||||
export async function redirectIfAuthed() {
|
||||
const res = await fetch('/api/whoami')
|
||||
const res = await fetch(`${BASE}/whoami`, { credentials: 'include' })
|
||||
if (res.ok) {
|
||||
window.location.href = '/'
|
||||
}
|
||||
|
||||
+4
-7
@@ -1,17 +1,14 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import { resolve } from 'path'
|
||||
|
||||
const API_URL = process.env.API_URL ?? 'http://192.168.56.1:3000'
|
||||
const API_URL = process.env.API_URL ?? 'http://localhost:3000'
|
||||
|
||||
export default defineConfig({
|
||||
define: {
|
||||
__API_URL__: JSON.stringify(API_URL),
|
||||
},
|
||||
server: {
|
||||
port: 5173,
|
||||
proxy: {
|
||||
'/api': {
|
||||
target: API_URL,
|
||||
rewrite: (path) => path.replace(/^\/api/, ''),
|
||||
},
|
||||
},
|
||||
},
|
||||
build: {
|
||||
outDir: 'dist',
|
||||
|
||||
Reference in New Issue
Block a user