@veryfi/veryfi-sdk 1.2.3 → 1.2.4
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/lib/main.js +63 -15
- package/lib/types/main.d.ts +53 -0
- package/package.json +2 -1
package/lib/main.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
const {createHmac} = require('crypto');
|
|
2
2
|
const axios = require('axios').default;
|
|
3
3
|
const path = require('path');
|
|
4
|
+
const FormData = require('form-data');
|
|
4
5
|
const fs = require('fs');
|
|
6
|
+
const {promisify} = require("util");
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* Create instance of a Client
|
|
@@ -21,12 +23,13 @@ function Client(
|
|
|
21
23
|
api_key,
|
|
22
24
|
base_url = "https://api.veryfi.com/",
|
|
23
25
|
api_version = "v8",
|
|
24
|
-
timeout = 120
|
|
26
|
+
timeout = 120) {
|
|
25
27
|
this.client_id = client_id;
|
|
26
28
|
this.client_secret = client_secret;
|
|
27
29
|
this.username = username;
|
|
28
30
|
this.api_key = api_key;
|
|
29
31
|
this.base_url = base_url;
|
|
32
|
+
this.timeout = timeout;
|
|
30
33
|
this.api_version = api_version;
|
|
31
34
|
this.CATEGORIES = [
|
|
32
35
|
"Advertising & Marketing",
|
|
@@ -64,7 +67,7 @@ Client.prototype._check_w2_version = function () {
|
|
|
64
67
|
*/
|
|
65
68
|
Client.prototype._get_headers = function (has_files = false) {
|
|
66
69
|
let final_headers = {
|
|
67
|
-
"User-Agent": "Node.js Veryfi-Nodejs/1.2.
|
|
70
|
+
"User-Agent": "Node.js Veryfi-Nodejs/1.2.4",
|
|
68
71
|
"Accept": "application/json",
|
|
69
72
|
"Content-Type": "application/json",
|
|
70
73
|
"Client-Id": this.client_id,
|
|
@@ -117,11 +120,10 @@ Client.prototype._generate_signature = function (payload_params, timestamp) {
|
|
|
117
120
|
* @param {string} endpoint_name Endpoint name such as 'documents', 'users', etc.
|
|
118
121
|
* @param {{}} request_arguments JSON payload to send to Veryfi
|
|
119
122
|
* @param params {{}} query params.
|
|
120
|
-
* @param
|
|
123
|
+
* @param {Boolean} has_files Are there any files to be submitted as binary
|
|
121
124
|
* @returns {JSON} A JSON of the response data.
|
|
122
125
|
*/
|
|
123
|
-
Client.prototype._request = async function (http_verb, endpoint_name, request_arguments, params = {},
|
|
124
|
-
let has_files = (Boolean(file_stream));
|
|
126
|
+
Client.prototype._request = async function (http_verb, endpoint_name, request_arguments, params = {}, has_files = false) {
|
|
125
127
|
let headers = this._get_headers(has_files);
|
|
126
128
|
let api_url = `${this._get_url()}/partner${endpoint_name}`;
|
|
127
129
|
|
|
@@ -134,12 +136,21 @@ Client.prototype._request = async function (http_verb, endpoint_name, request_ar
|
|
|
134
136
|
});
|
|
135
137
|
}
|
|
136
138
|
|
|
137
|
-
if (has_files
|
|
138
|
-
|
|
139
|
+
if (has_files) {
|
|
140
|
+
const formData = new FormData();
|
|
141
|
+
for (const key in request_arguments) {
|
|
142
|
+
if (request_arguments.hasOwnProperty(key)) {
|
|
143
|
+
const value = request_arguments[key];
|
|
144
|
+
formData.append(key, value);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
headers = Object.assign(headers, formData.getHeaders());
|
|
148
|
+
const getLength = promisify(formData.getLength).bind(formData);
|
|
149
|
+
headers['Content-Length'] = await getLength()
|
|
150
|
+
request_arguments = formData;
|
|
139
151
|
} else {
|
|
140
|
-
|
|
152
|
+
request_arguments = JSON.stringify(request_arguments);
|
|
141
153
|
}
|
|
142
|
-
|
|
143
154
|
try {
|
|
144
155
|
return await axios.request({
|
|
145
156
|
method: http_verb,
|
|
@@ -150,9 +161,10 @@ Client.prototype._request = async function (http_verb, endpoint_name, request_ar
|
|
|
150
161
|
params: params
|
|
151
162
|
});
|
|
152
163
|
} catch (response) {
|
|
153
|
-
let errorStatus = response
|
|
154
|
-
let errorInfo = response
|
|
155
|
-
|
|
164
|
+
let errorStatus = response?.response?.status;
|
|
165
|
+
let errorInfo = response?.response?.data?.error;
|
|
166
|
+
let message = response?.message;
|
|
167
|
+
throw new Error(`Error: ${errorStatus} ${errorInfo} ${message}`);
|
|
156
168
|
}
|
|
157
169
|
}
|
|
158
170
|
|
|
@@ -228,21 +240,57 @@ Client.prototype.process_document = async function (
|
|
|
228
240
|
/**
|
|
229
241
|
* Process a document and extract all the fields from it
|
|
230
242
|
* @example
|
|
231
|
-
* veryfi_client.
|
|
243
|
+
* veryfi_client.process_document_stream('ReadStream',
|
|
244
|
+
* 'receipt.png',
|
|
245
|
+
* ['Entertainment','Food'],
|
|
246
|
+
* true,
|
|
247
|
+
* {'extra': 'parameters'})
|
|
248
|
+
*
|
|
249
|
+
* @memberof Client
|
|
250
|
+
* @param {ReadStream} file ReadStream of a file to submit for data extraction
|
|
251
|
+
* @param {String} file_name The file name including the extension
|
|
252
|
+
* @param {Array} categories List of categories Veryfi can use to categorize the document
|
|
253
|
+
* @param {Boolean} delete_after_processing Delete this document from Veryfi after data has been extracted
|
|
254
|
+
* @param {Object} kwargs Additional request parameters
|
|
255
|
+
* @returns {JSON} Data extracted from the document
|
|
256
|
+
*/
|
|
257
|
+
Client.prototype.process_document_stream = async function (
|
|
258
|
+
file,
|
|
259
|
+
file_name,
|
|
260
|
+
delete_after_processing = false,
|
|
261
|
+
{...kwargs} = {}
|
|
262
|
+
) {
|
|
263
|
+
|
|
264
|
+
let endpoint_name = "/documents/";
|
|
265
|
+
|
|
266
|
+
let request_arguments = {
|
|
267
|
+
"file_name": file_name,
|
|
268
|
+
"file": file,
|
|
269
|
+
"auto_delete": delete_after_processing.toString(),
|
|
270
|
+
};
|
|
271
|
+
request_arguments = Object.assign(request_arguments, kwargs);
|
|
272
|
+
let document = await this._request("POST", endpoint_name, request_arguments, {}, true);
|
|
273
|
+
return document['data'];
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Process a document and extract all the fields from it
|
|
278
|
+
* @example
|
|
279
|
+
* veryfi_client.process_document_base64string('base64_encoded_string',
|
|
232
280
|
* 'receipt.png',
|
|
233
281
|
* ['Entertainment','Food'],
|
|
234
282
|
* true,
|
|
235
283
|
* {'extra': 'parameters'})
|
|
236
284
|
*
|
|
237
285
|
* @memberof Client
|
|
238
|
-
* @param {String} base64_encoded_string Buffer of a file to submit for data extraction
|
|
286
|
+
* @param {String} base64_encoded_string Buffer string of a file to submit for data extraction
|
|
239
287
|
* @param {String} file_name The file name including the extension
|
|
240
288
|
* @param {Array} categories List of categories Veryfi can use to categorize the document
|
|
241
289
|
* @param {Boolean} delete_after_processing Delete this document from Veryfi after data has been extracted
|
|
242
290
|
* @param {Object} kwargs Additional request parameters
|
|
243
291
|
* @returns {JSON} Data extracted from the document
|
|
244
292
|
*/
|
|
245
|
-
Client.prototype.
|
|
293
|
+
Client.prototype.process_document_base64string = async function (
|
|
246
294
|
base64_encoded_string,
|
|
247
295
|
file_name,
|
|
248
296
|
categories = null,
|
package/lib/types/main.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Additional request parameters type
|
|
3
5
|
*/
|
|
@@ -44,6 +46,7 @@ export declare type VeryfiDocument = {
|
|
|
44
46
|
payment_display_name?: null | string;
|
|
45
47
|
payment_terms?: null | string;
|
|
46
48
|
payment_type?: null | string;
|
|
49
|
+
pdf_url?: null | string;
|
|
47
50
|
phone_number?: null | string;
|
|
48
51
|
purchase_order_number?: null | string;
|
|
49
52
|
rounding?: null | number;
|
|
@@ -186,6 +189,56 @@ export declare class Client {
|
|
|
186
189
|
*/
|
|
187
190
|
public get_document(document_id: string): Promise<VeryfiDocument>;
|
|
188
191
|
|
|
192
|
+
/**
|
|
193
|
+
* Process a document and extract all the fields from it
|
|
194
|
+
* @example
|
|
195
|
+
* veryfi_client.process_document_buffer('buffer',
|
|
196
|
+
* 'receipt.png',
|
|
197
|
+
* ['Entertainment','Food'],
|
|
198
|
+
* true,
|
|
199
|
+
* {'extra': 'parameters'})
|
|
200
|
+
*
|
|
201
|
+
* @memberof Client
|
|
202
|
+
* @param {ReadStream} file ReadStream of a file to submit for data extraction
|
|
203
|
+
* @param {String} file_name The file name including the extension
|
|
204
|
+
* @param {Array} categories List of categories Veryfi can use to categorize the document
|
|
205
|
+
* @param {Boolean} delete_after_processing Delete this document from Veryfi after data has been extracted
|
|
206
|
+
* @param {Object} kwargs Additional request parameters
|
|
207
|
+
* @returns {JSON} Data extracted from the document
|
|
208
|
+
*/
|
|
209
|
+
public process_document_stream(
|
|
210
|
+
file: fs.ReadStream,
|
|
211
|
+
file_name: string,
|
|
212
|
+
categories?: string[],
|
|
213
|
+
delete_after_processing?: boolean,
|
|
214
|
+
{ ...kwargs }?: VeryfiExtraArgs
|
|
215
|
+
): Promise<VeryfiDocument>;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Process a document and extract all the fields from it
|
|
219
|
+
* @example
|
|
220
|
+
* veryfi_client.process_document_buffer_string('base64_encoded_string',
|
|
221
|
+
* 'receipt.png',
|
|
222
|
+
* ['Entertainment','Food'],
|
|
223
|
+
* true,
|
|
224
|
+
* {'extra': 'parameters'})
|
|
225
|
+
*
|
|
226
|
+
* @memberof Client
|
|
227
|
+
* @param {String} base64_encoded_string Buffer string of a file to submit for data extraction
|
|
228
|
+
* @param {String} file_name The file name including the extension
|
|
229
|
+
* @param {Array} categories List of categories Veryfi can use to categorize the document
|
|
230
|
+
* @param {Boolean} delete_after_processing Delete this document from Veryfi after data has been extracted
|
|
231
|
+
* @param {Object} kwargs Additional request parameters
|
|
232
|
+
* @returns {JSON} Data extracted from the document
|
|
233
|
+
*/
|
|
234
|
+
public process_document_base64string(
|
|
235
|
+
base64_encoded_string: string,
|
|
236
|
+
file_name: string,
|
|
237
|
+
categories?: string[],
|
|
238
|
+
delete_after_processing?: boolean,
|
|
239
|
+
{ ...kwargs }?: VeryfiExtraArgs
|
|
240
|
+
): Promise<VeryfiDocument>;
|
|
241
|
+
|
|
189
242
|
/**
|
|
190
243
|
* Process a document and extract all the fields from it
|
|
191
244
|
* @example
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veryfi/veryfi-sdk",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"description": "Node.js module for communicating with the Veryfi OCR API",
|
|
5
5
|
"main": "lib/main.js",
|
|
6
6
|
"typings": "lib/types/main.d.ts",
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"@jest/globals": "^29.0.3",
|
|
36
36
|
"axios": "^0.21.1",
|
|
37
37
|
"docdash": "^1.2.0",
|
|
38
|
+
"form-data": "^4.0.0",
|
|
38
39
|
"jest-config": "^29.0.3"
|
|
39
40
|
},
|
|
40
41
|
"repository": {
|