mirror of
https://github.com/gtsteffaniak/filebrowser.git
synced 2026-11-25 16:09:41 -05:00
Signed-off-by: Joel Godfrey <m.joelgodfrey@gmail.com> Co-authored-by: Kevin B. <kurami32_kbm@proton.me> Co-authored-by: zarev <60230221+zarevskaya@users.noreply.github.com> Co-authored-by: Joel Godfrey <82655889+BaccanoMob@users.noreply.github.com> Co-authored-by: Stephan Paternotte <Stephan-P@users.noreply.github.com>
91 lines
2.1 KiB
JavaScript
91 lines
2.1 KiB
JavaScript
import { fetchURL, fetchJSON } from '@/api/utils'
|
|
import { getApiPath, getPublicApiPath } from '@/utils/url.js'
|
|
import { notify } from '@/notify'
|
|
|
|
// GET /api/users (list all)
|
|
export async function getAllUsers() {
|
|
try {
|
|
const apiPath = getApiPath('users')
|
|
return await fetchJSON(apiPath)
|
|
} catch (err) {
|
|
notify.showError(err.message || 'Failed to fetch users')
|
|
throw err
|
|
}
|
|
}
|
|
|
|
// GET /api/users or /public/api/users (get single user)
|
|
export async function get(id) {
|
|
try {
|
|
let apiPath = getPublicApiPath('users', { id: id })
|
|
return await fetchJSON(apiPath)
|
|
} catch (err) {
|
|
notify.showError(err.message || `Failed to fetch user with ID: ${id}`)
|
|
throw err
|
|
}
|
|
}
|
|
|
|
// POST /api/users (create user)
|
|
export async function create(user) {
|
|
try {
|
|
const apiPath = getApiPath('users')
|
|
const res = await fetchURL(apiPath, {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
which: [],
|
|
data: user
|
|
})
|
|
})
|
|
|
|
if (res.status === 201) {
|
|
return res.headers.get('Location')
|
|
} else {
|
|
throw new Error('Failed to create user')
|
|
}
|
|
} catch (err) {
|
|
notify.showError(err.message || 'Error creating user')
|
|
throw err
|
|
}
|
|
}
|
|
|
|
// PUT /api/users (update user)
|
|
export async function update(user, which = ['all']) {
|
|
const excludeKeys = ['id', 'name']
|
|
which = which.filter(item => !excludeKeys.includes(item))
|
|
if (user.username === 'anonymous') {
|
|
return
|
|
}
|
|
|
|
let userData = user
|
|
if (which.length !== 1 || which[0] !== 'all') {
|
|
userData = {}
|
|
which.forEach(key => {
|
|
if (key in user) {
|
|
userData[key] = user[key]
|
|
}
|
|
})
|
|
}
|
|
|
|
const apiPath = getApiPath('users', { id: user.id })
|
|
await fetchURL(apiPath, {
|
|
method: 'PUT',
|
|
body: JSON.stringify({
|
|
which: which,
|
|
data: userData
|
|
})
|
|
})
|
|
}
|
|
|
|
// DELETE /api/users (remove user)
|
|
export async function remove(id) {
|
|
try {
|
|
const apiPath = getApiPath('users', { id: id })
|
|
await fetchURL(apiPath, {
|
|
method: 'DELETE'
|
|
})
|
|
} catch (err) {
|
|
notify.showError(err.message || `Failed to delete user with ID: ${id}`)
|
|
throw err
|
|
}
|
|
}
|
|
|