Repository: Mintplex-Labs/anything-llm
File: collector/utils/extensions/PaperlessNgx/PaperlessNgxLoader/index.js (JavaScript)

Code before:
  async fetchAllDocuments() {
    try {
      const documents = [];
      let nextUrl = `${this.baseUrl}/api/documents/`;
      let page = 1;

      while (nextUrl) {
        console.log(`Fetching documents page ${page} from Paperless-ngx`);

        try {
          const response = await fetch(nextUrl, {
            headers: {
              "Content-Type": "application/json",
              ...this.baseHeaders,
            },
          });

          if (!response.ok) {
            throw new Error(
              `Failed to fetch documents from Paperless-ngx: ${response.status}`
            );
          }

          const data = await response.json();

          // Filter valid docs (throws if doc is invalid)
          const validResults = data.results.filter((doc) => doc?.id);
          documents.push(...validResults);
          nextUrl = data.next || null;
          page++;
        } catch (error) {
          console.error(
            `Error fetching page ${page} from Paperless-ngx:`,
            error.message
          );
          break;
        }
      }

      console.log(
        `Fetched ${documents.length} documents from Paperless-ngx (Pages: ${
          page - 1
        })`
      );

      const documentsWithContent = await Promise.all(
        documents.map(async (doc) => {
          const content = await this.fetchDocumentContent(doc.id);
          return { ...doc, content };
        })
      );

Review comment (suggestion):
This is not true. Filter will not throw here and the option-chain will not cause an error either unless the array object is null or non-object.

Code after:
  async fetchAllDocuments() {
    try {
      const documents = [];
      let nextUrl = `${this.baseUrl}/api/documents/`;
      let page = 1;

      while (nextUrl) {
        console.log(`Fetching documents page ${page} from Paperless-ngx`);
        try {
          const data = await fetch(nextUrl, {
            headers: {
              "Content-Type": "application/json",
              ...this.baseHeaders,
            },
          }).then((res) => {
            if (!res.ok)
              throw new Error(
                `Failed to fetch documents from Paperless-ngx: ${res.status}`
              );
            return res.json();
          });

          const validResults = data.results.filter((doc) => doc?.id);
          if (!validResults.length) break;

          documents.push(...validResults);

          if (data.next === nextUrl) break;
          nextUrl = data.next || null;
          page++;
        } catch (error) {
          console.error(
            `Error fetching page ${page} from Paperless-ngx:`,
            error
          );
          break;
        }
      }

      console.log(
        `Fetched ${documents.length} documents from Paperless-ngx (Pages: ${
          page - 1
        })`
      );

      const documentsWithContent = await Promise.all(
        documents.map(async (doc) => {
          const content = await this.fetchDocumentContent(doc.id);
          return { ...doc, content };
        })
      );