Skip to content

Commit

Permalink
refactor: improve typings for client
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackDark committed Nov 20, 2024
1 parent 6e39941 commit b9ad772
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 105 deletions.
34 changes: 12 additions & 22 deletions src/clients/radarr-client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { KyHttpClient } from "../__generated__/ky-client";
import { MergedCustomFormatResource } from "../__generated__/mergedTypes";
import { Api } from "../__generated__/radarr/Api";
import { CustomFormatResource, QualityDefinitionResource, QualityProfileResource } from "../__generated__/radarr/data-contracts";
import { logger } from "../logger";
import { IArrClient, validateClientParams } from "./unified-client";

export class RadarrClient implements IArrClient {
export class RadarrClient implements IArrClient<QualityProfileResource, QualityDefinitionResource, CustomFormatResource> {
private api!: Api<unknown>;

constructor(baseUrl: string, apiKey: string) {
Expand All @@ -29,20 +29,23 @@ export class RadarrClient implements IArrClient {
return this.api.v3QualitydefinitionList();
}

updateQualityDefinitions(definitions: any) {
return this.api.v3QualitydefinitionUpdateUpdate(definitions);
async updateQualityDefinitions(definitions: QualityDefinitionResource[]) {
await this.api.v3QualitydefinitionUpdateUpdate(definitions);
return this.getQualityDefinitions();
}

// Quality Profiles
getQualityProfiles() {
getQualityProfiles(): Promise<QualityProfileResource[]> {
return this.api.v3QualityprofileList();
}

createQualityProfile(profile: any) {
createQualityProfile(profile: QualityProfileResource): Promise<QualityProfileResource> {
if (profile.language) {
}
return this.api.v3QualityprofileCreate(profile);
}

updateQualityProfile(id: string, profile: any) {
updateQualityProfile(id: string, profile: QualityProfileResource): Promise<QualityProfileResource> {
return this.api.v3QualityprofileUpdate(id, profile);
}

Expand All @@ -51,31 +54,18 @@ export class RadarrClient implements IArrClient {
return this.api.v3CustomformatList();
}

createCustomFormat(format: MergedCustomFormatResource) {
createCustomFormat(format: CustomFormatResource) {
return this.api.v3CustomformatCreate(format);
}

updateCustomFormat(id: string, format: MergedCustomFormatResource) {
updateCustomFormat(id: string, format: CustomFormatResource) {
return this.api.v3CustomformatUpdate(id, format);
}

deleteCustomFormat(id: string) {
return this.api.v3CustomformatDelete(+id);
}

// Metadata Profiles
async getMetadataProfiles() {
throw new Error("Metadata profiles are not supported in Radarr");
}

async createMetadataProfile(profile: any) {
throw new Error("Metadata profiles are not supported in Radarr");
}

async updateMetadataProfile(id: number, profile: any) {
throw new Error("Metadata profiles are not supported in Radarr");
}

// System/Health Check
getSystemStatus() {
return this.api.v3SystemStatusList();
Expand Down
26 changes: 16 additions & 10 deletions src/clients/readarr-client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { KyHttpClient } from "../__generated__/ky-client";
import { MergedCustomFormatResource } from "../__generated__/mergedTypes";
import { Api } from "../__generated__/readarr/Api";
import {
CustomFormatResource,
MetadataProfileResource,
QualityDefinitionResource,
QualityProfileResource,
} from "../__generated__/readarr/data-contracts";
import { logger } from "../logger";
import { IArrClient, validateClientParams } from "./unified-client";

export class ReadarrClient implements IArrClient {
export class ReadarrClient implements IArrClient<QualityProfileResource, QualityDefinitionResource, CustomFormatResource> {
private api!: Api<unknown>;

constructor(baseUrl: string, apiKey: string) {
Expand All @@ -29,20 +34,21 @@ export class ReadarrClient implements IArrClient {
return this.api.v1QualitydefinitionList();
}

updateQualityDefinitions(definitions: any) {
return this.api.v1QualitydefinitionUpdateUpdate(definitions);
async updateQualityDefinitions(definitions: QualityDefinitionResource[]) {
await this.api.v1QualitydefinitionUpdateUpdate(definitions);
return this.api.v1QualitydefinitionList();
}

// Quality Profiles
getQualityProfiles() {
return this.api.v1QualityprofileList();
}

createQualityProfile(profile: any) {
createQualityProfile(profile: QualityProfileResource) {
return this.api.v1QualityprofileCreate(profile);
}

updateQualityProfile(id: string, profile: any) {
updateQualityProfile(id: string, profile: QualityProfileResource) {
return this.api.v1QualityprofileUpdate(id, profile);
}

Expand All @@ -51,11 +57,11 @@ export class ReadarrClient implements IArrClient {
return this.api.v1CustomformatList();
}

createCustomFormat(format: MergedCustomFormatResource) {
createCustomFormat(format: CustomFormatResource) {
return this.api.v1CustomformatCreate(format);
}

updateCustomFormat(id: string, format: MergedCustomFormatResource) {
updateCustomFormat(id: string, format: CustomFormatResource) {
return this.api.v1CustomformatUpdate(id, format);
}

Expand All @@ -68,11 +74,11 @@ export class ReadarrClient implements IArrClient {
return this.api.v1MetadataprofileList();
}

async createMetadataProfile(profile: any) {
async createMetadataProfile(profile: MetadataProfileResource) {
return this.api.v1MetadataprofileCreate(profile);
}

async updateMetadataProfile(id: number, profile: any) {
async updateMetadataProfile(id: number, profile: MetadataProfileResource) {
return this.api.v1MetadataprofileUpdate(id.toString(), profile);
}

Expand Down
36 changes: 15 additions & 21 deletions src/clients/sonarr-client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { KyHttpClient } from "../__generated__/ky-client";
import { MergedCustomFormatResource } from "../__generated__/mergedTypes";
import { Api } from "../__generated__/sonarr/Api";
import { CustomFormatResource, QualityDefinitionResource, QualityProfileResource } from "../__generated__/sonarr/data-contracts";
import { logger } from "../logger";
import { IArrClient, validateClientParams } from "./unified-client";

export class SonarrClient implements IArrClient {
export type SonarrQualityProfileResource = {
id?: number;
name?: string;
// Add other common properties that all quality profiles share
};

export class SonarrClient implements IArrClient<QualityProfileResource, QualityDefinitionResource, CustomFormatResource> {
private api!: Api<unknown>;

constructor(baseUrl: string, apiKey: string) {
Expand All @@ -29,20 +35,21 @@ export class SonarrClient implements IArrClient {
return this.api.v3QualitydefinitionList();
}

updateQualityDefinitions(definitions: any) {
return this.api.v3QualitydefinitionUpdateUpdate(definitions);
async updateQualityDefinitions(definitions: QualityDefinitionResource[]) {
this.api.v3QualitydefinitionUpdateUpdate(definitions);
return this.api.v3QualitydefinitionList();
}

// Quality Profiles
getQualityProfiles() {
return this.api.v3QualityprofileList();
}

createQualityProfile(profile: any) {
createQualityProfile(profile: SonarrQualityProfileResource) {
return this.api.v3QualityprofileCreate(profile);
}

updateQualityProfile(id: string, profile: any) {
updateQualityProfile(id: string, profile: SonarrQualityProfileResource) {
return this.api.v3QualityprofileUpdate(id, profile);
}

Expand All @@ -51,31 +58,18 @@ export class SonarrClient implements IArrClient {
return this.api.v3CustomformatList();
}

createCustomFormat(format: MergedCustomFormatResource) {
createCustomFormat(format: CustomFormatResource) {
return this.api.v3CustomformatCreate(format);
}

updateCustomFormat(id: string, format: MergedCustomFormatResource) {
updateCustomFormat(id: string, format: CustomFormatResource) {
return this.api.v3CustomformatUpdate(id, format);
}

deleteCustomFormat(id: string) {
return this.api.v3CustomformatDelete(+id);
}

// Metadata Profiles
async getMetadataProfiles() {
throw new Error("Metadata profiles are not supported in Sonarr");
}

async createMetadataProfile(profile: any) {
throw new Error("Metadata profiles are not supported in Sonarr");
}

async updateMetadataProfile(id: number, profile: any) {
throw new Error("Metadata profiles are not supported in Sonarr");
}

// System/Health Check
getSystemStatus() {
return this.api.v3SystemStatusList();
Expand Down
66 changes: 35 additions & 31 deletions src/clients/unified-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MergedCustomFormatResource } from "../__generated__/mergedTypes";
import { MergedCustomFormatResource, MergedQualityDefinitionResource, MergedQualityProfileResource } from "../__generated__/mergedTypes";
import { logger } from "../logger";
import { ArrType } from "../types/common.types";
import { RadarrClient } from "./radarr-client";
Expand Down Expand Up @@ -70,27 +70,40 @@ export const configureApi = async (type: ArrType, baseUrl: string, apiKey: strin
return unifiedClient;
};

export interface IArrClient {
export type ArrClientCustomFormat = {
id?: number;
};

export type ArrClientQualityDefinition = {
id?: number;
};

export type ArrClientQualityProfile = {
id?: number;
name?: string | null;
// Add other common properties that all quality profiles share
};

export interface IArrClient<
QP extends ArrClientQualityProfile = MergedQualityProfileResource,
QD extends ArrClientQualityDefinition = MergedQualityDefinitionResource,
CF extends ArrClientCustomFormat = MergedCustomFormatResource,
> {
// Quality Management
getQualityDefinitions(): Promise<any>;
updateQualityDefinitions(definitions: any): Promise<any>;
getQualityDefinitions(): Promise<QD[]>;
updateQualityDefinitions(definitions: QD[]): Promise<QD[]>;

// Quality Profiles
getQualityProfiles(): Promise<any>;
createQualityProfile(profile: any): Promise<any>;
updateQualityProfile(id: string, profile: any): Promise<any>;
getQualityProfiles(): Promise<QP[]>;
createQualityProfile(profile: QP): Promise<QP>;
updateQualityProfile(id: string, profile: QP): Promise<QP>;

// Custom Formats
getCustomFormats(): Promise<MergedCustomFormatResource[]>;
createCustomFormat(format: MergedCustomFormatResource): Promise<MergedCustomFormatResource>;
updateCustomFormat(id: string, format: MergedCustomFormatResource): Promise<MergedCustomFormatResource>;
getCustomFormats(): Promise<CF[]>;
createCustomFormat(format: CF): Promise<CF>;
updateCustomFormat(id: string, format: CF): Promise<CF>;
deleteCustomFormat(id: string): Promise<void>;

// Metadata Profiles (Readarr-specific)
getMetadataProfiles(): Promise<any>;
createMetadataProfile(profile: any): Promise<any>;
updateMetadataProfile(id: number, profile: any): Promise<any>;

// System/Health Check
getSystemStatus(): Promise<any>;
testConnection(): Promise<boolean>;
Expand Down Expand Up @@ -121,39 +134,30 @@ export class UnifiedClient implements IArrClient {
}
}

getSpecificClient<T extends IArrClient>(): T {
return this.api as T;
}

async getQualityDefinitions() {
return await this.api.getQualityDefinitions();
}

async updateQualityDefinitions(definitions: any) {
async updateQualityDefinitions(definitions: MergedQualityDefinitionResource[]) {
return await this.api.updateQualityDefinitions(definitions);
}

async createQualityProfile(profile: any) {
async createQualityProfile(profile: MergedQualityProfileResource) {
return await this.api.createQualityProfile(profile);
}

async getQualityProfiles() {
return await this.api.getQualityProfiles();
}

async updateQualityProfile(id: string, profile: any) {
async updateQualityProfile(id: string, profile: MergedQualityProfileResource) {
return await this.api.updateQualityProfile(id, profile);
}

// Readarr-specific methods
async createMetadataProfile(profile: any) {
return await this.api.createMetadataProfile(profile);
}

async getMetadataProfiles() {
return await this.api.getMetadataProfiles();
}

async updateMetadataProfile(id: number, profile: any) {
return await this.api.updateMetadataProfile(id, profile);
}

async getCustomFormats() {
return await this.api.getCustomFormats();
}
Expand Down
Loading

0 comments on commit b9ad772

Please sign in to comment.