From 83149bfec64fbab37280d273dc9b3825cfbe2ee4 Mon Sep 17 00:00:00 2001 From: Viren070 Date: Sat, 28 Dec 2024 17:21:23 +0000 Subject: [PATCH] feat: only use strings for addon options --- packages/addon/src/addon.ts | 14 +++++++--- packages/addon/src/config.ts | 26 +++++-------------- .../frontend/src/components/AddonsList.tsx | 12 ++++----- packages/types/src/types.ts | 2 +- packages/wrappers/src/torbox.ts | 4 +-- packages/wrappers/src/torrentio.ts | 15 ++++++----- 6 files changed, 34 insertions(+), 39 deletions(-) diff --git a/packages/addon/src/addon.ts b/packages/addon/src/addon.ts index 5302300f..0241d3f7 100644 --- a/packages/addon/src/addon.ts +++ b/packages/addon/src/addon.ts @@ -316,16 +316,22 @@ export class AIOStreams { ); } case 'gdrive': { - let addonUrl = addon.options.addonUrl as string; - addonUrl = addonUrl.replace('/manifest.json', '/') - const wrapper = new BaseWrapper(addon.options.overrideName ? addon.options.overrideName as string: 'GDrive', addonUrl, addon.options.indexerTimeout ? addon.options.indexerTimeout as number : undefined); + const addonUrl = addon.options.addonUrl?.replace('/manifest.json', '/') + '/'; + const wrapper = new BaseWrapper( + addon.options.overrideName || 'GDrive', + addonUrl, + addon.options.indexerTimeout ? parseInt(addon.options.indexerTimeout) : undefined + ); return await wrapper.getParsedStreams(streamRequest); } default: { + if (!addon.options.url) { + throw new Error('Addon URL not found'); + } console.log( `Using base wrapper for addon ${addon.options.name} with url ${addon.options.url}` ); - const wrapper = new BaseWrapper(addon.options.name as string, addon.options.url as string, addon.options.indexerTimeout ? addon.options.indexerTimeout as number : undefined); + const wrapper = new BaseWrapper(addon.options.name || 'Custom', addon.options.url.trim(), addon.options.indexerTimeout ? parseInt(addon.options.indexerTimeout) : undefined); return await wrapper.getParsedStreams(streamRequest); } } diff --git a/packages/addon/src/config.ts b/packages/addon/src/config.ts index b94e9a20..00ea596f 100644 --- a/packages/addon/src/config.ts +++ b/packages/addon/src/config.ts @@ -332,33 +332,21 @@ export function validateConfig(config: Config): { } if (option.type === 'number' && addon.options[option.id]) { - if (typeof addon.options[option.id] !== 'number') { + const input = addon.options[option.id]; + if (input !== undefined && !parseInt(input)) { return createResponse( false, 'invalidNumber', `${option.label} must be a number` ); - } - - if (option.constraints) { - if ( - option.constraints.min !== undefined && - addon.options[option.id] as number < option.constraints.min - ) { + } else if (input !== undefined) { + const value = parseInt(input); + const { min, max } = option.constraints || {}; + if ((min !== undefined && value < min) || (max !== undefined && value > max)) { return createResponse( false, 'invalidNumber', - `${option.label} must be greater than or equal to ${option.constraints.min}` - ); - } - if ( - option.constraints.max !== undefined && - addon.options[option.id] as number > option.constraints.max - ) { - return createResponse( - false, - 'invalidNumber', - `${option.label} must be less than or equal to ${option.constraints.max}` + `${option.label} must be between ${min} and ${max}` ); } } diff --git a/packages/frontend/src/components/AddonsList.tsx b/packages/frontend/src/components/AddonsList.tsx index e249f63d..8d5a570f 100644 --- a/packages/frontend/src/components/AddonsList.tsx +++ b/packages/frontend/src/components/AddonsList.tsx @@ -33,10 +33,10 @@ const AddonsList: React.FC = ({ const updateOption = ( addonIndex: number, optionKey: string, - value?: string | boolean | number + value?: string ) => { const newAddons = [...addons]; - newAddons[addonIndex].options[optionKey] = typeof value === 'string' ? value.trim() : value; + newAddons[addonIndex].options[optionKey] = value; setAddons(newAddons); }; @@ -92,12 +92,12 @@ const AddonsList: React.FC = ({ {option.type === 'checkbox' && ( updateOption( index, option.id, - e.target.checked + e.target.checked ? 'true' : 'false' ) } className={styles.checkbox} @@ -118,9 +118,9 @@ const AddonsList: React.FC = ({ {option.type === 'number' && ( - updateOption(index, option.id, e.target.value ? parseInt(e.target.value) : undefined) + updateOption(index, option.id, e.target.value ? e.target.value : undefined) } className={styles.textInput} /> diff --git a/packages/types/src/types.ts b/packages/types/src/types.ts index 16e53a3d..8ff569f8 100644 --- a/packages/types/src/types.ts +++ b/packages/types/src/types.ts @@ -108,7 +108,7 @@ export interface Config { minSize: number | null; addons: { id: string; - options: { [key: string]: string | boolean | number | undefined }; + options: { [key: string]: string | undefined}; }[]; services: { name: string; diff --git a/packages/wrappers/src/torbox.ts b/packages/wrappers/src/torbox.ts index f8e3d9f1..f94c5532 100644 --- a/packages/wrappers/src/torbox.ts +++ b/packages/wrappers/src/torbox.ts @@ -101,7 +101,7 @@ export class Torbox extends BaseWrapper { export async function getTorboxStreams( config: Config, torboxOptions: { - indexerTimeout?: number; + indexerTimeout?: string; overrideName?: string; }, streamRequest: StreamRequest @@ -118,6 +118,6 @@ export async function getTorboxStreams( throw new Error('Torbox API key not found'); } - const torbox = new Torbox(torboxApiKey, torboxOptions.indexerTimeout, torboxOptions.overrideName); + const torbox = new Torbox(torboxApiKey, torboxOptions.indexerTimeout ? parseInt(torboxOptions.indexerTimeout) : undefined, torboxOptions.overrideName); return await torbox.getParsedStreams(streamRequest); } diff --git a/packages/wrappers/src/torrentio.ts b/packages/wrappers/src/torrentio.ts index fbf0f7a5..576a762f 100644 --- a/packages/wrappers/src/torrentio.ts +++ b/packages/wrappers/src/torrentio.ts @@ -69,9 +69,9 @@ export class Torrentio extends BaseWrapper { export async function getTorrentioStreams( config: Config, torrentioOptions: { - useMultipleInstances?: boolean; + useMultipleInstances?: string; overrideUrl?: string; - indexerTimeout?: number; + indexerTimeout?: string; overrideName?: string; }, streamRequest: StreamRequest @@ -86,10 +86,11 @@ export async function getTorrentioStreams( 'debridlink', ]; const parsedStreams: ParsedStream[] = []; + const indexerTimeout = torrentioOptions.indexerTimeout ? parseInt(torrentioOptions.indexerTimeout) : undefined; // If overrideUrl is provided, use it to get streams and skip all other steps if (torrentioOptions.overrideUrl) { - const torrentio = new Torrentio(null, torrentioOptions.overrideUrl as string, torrentioOptions.indexerTimeout); + const torrentio = new Torrentio(null, torrentioOptions.overrideUrl as string, indexerTimeout); return torrentio.getParsedStreams(streamRequest); } @@ -100,7 +101,7 @@ export async function getTorrentioStreams( // if no usable services found, use torrentio without any configuration if (usableServices.length < 0) { - const torrentio = new Torrentio(null, null, torrentioOptions.indexerTimeout); + const torrentio = new Torrentio(null, null, indexerTimeout); return await torrentio.getParsedStreams(streamRequest); } @@ -115,14 +116,14 @@ export async function getTorrentioStreams( : `${serviceId}=${credentials.apiKey}`; }; - if (torrentioOptions.useMultipleInstances) { + if (torrentioOptions.useMultipleInstances === 'true') { for (const service of usableServices) { if (!service.enabled) { continue; } console.log('Creating Torrentio instance with service:', service.id); let configString = getServicePair(service.id, service.credentials); - const torrentio = new Torrentio(configString, null, torrentioOptions.indexerTimeout); + const torrentio = new Torrentio(configString, null, indexerTimeout); const streams = await torrentio.getParsedStreams(streamRequest); parsedStreams.push(...streams); } @@ -135,7 +136,7 @@ export async function getTorrentioStreams( } configString += getServicePair(service.id, service.credentials) + '|'; } - const torrentio = new Torrentio(configString, null, torrentioOptions.indexerTimeout, torrentioOptions.overrideName); + const torrentio = new Torrentio(configString, null, indexerTimeout, torrentioOptions.overrideName); return await torrentio.getParsedStreams(streamRequest); } }