diff --git a/packages/core/package.json b/packages/core/package.json index 01776b8..aa0e8e1 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -57,6 +57,7 @@ "devDependencies": { "ava": "latest", "proxyquire": "latest", + "puppeteer-core": "25", "tinyspawn": "latest", "tsd": "latest" }, diff --git a/packages/core/src/index.d.ts b/packages/core/src/index.d.ts index 2e4d095..7289319 100644 --- a/packages/core/src/index.d.ts +++ b/packages/core/src/index.d.ts @@ -1,3 +1,6 @@ +import type { HTTPResponse, Page } from 'puppeteer-core' with { + 'resolution-mode': 'import' +} import createGoogleClient from '@microlink/google' type GoogleClient = ReturnType @@ -117,7 +120,15 @@ interface Embed { [key: string]: unknown } -type FunctionInput = string | ((args: Record) => unknown) +export type FunctionArgs = { + page: Page + response: HTTPResponse + headers: Record + url: string + [key: string]: any +} + +export type FunctionInput = (args: FunctionArgs) => any interface FunctionResult { isFulfilled: boolean @@ -169,6 +180,11 @@ interface MicrolinkClient { code: FunctionInput, options?: Options ): Promise> + function ( + url: string, + code: string, + options?: Options + ): Promise> } interface create { diff --git a/packages/core/test/index.test-d.ts b/packages/core/test/index.test-d.ts index 5f3314a..cce5d46 100644 --- a/packages/core/test/index.test-d.ts +++ b/packages/core/test/index.test-d.ts @@ -71,6 +71,18 @@ async function assertions (): Promise { const fnResult = await client.function('https://example.com', '() => 1') expectType(fnResult.isFulfilled) + + const title = await client.function( + 'https://example.com', + async ({ page, response, headers, url }) => { + expectType>(page.title()) + expectType(response.ok()) + expectType>(headers) + expectType(url) + return page.title() + } + ) + expectType(title.isFulfilled) } void assertions() diff --git a/packages/function/src/index.d.ts b/packages/function/src/index.d.ts index 62de41a..7922c81 100644 --- a/packages/function/src/index.d.ts +++ b/packages/function/src/index.d.ts @@ -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 + url: string [key: string]: any -}) => any +} + +export type FunctionInput = (args: FunctionArgs) => any declare function microlinkFunction( fn: FunctionInput, diff --git a/packages/function/test/index.test-d.ts b/packages/function/test/index.test-d.ts index 9cc48ba..40a8ed7 100644 --- a/packages/function/test/index.test-d.ts +++ b/packages/function/test/index.test-d.ts @@ -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}`)