mindee 2.1.1 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/README.md +30 -9
- package/package.json +3 -2
- package/src/api/endpoint.d.ts +19 -17
- package/src/api/endpoint.js +27 -39
- package/src/api/index.d.ts +2 -2
- package/src/api/index.js +2 -11
- package/src/api/response.d.ts +8 -41
- package/src/api/response.js +18 -89
- package/src/cli.js +46 -12
- package/src/client.d.ts +47 -13
- package/src/client.js +73 -27
- package/src/documents/cropper/cropperV1.d.ts +7 -0
- package/src/documents/{cropper.js → cropper/cropperV1.js} +12 -8
- package/src/documents/custom.d.ts +2 -6
- package/src/documents/custom.js +12 -7
- package/src/documents/document.d.ts +8 -3
- package/src/documents/document.js +11 -5
- package/src/documents/documentConfig.d.ts +20 -34
- package/src/documents/documentConfig.js +33 -57
- package/src/documents/{financialDocument.d.ts → financialDocument/financialDocumentV1.d.ts} +4 -4
- package/src/documents/{financialDocument.js → financialDocument/financialDocumentV1.js} +28 -22
- package/src/documents/fr/idCard/idCardV1.d.ts +3 -0
- package/src/documents/fr/idCard/idCardV1.js +7 -0
- package/src/documents/index.d.ts +15 -13
- package/src/documents/index.js +21 -18
- package/src/documents/{invoice.d.ts → invoice/invoiceV3.d.ts} +4 -4
- package/src/documents/{invoice.js → invoice/invoiceV3.js} +30 -24
- package/src/documents/{passport.d.ts → passport/passportV1.d.ts} +4 -4
- package/src/documents/{passport.js → passport/passportV1.js} +19 -14
- package/src/documents/receipt/receiptV3.d.ts +17 -0
- package/src/documents/{receipt.js → receipt/receiptV3.js} +28 -22
- package/src/documents/receipt/receiptV4.d.ts +27 -0
- package/src/documents/receipt/receiptV4.js +96 -0
- package/src/documents/receipt/tax.d.ts +25 -0
- package/src/documents/receipt/tax.js +40 -0
- package/src/documents/us/bankCheck/bankCheckV1.d.ts +3 -0
- package/src/documents/us/bankCheck/bankCheckV1.js +7 -0
- package/src/errors/MindeeError.d.ts +3 -0
- package/src/errors/MindeeError.js +10 -0
- package/src/errors/index.d.ts +1 -0
- package/src/errors/index.js +5 -0
- package/src/fields/{customDocs.d.ts → apiBuilder.d.ts} +5 -1
- package/src/fields/{customDocs.js → apiBuilder.js} +0 -0
- package/src/fields/index.d.ts +1 -1
- package/src/fields/index.js +4 -4
- package/src/index.d.ts +2 -1
- package/src/index.js +11 -9
- package/src/inputs/base.d.ts +33 -0
- package/src/inputs/base.js +116 -0
- package/src/inputs/index.d.ts +3 -0
- package/src/inputs/index.js +18 -0
- package/src/inputs/pageOptions.d.ts +20 -0
- package/src/inputs/pageOptions.js +8 -0
- package/src/inputs/sources.d.ts +55 -0
- package/src/inputs/sources.js +121 -0
- package/src/pdf/index.d.ts +1 -0
- package/src/pdf/index.js +6 -0
- package/src/pdf/pdfOperation.d.ts +14 -0
- package/src/pdf/pdfOperation.js +69 -0
- package/src/constants.d.ts +0 -13
- package/src/constants.js +0 -60
- package/src/documents/cropper.d.ts +0 -7
- package/src/documents/receipt.d.ts +0 -17
- package/src/inputs.d.ts +0 -69
- package/src/inputs.js +0 -196
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
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");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
7
|
+
var _ReceiptV4_instances, _ReceiptV4_initFromApiPrediction;
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.ReceiptV4 = void 0;
|
|
10
|
+
const document_1 = require("../document");
|
|
11
|
+
const fields_1 = require("../../fields");
|
|
12
|
+
const tax_1 = require("./tax");
|
|
13
|
+
class ReceiptV4 extends document_1.Document {
|
|
14
|
+
constructor({ prediction, orientation = undefined, extras = undefined, inputSource = undefined, fullText = undefined, pageId = undefined, }) {
|
|
15
|
+
super({
|
|
16
|
+
inputSource: inputSource,
|
|
17
|
+
pageId: pageId,
|
|
18
|
+
orientation: orientation,
|
|
19
|
+
extras: extras,
|
|
20
|
+
fullText: fullText,
|
|
21
|
+
});
|
|
22
|
+
_ReceiptV4_instances.add(this);
|
|
23
|
+
/** List of taxes detected on the receipt. */
|
|
24
|
+
this.taxes = [];
|
|
25
|
+
__classPrivateFieldGet(this, _ReceiptV4_instances, "m", _ReceiptV4_initFromApiPrediction).call(this, prediction, pageId);
|
|
26
|
+
}
|
|
27
|
+
toString() {
|
|
28
|
+
const taxes = this.taxes.map((item) => item.toString()).join("\n ");
|
|
29
|
+
const outStr = `----- Receipt V4 -----
|
|
30
|
+
Filename: ${this.filename}
|
|
31
|
+
Total amount: ${this.totalAmount}
|
|
32
|
+
Total net: ${this.totalNet}
|
|
33
|
+
Tip: ${this.tip}
|
|
34
|
+
Date: ${this.date}
|
|
35
|
+
Category: ${this.category}
|
|
36
|
+
Time: ${this.time}
|
|
37
|
+
Supplier name: ${this.supplier}
|
|
38
|
+
Taxes: ${taxes}
|
|
39
|
+
Total taxes: ${this.totalTax}
|
|
40
|
+
Locale: ${this.locale}
|
|
41
|
+
----------------------
|
|
42
|
+
`;
|
|
43
|
+
return ReceiptV4.cleanOutString(outStr);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.ReceiptV4 = ReceiptV4;
|
|
47
|
+
_ReceiptV4_instances = new WeakSet(), _ReceiptV4_initFromApiPrediction = function _ReceiptV4_initFromApiPrediction(apiPrediction, pageId) {
|
|
48
|
+
this.locale = new fields_1.Locale({
|
|
49
|
+
prediction: apiPrediction.locale,
|
|
50
|
+
pageId: pageId,
|
|
51
|
+
});
|
|
52
|
+
this.totalTax = new fields_1.Amount({
|
|
53
|
+
prediction: apiPrediction.total_tax,
|
|
54
|
+
valueKey: "value",
|
|
55
|
+
pageId: pageId,
|
|
56
|
+
});
|
|
57
|
+
this.totalAmount = new fields_1.Amount({
|
|
58
|
+
prediction: apiPrediction.total_amount,
|
|
59
|
+
valueKey: "value",
|
|
60
|
+
pageId: pageId,
|
|
61
|
+
});
|
|
62
|
+
this.totalNet = new fields_1.Amount({
|
|
63
|
+
prediction: apiPrediction.total_net,
|
|
64
|
+
valueKey: "value",
|
|
65
|
+
pageId: pageId,
|
|
66
|
+
});
|
|
67
|
+
this.tip = new fields_1.Amount({
|
|
68
|
+
prediction: apiPrediction.tip,
|
|
69
|
+
valueKey: "value",
|
|
70
|
+
pageId: pageId,
|
|
71
|
+
});
|
|
72
|
+
this.date = new fields_1.DateField({
|
|
73
|
+
prediction: apiPrediction.date,
|
|
74
|
+
pageId: pageId,
|
|
75
|
+
});
|
|
76
|
+
this.category = new fields_1.Field({
|
|
77
|
+
prediction: apiPrediction.category,
|
|
78
|
+
pageId: pageId,
|
|
79
|
+
});
|
|
80
|
+
this.supplier = new fields_1.Field({
|
|
81
|
+
prediction: apiPrediction.supplier,
|
|
82
|
+
pageId: pageId,
|
|
83
|
+
});
|
|
84
|
+
this.time = new fields_1.Field({
|
|
85
|
+
prediction: apiPrediction.time,
|
|
86
|
+
pageId: pageId,
|
|
87
|
+
});
|
|
88
|
+
apiPrediction.taxes.map((taxPrediction) => this.taxes.push(new tax_1.ReceiptTaxField({
|
|
89
|
+
prediction: taxPrediction,
|
|
90
|
+
pageId: pageId,
|
|
91
|
+
valueKey: "value",
|
|
92
|
+
rateKey: "rate",
|
|
93
|
+
codeKey: "code",
|
|
94
|
+
basisKey: "basis",
|
|
95
|
+
})));
|
|
96
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { TaxField } from "../../fields";
|
|
2
|
+
interface ReceiptTaxConstructor {
|
|
3
|
+
prediction: any;
|
|
4
|
+
valueKey?: string;
|
|
5
|
+
rateKey?: string;
|
|
6
|
+
codeKey?: string;
|
|
7
|
+
basisKey?: string;
|
|
8
|
+
reconstructed?: boolean;
|
|
9
|
+
pageId?: number;
|
|
10
|
+
}
|
|
11
|
+
export declare class ReceiptTaxField extends TaxField {
|
|
12
|
+
/** The tax base */
|
|
13
|
+
basis?: number;
|
|
14
|
+
/**
|
|
15
|
+
* @param {Object} prediction - Prediction object from HTTP response
|
|
16
|
+
* @param {String} valueKey - Key to use in the prediction dict to get the tax value
|
|
17
|
+
* @param {String} rateKey - Key to use to get the tax rate in the prediction dict
|
|
18
|
+
* @param {String} codeKey - Key to use to get the tax code in the prediction dict
|
|
19
|
+
* @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
|
|
20
|
+
* @param {Integer} pageNumber - Page ID for multi-page document
|
|
21
|
+
*/
|
|
22
|
+
constructor({ prediction, valueKey, rateKey, codeKey, basisKey, reconstructed, pageId, }: ReceiptTaxConstructor);
|
|
23
|
+
toString(): string;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReceiptTaxField = void 0;
|
|
4
|
+
const fields_1 = require("../../fields");
|
|
5
|
+
const amount_1 = require("../../fields/amount");
|
|
6
|
+
class ReceiptTaxField extends fields_1.TaxField {
|
|
7
|
+
/**
|
|
8
|
+
* @param {Object} prediction - Prediction object from HTTP response
|
|
9
|
+
* @param {String} valueKey - Key to use in the prediction dict to get the tax value
|
|
10
|
+
* @param {String} rateKey - Key to use to get the tax rate in the prediction dict
|
|
11
|
+
* @param {String} codeKey - Key to use to get the tax code in the prediction dict
|
|
12
|
+
* @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
|
|
13
|
+
* @param {Integer} pageNumber - Page ID for multi-page document
|
|
14
|
+
*/
|
|
15
|
+
constructor({ prediction, valueKey = "value", rateKey = "rate", codeKey = "code", basisKey = "basis", reconstructed = false, pageId = undefined, }) {
|
|
16
|
+
super({ prediction, valueKey, rateKey, codeKey, reconstructed, pageId });
|
|
17
|
+
/** The tax base */
|
|
18
|
+
this.basis = undefined;
|
|
19
|
+
this.basis = parseFloat(prediction[basisKey]);
|
|
20
|
+
if (isNaN(this.basis))
|
|
21
|
+
this.basis = undefined;
|
|
22
|
+
}
|
|
23
|
+
toString() {
|
|
24
|
+
let outStr = "";
|
|
25
|
+
if (this.value !== undefined) {
|
|
26
|
+
outStr += `${(0, amount_1.floatToString)(this.value)}`;
|
|
27
|
+
}
|
|
28
|
+
if (this.rate !== undefined) {
|
|
29
|
+
outStr += ` ${(0, amount_1.floatToString)(this.rate)}%`;
|
|
30
|
+
}
|
|
31
|
+
if (this.code !== undefined) {
|
|
32
|
+
outStr += ` ${this.code}`;
|
|
33
|
+
}
|
|
34
|
+
if (this.basis !== undefined) {
|
|
35
|
+
outStr += ` ${this.basis}`;
|
|
36
|
+
}
|
|
37
|
+
return outStr.trim();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.ReceiptTaxField = ReceiptTaxField;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MindeeError = void 0;
|
|
4
|
+
class MindeeError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "MindeeError";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.MindeeError = MindeeError;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { MindeeError } from "./MindeeError";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MindeeError = void 0;
|
|
4
|
+
var MindeeError_1 = require("./MindeeError");
|
|
5
|
+
Object.defineProperty(exports, "MindeeError", { enumerable: true, get: function () { return MindeeError_1.MindeeError; } });
|
|
@@ -2,6 +2,10 @@ import { FieldConstructor, stringDict } from "./field";
|
|
|
2
2
|
import { Polygon } from "../geometry";
|
|
3
3
|
export declare class ClassificationField {
|
|
4
4
|
value: string;
|
|
5
|
+
/**
|
|
6
|
+
* The confidence score of the prediction.
|
|
7
|
+
* Note: Score is calculated on **word selection**, not its textual content (OCR).
|
|
8
|
+
*/
|
|
5
9
|
confidence: number;
|
|
6
10
|
constructor({ prediction }: {
|
|
7
11
|
prediction: stringDict;
|
|
@@ -34,7 +38,7 @@ export declare class ListField {
|
|
|
34
38
|
/** True if the field was reconstructed or computed using other fields. */
|
|
35
39
|
reconstructed: boolean;
|
|
36
40
|
/** The document page on which the information was found. */
|
|
37
|
-
pageId
|
|
41
|
+
pageId: number;
|
|
38
42
|
constructor({ prediction, reconstructed, pageId }: FieldConstructor);
|
|
39
43
|
contentsList(): Array<any>;
|
|
40
44
|
contentsString(separator?: string): string;
|
|
File without changes
|
package/src/fields/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export { Locale } from "./locale";
|
|
|
5
5
|
export { Amount } from "./amount";
|
|
6
6
|
export { DateField } from "./date";
|
|
7
7
|
export { BaseField, stringDict, Field } from "./field";
|
|
8
|
-
export { ListField, ListFieldValue, ClassificationField } from "./
|
|
8
|
+
export { ListField, ListFieldValue, ClassificationField } from "./apiBuilder";
|
|
9
9
|
export { FullText } from "./fullText";
|
|
10
10
|
export { CompanyRegistration } from "./companyRegistration";
|
|
11
11
|
export { CropperField } from "./cropper";
|
package/src/fields/index.js
CHANGED
|
@@ -16,10 +16,10 @@ Object.defineProperty(exports, "DateField", { enumerable: true, get: function ()
|
|
|
16
16
|
var field_1 = require("./field");
|
|
17
17
|
Object.defineProperty(exports, "BaseField", { enumerable: true, get: function () { return field_1.BaseField; } });
|
|
18
18
|
Object.defineProperty(exports, "Field", { enumerable: true, get: function () { return field_1.Field; } });
|
|
19
|
-
var
|
|
20
|
-
Object.defineProperty(exports, "ListField", { enumerable: true, get: function () { return
|
|
21
|
-
Object.defineProperty(exports, "ListFieldValue", { enumerable: true, get: function () { return
|
|
22
|
-
Object.defineProperty(exports, "ClassificationField", { enumerable: true, get: function () { return
|
|
19
|
+
var apiBuilder_1 = require("./apiBuilder");
|
|
20
|
+
Object.defineProperty(exports, "ListField", { enumerable: true, get: function () { return apiBuilder_1.ListField; } });
|
|
21
|
+
Object.defineProperty(exports, "ListFieldValue", { enumerable: true, get: function () { return apiBuilder_1.ListFieldValue; } });
|
|
22
|
+
Object.defineProperty(exports, "ClassificationField", { enumerable: true, get: function () { return apiBuilder_1.ClassificationField; } });
|
|
23
23
|
var fullText_1 = require("./fullText");
|
|
24
24
|
Object.defineProperty(exports, "FullText", { enumerable: true, get: function () { return fullText_1.FullText; } });
|
|
25
25
|
var companyRegistration_1 = require("./companyRegistration");
|
package/src/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { CustomV1, FinancialDocumentV1, InvoiceV3, PassportV1, ReceiptV3, ReceiptV4, CropperV1, } from "./documents";
|
|
2
2
|
export { Client } from "./client";
|
|
3
|
+
export { PageOptionsOperation } from "./inputs";
|
package/src/index.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Client = exports.
|
|
4
|
-
var
|
|
5
|
-
Object.defineProperty(exports, "
|
|
6
|
-
Object.defineProperty(exports, "
|
|
7
|
-
Object.defineProperty(exports, "
|
|
8
|
-
Object.defineProperty(exports, "
|
|
9
|
-
Object.defineProperty(exports, "
|
|
10
|
-
Object.defineProperty(exports, "
|
|
11
|
-
Object.defineProperty(exports, "
|
|
3
|
+
exports.PageOptionsOperation = exports.Client = exports.CropperV1 = exports.ReceiptV4 = exports.ReceiptV3 = exports.PassportV1 = exports.InvoiceV3 = exports.FinancialDocumentV1 = exports.CustomV1 = void 0;
|
|
4
|
+
var documents_1 = require("./documents");
|
|
5
|
+
Object.defineProperty(exports, "CustomV1", { enumerable: true, get: function () { return documents_1.CustomV1; } });
|
|
6
|
+
Object.defineProperty(exports, "FinancialDocumentV1", { enumerable: true, get: function () { return documents_1.FinancialDocumentV1; } });
|
|
7
|
+
Object.defineProperty(exports, "InvoiceV3", { enumerable: true, get: function () { return documents_1.InvoiceV3; } });
|
|
8
|
+
Object.defineProperty(exports, "PassportV1", { enumerable: true, get: function () { return documents_1.PassportV1; } });
|
|
9
|
+
Object.defineProperty(exports, "ReceiptV3", { enumerable: true, get: function () { return documents_1.ReceiptV3; } });
|
|
10
|
+
Object.defineProperty(exports, "ReceiptV4", { enumerable: true, get: function () { return documents_1.ReceiptV4; } });
|
|
11
|
+
Object.defineProperty(exports, "CropperV1", { enumerable: true, get: function () { return documents_1.CropperV1; } });
|
|
12
12
|
var client_1 = require("./client");
|
|
13
13
|
Object.defineProperty(exports, "Client", { enumerable: true, get: function () { return client_1.Client; } });
|
|
14
|
+
var inputs_1 = require("./inputs");
|
|
15
|
+
Object.defineProperty(exports, "PageOptionsOperation", { enumerable: true, get: function () { return inputs_1.PageOptionsOperation; } });
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { PageOptions } from "./pageOptions";
|
|
3
|
+
interface InputProps {
|
|
4
|
+
inputType: string;
|
|
5
|
+
}
|
|
6
|
+
export declare const INPUT_TYPE_STREAM = "stream";
|
|
7
|
+
export declare const INPUT_TYPE_BASE64 = "base64";
|
|
8
|
+
export declare const INPUT_TYPE_BYTES = "bytes";
|
|
9
|
+
export declare const INPUT_TYPE_PATH = "path";
|
|
10
|
+
export declare const INPUT_TYPE_URL = "URL";
|
|
11
|
+
export declare class InputSource {
|
|
12
|
+
inputType: string;
|
|
13
|
+
filename: string;
|
|
14
|
+
filepath?: string;
|
|
15
|
+
mimeType: string;
|
|
16
|
+
fileObject: Buffer | string;
|
|
17
|
+
/**
|
|
18
|
+
* @param {String} inputType - the type of input used in file ("base64", "path", "dummy").
|
|
19
|
+
* NB: dummy is only used for tests purposes
|
|
20
|
+
* @param {Boolean} cutPages
|
|
21
|
+
* NB: Because of async calls, init() should be called after creating the object
|
|
22
|
+
*/
|
|
23
|
+
constructor({ inputType }: InputProps);
|
|
24
|
+
init(): Promise<void>;
|
|
25
|
+
isPdf(): boolean;
|
|
26
|
+
checkMimetype(): Promise<string>;
|
|
27
|
+
/**
|
|
28
|
+
* Merge PDF pages.
|
|
29
|
+
* @param pageOptions
|
|
30
|
+
*/
|
|
31
|
+
cutPdf(pageOptions: PageOptions): Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.InputSource = exports.INPUT_TYPE_URL = exports.INPUT_TYPE_PATH = exports.INPUT_TYPE_BYTES = exports.INPUT_TYPE_BASE64 = exports.INPUT_TYPE_STREAM = void 0;
|
|
27
|
+
const fileType = __importStar(require("file-type"));
|
|
28
|
+
const path = __importStar(require("path"));
|
|
29
|
+
const pdf_1 = require("../pdf");
|
|
30
|
+
const logger_1 = require("../logger");
|
|
31
|
+
const handler_1 = require("../errors/handler");
|
|
32
|
+
exports.INPUT_TYPE_STREAM = "stream";
|
|
33
|
+
exports.INPUT_TYPE_BASE64 = "base64";
|
|
34
|
+
exports.INPUT_TYPE_BYTES = "bytes";
|
|
35
|
+
exports.INPUT_TYPE_PATH = "path";
|
|
36
|
+
exports.INPUT_TYPE_URL = "URL";
|
|
37
|
+
const MIMETYPES = new Map([
|
|
38
|
+
[".pdf", "application/pdf"],
|
|
39
|
+
[".heic", "image/heic"],
|
|
40
|
+
[".jpg", "image/jpeg"],
|
|
41
|
+
[".jpeg", "image/jpeg"],
|
|
42
|
+
[".png", "image/png"],
|
|
43
|
+
[".tif", "image/tiff"],
|
|
44
|
+
[".tiff", "image/tiff"],
|
|
45
|
+
[".webp", "image/webp"],
|
|
46
|
+
]);
|
|
47
|
+
const ALLOWED_INPUT_TYPES = [
|
|
48
|
+
exports.INPUT_TYPE_STREAM,
|
|
49
|
+
exports.INPUT_TYPE_BASE64,
|
|
50
|
+
exports.INPUT_TYPE_BYTES,
|
|
51
|
+
exports.INPUT_TYPE_PATH,
|
|
52
|
+
exports.INPUT_TYPE_URL,
|
|
53
|
+
];
|
|
54
|
+
class InputSource {
|
|
55
|
+
/**
|
|
56
|
+
* @param {String} inputType - the type of input used in file ("base64", "path", "dummy").
|
|
57
|
+
* NB: dummy is only used for tests purposes
|
|
58
|
+
* @param {Boolean} cutPages
|
|
59
|
+
* NB: Because of async calls, init() should be called after creating the object
|
|
60
|
+
*/
|
|
61
|
+
constructor({ inputType }) {
|
|
62
|
+
this.filename = "";
|
|
63
|
+
this.mimeType = "";
|
|
64
|
+
// Check if inputType is valid
|
|
65
|
+
if (!ALLOWED_INPUT_TYPES.includes(inputType)) {
|
|
66
|
+
const allowed = Array.from(ALLOWED_INPUT_TYPES.keys()).join(", ");
|
|
67
|
+
handler_1.errorHandler.throw(new Error(`Invalid input type, must be one of ${allowed}.`));
|
|
68
|
+
}
|
|
69
|
+
this.inputType = inputType;
|
|
70
|
+
logger_1.logger.debug(`Loading file from: ${inputType}`);
|
|
71
|
+
}
|
|
72
|
+
async init() {
|
|
73
|
+
throw new Error("not Implemented");
|
|
74
|
+
}
|
|
75
|
+
isPdf() {
|
|
76
|
+
return this.mimeType === "application/pdf";
|
|
77
|
+
}
|
|
78
|
+
async checkMimetype() {
|
|
79
|
+
if (!(this.fileObject instanceof Buffer)) {
|
|
80
|
+
throw new Error(`MIME type cannot be verified on input source of type ${this.inputType}.`);
|
|
81
|
+
}
|
|
82
|
+
let mimeType;
|
|
83
|
+
const fileExt = path.extname(this.filename);
|
|
84
|
+
if (fileExt) {
|
|
85
|
+
mimeType = MIMETYPES.get(fileExt.toLowerCase()) || "";
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
const guess = await fileType.fromBuffer(this.fileObject);
|
|
89
|
+
if (guess !== undefined) {
|
|
90
|
+
mimeType = guess.mime;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
throw "Could not determine the MIME type of the file";
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (!mimeType) {
|
|
97
|
+
const allowed = Array.from(MIMETYPES.keys()).join(", ");
|
|
98
|
+
const err = new Error(`Invalid file type, must be one of ${allowed}.`);
|
|
99
|
+
handler_1.errorHandler.throw(err);
|
|
100
|
+
}
|
|
101
|
+
logger_1.logger.debug(`File is of type: ${mimeType}`);
|
|
102
|
+
return mimeType;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Merge PDF pages.
|
|
106
|
+
* @param pageOptions
|
|
107
|
+
*/
|
|
108
|
+
async cutPdf(pageOptions) {
|
|
109
|
+
if (!(this.fileObject instanceof Buffer)) {
|
|
110
|
+
throw new Error(`Cannot modify an input source of type ${this.inputType}.`);
|
|
111
|
+
}
|
|
112
|
+
const processedPdf = await (0, pdf_1.extractPages)(this.fileObject, pageOptions);
|
|
113
|
+
this.fileObject = processedPdf.file;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.InputSource = InputSource;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { PageOptions, PageOptionsOperation } from "./pageOptions";
|
|
2
|
+
export { Base64Input, BytesInput, PathInput, StreamInput, UrlInput, } from "./sources";
|
|
3
|
+
export { InputSource, INPUT_TYPE_BASE64, INPUT_TYPE_BYTES, INPUT_TYPE_PATH, INPUT_TYPE_STREAM, INPUT_TYPE_URL, } from "./base";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.INPUT_TYPE_URL = exports.INPUT_TYPE_STREAM = exports.INPUT_TYPE_PATH = exports.INPUT_TYPE_BYTES = exports.INPUT_TYPE_BASE64 = exports.InputSource = exports.UrlInput = exports.StreamInput = exports.PathInput = exports.BytesInput = exports.Base64Input = exports.PageOptionsOperation = void 0;
|
|
4
|
+
var pageOptions_1 = require("./pageOptions");
|
|
5
|
+
Object.defineProperty(exports, "PageOptionsOperation", { enumerable: true, get: function () { return pageOptions_1.PageOptionsOperation; } });
|
|
6
|
+
var sources_1 = require("./sources");
|
|
7
|
+
Object.defineProperty(exports, "Base64Input", { enumerable: true, get: function () { return sources_1.Base64Input; } });
|
|
8
|
+
Object.defineProperty(exports, "BytesInput", { enumerable: true, get: function () { return sources_1.BytesInput; } });
|
|
9
|
+
Object.defineProperty(exports, "PathInput", { enumerable: true, get: function () { return sources_1.PathInput; } });
|
|
10
|
+
Object.defineProperty(exports, "StreamInput", { enumerable: true, get: function () { return sources_1.StreamInput; } });
|
|
11
|
+
Object.defineProperty(exports, "UrlInput", { enumerable: true, get: function () { return sources_1.UrlInput; } });
|
|
12
|
+
var base_1 = require("./base");
|
|
13
|
+
Object.defineProperty(exports, "InputSource", { enumerable: true, get: function () { return base_1.InputSource; } });
|
|
14
|
+
Object.defineProperty(exports, "INPUT_TYPE_BASE64", { enumerable: true, get: function () { return base_1.INPUT_TYPE_BASE64; } });
|
|
15
|
+
Object.defineProperty(exports, "INPUT_TYPE_BYTES", { enumerable: true, get: function () { return base_1.INPUT_TYPE_BYTES; } });
|
|
16
|
+
Object.defineProperty(exports, "INPUT_TYPE_PATH", { enumerable: true, get: function () { return base_1.INPUT_TYPE_PATH; } });
|
|
17
|
+
Object.defineProperty(exports, "INPUT_TYPE_STREAM", { enumerable: true, get: function () { return base_1.INPUT_TYPE_STREAM; } });
|
|
18
|
+
Object.defineProperty(exports, "INPUT_TYPE_URL", { enumerable: true, get: function () { return base_1.INPUT_TYPE_URL; } });
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface PageOptions {
|
|
2
|
+
/**
|
|
3
|
+
* Zero-based list of page indexes.
|
|
4
|
+
*
|
|
5
|
+
* A negative index can be used, indicating an offset from the end of the document.
|
|
6
|
+
*
|
|
7
|
+
* [0, -1] represents the fist and last pages of the document.
|
|
8
|
+
*/
|
|
9
|
+
pageIndexes: number[];
|
|
10
|
+
/** Operation to apply on the document, given the `pageIndexes` specified. */
|
|
11
|
+
operation: PageOptionsOperation;
|
|
12
|
+
/**
|
|
13
|
+
* Apply the operation only if document has at least this many pages.
|
|
14
|
+
*/
|
|
15
|
+
onMinPages: number;
|
|
16
|
+
}
|
|
17
|
+
export declare enum PageOptionsOperation {
|
|
18
|
+
KeepOnly = "KEEP_ONLY",
|
|
19
|
+
Remove = "REMOVE"
|
|
20
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PageOptionsOperation = void 0;
|
|
4
|
+
var PageOptionsOperation;
|
|
5
|
+
(function (PageOptionsOperation) {
|
|
6
|
+
PageOptionsOperation["KeepOnly"] = "KEEP_ONLY";
|
|
7
|
+
PageOptionsOperation["Remove"] = "REMOVE";
|
|
8
|
+
})(PageOptionsOperation = exports.PageOptionsOperation || (exports.PageOptionsOperation = {}));
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { ReadStream } from "fs";
|
|
4
|
+
import { Buffer } from "node:buffer";
|
|
5
|
+
import { InputSource } from "./base";
|
|
6
|
+
interface PathInputProps {
|
|
7
|
+
inputPath: string;
|
|
8
|
+
}
|
|
9
|
+
export declare class PathInput extends InputSource {
|
|
10
|
+
readonly inputPath: string;
|
|
11
|
+
fileObject: Buffer;
|
|
12
|
+
constructor({ inputPath }: PathInputProps);
|
|
13
|
+
init(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
interface Base64InputProps {
|
|
16
|
+
inputString: string;
|
|
17
|
+
filename: string;
|
|
18
|
+
}
|
|
19
|
+
export declare class Base64Input extends InputSource {
|
|
20
|
+
private inputString;
|
|
21
|
+
fileObject: Buffer;
|
|
22
|
+
constructor({ inputString, filename }: Base64InputProps);
|
|
23
|
+
init(): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
interface StreamInputProps {
|
|
26
|
+
inputStream: ReadStream;
|
|
27
|
+
filename: string;
|
|
28
|
+
}
|
|
29
|
+
export declare class StreamInput extends InputSource {
|
|
30
|
+
private readonly inputStream;
|
|
31
|
+
fileObject: Buffer;
|
|
32
|
+
constructor({ inputStream, filename }: StreamInputProps);
|
|
33
|
+
init(): Promise<void>;
|
|
34
|
+
stream2buffer(stream: ReadStream): Promise<Buffer>;
|
|
35
|
+
}
|
|
36
|
+
interface BytesInputProps {
|
|
37
|
+
inputBytes: string;
|
|
38
|
+
filename: string;
|
|
39
|
+
}
|
|
40
|
+
export declare class BytesInput extends InputSource {
|
|
41
|
+
private inputBytes;
|
|
42
|
+
fileObject: Buffer;
|
|
43
|
+
constructor({ inputBytes, filename }: BytesInputProps);
|
|
44
|
+
init(): Promise<void>;
|
|
45
|
+
}
|
|
46
|
+
interface UrlInputProps {
|
|
47
|
+
url: string;
|
|
48
|
+
}
|
|
49
|
+
export declare class UrlInput extends InputSource {
|
|
50
|
+
private readonly url;
|
|
51
|
+
fileObject: string;
|
|
52
|
+
constructor({ url }: UrlInputProps);
|
|
53
|
+
init(): Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
export {};
|