fix set volume route and formatting

This commit is contained in:
Darius
2026-02-03 22:12:26 +01:00
parent 6859ba27b9
commit e06d1c6e44

View File

@@ -3,137 +3,139 @@ import { z } from "zod";
import type { TidalService } from "../tidal/service.js";
export async function tidalRoutes(
fastify: FastifyInstance,
{
tidalService,
verifyAPIKey,
}: {
tidalService: TidalService;
verifyAPIKey: (
request: FastifyRequest,
reply: FastifyReply,
) => Promise<void>;
},
fastify: FastifyInstance,
{
tidalService,
verifyAPIKey,
}: {
tidalService: TidalService;
verifyAPIKey: (
request: FastifyRequest,
reply: FastifyReply,
) => Promise<void>;
},
) {
fastify.get(
"/tidal/song",
{
schema: {
description: "Get currently playing song",
tags: ["tidal"],
response: {
200: z.object({
title: z.string(),
artists: z.string(),
status: z.string(),
current: z.string(),
duration: z.string(),
url: z.string(),
}),
418: z.object({
error: z.string(),
}),
},
},
},
async (_request, reply) => {
const service_result = await tidalService.getSong();
fastify.get(
"/tidal/song",
{
schema: {
description: "Get currently playing song",
tags: ["tidal"],
response: {
200: z.object({
title: z.string(),
artists: z.string(),
status: z.string(),
current: z.string(),
duration: z.string(),
url: z.string(),
}),
418: z.object({
error: z.string(),
}),
},
},
},
async (_request, reply) => {
const service_result = await tidalService.getSong();
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
return service_result.result;
},
);
return service_result.result;
},
);
fastify.get(
"/tidal/songFormatted",
{
schema: {
description: "Get currently playing song (formatted)",
tags: ["tidal"],
response: {
200: z.string(),
418: z.object({
error: z.string(),
}),
},
},
},
async (_request, reply) => {
const service_result = await tidalService.getSongFormatted();
fastify.get(
"/tidal/songFormatted",
{
schema: {
description: "Get currently playing song (formatted)",
tags: ["tidal"],
response: {
200: z.string(),
418: z.object({
error: z.string(),
}),
},
},
},
async (_request, reply) => {
const service_result = await tidalService.getSongFormatted();
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
return service_result.result;
},
);
return service_result.result;
},
);
fastify.get(
"/tidal/volume",
{
schema: {
description: "Get current volume level",
tags: ["tidal"],
response: {
200: z.object({
volume: z.number(),
}),
418: z.object({
error: z.string(),
}),
},
},
},
async (_request, reply) => {
const service_result = await tidalService.getVolume();
fastify.get(
"/tidal/volume",
{
schema: {
description: "Get current volume level",
tags: ["tidal"],
response: {
200: z.object({
volume: z.number(),
}),
418: z.object({
error: z.string(),
}),
},
},
},
async (_request, reply) => {
const service_result = await tidalService.getVolume();
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
return service_result.result;
},
);
return service_result.result;
},
);
fastify.post(
"/tidal/volume",
{
preHandler: verifyAPIKey,
schema: {
description:
"Set volume level (accepts absolute number or relative +/- value)",
tags: ["tidal"],
body: z.string(),
response: {
200: z.object({
volume: z.number(),
}),
401: z.object({
error: z.literal("Invalid API key"),
}),
418: z.object({
error: z.string(),
}),
},
},
},
async (request, reply) => {
const volume = request.body as string;
const service_result = await tidalService.setVolume(volume);
fastify.post(
"/tidal/volume",
{
preHandler: verifyAPIKey,
schema: {
description:
"Set volume level (accepts absolute number or relative +/- value)",
tags: ["tidal"],
body: z.object({
volume: z.string(),
}),
response: {
200: z.object({
volume: z.number(),
}),
401: z.object({
error: z.literal("Invalid API key"),
}),
418: z.object({
error: z.string(),
}),
},
},
},
async (request, reply) => {
const volume = request.body as string;
const service_result = await tidalService.setVolume(volume);
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
return service_result.result;
},
);
return service_result.result;
},
);
}