Holds everything web, mobile and API must agree on: DTOs, zod input schemas, plan entitlements (the single place feature gates live), streaming-service link helpers, feed ordering and a typed API client. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MzFywujuH9TiuXbr9vjoX8
87 lines
3.8 KiB
TypeScript
87 lines
3.8 KiB
TypeScript
import type {
|
|
ConnectionDTO,
|
|
FeedItemDTO,
|
|
MeDTO,
|
|
ProfileDTO,
|
|
TrackSearchResultDTO,
|
|
UserSummaryDTO,
|
|
} from './dto';
|
|
import type { LoginInput, RegisterInput, SetFeaturedInput, UpdateMeInput } from './schemas';
|
|
|
|
export class ApiError extends Error {
|
|
constructor(
|
|
public status: number,
|
|
message: string,
|
|
) {
|
|
super(message);
|
|
}
|
|
}
|
|
|
|
export interface ApiClientOptions {
|
|
baseUrl: string;
|
|
/** Bearer token (mobile). Web relies on the session cookie instead. */
|
|
getToken?: () => string | null | undefined;
|
|
/** 'include' for browsers talking to the API cross-origin. */
|
|
credentials?: 'include' | 'same-origin' | 'omit';
|
|
}
|
|
|
|
export type AuthResponse = { user: MeDTO; token: string };
|
|
|
|
/** Typed client for the musicshare API, shared by web and mobile. */
|
|
export function createApiClient({ baseUrl, getToken, credentials }: ApiClientOptions) {
|
|
async function request<T>(path: string, init: { method?: string; body?: unknown } = {}): Promise<T> {
|
|
const headers: Record<string, string> = {};
|
|
const token = getToken?.();
|
|
if (token) headers.Authorization = `Bearer ${token}`;
|
|
if (init.body !== undefined) headers['Content-Type'] = 'application/json';
|
|
const res = await fetch(baseUrl + path, {
|
|
method: init.method ?? (init.body !== undefined ? 'POST' : 'GET'),
|
|
headers,
|
|
credentials,
|
|
body: init.body !== undefined ? JSON.stringify(init.body) : undefined,
|
|
});
|
|
const data = await res.json().catch(() => ({}));
|
|
if (!res.ok) throw new ApiError(res.status, (data as { error?: string }).error ?? `Request failed (${res.status})`);
|
|
return data as T;
|
|
}
|
|
|
|
const q = encodeURIComponent;
|
|
|
|
return {
|
|
request,
|
|
/** For SWR-style fetchers keyed by path. */
|
|
get: <T>(path: string) => request<T>(path),
|
|
|
|
register: (input: RegisterInput) => request<AuthResponse>('/auth/register', { body: input }),
|
|
login: (input: LoginInput) => request<AuthResponse>('/auth/login', { body: input }),
|
|
logout: () => request('/auth/logout', { method: 'POST', body: {} }),
|
|
|
|
me: () => request<MeDTO>('/me'),
|
|
updateMe: (input: UpdateMeInput) => request<MeDTO>('/me', { method: 'PATCH', body: input }),
|
|
deleteAccount: () => request('/me', { method: 'DELETE' }),
|
|
setFeatured: (input: SetFeaturedInput) => request('/me/featured', { method: 'PUT', body: input }),
|
|
clearFeatured: () => request('/me/featured', { method: 'DELETE' }),
|
|
|
|
feed: () => request<FeedItemDTO[]>('/feed'),
|
|
profile: (username: string) => request<ProfileDTO>(`/users/${q(username)}`),
|
|
searchUsers: (query: string) => request<UserSummaryDTO[]>(`/users/search?q=${q(query)}`),
|
|
follow: (username: string) => request(`/users/${q(username)}/follow`, { method: 'POST', body: {} }),
|
|
unfollow: (username: string) => request(`/users/${q(username)}/follow`, { method: 'DELETE' }),
|
|
pin: (username: string) => request(`/users/${q(username)}/pin`, { method: 'PUT', body: {} }),
|
|
unpin: (username: string) => request(`/users/${q(username)}/pin`, { method: 'DELETE' }),
|
|
|
|
searchTracks: (query: string) => request<TrackSearchResultDTO[]>(`/tracks/search?q=${q(query)}`),
|
|
|
|
connections: () => request<ConnectionDTO[]>('/connections'),
|
|
authorizeUrl: (provider: 'spotify' | 'plex', body: { client: 'web' | 'mobile'; returnTo?: string }) =>
|
|
request<{ url: string }>(`/connections/${provider}/authorize`, { body }),
|
|
connectUsername: (provider: 'lastfm' | 'listenbrainz', username: string) =>
|
|
request<ConnectionDTO>(`/connections/${provider}`, { body: { username } }),
|
|
connectJellyfin: (input: { serverUrl: string; username: string; password: string }) =>
|
|
request<ConnectionDTO>('/connections/jellyfin', { body: input }),
|
|
disconnect: (provider: string) => request(`/connections/${q(provider)}`, { method: 'DELETE' }),
|
|
};
|
|
}
|
|
|
|
export type ApiClient = ReturnType<typeof createApiClient>;
|