OpenLink

TypeScript

Full type definitions and interfaces for openlink.

OpenLink is written in TypeScript and includes complete type definitions.

PreviewOptions

Configuration options for the preview function.

interface PreviewOptions {
  timeout?: number
  headers?: Record<string, string>
  followRedirects?: boolean
  fetch?: typeof fetch
  includeRaw?: boolean
  validateUrl?: boolean
}
PropertyTypeDefaultDescription
timeoutnumber10000Request timeout in ms
headersRecord<string, string>See belowCustom headers
followRedirectsbooleantrueFollow HTTP redirects
fetchtypeof fetchglobalThis.fetchCustom fetch function
includeRawbooleanfalseInclude raw metadata
validateUrlbooleantrueValidate URL first

PreviewResult

The normalized result returned by preview and extract.

interface PreviewResult {
  url: string
  title: string | null
  description: string | null
  image: string | null
  favicon: string
  siteName: string
  domain: string
  type: string
  author: string | null
  publishedTime: string | null
  themeColor: string | null
  twitterCard: string | null
  locale: string | null
  video: string | null
  audio: string | null
  keywords: string[] | null
  raw?: ParseResult
}
PropertyTypeDescription
urlstringCanonical URL
titlestring | nullPage title
descriptionstring | nullPage description
imagestring | nullPrimary image URL
faviconstringFavicon URL
siteNamestringSite name or domain
domainstringHostname
typestringContent type (og:type)
authorstring | nullAuthor name
publishedTimestring | nullPublish date
themeColorstring | nullTheme color
twitterCardstring | nullTwitter card type
localestring | nullContent locale
videostring | nullVideo URL
audiostring | nullAudio URL
keywordsstring[] | nullKeywords array
rawParseResultRaw metadata (if enabled)

ParseResult

Raw parsed metadata from HTML, returned by parse.

interface ParseResult {
  ogTitle: string | null
  ogDescription: string | null
  ogImage: string | null
  ogImageWidth: string | null
  ogImageHeight: string | null
  ogImageAlt: string | null
  ogType: string | null
  ogSiteName: string | null
  ogUrl: string | null
  ogLocale: string | null
  ogVideo: string | null
  ogAudio: string | null
  articleAuthor: string | null
  articlePublishedTime: string | null
  twitterTitle: string | null
  twitterDescription: string | null
  twitterImage: string | null
  twitterCard: string | null
  twitterSite: string | null
  twitterCreator: string | null
  title: string | null
  description: string | null
  favicon: string | null
  appleTouchIcon: string | null
  canonical: string | null
  themeColor: string | null
  keywords: string | null
  author: string | null
  robots: string | null
}

PreviewError

Custom error class with error codes for programmatic handling.

class PreviewError extends Error {
  code: "INVALID_URL" | "TIMEOUT" | "FETCH_ERROR" | "HTTP_ERROR"
  status?: number
  cause?: Error

  constructor(
    message: string,
    code: PreviewError["code"],
    options?: { status?: number; cause?: Error }
  )
}

Error Codes

CodeDescription
INVALID_URLURL format is invalid
TIMEOUTRequest timed out
FETCH_ERRORNetwork error
HTTP_ERRORNon-2xx HTTP status

Example

import { preview, PreviewError } from "openlink"

try {
  const data = await preview("https://example.com")
} catch (error) {
  if (error instanceof PreviewError) {
    switch (error.code) {
      case "TIMEOUT":
        console.log("Request timed out")
        break
      case "HTTP_ERROR":
        console.log(`HTTP ${error.status}`)
        break
      default:
        console.log(error.message)
    }
  }
}

Type Imports

Import types directly for use in your code:

import type {
  PreviewOptions,
  PreviewResult,
  ParseResult
} from "openlink"

function processPreview(result: PreviewResult) {
  // TypeScript knows all properties
  console.log(result.title)
}

const options: PreviewOptions = {
  timeout: 5000,
  includeRaw: true
}

On this page