@veryfi/veryfi-sdk 1.5.1 → 1.5.2
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/client/request.js +56 -22
- package/package.json +1 -5
package/lib/client/request.js
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
const Client = require('../client/constructor');
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
const { Readable } = require('stream');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Convert a Node.js Readable stream to a Buffer.
|
|
6
|
+
* @private
|
|
7
|
+
* @param {stream.Readable} stream
|
|
8
|
+
* @returns {Promise<Buffer>}
|
|
9
|
+
*/
|
|
10
|
+
async function streamToBuffer(stream) {
|
|
11
|
+
const chunks = [];
|
|
12
|
+
for await (const chunk of stream) {
|
|
13
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
14
|
+
}
|
|
15
|
+
return Buffer.concat(chunks);
|
|
16
|
+
}
|
|
5
17
|
|
|
6
18
|
/**
|
|
7
19
|
* Submit the HTTP request.
|
|
@@ -22,6 +34,15 @@ Client.prototype._request = async function (http_verb, endpoint_name, request_ar
|
|
|
22
34
|
let partner = (options && options.skip_partner) ? "" : "/partner";
|
|
23
35
|
let api_url = `${this.base_url}api/${api_version}${partner}${endpoint_name}`;
|
|
24
36
|
|
|
37
|
+
if (params && Object.keys(params).length > 0) {
|
|
38
|
+
const query = new URLSearchParams(
|
|
39
|
+
Object.entries(params).filter(([, v]) => v != null).map(([k, v]) => [k, String(v)])
|
|
40
|
+
).toString();
|
|
41
|
+
if (query) {
|
|
42
|
+
api_url += `?${query}`;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
25
46
|
if (this.client_secret) {
|
|
26
47
|
let timestamp = Date.now();
|
|
27
48
|
let signature = this._generate_signature(request_arguments, timestamp);
|
|
@@ -31,33 +52,46 @@ Client.prototype._request = async function (http_verb, endpoint_name, request_ar
|
|
|
31
52
|
});
|
|
32
53
|
}
|
|
33
54
|
|
|
55
|
+
let body;
|
|
34
56
|
if (has_files) {
|
|
35
57
|
const formData = new FormData();
|
|
36
|
-
|
|
37
|
-
if (value
|
|
38
|
-
|
|
58
|
+
for (const [key, value] of Object.entries(request_arguments)) {
|
|
59
|
+
if (value == null) continue;
|
|
60
|
+
if (key === "file") {
|
|
61
|
+
const buf = value instanceof Readable ? await streamToBuffer(value) : Buffer.from(value);
|
|
62
|
+
formData.append(key, new Blob([buf]));
|
|
63
|
+
} else {
|
|
64
|
+
formData.append(key, value.toString());
|
|
39
65
|
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
request_arguments = formData;
|
|
66
|
+
}
|
|
67
|
+
// Let fetch set Content-Type with boundary automatically
|
|
68
|
+
delete headers["Content-Type"];
|
|
69
|
+
body = formData;
|
|
45
70
|
} else {
|
|
46
|
-
|
|
71
|
+
body = JSON.stringify(request_arguments);
|
|
47
72
|
}
|
|
73
|
+
|
|
74
|
+
const controller = new AbortController();
|
|
75
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout * 1000);
|
|
76
|
+
|
|
48
77
|
try {
|
|
49
|
-
|
|
78
|
+
const response = await fetch(api_url, {
|
|
50
79
|
method: http_verb,
|
|
51
|
-
maxBodyLength: Infinity,
|
|
52
|
-
url: api_url,
|
|
53
80
|
headers: headers,
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
params: params
|
|
81
|
+
body: ["GET", "HEAD"].includes(http_verb.toUpperCase()) ? undefined : body,
|
|
82
|
+
signal: controller.signal,
|
|
57
83
|
});
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
84
|
+
const data = await response.json();
|
|
85
|
+
if (!response.ok) {
|
|
86
|
+
throw new Error(`Error: ${response.status} ${JSON.stringify(data)}`);
|
|
87
|
+
}
|
|
88
|
+
return { data };
|
|
89
|
+
} catch (err) {
|
|
90
|
+
if (err.name === 'AbortError') {
|
|
91
|
+
throw new Error(`Error: Request timed out after ${this.timeout} seconds`);
|
|
92
|
+
}
|
|
93
|
+
throw err;
|
|
94
|
+
} finally {
|
|
95
|
+
clearTimeout(timeoutId);
|
|
62
96
|
}
|
|
63
97
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veryfi/veryfi-sdk",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.2",
|
|
4
4
|
"description": "Node.js module for communicating with the Veryfi OCR API",
|
|
5
5
|
"main": "lib/client/client.js",
|
|
6
6
|
"typings": "lib/types/Client.ts",
|
|
@@ -34,10 +34,6 @@
|
|
|
34
34
|
"ts-jest": "^29.4.12",
|
|
35
35
|
"typescript": "^5.9.3"
|
|
36
36
|
},
|
|
37
|
-
"dependencies": {
|
|
38
|
-
"axios": "^1.19.0",
|
|
39
|
-
"form-data": "^4.0.6"
|
|
40
|
-
},
|
|
41
37
|
"repository": {
|
|
42
38
|
"type": "git",
|
|
43
39
|
"url": "https://github.com/veryfi/veryfi-nodejs"
|