feat(api): real login + listening wired to the backend contract
Replace the faked ConnectPage login with a real /auth/login -> /auth/me
flow, including loading/error states. Add a backend-contract adapter layer
(api/mappers.ts) translating the backend's snake_case, lean *Out schemas and
{items,total,limit,offset} paging into the UI's camelCase domain types, so
swapping backends only touches the mappers.
- auth: chained login (tokens) + /auth/me (user); refresh on snake_case;
expiresIn optional (reauth is 401-driven, backend sends no TTL)
- streaming: GET /stream/{id}?token= (token query param for <audio>); SW
audio cache route + tests follow the path change (token stays cache-stable)
- library/playlists/likes/admin: correct paths (/tracks not /library/tracks),
page/pageSize<->limit/offset, duration_seconds->durationMs, likes as
append-only POST /likes event-log, admin is_superuser<->role
- downloads/storage: marked provisional (backend routes still stubs)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,36 +1,61 @@
|
||||
import { api } from '../index';
|
||||
import {
|
||||
toPage,
|
||||
toPlaylist,
|
||||
toTrack,
|
||||
type RawPaged,
|
||||
type RawPlaylist,
|
||||
type RawTrack,
|
||||
} from '../mappers';
|
||||
import type { Playlist, PlaylistTrack, PaginatedResponse } from '../types';
|
||||
|
||||
export const playlistsApi = api.injectEndpoints({
|
||||
endpoints: (build) => ({
|
||||
getPlaylists: build.query<PaginatedResponse<Playlist>, void>({
|
||||
query: () => '/playlists',
|
||||
transformResponse: (raw: RawPaged<RawPlaylist>) =>
|
||||
toPage(raw, toPlaylist),
|
||||
providesTags: ['Playlist'],
|
||||
}),
|
||||
getPlaylist: build.query<Playlist, string>({
|
||||
query: (id) => `/playlists/${id}`,
|
||||
transformResponse: (raw: RawPlaylist) => toPlaylist(raw),
|
||||
providesTags: (_r, _e, id) => [{ type: 'Playlist', id }],
|
||||
}),
|
||||
getPlaylistTracks: build.query<PlaylistTrack[], string>({
|
||||
query: (id) => `/playlists/${id}/tracks`,
|
||||
// The backend returns plain tracks in playlist order; position/addedAt
|
||||
// aren't on TrackOut, so derive position from order and default addedAt.
|
||||
transformResponse: (raw: RawPaged<RawTrack>) =>
|
||||
raw.items.map((r, i) => ({
|
||||
...toTrack(r),
|
||||
position: i,
|
||||
addedAt: r.created_at,
|
||||
})),
|
||||
providesTags: (_r, _e, id) => [{ type: 'Playlist', id }, 'Track'],
|
||||
}),
|
||||
createPlaylist: build.mutation<
|
||||
Playlist,
|
||||
{ name: string; description?: string; isPublic?: boolean }
|
||||
{ name: string; description?: string }
|
||||
>({
|
||||
query: (body) => ({ url: '/playlists', method: 'POST', body }),
|
||||
query: ({ name, description }) => ({
|
||||
url: '/playlists',
|
||||
method: 'POST',
|
||||
body: { name, description },
|
||||
}),
|
||||
transformResponse: (raw: RawPlaylist) => toPlaylist(raw),
|
||||
invalidatesTags: ['Playlist'],
|
||||
}),
|
||||
updatePlaylist: build.mutation<
|
||||
Playlist,
|
||||
{ id: string; name?: string; description?: string; isPublic?: boolean }
|
||||
{ id: string; name?: string; description?: string }
|
||||
>({
|
||||
query: ({ id, ...body }) => ({
|
||||
query: ({ id, name, description }) => ({
|
||||
url: `/playlists/${id}`,
|
||||
method: 'PATCH',
|
||||
body,
|
||||
body: { name, description },
|
||||
}),
|
||||
transformResponse: (raw: RawPlaylist) => toPlaylist(raw),
|
||||
invalidatesTags: (_r, _e, { id }) => [{ type: 'Playlist', id }],
|
||||
}),
|
||||
deletePlaylist: build.mutation<void, string>({
|
||||
@@ -39,12 +64,12 @@ export const playlistsApi = api.injectEndpoints({
|
||||
}),
|
||||
addTrackToPlaylist: build.mutation<
|
||||
void,
|
||||
{ playlistId: string; trackId: string }
|
||||
{ playlistId: string; trackId: string; position?: number }
|
||||
>({
|
||||
query: ({ playlistId, trackId }) => ({
|
||||
query: ({ playlistId, trackId, position }) => ({
|
||||
url: `/playlists/${playlistId}/tracks`,
|
||||
method: 'POST',
|
||||
body: { trackId },
|
||||
body: { track_id: trackId, position },
|
||||
}),
|
||||
invalidatesTags: (_r, _e, { playlistId }) => [
|
||||
{ type: 'Playlist', id: playlistId },
|
||||
@@ -52,10 +77,10 @@ export const playlistsApi = api.injectEndpoints({
|
||||
}),
|
||||
removeTrackFromPlaylist: build.mutation<
|
||||
void,
|
||||
{ playlistId: string; trackId: string; position: number }
|
||||
{ playlistId: string; trackId: string }
|
||||
>({
|
||||
query: ({ playlistId, position }) => ({
|
||||
url: `/playlists/${playlistId}/tracks/${position}`,
|
||||
query: ({ playlistId, trackId }) => ({
|
||||
url: `/playlists/${playlistId}/tracks/${trackId}`,
|
||||
method: 'DELETE',
|
||||
}),
|
||||
invalidatesTags: (_r, _e, { playlistId }) => [
|
||||
|
||||
Reference in New Issue
Block a user