feat(api): add languages, music-video-credits-templates, stream-selectors, createChannel clients (#212)
New web/src/api/languages.ts module (getLanguages) plus channels.ts additions (getMusicVideoCreditsTemplates, getChannelStreamSelectors, createChannel) for the channel-editor gaps in #212. Each has URL-building tests.
This commit is contained in:
@@ -1,5 +1,13 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { getChannelById, updateChannel, type UpdateChannelRequest } from './channels';
|
||||
import {
|
||||
createChannel,
|
||||
getChannelById,
|
||||
getChannelStreamSelectors,
|
||||
getMusicVideoCreditsTemplates,
|
||||
updateChannel,
|
||||
type CreateChannelRequest,
|
||||
type UpdateChannelRequest
|
||||
} from './channels';
|
||||
|
||||
const sampleChannel = {
|
||||
id: 5,
|
||||
@@ -90,3 +98,69 @@ describe('updateChannel', () => {
|
||||
expect(JSON.parse(init?.body as string)).toMatchObject({ name: 'Renamed', number: '5' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('createChannel', () => {
|
||||
beforeEach(() => {
|
||||
window.localStorage.clear();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('POSTs the request body to /api/channels and returns the created view model', async () => {
|
||||
const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(
|
||||
new Response(JSON.stringify(sampleChannel), {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
status: 200
|
||||
})
|
||||
);
|
||||
|
||||
const body = { ...sampleChannel, id: undefined } as unknown as CreateChannelRequest;
|
||||
|
||||
await expect(createChannel(body)).resolves.toMatchObject({ id: 5 });
|
||||
|
||||
const [url, init] = fetchSpy.mock.calls[0];
|
||||
expect(url).toBe('/api/channels');
|
||||
expect((init?.method ?? '').toUpperCase()).toBe('POST');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getMusicVideoCreditsTemplates', () => {
|
||||
beforeEach(() => {
|
||||
window.localStorage.clear();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('GETs /api/channels/music-video-credits-templates', async () => {
|
||||
const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(
|
||||
new Response(JSON.stringify(['default']), {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
status: 200
|
||||
})
|
||||
);
|
||||
|
||||
await expect(getMusicVideoCreditsTemplates()).resolves.toEqual(['default']);
|
||||
|
||||
const [url] = fetchSpy.mock.calls[0];
|
||||
expect(url).toBe('/api/channels/music-video-credits-templates');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getChannelStreamSelectors', () => {
|
||||
beforeEach(() => {
|
||||
window.localStorage.clear();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('GETs /api/channels/stream-selectors', async () => {
|
||||
const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(
|
||||
new Response(JSON.stringify(['selector.py']), {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
status: 200
|
||||
})
|
||||
);
|
||||
|
||||
await expect(getChannelStreamSelectors()).resolves.toEqual(['selector.py']);
|
||||
|
||||
const [url] = fetchSpy.mock.calls[0];
|
||||
expect(url).toBe('/api/channels/stream-selectors');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,6 +11,7 @@ export type BulkMoveChannelsToGroupRequest = components['schemas']['BulkMoveChan
|
||||
export type BulkDeleteChannelsRequest = components['schemas']['BulkDeleteChannelsRequest'];
|
||||
export type CreateChannelFromLineupRequest = components['schemas']['CreateChannelFromLineupRequest'];
|
||||
export type CreateChannelFromLineupResponseModel = components['schemas']['CreateChannelFromLineupResponseModel'];
|
||||
export type CreateChannelRequest = components['schemas']['CreateChannelRequest'];
|
||||
|
||||
export interface ChannelsScreenData {
|
||||
channels: ChannelSummary[];
|
||||
@@ -46,6 +47,21 @@ export function updateChannel(channelId: number, body: UpdateChannelRequest): Pr
|
||||
});
|
||||
}
|
||||
|
||||
export function createChannel(body: CreateChannelRequest): Promise<Channel> {
|
||||
return request<Channel>('/api/channels', {
|
||||
body,
|
||||
method: 'POST'
|
||||
});
|
||||
}
|
||||
|
||||
export function getMusicVideoCreditsTemplates(): Promise<string[]> {
|
||||
return request<string[]>('/api/channels/music-video-credits-templates');
|
||||
}
|
||||
|
||||
export function getChannelStreamSelectors(): Promise<string[]> {
|
||||
return request<string[]>('/api/channels/stream-selectors');
|
||||
}
|
||||
|
||||
export async function getChannelsScreenData(): Promise<ChannelsScreenData> {
|
||||
const [channels, channelStates] = await Promise.all([
|
||||
getChannels(),
|
||||
|
||||
@@ -12,6 +12,7 @@ export * from './ffmpegProfiles';
|
||||
export * from './fillerPresets';
|
||||
export * from './guide';
|
||||
export * from './imageFolders';
|
||||
export * from './languages';
|
||||
export * from './libraries';
|
||||
export * from './libraryBrowse';
|
||||
export * from './logs';
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { getLanguages } from './languages';
|
||||
|
||||
function jsonResponse(body: unknown, status = 200): Response {
|
||||
return new Response(JSON.stringify(body), { headers: { 'Content-Type': 'application/json' }, status });
|
||||
}
|
||||
|
||||
describe('getLanguages', () => {
|
||||
beforeEach(() => {
|
||||
window.localStorage.clear();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('GETs /api/languages and returns the list', async () => {
|
||||
const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(
|
||||
jsonResponse([{ code: 'eng', englishName: 'English' }])
|
||||
);
|
||||
|
||||
await expect(getLanguages()).resolves.toEqual([{ code: 'eng', englishName: 'English' }]);
|
||||
|
||||
const [url, init] = fetchSpy.mock.calls[0];
|
||||
expect(url).toBe('/api/languages');
|
||||
expect((init?.method ?? 'GET').toUpperCase()).toBe('GET');
|
||||
});
|
||||
|
||||
it('rejects with the ApiError status on failure', async () => {
|
||||
vi.spyOn(window, 'fetch').mockResolvedValue(
|
||||
jsonResponse({ status: 500, title: 'Internal Server Error' }, 500)
|
||||
);
|
||||
|
||||
await expect(getLanguages()).rejects.toMatchObject({ status: 500 });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
import { ApiError, request } from './client';
|
||||
import type { components } from './generated/v1';
|
||||
|
||||
export type LanguageCode = components['schemas']['LanguageCodeResponseModel'];
|
||||
|
||||
export function getLanguages(): Promise<LanguageCode[]> {
|
||||
return request<LanguageCode[]>('/api/languages');
|
||||
}
|
||||
|
||||
export function messageFromLanguagesError(error: unknown, fallback = 'Unable to load languages'): string {
|
||||
if (error instanceof ApiError) {
|
||||
return error.detail ?? error.message;
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return error.message;
|
||||
}
|
||||
|
||||
return fallback;
|
||||
}
|
||||
Reference in New Issue
Block a user