microlinkhq / metascraper

Get unified metadata from websites using Open Graph, Microdata, RDFa, Twitter Cards, JSON-LD, HTML, and more.
https://metascraper.js.org
MIT License
2.35k stars 168 forks source link

Wrong Typescript declared types #642

Closed xxRockOnxx closed 1 year ago

xxRockOnxx commented 1 year ago

Prerequisites

Subject of the issue

The Typescript types are not correct.

Package like metascraper-image exports a function that returns an object.

i.e

module.exports = () => ({
  image: [
    toImage($ => $('meta[property="og:image:secure_url"]').attr('content')),
    toImage($ => $('meta[property="og:image:url"]').attr('content')),
    toImage($ => $('meta[property="og:image"]').attr('content')),
    toImage($ => $('meta[name="twitter:image:src"]').attr('content')),
    toImage($ => $('meta[property="twitter:image:src"]').attr('content')),
    toImage($ => $('meta[name="twitter:image"]').attr('content')),
    toImage($ => $('meta[property="twitter:image"]').attr('content')),
    toImage($ => $('meta[itemprop="image"]').attr('content')),
    toImage($jsonld('image.0.url')),
    toImage($jsonld('image.url')),
    toImage($jsonld('image.url')),
    toImage($jsonld('image')),
    toImage($ => $filter($, $('article img[src]'), getSrc)),
    toImage($ => $filter($, $('#content img[src]'), getSrc)),
    toImage($ => $('img[alt*="author" i]').attr('src')),
    toImage($ => $('img[src]:not([aria-hidden="true"])').attr('src'))
  ]
})

However in the declaration file, metascraper-* packages exports a function that returns a function that returns an object which is not true.

i.e

type Rule = () => RuleSet;

declare module 'metascraper-*' {
  export default function rules(): import('metascraper').Rule;
}

For more clarity, the expanded equivalent is:

export default function rules(): () => RuleSet

I believe it should return RuleSet directly instead.


Next, the "constructor" function accepts wrong types.

It should accept RuleSet and not Rules.

If it were to accept Rules, the "constructor" should look like:

const metascraper = Metascraper([
  MetascraperImage
])

instead of

const metascraper = Metascraper([
  MetascraperImage()
])

htmlDom from CheckOptions interface should also be

interface CheckOptions {
  htmlDom: import('cheerio').CheerioAPI;
  url: string
}

instead of

interface CheckOptions {
  htmlDom: typeof import('cheerio');
  url: string
}

Otherwise it causes the error "This expression is not callable."

I can submit a PR but I'm not sure yet if this is intentional or if I'm missing something.

Kikobeats commented 1 year ago

Hello, they're definitely outdated; Can you send a PR? 🙏

Also, I think it's missing rules set can contain a test function:

https://github.com/microlinkhq/metascraper/blob/v5.34.7/packages/metascraper-youtube/index.js#L47

xxRockOnxx commented 1 year ago

@Kikobeats what is the release cycle or when is the next version coming out?

Kikobeats commented 1 year ago

as soon as PR is merged, a new version is released

xxRockOnxx commented 1 year ago

it's been merged and it's still on 5.34.7

Kikobeats commented 1 year ago

oh sorry; metascraper@5.34.8 released 🙂