@veryfi/veryfi-sdk 1.5.1 → 1.5.3

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/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  <img src="https://user-images.githubusercontent.com/30125790/212157461-58bdc714-2f89-44c2-8e4d-d42bee74854e.png#gh-dark-mode-only" width="200">
4
4
  <img src="https://user-images.githubusercontent.com/30125790/212157486-bfd08c5d-9337-4b78-be6f-230dc63838ba.png#gh-light-mode-only" width="200">
5
5
 
6
- [![Node.js - version](https://img.shields.io/badge/node-%3E%3D%206.0.0-brightgreen)](https://www.npmjs.com/package/@veryfi/veryfi-sdk)
6
+ [![Node.js - version](https://img.shields.io/badge/node-%3E%3D26.12.1-brightgreen)](https://www.npmjs.com/package/@veryfi/veryfi-sdk)
7
7
  [![npm](https://img.shields.io/badge/npm-v7.0.0-blue)](https://www.npmjs.com/package/@veryfi/veryfi-sdk)
8
8
  [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
9
9
 
@@ -1,7 +1,19 @@
1
1
  const Client = require('../client/constructor');
2
- const axios = require('axios').default;
3
- const FormData = require('form-data');
4
- const {promisify} = require("util");
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
- Object.entries(request_arguments).forEach(([key, value]) => {
37
- if (value != null) {
38
- formData.append(key, key === "file" ? value : value.toString());
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
- headers = Object.assign(headers, formData.getHeaders());
42
- const getLength = promisify(formData.getLength).bind(formData);
43
- headers['Content-Length'] = await getLength()
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
- request_arguments = JSON.stringify(request_arguments);
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
- return await axios.request({
78
+ const response = await fetch(api_url, {
50
79
  method: http_verb,
51
- maxBodyLength: Infinity,
52
- url: api_url,
53
80
  headers: headers,
54
- data: request_arguments,
55
- timeout: this.timeout * 1000,
56
- params: params
81
+ body: ["GET", "HEAD"].includes(http_verb.toUpperCase()) ? undefined : body,
82
+ signal: controller.signal,
57
83
  });
58
- } catch (response) {
59
- let errorStatus = response?.response?.status;
60
- let errorInfo = response?.response?.data;
61
- throw new Error(`Error: ${errorStatus} ${JSON.stringify(errorInfo)}`);
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,9 @@
1
1
  {
2
2
  "name": "@veryfi/veryfi-sdk",
3
- "version": "1.5.1",
3
+ "version": "1.5.3",
4
+ "engines": {
5
+ "node": ">=26.12.1"
6
+ },
4
7
  "description": "Node.js module for communicating with the Veryfi OCR API",
5
8
  "main": "lib/client/client.js",
6
9
  "typings": "lib/types/Client.ts",
@@ -34,10 +37,6 @@
34
37
  "ts-jest": "^29.4.12",
35
38
  "typescript": "^5.9.3"
36
39
  },
37
- "dependencies": {
38
- "axios": "^1.19.0",
39
- "form-data": "^4.0.6"
40
- },
41
40
  "repository": {
42
41
  "type": "git",
43
42
  "url": "https://github.com/veryfi/veryfi-nodejs"