@@ -12,17 +12,13 @@ import ArgumentError from './errors/ArgumentError.js';
1212import DirectoryError from './errors/DirectoryError.js' ;
1313import { Image } from './index.js' ;
1414
15- export type DownloadOptions = {
15+ export type ImageOptions = {
1616 /**
1717 * The directory to save the image to.
1818 *
1919 * If not provided, the current working directory will be used.
2020 */
2121 directory ?: string ;
22- /**
23- * The headers to send with the request.
24- */
25- headers ?: Record < string , string | string [ ] | undefined > ;
2622 /**
2723 * The name of the image file.
2824 *
@@ -33,12 +29,6 @@ export type DownloadOptions = {
3329 * If a name with same extension already exists, ` (1)`, ` (2)`, etc. will be added to the end of the name.
3430 */
3531 name ?: string ;
36- /**
37- * Set the maximum number of times to retry the request if it fails.
38- *
39- * @default 2
40- */
41- maxRetry ?: number ;
4232 /**
4333 * The extension of the image.
4434 *
@@ -47,6 +37,19 @@ export type DownloadOptions = {
4737 * If the URL doesn't have an extension, `jpg` will be used.
4838 */
4939 extension ?: string ;
40+ } ;
41+
42+ export type DownloadOptions = {
43+ /**
44+ * The headers to send with the request.
45+ */
46+ headers ?: Record < string , string | string [ ] | undefined > ;
47+ /**
48+ * Set the maximum number of times to retry the request if it fails.
49+ *
50+ * @default 2
51+ */
52+ maxRetry ?: number ;
5053 /**
5154 * Set timeout for each request in milliseconds.
5255 */
@@ -58,11 +61,15 @@ export type DownloadOptions = {
5861} ;
5962
6063/**
61- * Set the options with the default values if they are not provided.
64+ * Parses and validates the image parameters.
65+ *
66+ * If image options are not provided, the default values will be used.
67+ *
68+ * See {@link ImageOptions} for more information.
6269 *
6370 * @throws {ArgumentError } If there is an invalid value.
6471 */
65- export function parseImageParams ( url : string , options ?: DownloadOptions ) {
72+ export function parseImageParams ( url : string , options ?: ImageOptions ) {
6673 const lowerImgExts = [ ...imageExtensions ] . map ( ( ext ) => ext . toLowerCase ( ) ) ;
6774 const originalExt = path . extname ( url ) . replace ( '.' , '' ) ;
6875 const img : Image = {
@@ -135,16 +142,14 @@ export function parseImageParams(url: string, options?: DownloadOptions) {
135142}
136143
137144/**
138- * Downloads an image from a URL .
139- * @param url The URL of the image to download .
140- * @param options The options to use .
145+ * Downloads an image.
146+ * @param img The validated image parameters. See { @link parseImageParams} .
147+ * @param options The download options .
141148 * @returns The file path.
142149 * @throws {DirectoryError } If the directory cannot be created.
143150 * @throws {Error } If there are any other errors.
144151 */
145- export async function download ( url : string , options : DownloadOptions = { } ) {
146- const img = parseImageParams ( url , options ) ;
147-
152+ export async function download ( img : Image , options : DownloadOptions = { } ) {
148153 // Create the directory if it doesn't exist.
149154 if ( ! fs . existsSync ( img . directory ) ) {
150155 try {
0 commit comments