Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbenincasa committed Oct 11, 2024
1 parent 3213faa commit f676f3f
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 19 deletions.
5 changes: 3 additions & 2 deletions server/src/api/channelsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { scheduleTimeSlots } from '@tunarr/shared';
import {
BasicIdParamSchema,
BasicPagingSchema,
GetChannelProgrammingResponseSchema,
TimeSlotScheduleSchema,
UpdateChannelProgrammingRequestSchema,
} from '@tunarr/types/api';
Expand Down Expand Up @@ -50,7 +51,7 @@ export const channelsApi: RouterPluginAsyncCallback = async (fastify) => {
'/channels',
{
schema: {
operationId: 'getChannelsV2',
operationId: 'getChannels',
tags: ['Channels'],
response: {
200: z.array(ChannelSchema),
Expand Down Expand Up @@ -288,7 +289,7 @@ export const channelsApi: RouterPluginAsyncCallback = async (fastify) => {
querystring: BasicPagingSchema,
tags: ['Channels'],
response: {
200: CondensedChannelProgrammingSchema,
200: GetChannelProgrammingResponseSchema,
404: z.object({ error: z.string() }),
},
},
Expand Down
10 changes: 10 additions & 0 deletions server/src/api/customShowsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const customShowsApiV2: RouterPluginAsyncCallback = async (fastify) => {
'/custom-shows',
{
schema: {
tags: ['Custom Shows'],
response: {
200: z.array(CustomShowSchema),
},
Expand All @@ -51,6 +52,7 @@ export const customShowsApiV2: RouterPluginAsyncCallback = async (fastify) => {
'/custom-shows/:id',
{
schema: {
tags: ['Custom Shows'],
params: IdPathParamSchema,
response: {
200: CustomShowSchema,
Expand Down Expand Up @@ -79,6 +81,7 @@ export const customShowsApiV2: RouterPluginAsyncCallback = async (fastify) => {
'/custom-shows/:id',
{
schema: {
tags: ['Custom Shows'],
params: IdPathParamSchema,
body: UpdateCustomShowRequestSchema,
response: {
Expand Down Expand Up @@ -110,6 +113,7 @@ export const customShowsApiV2: RouterPluginAsyncCallback = async (fastify) => {
'/custom-shows/:id/programs',
{
schema: {
tags: ['Custom Shows'],
params: IdPathParamSchema,
response: {
200: z.array(CustomProgramSchema),
Expand All @@ -128,6 +132,9 @@ export const customShowsApiV2: RouterPluginAsyncCallback = async (fastify) => {
'/custom-shows',
{
schema: {
tags: ['Custom Shows'],
operationId: 'createCustomShow',
description: 'Creates a new Custom Show',
body: CreateCustomShowRequestSchema,
response: {
201: z.object({ id: z.string() }),
Expand All @@ -145,6 +152,9 @@ export const customShowsApiV2: RouterPluginAsyncCallback = async (fastify) => {
'/custom-shows/:id',
{
schema: {
tags: ['Custom Shows'],
operationId: 'deleteCustomShow',
description: 'Delets a custom show with the given ID',
params: IdPathParamSchema,
response: {
200: z.object({ id: z.string() }),
Expand Down
47 changes: 32 additions & 15 deletions server/src/api/hdhrApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,44 @@ export class HdhrApiRouter {
.send(req.serverCtx.hdhrService.getHdhrDeviceXml(host));
});

fastify.get('/discover.json', (req, res) => {
return res.send(
req.serverCtx.hdhrService.getHdhrDevice(
req.protocol + '://' + req.hostname,
),
);
});
fastify.get(
'/discover.json',
{
schema: {
tags: ['HDHR'],
},
},
(req, res) => {
return res.send(
req.serverCtx.hdhrService.getHdhrDevice(
req.protocol + '://' + req.hostname,
),
);
},
);

fastify.get('/lineup_status.json', (_, res) => {
return res.send({
ScanInProgress: 0,
ScanPossible: 1,
Source: 'Cable',
SourceList: ['Cable'],
});
});
fastify.get(
'/lineup_status.json',
{
schema: {
tags: ['HDHR'],
},
},
(_, res) => {
return res.send({
ScanInProgress: 0,
ScanPossible: 1,
Source: 'Cable',
SourceList: ['Cable'],
});
},
);

fastify.get(
'/lineup.json',
{
schema: {
tags: ['HDHR'],
response: {
200: z.array(HdhrLineupSchema),
},
Expand Down
24 changes: 23 additions & 1 deletion server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fastifyMultipart from '@fastify/multipart';
import fpStatic from '@fastify/static';
import fastifySwagger from '@fastify/swagger';
import { RequestContext } from '@mikro-orm/core';
import fastifyApiReference from '@scalar/fastify-api-reference';
import constants from '@tunarr/shared/constants';
import fastify, { FastifySchema } from 'fastify';
import fastifyGracefulShutdown from 'fastify-graceful-shutdown';
Expand Down Expand Up @@ -49,6 +50,7 @@ import { runFixers } from './tasks/fixers/index.js';
import { copyDirectoryContents, fileExists } from './util/fsUtil.js';
import { filename, isNonEmptyString, run } from './util/index.js';
import { LoggerFactory, RootLogger } from './util/logging/LoggerFactory.js';
import { getTunarrVersion } from './util/version.js';

const currentDirectory = dirname(filename(import.meta.url));

Expand Down Expand Up @@ -197,13 +199,25 @@ export async function initServer(opts: ServerOptions) {
info: {
title: 'Tunarr',
description: 'Tunarr API',
version: '1.0.0',
version: getTunarrVersion(),
},
servers: [],
tags: [
{
name: 'Channels',
},
{
name: 'Custom Shows',
},
{
name: 'Filler Lists',
},
{
name: 'HDHR',
},
{
name: 'Settings',
},
],
},
transform: jsonSchemaTransform,
Expand All @@ -215,6 +229,14 @@ export async function initServer(opts: ServerOptions) {
// ? join(dirname(process.argv[1]), 'static')
// : undefined,
// })
.register(fastifyApiReference, {
routePrefix: '/docs',
configuration: {
spec: {
content: () => app.swagger(),
},
},
})
.register(cors, {
origin: '*', // Testing
})
Expand Down
30 changes: 30 additions & 0 deletions types/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import {
} from '../SystemSettings.js';
import {
ChannelProgramSchema,
CondensedChannelProgrammingSchema,
CondensedContentProgramSchema,
CondensedCustomProgramSchema,
ContentProgramSchema,
CustomProgramSchema,
FlexProgramSchema,
RedirectProgramSchema,
} from '../schemas/programmingSchema.js';
import {
BackupSettingsSchema,
Expand Down Expand Up @@ -255,3 +260,28 @@ export const ChannelSessionsResponseSchema = z.object({
export type ChannelSessionsResponse = z.infer<
typeof ChannelSessionsResponseSchema
>;

const ContentProgramWithNoOriginalSchema = ContentProgramSchema.omit({
originalProgram: true,
});

const CondensedChannelProgramWithNoOriginalSchema = z.discriminatedUnion(
'type',
[
CondensedContentProgramSchema.omit({ originalProgram: true }),
CondensedCustomProgramSchema.extend({
program: CondensedContentProgramSchema.omit({
originalProgram: true,
}).optional(),
}),
RedirectProgramSchema,
FlexProgramSchema,
],
);

// This is sorta hacky.
export const GetChannelProgrammingResponseSchema =
CondensedChannelProgrammingSchema.extend({
lineup: z.array(CondensedChannelProgramWithNoOriginalSchema),
programs: z.record(ContentProgramWithNoOriginalSchema),
});
4 changes: 3 additions & 1 deletion types/src/schemas/programmingSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ export const CondensedContentProgramSchema = BaseProgramSchema.extend({
type: z.literal('content'),
id: z.string().optional(), // Populated if persisted
// Only populated on client requests to the server
originalProgram: OriginalProgramSchema.optional(),
originalProgram: OriginalProgramSchema.optional().describe(
"The program pulled from the relevant media source's API. This is generally only required on save/update operations",
),
});

export const ContentProgramTypeSchema = z.union([
Expand Down

0 comments on commit f676f3f

Please sign in to comment.