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
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"devDependencies": {
"ava": "latest",
"proxyquire": "latest",
"puppeteer-core": "25",
"tinyspawn": "latest",
"tsd": "latest"
},
Expand Down
18 changes: 17 additions & 1 deletion packages/core/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { HTTPResponse, Page } from 'puppeteer-core' with {
'resolution-mode': 'import'
}
import createGoogleClient from '@microlink/google'

type GoogleClient = ReturnType<typeof createGoogleClient>
Expand Down Expand Up @@ -117,7 +120,15 @@ interface Embed {
[key: string]: unknown
}

type FunctionInput = string | ((args: Record<string, unknown>) => unknown)
export type FunctionArgs = {
page: Page
response: HTTPResponse
headers: Record<string, string>
url: string
[key: string]: any
}

export type FunctionInput = (args: FunctionArgs) => any

interface FunctionResult<T = unknown> {
isFulfilled: boolean
Expand Down Expand Up @@ -169,6 +180,11 @@ interface MicrolinkClient {
code: FunctionInput,
options?: Options
): Promise<FunctionResult<T>>
function<T = unknown> (
url: string,
code: string,
options?: Options
): Promise<FunctionResult<T>>
}

interface create {
Expand Down
12 changes: 12 additions & 0 deletions packages/core/test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ async function assertions (): Promise<void> {

const fnResult = await client.function('https://example.com', '() => 1')
expectType<boolean>(fnResult.isFulfilled)

const title = await client.function(
'https://example.com',
async ({ page, response, headers, url }) => {
expectType<Promise<string>>(page.title())
expectType<boolean>(response.ok())
expectType<Record<string, string>>(headers)
expectType<string>(url)
return page.title()
}
)
expectType<boolean>(title.isFulfilled)
}

void assertions()
Expand Down
8 changes: 6 additions & 2 deletions packages/function/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ export type FunctionRejected = {

export type FunctionResponse = FunctionFulfilled | FunctionRejected

export type FunctionInput = (args: {
export type FunctionArgs = {
page: Page
response: HTTPResponse
headers: Record<string, string>
url: string
[key: string]: any
}) => any
}

export type FunctionInput = (args: FunctionArgs) => any

declare function microlinkFunction(
fn: FunctionInput,
Expand Down
2 changes: 2 additions & 0 deletions packages/function/test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ microlinkFn(() => document.getElementsByTagName('*').length)
microlinkFn(({ page }) => page.title())
microlinkFn(() => 420)
microlinkFn(({ response }) => response.ok())
microlinkFn(({ headers }) => headers['user-agent'])
microlinkFn(({ url }) => url)
microlinkFn(({ name }) => `Greetings, ${name}`)