Skip to content

type

Type:
<string> | <string[]>

Default: 'auto'
Values:
'audio' | 'author' | 'auto' | 'boolean' | 'date' | 'description' | 'email' | 'image' | 'ip' | 'lang' | 'logo' | 'number' | 'object' | 'publisher' | 'regexp' | 'string' | 'title' | 'url' | 'video'
It defines how the value extracted should be considered.
import createClient from 'microlink.io'

const microlink = createClient()

const productHunt = id =>
  microlink.extract(`https://www.producthunt.com/posts/${id}`, {
    name: {
      selector: 'h1 a',
      attr: 'text',
      type: 'string'
    },
    upvotes: {
      selector: '.bigButtonCount_10448',
      attr: 'text',
      type: 'number'
    }
  })

const productSlug = 'microlink-2-0'
const { name, upvotes } = await productHunt(productSlug)

console.log(`'${name}' has ${upvotes} upvotes`)
The data shape ensures that the extracted value will only be considered as valid when it's of the declared shape: a rule whose value doesn't match its type resolves to null, which is what lets fallback rules move on to the next candidate.
Media types do more than validate. 'image', 'video', 'audio', and 'logo' resolve the value to an absolute URL and expand it into an asset object with url, type, width, height, size, and size_pretty, the same shape the normalized data fields use:
const { cover } = await microlink.extract('https://www.youtube.com/watch?v=9P6rdqiybaw', {
  cover: {
    selector: 'meta[property="og:image"]',
    attr: 'content',
    type: 'image'
  }
})

console.log(cover.width, cover.height, cover.size_pretty)