From f8bbd74c6f1ccbd50cabb3276daaf1f10aa20193 Mon Sep 17 00:00:00 2001 From: John Doe Date: Wed, 26 Feb 2025 13:40:10 +0530 Subject: [PATCH 1/3] refactor: update type imports and enhance option validation in tests --- src/anyparser.d.ts | 3 ++- tests/validator/option.spec.ts | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/anyparser.d.ts b/src/anyparser.d.ts index a7aee36..d2d1bdd 100644 --- a/src/anyparser.d.ts +++ b/src/anyparser.d.ts @@ -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' @@ -117,3 +117,4 @@ export interface AnyparserCrawlResult { export type AnyparserResult = AnyparserCrawlResult | AnyparserPdfResult | AnyparserResultBase export type Result = AnyparserResult[] | string +export type { OcrPresetType, OcrLanguageType } diff --git a/tests/validator/option.spec.ts b/tests/validator/option.spec.ts index 29c6bad..8bf4273 100644 --- a/tests/validator/option.spec.ts +++ b/tests/validator/option.spec.ts @@ -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', () => { @@ -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() }) @@ -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() }) }) From 2e6c455521d538eb0a91100e82734557fc24b3c7 Mon Sep 17 00:00:00 2001 From: John Doe Date: Wed, 26 Feb 2025 13:54:07 +0530 Subject: [PATCH 2/3] Bump version to 1.0.1 and update User-Agent header with version info --- package-lock.json | 4 ++-- package.json | 2 +- src/index.ts | 11 ++++++----- src/parser.ts | 11 +++++++++-- src/version.ts | 2 ++ tests/parser.spec.ts | 14 ++++++++++++-- 6 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 src/version.ts diff --git a/package-lock.json b/package-lock.json index 04e4e32..ad06e06 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@anyparser/core", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@anyparser/core", - "version": "1.0.0", + "version": "1.0.1", "license": "Apache-2.0", "devDependencies": { "@arethetypeswrong/cli": "^0.17.4", diff --git a/package.json b/package.json index cdaf238..d84f88f 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "ts" ], "homepage": "https://github.com/anyparser/anyparserjs", - "version": "1.0.0", + "version": "1.0.1", "main": "dist/index.js", "exports": { ".": { diff --git a/src/index.ts b/src/index.ts index 7bc776d..f4f69f3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 } diff --git a/src/parser.ts b/src/parser.ts index 7b3fef5..04d12d0 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -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. */ @@ -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 + } : + {}) } } diff --git a/src/version.ts b/src/version.ts new file mode 100644 index 0000000..0710b20 --- /dev/null +++ b/src/version.ts @@ -0,0 +1,2 @@ +export const ANYPARSER_VERSION = '1.0.1' +export const version = ANYPARSER_VERSION diff --git a/tests/parser.spec.ts b/tests/parser.spec.ts index 82dc83c..d2cd30c 100644 --- a/tests/parser.spec.ts +++ b/tests/parser.spec.ts @@ -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') @@ -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 + } } ) @@ -75,6 +79,7 @@ describe('Anyparser', () => { model: 'text', encoding: 'utf-8' }) + mockBuildForm.mockReturnValue(mockFormData) mockWrappedFetch.mockResolvedValue(mockResponse) @@ -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) }) From 886021832acbedd1b6e1449114c0d89aee82d0a8 Mon Sep 17 00:00:00 2001 From: John Doe Date: Wed, 26 Feb 2025 13:59:48 +0530 Subject: [PATCH 3/3] fix: enhance branch check to allow publishing from release branches --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d84f88f..99f16a2 100644 --- a/package.json +++ b/package.json @@ -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": {