refactor(ui): point lint at useDirectDataProvider and simplify uploadImage args (#6482)

This commit is contained in:
Deluan Quintão 2026-11-16 00:25:15 -05:00 committed by GitHub
parent 1f0238ce4b
commit 2ae6defbc4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 8 additions and 63 deletions

View File

@ -53,11 +53,11 @@
{
"group": ["**/dataProvider"],
"importNames": ["httpClient"],
"message": "Do not call httpClient directly. Add a method to wrapperDataProvider and call it through useDataProvider(), or add the Subsonic call to src/subsonic."
"message": "Do not call httpClient directly. Add a method to wrapperDataProvider and call it through useDirectDataProvider(), or add the Subsonic call to src/subsonic."
},
{
"group": ["**/dataProvider/httpClient"],
"message": "Do not call httpClient directly. Add a method to wrapperDataProvider and call it through useDataProvider(), or add the Subsonic call to src/subsonic."
"message": "Do not call httpClient directly. Add a method to wrapperDataProvider and call it through useDirectDataProvider(), or add the Subsonic call to src/subsonic."
}
]
}

View File

@ -64,7 +64,7 @@ export const ImageUploadOverlay = ({
if (!file || !entityId) return
try {
await dataProvider.uploadImage(entityType, { id: entityId, file })
await dataProvider.uploadImage(entityType, entityId, file)
notify(`message.coverUploaded`, 'success')
if (onImageChange) onImageChange()
refresh()

View File

@ -46,8 +46,8 @@ describe('useDirectDataProvider', () => {
const provider = { uploadImage: vi.fn().mockRejectedValue(error) }
const { result } = renderWithOptimistic(provider, false)
await expect(
result.current.uploadImage('playlist', { id: 'pl-1' }),
).rejects.toBe(error)
await expect(result.current.uploadImage('playlist', 'pl-1')).rejects.toBe(
error,
)
})
})

View File

@ -239,8 +239,7 @@ const wrapperDataProvider = {
httpClient(`${REST_URL}/metadata/${REFRESH_KIND[resource]}/${id}/refresh`, {
method: 'POST',
}).then(() => ({ data: { id } })),
// useDataProvider forwards only two arguments when the first is a string, so id and file share one.
uploadImage: (resource, { id, file }) => {
uploadImage: (resource, id, file) => {
const formData = new FormData()
formData.append('image', file)
return fetchData(`${REST_URL}/${resource}/${id}/image`, {

View File

@ -140,23 +140,13 @@ describe('wrapperDataProvider', () => {
it('posts the file as multipart form data under the image field', async () => {
const file = new File(['img'], 'cover.png', { type: 'image/png' })
await wrapperDataProvider.uploadImage('artist', { id: 'ar-1', file })
await wrapperDataProvider.uploadImage('artist', 'ar-1', file)
const [url, options] = mockHttpClient.mock.calls[0]
expect(url).toBe('/api/artist/ar-1/image')
expect(options.method).toBe('POST')
expect(options.body).toBeInstanceOf(FormData)
expect(options.body.get('image')).toBe(file)
// An empty Headers keeps httpClient from forcing a JSON content type over the multipart boundary
expect([...options.headers.keys()]).toEqual([])
})
it('deletes the uploaded image', async () => {
await wrapperDataProvider.deleteImage('playlist', 'pl-1')
expect(mockHttpClient).toHaveBeenCalledWith('/api/playlist/pl-1/image', {
method: 'DELETE',
})
})
})
@ -184,32 +174,6 @@ describe('wrapperDataProvider', () => {
})
describe('scrobbler links', () => {
it('reads the Last.fm link status', async () => {
mockHttpClient.mockResolvedValue({ json: { status: true, apiKey: 'k' } })
const result = await wrapperDataProvider.getLastfmLink()
expect(mockHttpClient).toHaveBeenCalledWith('/api/lastfm/link')
expect(result).toEqual({ data: { status: true, apiKey: 'k' } })
})
it('unlinks Last.fm', async () => {
await wrapperDataProvider.unlinkLastfm()
expect(mockHttpClient).toHaveBeenCalledWith('/api/lastfm/link', {
method: 'DELETE',
})
})
it('reads the ListenBrainz link status', async () => {
mockHttpClient.mockResolvedValue({ json: { status: false } })
const result = await wrapperDataProvider.getListenBrainzLink()
expect(mockHttpClient).toHaveBeenCalledWith('/api/listenbrainz/link')
expect(result).toEqual({ data: { status: false } })
})
it('links ListenBrainz with the given token', async () => {
mockHttpClient.mockResolvedValue({ json: { user: 'sam' } })
@ -221,23 +185,5 @@ describe('wrapperDataProvider', () => {
})
expect(result).toEqual({ data: { user: 'sam' } })
})
it('unlinks ListenBrainz', async () => {
await wrapperDataProvider.unlinkListenBrainz()
expect(mockHttpClient).toHaveBeenCalledWith('/api/listenbrainz/link', {
method: 'DELETE',
})
})
})
describe('rescanPlugins', () => {
it('posts to the plugin rescan endpoint', async () => {
await wrapperDataProvider.rescanPlugins()
expect(mockHttpClient).toHaveBeenCalledWith('/api/plugin/rescan', {
method: 'POST',
})
})
})
})