Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"ts"
],
"homepage": "https://github.com/anyparser/anyparserjs",
"version": "1.0.0",
"version": "1.0.1",
"main": "dist/index.js",
"exports": {
".": {
Expand Down Expand Up @@ -80,7 +80,7 @@
"test": "vitest run",
"coverage": "vitest run --coverage",
"check:state": "git diff --quiet && git diff --cached --quiet || { echo 'Git working directory is not clean. Please commit or stash changes before publishing.'; exit 1; }",
"check:branch": "if [ $(git symbolic-ref --short HEAD) != 'master' ]; then echo 'You must be on the master branch to publish.'; exit 1; fi",
"check:branch": "if [[ $(git symbolic-ref --short HEAD) != 'master' && ! $(git symbolic-ref --short HEAD) =~ ^release/ ]]; then echo 'You must be on the master branch or a release branch (release/...).'; exit 1; fi",
"prepublishOnly": "npm run check:state && npm run check:branch && npm run build"
},
"engines": {
Expand Down
3 changes: 2 additions & 1 deletion src/anyparser.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { OcrPresetType, OcrLanguageType } from './config/hardcoded'
import type { OcrPresetType, OcrLanguageType } from './config/hardcoded.ts'
export type AnyparserFormatType = 'json' | 'markdown' | 'html'
export type AnyparserModelType = 'text' | 'ocr' | 'vlm' | 'lam' | 'crawler'
export type AnyparserEncodingType = 'utf-8' | 'latin1'
Expand Down Expand Up @@ -117,3 +117,4 @@ export interface AnyparserCrawlResult {

export type AnyparserResult = AnyparserCrawlResult | AnyparserPdfResult | AnyparserResultBase
export type Result = AnyparserResult[] | string
export type { OcrPresetType, OcrLanguageType }
11 changes: 6 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
* Anyparser main entry point
* @module anyparser
*/
import { ANYPARSER_VERSION, version } from './version.ts'
import { OCR_LANGUAGES, OCR_PRESETS } from './config/hardcoded.ts'
import { Anyparser } from './parser.ts'
import type { OcrLanguageType, OcrPresetType, AnyparserOption, AnyparserImageReference, AnyparserResultBase, AnyparserCrawlDirectiveBase, AnyparserCrawlDirective, AnyparserUrl, AnyparserRobotsTxtDirective, AnyparserPdfPage, AnyparserPdfResult, AnyparserCrawlResult, AnyparserResult, Result } from './anyparser.d.ts'

export * from './parser.ts'

export { OCR_LANGUAGES, OCR_PRESETS } from './config/hardcoded.ts'

export type { OcrLanguageType, OcrPresetType, AnyparserOption, AnyparserImageReference, AnyparserResultBase, AnyparserCrawlDirectiveBase, AnyparserCrawlDirective, AnyparserUrl, AnyparserRobotsTxtDirective, AnyparserPdfPage, AnyparserPdfResult, AnyparserCrawlResult, AnyparserResult, Result } from './anyparser.d.ts'
export { version, ANYPARSER_VERSION, OCR_LANGUAGES, OCR_PRESETS, Anyparser }
export type { OcrLanguageType, OcrPresetType, AnyparserOption, AnyparserImageReference, AnyparserResultBase, AnyparserCrawlDirectiveBase, AnyparserCrawlDirective, AnyparserUrl, AnyparserRobotsTxtDirective, AnyparserPdfPage, AnyparserPdfResult, AnyparserCrawlResult, AnyparserResult, Result }
11 changes: 9 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { wrappedFetch } from '@src/utils/fetcher.ts'
import type { AnyparserOption, Result } from '@anyparser/core'
import { ANYPARSER_VERSION } from '@anyparser/core'
import { buildForm } from './form.ts'
import { validateAndParse } from './validator/index.ts'
import { transformToCamel } from '@src/utils/camel-case.ts'

import type { AnyparserOption, Result } from '@anyparser/core'

/**
* Main class for parsing items using the Anyparser API.
*/
Expand Down Expand Up @@ -34,7 +36,12 @@ export class Anyparser {
method: 'POST',
body: formData,
headers: {
...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {})
...(apiKey ?
{
Authorization: `Bearer ${apiKey}`,
'User-Agent': `@anyparser/core@${ANYPARSER_VERSION}` // eslint-disable-line @typescript-eslint/naming-convention
} :
{})
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ANYPARSER_VERSION = '1.0.1'
export const version = ANYPARSER_VERSION
14 changes: 12 additions & 2 deletions tests/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Anyparser } from '@src/parser.ts'
import * as validator from '@src/validator/index.ts'
import * as form from '@src/form.ts'
import { wrappedFetch } from '@src/utils/fetcher.ts'
import { ANYPARSER_VERSION } from '@src/version.ts'

// Mock dependencies
vi.mock('@src/validator')
Expand Down Expand Up @@ -53,7 +54,10 @@ describe('Anyparser', () => {
{
method: 'POST',
body: mockFormData,
headers: { Authorization: `Bearer ${mockApiKey}` }
headers: {
Authorization: `Bearer ${mockApiKey}`,
'User-Agent': `@anyparser/core@${ANYPARSER_VERSION}` // eslint-disable-line @typescript-eslint/naming-convention
}
}
)

Expand All @@ -75,6 +79,7 @@ describe('Anyparser', () => {
model: 'text',
encoding: 'utf-8'
})

mockBuildForm.mockReturnValue(mockFormData)
mockWrappedFetch.mockResolvedValue(mockResponse)

Expand All @@ -83,14 +88,19 @@ describe('Anyparser', () => {

expect(mockValidateAndParse).toHaveBeenCalledWith('test.pdf', mockOptions)
expect(mockBuildForm).toHaveBeenCalled()

expect(mockWrappedFetch).toHaveBeenCalledWith(
new URL('/parse/v1', mockApiUrl),
{
method: 'POST',
body: mockFormData,
headers: { Authorization: `Bearer ${mockApiKey}` }
headers: {
Authorization: `Bearer ${mockApiKey}`,
'User-Agent': `@anyparser/core@${ANYPARSER_VERSION}` // eslint-disable-line @typescript-eslint/naming-convention
}
}
)

expect(result).toBe(mockTextResponse)
})

Expand Down
7 changes: 5 additions & 2 deletions tests/validator/option.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, test, describe } from 'vitest'
import { validateOption } from '@src/validator/option.ts'
import { OCR_LANGUAGES, OCR_PRESETS } from '@src/config/hardcoded.ts'
import type { AnyparserOption } from '@src/anyparser.js'

describe('validateOption', () => {
test('should validate options with apiUrl', () => {
Expand All @@ -21,7 +22,8 @@ describe('validateOption', () => {
const validOption = {
apiUrl: new URL('https://api.example.com'),
ocrLanguage: [Object.values(OCR_LANGUAGES)[0]]
}
} as AnyparserOption

expect(() => validateOption(validOption)).not.toThrow()
})

Expand Down Expand Up @@ -54,7 +56,8 @@ describe('validateOption', () => {
apiUrl: new URL('https://api.example.com'),
ocrLanguage: [Object.values(OCR_LANGUAGES)[0]],
ocrPreset: Object.values(OCR_PRESETS)[0]
}
} as AnyparserOption

expect(() => validateOption(validOption)).not.toThrow()
})
})