API: torrents: schema validate add-urls and add-files endpoints

Those endpoints use extensive amount of user-provided properties
and will be frequently used by third party developers. With Node
15, unhandled promise rejections directly crash the server, as
such, it is safer to schema validate the request before processing
it.

This change also prepares the code paths for later change that adds
destination fallbacks.
This commit is contained in:
Jesse Chan
2020-11-13 01:43:05 +08:00
parent 7abeede3c6
commit 80ffb24d8d
22 changed files with 290 additions and 212 deletions
+41
View File
@@ -0,0 +1,41 @@
import {array, boolean, object, record, string} from 'zod';
import type {infer as zodInfer} from 'zod';
// POST /api/torrents/add-urls
export const addTorrentByURLSchema = object({
// URLs to download torrents from
urls: array(string().url()).nonempty(),
// Cookies to attach to requests, arrays of strings in the format "name=value" with domain as key
cookies: record(array(string())).optional(),
// Path of destination
destination: string(),
// Tags
tags: array(string()).optional(),
// Whether destination is the base path [default: false]
isBasePath: boolean().optional(),
// Whether destination contains completed contents [default: false]
isCompleted: boolean().optional(),
// Whether to start torrent [default: false]
start: boolean().optional(),
});
export type AddTorrentByURLOptions = zodInfer<typeof addTorrentByURLSchema>;
// POST /api/torrents/add-files
export const addTorrentByFileSchema = object({
// Torrent files in base64
files: array(string()).nonempty(),
// Path of destination
destination: string(),
// Tags
tags: array(string()).optional(),
// Whether destination is the base path [default: false]
isBasePath: boolean().optional(),
// Whether destination contains completed contents [default: false]
isCompleted: boolean().optional(),
// Whether to start torrent [default: false]
start: boolean().optional(),
});
export type AddTorrentByFileOptions = zodInfer<typeof addTorrentByFileSchema>;