Skip to content

Commit

Permalink
refactor: optimize how custom_format fields are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackDark committed Sep 30, 2024
1 parent ece7d1d commit bf78edd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
12 changes: 8 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import { CustomFormatResource, CustomFormatSpecificationSchema } from "./__gener

export type DynamicImportType<T> = { default: T };

type RequireAtLeastOne<T> = {
[K in keyof T]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<keyof T, K>>>;
}[keyof T];

/** Used in the UI of Sonarr/Radarr to import. Trash JSON are based on that so users can copy&paste stuff */
export type UserFriendlyField = {
name?: string | null;
name?: string | null; // TODO validate if this can really appear? As Input
value?: any;
} & Pick<CustomFormatSpecificationSchema, "negate" | "required">;

export type TrashCFSpF = { min: number; max: number };
export type TrashCFSpF = { min: number; max: number; exceptLanguage: boolean; value: any };

/*
Language values:
Expand All @@ -26,12 +30,12 @@ export type CustomFormatImportImplementation =

export type TC1 = Omit<CustomFormatSpecificationSchema, "fields"> & {
implementation: "ReleaseTitleSpecification" | "LanguageSpecification";
fields?: UserFriendlyField | null;
fields?: RequireAtLeastOne<TrashCFSpF> | null;
};

export type TC2 = Omit<CustomFormatSpecificationSchema, "fields"> & {
implementation: "SizeSpecification";
fields?: TrashCFSpF;
fields?: RequireAtLeastOne<TrashCFSpF>;
};

export type TCM = TC1 | TC2;
Expand Down
21 changes: 2 additions & 19 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,8 @@ export const mapImportCfToRequestCf = (cf: TrashCF | ConfigarrCF): CustomFormatR
throw new Error(`Spec is not correctly defined: ${spec.name}`);
}

switch (spec.implementation) {
case "SizeSpecification":
newFields.push({
name: "min",
value: spec.fields.min,
});
newFields.push({
name: "max",
value: spec.fields.max,
});
break;
case "ReleaseTitleSpecification":
case "LanguageSpecification":
default:
newFields.push({
value: spec.fields.value,
});
break;
}
// 2024-09-30: Test if this handles all cases
newFields.push(...Object.entries(spec.fields).map(([key, value]) => ({ name: key, value })));

return {
...spec,
Expand Down

0 comments on commit bf78edd

Please sign in to comment.