Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions packages/core/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,58 @@ interface Metadata {
title?: string | null
description?: string | null
url?: string | null
author?: string | null
lang?: string | null
publisher?: string | null
date?: string | null
image?: Asset | null
logo?: Asset | null
[key: string]: unknown
}

/** Same shape as `?data=` / `microlink.extract` rules. */
interface ExtractRule {
attr?: string | string[] | ExtractRules
evaluate?: string | (() => unknown)
selector?: string | string[]
selectorAll?: string | string[]
type?:
| 'audio'
| 'author'
| 'auto'
| 'boolean'
| 'date'
| 'description'
| 'email'
| 'image'
| 'ip'
| 'lang'
| 'logo'
| 'number'
| 'object'
| 'publisher'
| 'regexp'
| 'string'
| 'title'
| 'url'
| 'video'
}

interface ExtractRules {
[field: string]: ExtractRule | ExtractRule[]
}

interface Embed {
html: string
scripts?: unknown[]
[key: string]: unknown
}

export type FunctionArgs = {
page: Page
page: Page & {
metadata(): Promise<Metadata>
extract(rules: ExtractRules): Promise<Record<string, unknown>>
}
response: HTTPResponse
headers: Record<string, string>
url: string
Expand Down Expand Up @@ -163,7 +204,7 @@ interface MicrolinkClient {
audios (url: string, options?: CollectionOptions): Promise<string[]>
extract (
url: string,
rules: Record<string, unknown>,
rules: ExtractRules,
options?: Options
): Promise<Record<string, unknown>>
screenshot (url: string, options?: ScreenshotOptions): Promise<Asset>
Expand Down
19 changes: 19 additions & 0 deletions packages/core/test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ async function assertions (): Promise<void> {
avatar: { selector: 'img', attr: 'src', type: 'image' }
})
expectAssignable<Record<string, unknown>>(data)
expectAssignable<Record<string, unknown>>(
await client.extract('https://example.com', {
avatar: [
{ selector: 'meta[property="og:image"]', attr: 'content', type: 'image' },
{ selector: 'img', attr: 'src', type: 'image' }
]
})
)

const news = await client.search('coffee', { type: 'news', limit: 3 })
expectAssignable<{ results: Array<{ publisher: string }> }>(news)
Expand All @@ -79,6 +87,17 @@ async function assertions (): Promise<void> {
expectType<boolean>(response.ok())
expectType<Record<string, string>>(headers)
expectType<string>(url)
const metadata = await page.metadata()
expectType<string | null | undefined>(metadata.title)
expectType<Record<string, unknown>>(
await page.extract({
title: { selector: 'h1', type: 'string' },
avatar: [
{ selector: 'meta[property="og:image"]', attr: 'content', type: 'image' },
{ selector: 'img', attr: 'src', type: 'image' }
]
})
)
return page.title()
}
)
Expand Down
Loading