ocr-space-api-wrapper
Advanced tools
Comparing version
@@ -5,5 +5,6 @@ { | ||
"es2021": true, | ||
"node": true | ||
"node": true, | ||
"mocha": true | ||
}, | ||
"extends": "google", | ||
"extends": ["eslint:recommended", "google"], | ||
"parserOptions": { | ||
@@ -14,3 +15,3 @@ "ecmaVersion": 12 | ||
"max-len": [ | ||
"error", 100 | ||
"error", 120 | ||
], | ||
@@ -17,0 +18,0 @@ "object-curly-spacing": [ |
36
index.js
@@ -20,2 +20,3 @@ const fs = require('fs'); | ||
* @param {object} options Options | ||
* @return {object} OCR results | ||
*/ | ||
@@ -32,24 +33,24 @@ async function ocrSpace(input, options = {}) { | ||
} = options; | ||
const data = new FormData(); | ||
const formData = new FormData(); | ||
const detectedInput = detectInput(input); | ||
switch (detectedInput) { | ||
case 'file': | ||
data.append('file', fs.createReadStream(input)); | ||
formData.append('file', fs.createReadStream(input)); | ||
break; | ||
case 'url': | ||
case 'base64Image': | ||
data.append(detectedInput, input); | ||
formData.append(detectedInput, input); | ||
break; | ||
} | ||
data.append('language', String(language || 'eng')); | ||
data.append('isOverlayRequired', String(isOverlayRequired || 'false')); | ||
formData.append('language', String(language || 'eng')); | ||
formData.append('isOverlayRequired', String(isOverlayRequired || 'false')); | ||
if (filetype) { | ||
data.append('filetype', String(filetype)); | ||
formData.append('filetype', String(filetype)); | ||
} | ||
data.append('detectOrientation', String(detectOrientation || 'false')); | ||
data.append('isCreateSearchablePdf', String(isCreateSearchablePdf || 'false')); | ||
data.append('isSearchablePdfHideTextLayer', String(isSearchablePdfHideTextLayer || 'false')); | ||
data.append('scale', String(scale || 'false')); | ||
data.append('isTable', String(isTable || 'false')); | ||
data.append('OCREngine', String(OCREngine || '1')); | ||
formData.append('detectOrientation', String(detectOrientation || 'false')); | ||
formData.append('isCreateSearchablePdf', String(isCreateSearchablePdf || 'false')); | ||
formData.append('isSearchablePdfHideTextLayer', String(isSearchablePdfHideTextLayer || 'false')); | ||
formData.append('scale', String(scale || 'false')); | ||
formData.append('isTable', String(isTable || 'false')); | ||
formData.append('OCREngine', String(OCREngine || '1')); | ||
const request = { | ||
@@ -60,11 +61,10 @@ method: 'POST', | ||
apikey: String(apiKey || 'helloworld'), | ||
...data.getHeaders(), | ||
...formData.getHeaders(), | ||
}, | ||
data, | ||
data: formData, | ||
maxContentLength: Infinity, | ||
maxBodyLength: Infinity, | ||
}; | ||
const response = await axios(request); | ||
// console.log(response.data) | ||
return response.data; | ||
const { data } = await axios(request); | ||
return data; | ||
} catch (error) { | ||
@@ -75,2 +75,2 @@ console.error(error); | ||
module.exports = ocrSpace; | ||
exports.ocrSpace = ocrSpace; |
{ | ||
"name": "ocr-space-api-wrapper", | ||
"version": "1.0.7", | ||
"version": "2.1.3", | ||
"description": "Node.js wrapper for ocr.space APIs.", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node index", | ||
"lint": "eslint ./" | ||
"start": "node index.js", | ||
"lint": "eslint ./", | ||
"test": "nyc mocha --timeout 60000 --bail", | ||
"coverage": "nyc --reporter=lcov --reporter=text mocha --timeout 60000 --bail" | ||
}, | ||
@@ -21,3 +23,5 @@ "repository": { | ||
"nodejs", | ||
"optical character recognition" | ||
"optical character recognition", | ||
"pdf", | ||
"image" | ||
], | ||
@@ -31,9 +35,11 @@ "author": "Davide Violante", | ||
"dependencies": { | ||
"axios": "^0.23.0", | ||
"axios": "^1.1.3", | ||
"form-data": "^4.0.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^8.0.1", | ||
"eslint-config-google": "^0.14.0" | ||
"eslint": "^8.12.0", | ||
"eslint-config-google": "^0.14.0", | ||
"mocha": "^10.0.0", | ||
"nyc": "^15.1.0" | ||
} | ||
} |
# OCR.Space Node.js API wrapper | ||
 [](https://www.paypal.me/dviolante) | ||
[](https://github.com/DavideViolante/ocr-space-api-wrapper/actions?query=workflow%3A"Node.js+CI") [](https://coveralls.io/github/DavideViolante/ocr-space-api-wrapper?branch=master)  [](https://www.paypal.me/dviolante) | ||
@@ -13,16 +13,16 @@ [](https://nodei.co/npm/ocr-space-api-wrapper/) | ||
```js | ||
const ocrSpace = require('ocr-space-api-wrapper'); | ||
const { ocrSpace } = require('ocr-space-api-wrapper'); | ||
async function main () { | ||
try { | ||
// Using the OCR.space default free token + remote file | ||
// Using the OCR.space default free API key (max 10reqs in 10mins) + remote file | ||
const res1 = await ocrSpace('http://dl.a9t9.com/ocrbenchmark/eng.png'); | ||
// Using your personal token + local file | ||
// Using your personal API key + local file | ||
const res2 = await ocrSpace('/path/to/file.pdf', { apiKey: '<API_KEY_HERE>' }); | ||
// Using your personal token + base64 image + custom language | ||
// Using your personal API key + base64 image + custom language | ||
const res3 = await ocrSpace('data:image/png;base64...', { apiKey: '<API_KEY_HERE>', language: 'ita' }); | ||
} catch (error) { | ||
console.log(error); | ||
console.error(error); | ||
} | ||
@@ -41,3 +41,3 @@ } | ||
This param is an object with the following keys: | ||
- `apiKey`: your API key for [ocr.space APIs](https://ocr.space/ocrapi). | ||
- `apiKey`: your API key for [ocr.space APIs](https://ocr.space/ocrapi). Default API key has a limit of max 10reqs in 10mins. | ||
- `ocrUrl`: a different URL for ocr.space APIs, for example when you purchase the PRO plan. | ||
@@ -49,2 +49,8 @@ - All other params as documented in the [official website](https://ocr.space/OCRAPI#PostParameters). | ||
## Run lint | ||
`npm run lint` | ||
## Run tests | ||
`npm test` | ||
## Bug or feedback | ||
@@ -51,0 +57,0 @@ Please open a new issue. |
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
455329
6692.91%12
100%1146
1187.64%58
11.54%4
100%2
100%+ Added
+ Added
- Removed
Updated