UNPKG

mindee

Version:

Mindee Client Library for Node.js

104 lines (103 loc) 4.51 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _WorkflowEndpoint_instances, _WorkflowEndpoint_workflowReqPost; Object.defineProperty(exports, "__esModule", { value: true }); exports.WorkflowEndpoint = void 0; const baseEndpoint_1 = require("./baseEndpoint"); const input_1 = require("../input"); const url_1 = require("url"); const form_data_1 = __importDefault(require("form-data")); const responseValidation_1 = require("./responseValidation"); const error_1 = require("./error"); /** * Endpoint for a workflow. */ class WorkflowEndpoint extends baseEndpoint_1.BaseEndpoint { constructor(settings, workflowId) { super(settings, `/v1/workflows/${workflowId}/executions`); _WorkflowEndpoint_instances.add(this); } /** * Sends a document to a workflow execution. * Throws an error if the server's response contains one. * @param {WorkflowParams} params parameters relating to prediction options. * @category Synchronous * @returns a `Promise` containing parsing results. */ async executeWorkflow(params) { await params.inputDoc.init(); if (params.pageOptions !== undefined) { await baseEndpoint_1.BaseEndpoint.cutDocPages(params.inputDoc, params.pageOptions); } const response = await __classPrivateFieldGet(this, _WorkflowEndpoint_instances, "m", _WorkflowEndpoint_workflowReqPost).call(this, params); if (!(0, responseValidation_1.isValidSyncResponse)(response)) { (0, error_1.handleError)(this.urlRoot, response, response.messageObj?.statusMessage); } return response; } /** * Send a file to a prediction API. * @param input * @param alias * @param priority * @param fullText * @param publicUrl * @param rag */ sendFileForPrediction(input, alias = null, priority = null, fullText = false, publicUrl = null, rag = null) { return new Promise((resolve, reject) => { const searchParams = new url_1.URLSearchParams(); if (fullText) { searchParams.append("full_text_ocr", "true"); } if (rag) { searchParams.append("rag", "true"); } const form = new form_data_1.default(); if (input instanceof input_1.LocalInputSource && input.fileObject instanceof Buffer) { form.append("document", input.fileObject, { filename: input.filename, }); } else { form.append("document", input.fileObject); } if (alias) { form.append("alias", alias); } if (publicUrl) { form.append("public_url", publicUrl); } if (priority) { form.append("priority", priority.toString()); } const headers = { ...this.settings.baseHeaders, ...form.getHeaders() }; let path = this.urlRoot; if (searchParams.toString().length > 0) { path += `?${searchParams}`; } const options = { method: "POST", headers: headers, hostname: this.settings.hostname, path: path, timeout: this.settings.timeout, }; const req = baseEndpoint_1.BaseEndpoint.readResponse(options, resolve, reject); form.pipe(req); // potential ECONNRESET if we don't end the request. req.end(); }); } } exports.WorkflowEndpoint = WorkflowEndpoint; _WorkflowEndpoint_instances = new WeakSet(), _WorkflowEndpoint_workflowReqPost = function _WorkflowEndpoint_workflowReqPost(params) { return this.sendFileForPrediction(params.inputDoc, params.alias, params.priority, params.fullText, params.publicUrl, params.rag); };