mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-07-07 09:51:37 +00:00
Commit service fil for recommandation fetaure
This commit is contained in:
parent
a0e7218e5e
commit
b8b7a272f9
1 changed files with 50 additions and 0 deletions
50
client/services/recommendationsApi.js
Normal file
50
client/services/recommendationsApi.js
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
// Minimal client for /api/recommendations
|
||||
const routerBase = (typeof window !== 'undefined' && window.__NUXT__?.config?.routerBasePath) || '/audiobookshelf'
|
||||
|
||||
const base = `${routerBase}/api/recommendations`
|
||||
|
||||
function qs(p = {}) {
|
||||
const q = new URLSearchParams(p)
|
||||
const s = q.toString()
|
||||
return s ? `?${s}` : ''
|
||||
}
|
||||
async function http(path, opts = {}) {
|
||||
let token = ''
|
||||
try {
|
||||
token = window?.$nuxt?.$store?.getters?.['user/getToken'] || ''
|
||||
} catch (_) {}
|
||||
if (!token) {
|
||||
try {
|
||||
token = localStorage.getItem('accessToken') || ''
|
||||
} catch (_) {}
|
||||
const res = await fetch(path, {
|
||||
credentials: 'include',
|
||||
method: opts.method || 'GET',
|
||||
headers: { 'Content-Type': 'application/json', ...(token ? { Authorization: `Bearer ${token}` } : {}) },
|
||||
body: opts.body ? JSON.stringify(opts.body) : undefined
|
||||
})
|
||||
if (res.status === 401) {
|
||||
const back = encodeURIComponent(location.pathname + location.search)
|
||||
location.assign(`${routerBase}/login?redirect=${back}`)
|
||||
throw new Error('Unauthorized')
|
||||
}
|
||||
if (!res.ok) {
|
||||
let msg = 'Request failed'
|
||||
try {
|
||||
const j = await res.json()
|
||||
msg = j.message || msg
|
||||
} catch (_) {}
|
||||
const e = new Error(msg)
|
||||
e.status = res.status
|
||||
throw e
|
||||
}
|
||||
return res.status === 204 ? null : res.json()
|
||||
}
|
||||
}
|
||||
export const getTags = () => http(`${base}/tags`)
|
||||
export const createRecommendation = (payload) => http(base, { method: 'POST', body: payload })
|
||||
export const getInbox = (p = {}) => http(`${base}/inbox${qs(p)}`)
|
||||
export const getSent = (p = {}) => http(`${base}/sent${qs(p)}`)
|
||||
export const getBookRecs = (bookId, p = {}) => http(`${base}/book/${encodeURIComponent(bookId)}${qs(p)}`)
|
||||
export const deleteRec = (id) => http(`${base}/${id}`, { method: 'DELETE' })
|
||||
export const updateRec = (id, body) => http(`${base}/${id}`, { method: 'PUT', body })
|
||||
Loading…
Add table
Add a link
Reference in a new issue