documentation
This commit is contained in:
139
src/tidal/routes.ts
Normal file
139
src/tidal/routes.ts
Normal file
@@ -0,0 +1,139 @@
|
||||
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||
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.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 };
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
if (!service_result.successful) {
|
||||
reply.code(418);
|
||||
return { error: 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();
|
||||
|
||||
if (!service_result.successful) {
|
||||
reply.code(418);
|
||||
return { error: 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);
|
||||
|
||||
if (!service_result.successful) {
|
||||
reply.code(418);
|
||||
return { error: service_result.result };
|
||||
}
|
||||
|
||||
return service_result.result;
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user