rewrite typeysation
This commit is contained in:
@@ -1,43 +1,43 @@
|
||||
import type { TidalSong, TidalVolume } from "@dpu/shared";
|
||||
import { BaseService, type ServiceResult } from "@dpu/shared/dist/fastify.js";
|
||||
import { logWarning } from "@dpu/shared/dist/logger.js";
|
||||
import type { TidalClient } from "./client.js";
|
||||
import type { TidalClient } from "./client";
|
||||
|
||||
export class TidalService {
|
||||
private client: TidalClient;
|
||||
|
||||
constructor(client: TidalClient) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
async getSongFormatted(): Promise<string> {
|
||||
const song = await this.getSong();
|
||||
if (song) {
|
||||
export class TidalService extends BaseService<TidalClient> {
|
||||
async getSongFormatted(): Promise<ServiceResult<string>> {
|
||||
const req = await this.getSong();
|
||||
if (req.successful) {
|
||||
const song = req.result as TidalSong;
|
||||
const status = song.status === "playing" ? "▶️" : "⏸️";
|
||||
return `listening to ${song.title} by ${song.artists}. ${status} ${song.current}/${song.duration}. link: ${song.url}`;
|
||||
return this.getSuccessfulResult(
|
||||
`listening to ${song.title} by ${song.artists}. ${status} ${song.current}/${song.duration}. link: ${song.url}`,
|
||||
);
|
||||
} else {
|
||||
return `no song found`;
|
||||
return this.getErrorResult(req.result as string);
|
||||
}
|
||||
}
|
||||
|
||||
async getSong(): Promise<TidalSong | null> {
|
||||
async getSong(): Promise<ServiceResult<TidalSong | string>> {
|
||||
try {
|
||||
const response = this.client.get<TidalSong>("current");
|
||||
const response = await this.getClient().get<TidalSong>("current");
|
||||
|
||||
return response;
|
||||
return this.getSuccessfulResult(response);
|
||||
} catch {
|
||||
logWarning("error getting song from tidal");
|
||||
return null;
|
||||
const error_message = "error getting song from tidal";
|
||||
logWarning(error_message);
|
||||
return this.getErrorResult(error_message);
|
||||
}
|
||||
}
|
||||
|
||||
async getVolume(): Promise<TidalVolume | null> {
|
||||
async getVolume(): Promise<ServiceResult<TidalVolume | string>> {
|
||||
try {
|
||||
const response = await this.client.get<TidalVolume>("volume");
|
||||
const response = await this.getClient().get<TidalVolume>("volume");
|
||||
|
||||
return response;
|
||||
return this.getSuccessfulResult(response);
|
||||
} catch {
|
||||
logWarning("error getting volume from tidal");
|
||||
return null;
|
||||
const error_message = "error getting volume from tidal";
|
||||
logWarning(error_message);
|
||||
return this.getErrorResult(error_message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,13 +45,16 @@ export class TidalService {
|
||||
return Math.min(Math.max(value, 0), 100);
|
||||
}
|
||||
|
||||
async setVolume(argument: string): Promise<TidalVolume | null> {
|
||||
async setVolume(
|
||||
argument: string,
|
||||
): Promise<ServiceResult<TidalVolume | string>> {
|
||||
const value = parseInt(argument, 10);
|
||||
// relative
|
||||
const adjustMatch = argument.match(/^([+-])(\d+)$/);
|
||||
if (adjustMatch) {
|
||||
const volume = await this.getVolume();
|
||||
if (volume) {
|
||||
const req = await this.getVolume();
|
||||
if (req.successful) {
|
||||
const volume = req.result as TidalVolume;
|
||||
const wantedVolume = volume.volume + value;
|
||||
const clampWantedVolume = this.clamp(wantedVolume);
|
||||
return await this.setVolumeToTidal(clampWantedVolume);
|
||||
@@ -65,19 +68,24 @@ export class TidalService {
|
||||
return await this.setVolumeToTidal(clampValue);
|
||||
}
|
||||
|
||||
return null;
|
||||
const error_message = "error parsing volume to set";
|
||||
logWarning(error_message);
|
||||
return this.getErrorResult(error_message);
|
||||
}
|
||||
|
||||
async setVolumeToTidal(volume: number): Promise<TidalVolume | null> {
|
||||
async setVolumeToTidal(
|
||||
volume: number,
|
||||
): Promise<ServiceResult<TidalVolume | string>> {
|
||||
try {
|
||||
const response = await this.client.post<TidalVolume>("volume", {
|
||||
const response = await this.getClient().post<TidalVolume>("volume", {
|
||||
volume,
|
||||
});
|
||||
|
||||
return response;
|
||||
return this.getSuccessfulResult(response);
|
||||
} catch {
|
||||
logWarning("error setting volume from tidal");
|
||||
return null;
|
||||
const error_message = "error setting volume from tidal";
|
||||
logWarning(error_message);
|
||||
return this.getErrorResult(error_message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user