ui: align js with api
This commit is contained in:
+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 = '/'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user