---
title: "Retrievers personalizados"
description: "Cómo extender AbstractXmlRetriever o AbstractBaseRetriever para descargar otros tipos de recursos XML y ajustar límites."
canonical_url: "https://nodecfdi.com/packages/xml-resource-retriever/advanced/custom-retrievers"
---

> Index of every page on this site: [llms.txt](https://nodecfdi.com/llms.txt)

Además de `XsdRetriever` y `XsltRetriever`, puedes crear tus propios retrievers para cualquier formato XML con referencias externas (por ejemplo, WSDL, Schematron o vocabularios propios) o para recursos que requieran lógica de validación distinta.

## Extender `AbstractXmlRetriever`

Para recursos XML que referencian otros documentos mediante atributos en un namespace específico, extiende `AbstractXmlRetriever` e implementa `searchNamespace()` y `searchElements()`.

```ts [src/wsdl_retriever.ts]
import type { SearchElement } from '@nodecfdi/xml-resource-retriever/types';
import { AbstractXmlRetriever } from '@nodecfdi/xml-resource-retriever';

export class WsdlRetriever extends AbstractXmlRetriever {
  protected searchNamespace(): string {
    return 'http://schemas.xmlsoap.org/wsdl/';
  }

  protected searchElements(): SearchElement[] {
    return [{ element: 'import', attribute: 'location' }];
  }
}
```

### Ajustar límites de recursión y concurrencia

`AbstractXmlRetriever` expone tres métodos protegidos que puedes sobrescribir para ajustar los límites por operación `retrieve()`:

<table>
<thead>
  <tr>
    <th>
      Método
    </th>
    
    <th>
      Valor por defecto
    </th>
    
    <th>
      Descripción
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        maxDepth()
      </code>
    </td>
    
    <td>
      <code>
        50
      </code>
    </td>
    
    <td>
      Profundidad máxima de anidamiento al seguir recursos enlazados.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        maxResources()
      </code>
    </td>
    
    <td>
      <code>
        500
      </code>
    </td>
    
    <td>
      Número máximo total de recursos descargados en una sola llamada a <code>
        retrieve()
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        maxConcurrentDownloads()
      </code>
    </td>
    
    <td>
      <code>
        6
      </code>
    </td>
    
    <td>
      Número máximo de descargas simultáneas (siempre se garantiza al menos <code>
        1
      </code>
      
      ).
    </td>
  </tr>
</tbody>
</table>

```ts [src/limited_xsd_retriever.ts]
import { XsdRetriever } from '@nodecfdi/xml-resource-retriever';

export class LimitedXsdRetriever extends XsdRetriever {
  protected override maxDepth(): number {
    return 15;
  }

  protected override maxResources(): number {
    return 100;
  }

  protected override maxConcurrentDownloads(): number {
    return 3;
  }
}
```

## Extender `AbstractBaseRetriever`

Si necesitas descargar recursos que **no** siguen el flujo estándar de parseo XML con `searchNamespace()` / `searchElements()`, extiende directamente [`AbstractBaseRetriever`](https://nodecfdi.com/packages/xml-resource-retriever/api-reference/retrievers).

Deberás implementar:

- `retrieve(url: string): Promise<string>`: lógica de descarga y gestión del historial usando `clearHistory()`, `addToHistory()`, `hasInHistory()`, `getFromHistory()` y `historySize()`.
- `checkIsValidDownloadedFile(source: string, localPath: string): Promise<void>`: validación ejecutada automáticamente al final de `download(url)`.

```ts [src/custom_retriever.ts]
import { stat, unlink } from 'node:fs/promises';
import { AbstractBaseRetriever } from '@nodecfdi/xml-resource-retriever';

export class SimpleFileRetriever extends AbstractBaseRetriever {
  public constructor(basePath: string) {
    super(basePath);
  }

  public async retrieve(url: string): Promise<string> {
    this.clearHistory();
    const localPath = await this.download(url);
    this.addToHistory(url, localPath);

    return localPath;
  }

  protected async checkIsValidDownloadedFile(source: string, localPath: string): Promise<void> {
    const info = await stat(localPath);
    if (info.size > 0) {
      return;
    }

    await unlink(localPath);
    throw new Error(`El recurso ${source} está vacío`);
  }
}
```

## Sitemap

See the full [sitemap](https://nodecfdi.com/sitemap.md) for all pages.
