Untitled

 avatar
unknown
plain_text
a month ago
1.6 kB
4
Indexable
  private async callExtractionAPI(document: any, template: any) {
    try {
      // Convert the file to FormData
      const formData = new FormData();

      // Add the document file
      formData.append('file', document.file);

      // Prepare the extraction request data
      const extractionRequest: Omit<ExtractionRequestDto, 'file'> = {
        fields: template.fields,
        options: {
          hasTable: template.options.hasTable,
          handwrittenTextRecognition:
            template.options.handwrittenTextRecognition,
          checkboxRecognition: template.options.checkboxRecognition,
          specificPageProcessing: template.options.specificPageProcessing,
          longDocument: template.options.longDocument,
          ...(template.options.specificPageProcessing && {
            specificPageProcessingOptions: {
              from: template.options.specificPageProcessingOptions.from,
              to: template.options.specificPageProcessingOptions.to,
            },
          }),
        },
        description: template.description,
      };

      // Add the JSON data to FormData
      formData.append(
        'data',
        new Blob([JSON.stringify(extractionRequest)], {
          type: 'application/json',
        }),
      );

      return await axios.post(process.env.EXTRACTION_API_URL, formData, {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
        timeout: 30000,
      });
    } catch (err) {
      if (err.response) {
        throw new Error(
          `API Error: ${err.response.data.message || err.message}`,
        );
      }
      throw err;
    }
  }
Leave a Comment