dimdenGD / chrome-lens-ocr

Library to use Google Lens OCR for free, via API used in Chromium.
145 stars 7 forks source link

TypeScript support #8

Closed sglkc closed 5 months ago

sglkc commented 5 months ago

Currently you have to depend on README to know the APIs, by adding types you can help save time in development. It should be easy since everything is already documented though. Should I make a PR?

dimdenGD commented 5 months ago

i don't want this project to be in ts

sglkc commented 5 months ago

i respect your preference

Beatlz commented 3 weeks ago

I'm wondering why this isn't included as a default? Puppeteer has full TS integration, why not include the types for this? Not trying to be pedantic, I'm genuinely interested.

dimdenGD commented 3 weeks ago

I don't like TS and having to build the project

sglkc commented 3 weeks ago

I don't like TS and having to build the project

You don't have to actually build the project, just a single declaration file, add types field in package.json, and since types are used in imports, private methods can be ignored.

I'm wondering why this isn't included as a default? Puppeteer has full TS integration, why not include the types for this? Not trying to be pedantic, I'm genuinely interested.

I'm considering to add this package to definitelytyped, but for teh mean time:

This is the declaration I always use ```ts declare module 'chrome-lens-ocr' { import type { IncomingHttpHeaders } from 'node:http' export const LENS_ENDPOINT = 'https://lens.google.com/v3/upload' export const LENS_API_ENDPOINT = 'https://lens.google.com/uploadbyurl' export const SUPPORTED_MIMES = [ 'image/x-icon', 'image/bmp', 'image/jpeg', 'image/png', 'image/tiff', 'image/webp', 'image/heic', ] as const export const MIME_TO_EXT: Record = { 'image/x-icon': 'ico', 'image/bmp': 'bmp', 'image/jpeg': 'jpg', 'image/png': 'png', 'image/tiff': 'tiff', 'image/webp': 'webp', 'image/heic': 'heic' }; export type LensOptions = { chromeVersion: string majorChromeVersion: string userAgent: string endpoint: string viewport: [number, number] headers: IncomingHttpHeaders | Headers fetchOptions: RequestInit } export class BoundingBox { centerPerX: number // center of the bounding box on X axis, in % of the image width centerPerY: number // center of the bounding box on Y axis, in % of the image height perWidth: number // width of the bounding box, in % of the image width perHeight: number // height of the bounding box, in % of the image height pixelCoords: { x: number // top-left corner X coordinate, in pixels y: number // top-left corner Y coordinate, in pixels width: number, // width of the bounding box, in pixels height: number, // height of the bounding box, in pixels } } export class Segment { text: string boundingBox: BoundingBox } export class LensResult { language: string segments: Segment[] } export class LensError extends Error { name: 'LensError' message: string code: string headers: Headers body: string } export class LensCore { cookies: NavigatorCookies constructor(options?: Partial, _fetchFunction?: typeof fetch) updateOptions(options: Partial): void scanByURL(url: string | URL, dimensions?: [number, number] = [0, 0]): Promise scanByData( data: Uint8Array, mime: SUPPORTED_MIMES, originalDimensions: [number, number] ): Promise static getAFData(text: string): object static parseResult( afData: object, imageDimensions: [number, number] ): LensResult } export default class Lens extends LensCore { constructor(options?: Partial, _fetchFunction?: typeof fetch) scanByFile(path: string): Promise scanByBuffer(buffer: Buffer): Promise } } ````
dimdenGD commented 3 weeks ago

If it doesn't require coding in TS, then you're free to add declaration file to project

Beatlz commented 3 weeks ago

I don't like TS and having to build the project

You don't have to actually build the project, just a single declaration file, add types field in package.json, and since types are used in imports, private methods can be ignored.

I'm wondering why this isn't included as a default? Puppeteer has full TS integration, why not include the types for this? Not trying to be pedantic, I'm genuinely interested.

I'm considering to add this package to definitelytyped, but for teh mean time:

This is the declaration I always use

declare module 'chrome-lens-ocr' {
    import type { IncomingHttpHeaders } from 'node:http'

    export const LENS_ENDPOINT = 'https://lens.google.com/v3/upload'
    export const LENS_API_ENDPOINT = 'https://lens.google.com/uploadbyurl'
    export const SUPPORTED_MIMES = [
        'image/x-icon',
        'image/bmp',
        'image/jpeg',
        'image/png',
        'image/tiff',
        'image/webp',
        'image/heic',
    ] as const

    export const MIME_TO_EXT: Record<typeof SUPPORTED_MIMES[number], string> = {
        'image/x-icon': 'ico',
        'image/bmp': 'bmp',
        'image/jpeg': 'jpg',
        'image/png': 'png',
        'image/tiff': 'tiff',
        'image/webp': 'webp',
        'image/heic': 'heic'
    };

    export type LensOptions = {
        chromeVersion: string
        majorChromeVersion: string
        userAgent: string
        endpoint: string
        viewport: [number, number]
        headers: IncomingHttpHeaders | Headers
        fetchOptions: RequestInit
    }

    export class BoundingBox {
        centerPerX: number // center of the bounding box on X axis, in % of the image width
        centerPerY: number // center of the bounding box on Y axis, in % of the image height
        perWidth: number // width of the bounding box, in % of the image width
        perHeight: number // height of the bounding box, in % of the image height
        pixelCoords: {
            x: number // top-left corner X coordinate, in pixels
            y: number // top-left corner Y coordinate, in pixels
            width: number, // width of the bounding box, in pixels
            height: number, // height of the bounding box, in pixels
        }
    }

    export class Segment {
        text: string
        boundingBox: BoundingBox
    }

    export class LensResult {
        language: string
        segments: Segment[]
    }

    export class LensError extends Error {
        name: 'LensError'
        message: string
        code: string
        headers: Headers
        body: string
    }

    export class LensCore {
        cookies: NavigatorCookies

        constructor(options?: Partial<LensOptions>, _fetchFunction?: typeof fetch)
        updateOptions(options: Partial<LensOptions>): void

        scanByURL(url: string | URL, dimensions?: [number, number] = [0, 0]): Promise<LensResult>
        scanByData(
            data: Uint8Array,
            mime: SUPPORTED_MIMES,
            originalDimensions: [number, number]
        ): Promise<LensResult>

        static getAFData(text: string): object
        static parseResult(
            afData: object,
            imageDimensions: [number, number]
        ): LensResult
    }

    export default class Lens extends LensCore {
        constructor(options?: Partial<LensOptions>, _fetchFunction?: typeof fetch)

        scanByFile(path: string): Promise<LensResult>
        scanByBuffer(buffer: Buffer): Promise<LensResult>
    }
}

That looks quite ok, why not include it in the build? People that (for whatever reason) don't like TS, can still just use the JS build