rework tidal api to be nicer now

This commit is contained in:
Darius
2026-02-03 22:32:55 +01:00
parent 2ebc64f6df
commit 09b38cb929
3 changed files with 39 additions and 54 deletions

View File

@@ -81,9 +81,7 @@ export async function tidalRoutes(
description: "Get current volume level",
tags: ["tidal"],
response: {
200: z.object({
volume: z.number(),
}),
200: z.number(),
418: z.object({
error: z.string(),
}),
@@ -110,13 +108,9 @@ export async function tidalRoutes(
description:
"Set volume level (accepts absolute number or relative +/- value)",
tags: ["tidal"],
body: z.object({
volume: z.string(),
}),
body: z.string(),
response: {
200: z.object({
volume: z.number(),
}),
200: z.number(),
401: z.object({
error: z.literal("Invalid API key"),
}),

View File

@@ -2,7 +2,6 @@ import {
BaseService,
type ServiceResult,
type TidalGetCurrent,
type TidalPutVolume,
} from "@dpu/shared";
import { logWarning } from "@dpu/shared/dist/logger.js";
import type { TidalClient } from "./client.js";
@@ -33,13 +32,13 @@ export class TidalService extends BaseService<TidalClient> {
}
}
async getVolume(): Promise<ServiceResult<TidalPutVolume | string>> {
async getVolume(): Promise<ServiceResult<number | string>> {
try {
const response = await this.getClient().get<TidalGetCurrent>("current");
return this.getSuccessfulResult({
volume: response.volume,
});
return this.getSuccessfulResult(
this.decimalToPercentage(response.volume),
);
} catch {
const error_message = "error getting volume from tidal";
logWarning(error_message);
@@ -48,20 +47,18 @@ export class TidalService extends BaseService<TidalClient> {
}
clamp(value: number): number {
return Math.min(Math.max(value, 0), 100);
return Math.min(Math.max(value, 0.0), 1.0);
}
async setVolume(
argument: string,
): Promise<ServiceResult<TidalPutVolume | string>> {
const value = this.percentageToDecimal(argument);
async setVolume(argument: string): Promise<ServiceResult<number | string>> {
const value = this.percentageToDecimal(parseFloat(argument));
// relative
const adjustMatch = argument.match(/^([+-])/);
if (adjustMatch) {
const req = await this.getVolume();
if (req.successful) {
const volume = req.result as TidalPutVolume;
const wantedVolume = volume.volume + value;
const volume = req.result as number;
const wantedVolume = volume + value;
const clampWantedVolume = this.clamp(wantedVolume);
return await this.setVolumeToTidal(clampWantedVolume);
}
@@ -81,16 +78,11 @@ export class TidalService extends BaseService<TidalClient> {
async setVolumeToTidal(
volume: number,
): Promise<ServiceResult<TidalPutVolume | string>> {
): Promise<ServiceResult<number | string>> {
try {
const response = await this.getClient().put<TidalPutVolume>(
`player/volume?volume=${volume}`,
{},
);
await this.getClient().put<number>(`player/volume?volume=${volume}`, {});
return this.getSuccessfulResult(
this.decimalToPercentage(response.volume).toString(),
);
return this.getSuccessfulResult(this.decimalToPercentage(volume));
} catch {
const error_message = "error setting volume from tidal";
logWarning(error_message);
@@ -102,8 +94,7 @@ export class TidalService extends BaseService<TidalClient> {
return decimal * 100;
}
percentageToDecimal(percentage: string): number {
const num = parseFloat(percentage);
return num / 100;
percentageToDecimal(percentage: number): number {
return percentage / 100;
}
}