Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript support #8

Closed
sglkc opened this issue Apr 22, 2024 · 7 comments · Fixed by #17
Closed

TypeScript support #8

sglkc opened this issue Apr 22, 2024 · 7 comments · Fixed by #17

Comments

@sglkc
Copy link
Contributor

sglkc commented Apr 22, 2024

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
Copy link
Owner

i don't want this project to be in ts

@sglkc
Copy link
Contributor Author

sglkc commented Apr 22, 2024

i respect your preference

@sglkc sglkc closed this as not planned Won't fix, can't repro, duplicate, stale Apr 22, 2024
@Beatlz
Copy link

Beatlz commented Aug 31, 2024

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
Copy link
Owner

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

@sglkc
Copy link
Contributor Author

sglkc commented Aug 31, 2024

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>
    }
}

@dimdenGD
Copy link
Owner

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

@Beatlz
Copy link

Beatlz commented Sep 1, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants