navidrome/ui/src/dataProvider/wrapperDataProvider.js
Deluan Quintão fcdd30ba8f
build(ui): migrate from CRA/Jest to Vite/Vitest (#3311)
* feat: create vite project

* feat: it's alive!

* feat: `make dev` working!

* feat: replace custom serviceWorker with vite plugin

* test: replace Jest with Vitest

* fix: run prettier

* fix: skip eslint for now.

* chore: remove ui.old folder

* refactor: replace lodash.pick with simple destructuring

* fix: eslint errors (wip)

* fix: eslint errors (wip)

* fix: display-name eslint errors (wip)

* fix: no-console eslint errors (wip)

* fix: react-refresh/only-export-components eslint errors (wip)

* fix: react-refresh/only-export-components eslint errors (wip)

* fix: react-refresh/only-export-components eslint errors (wip)

* fix: react-refresh/only-export-components eslint errors (wip)

* fix: build

* fix: pwa manifest

* refactor: pwa manifest

* refactor: simplify PORT configuration

* refactor: rename simple JS files

* test: cover playlistUtils

* fix: react-image-lightbox

* feat(ui): add sourcemaps to help debug issues
2024-09-28 11:54:36 -04:00

80 lines
2.3 KiB
JavaScript

import jsonServerProvider from 'ra-data-json-server'
import httpClient from './httpClient'
import { REST_URL } from '../consts'
const dataProvider = jsonServerProvider(REST_URL, httpClient)
const mapResource = (resource, params) => {
switch (resource) {
case 'playlistTrack': {
// /api/playlistTrack?playlist_id=123 => /api/playlist/123/tracks
let plsId = '0'
if (params.filter) {
plsId = params.filter.playlist_id
}
return [`playlist/${plsId}/tracks`, params]
}
default:
return [resource, params]
}
}
const callDeleteMany = (resource, params) => {
const ids = params.ids.map((id) => `id=${id}`)
const idsParam = ids.join('&')
return httpClient(`${REST_URL}/${resource}?${idsParam}`, {
method: 'DELETE',
}).then((response) => ({ data: response.json.ids || [] }))
}
const wrapperDataProvider = {
...dataProvider,
getList: (resource, params) => {
const [r, p] = mapResource(resource, params)
return dataProvider.getList(r, p)
},
getOne: (resource, params) => {
const [r, p] = mapResource(resource, params)
return dataProvider.getOne(r, p)
},
getMany: (resource, params) => {
const [r, p] = mapResource(resource, params)
return dataProvider.getMany(r, p)
},
getManyReference: (resource, params) => {
const [r, p] = mapResource(resource, params)
return dataProvider.getManyReference(r, p)
},
update: (resource, params) => {
const [r, p] = mapResource(resource, params)
return dataProvider.update(r, p)
},
updateMany: (resource, params) => {
const [r, p] = mapResource(resource, params)
return dataProvider.updateMany(r, p)
},
create: (resource, params) => {
const [r, p] = mapResource(resource, params)
return dataProvider.create(r, p)
},
delete: (resource, params) => {
const [r, p] = mapResource(resource, params)
return dataProvider.delete(r, p)
},
deleteMany: (resource, params) => {
const [r, p] = mapResource(resource, params)
if (r.endsWith('/tracks')) {
return callDeleteMany(r, p)
}
return dataProvider.deleteMany(r, p)
},
addToPlaylist: (playlistId, data) => {
return httpClient(`${REST_URL}/playlist/${playlistId}/tracks`, {
method: 'POST',
body: JSON.stringify(data),
}).then(({ json }) => ({ data: json }))
},
}
export default wrapperDataProvider