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

Type error with the how field of type ScrapeOptionElement #193

Closed
RepolloDev opened this issue Aug 3, 2024 · 3 comments · Fixed by #194
Closed

Type error with the how field of type ScrapeOptionElement #193

RepolloDev opened this issue Aug 3, 2024 · 3 comments · Fixed by #194

Comments

@RepolloDev
Copy link

A little context

I recently started using scrape-it to extract data from html pages, but there were cases where I needed to intervene in the extraction using the Cheerio API.

// test example
const anyHTML = '<html>...</html>'
const { data } = scrapeIt.scrapeHTML<{ data: unknown }>(anyHTML, {
  data: {
    listItem: 'main',
    data: {
      items: {
        selector: 'article',
        how: (element) => {
           const $items = element.find('p:nth-child(n+2)')
           // more cheerio methods
           return $items.text()
        }
      }
    }
  }
})

The problem

TypeScript throws a typing warning, if you run the code nothing happens, but it becomes a nuisance to have that warning and not have autocompletion with the Cheerio object passed to the function parameter.
image

Solución

Looking into the types of scrape-it, the how field has as its type a function whose parameter is a cheerio.Selector, which may cause the problem.

export interface ScrapeOptionElement {
        selector?: string;
        convert?: (value: any) => any;
        // Change cheerio.Selector to cheerio.Cheerio
        how?: string | ((element: cheerio.Selector) => any);
        attr?: string;
        trim?: boolean;
        closest?: string;
        eq?: number;
        texteq?: number;
    }
@IonicaBizau
Copy link
Owner

I am not good in handling TypeScript types, but contributions are welcome in that regard! Thank you very much!

@fadingNA
Copy link
Contributor

fadingNA commented Oct 1, 2024

@IonicaBizau can I work on this issue?

@IonicaBizau
Copy link
Owner

Yes, that would be great! :)

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