Referencia de API

Downloader y tipos

Referencia de la clase NodeDownloader y de las interfaces TypeScript exportadas por el paquete.

NodeDownloader se importa desde el punto de entrada principal, mientras que las interfaces y tipos se importan desde @nodecfdi/xml-resource-retriever/types.

import type {
  DownloaderInterface,
  NodeDownloaderOptions,
  RetrieverInterface,
  SearchElement,
  UrlParts,
} from '@nodecfdi/xml-resource-retriever/types';
import { NodeDownloader } from '@nodecfdi/xml-resource-retriever';

NodeDownloader

Implementa DownloaderInterface usando fetch nativo de Node.js con escritura atómica.

Constructor

declare class NodeDownloader implements DownloaderInterface {
  public constructor(options?: NodeDownloaderOptions);
}
  • Valida timeoutMs (defecto 30_000) y maxBytes (defecto 52_428_800) con validateNonNegativeFinite. Lanza RangeError si son negativos, NaN o Infinity.
  • Reenvía cualquier otra propiedad de RequestInit a fetch.

downloadTo(source: string, destination: string): Promise<void>

Descarga source hacia un archivo temporal ${destination}.${uuid}.tmp y lo renombra a destination al completarse.

  • Lanza AggregateError con el mensaje `Unable to download ${source} to ${destination}` y la causa original en error.errors[0] ante cualquier error de protocolo, red, HTTP no exitoso, límite de bytes, timeout o escritura en disco.

Interfaces y tipos

RetrieverInterface

export interface RetrieverInterface {
  getBasePath: () => string;
  buildPath: (url: string) => string;
  retrieve: (url: string) => Promise<string>;
  retrieveHistory: () => Record<string, string>;
  download: (url: string) => Promise<string>;
}

DownloaderInterface

export interface DownloaderInterface {
  downloadTo: (source: string, destination: string) => Promise<void>;
}

NodeDownloaderOptions

export interface NodeDownloaderOptions extends RequestInit {
  /**
   * Tiempo máximo en milisegundos para la petición y descarga del cuerpo.
   * Usa `0` para desactivar el timeout.
   * @defaultValue 30_000
   */
  timeoutMs?: number;

  /**
   * Tamaño máximo permitido de la respuesta en bytes.
   * Usa `0` para desactivar el límite.
   * @defaultValue 52_428_800 (50 MiB)
   */
  maxBytes?: number;
}

SearchElement

export interface SearchElement {
  element: string;
  attribute: string;
}

UrlParts

export interface UrlParts {
  scheme: string;
  host: string;
  port: string;
  path: string;
}