rewrite typeysation

This commit is contained in:
Darius
2025-11-21 16:49:12 +01:00
parent 27582e342f
commit e8459a7a7b
6 changed files with 131 additions and 514 deletions

View File

@@ -1,16 +1,10 @@
import { BaseClient } from "@dpu/shared/dist/fastify";
import { printNetworkError } from "@dpu/shared/dist/logger.js";
import type { AxiosInstance } from "axios";
export class TidalClient {
private axiosInstance: AxiosInstance;
constructor(axiosInstance: AxiosInstance) {
this.axiosInstance = axiosInstance;
}
export class TidalClient extends BaseClient {
async get<T>(endpoint: string): Promise<T> {
try {
const response = await this.axiosInstance.get<T>(`/${endpoint}`);
const response = await this.getAxios().get<T>(`/${endpoint}`);
return response.data;
} catch (error) {
@@ -21,7 +15,7 @@ export class TidalClient {
async post<T>(endpoint: string, data: unknown): Promise<T> {
try {
const response = await this.axiosInstance.post<T>(`/${endpoint}`, data);
const response = await this.getAxios().post<T>(`/${endpoint}`, data);
return response.data;
} catch (error) {
printNetworkError(error);

View File

@@ -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);
}
}
}