Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add missing types #132

Merged
merged 3 commits into from
Sep 3, 2023
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
120 changes: 18 additions & 102 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,111 +1,27 @@
export type ColorScheme = | "dark" | "light";
import { MqlPayload, MqlOptions, MicrolinkApiOptions } from './lightweight'

export type WaitUntilEvent = "load" | "domcontentloaded" | "networkidle0" | "networkidle2";

export type PixelUnit = string | number;

export type ScreenshotOverlay = {
background?: string,
browser?: 'dark' | 'light'
}

export type MqlOptions = {
endpoint?: string;
apiKey?: string;
retry?: number;
cache?: Map<string, any>;
}

export type PdfMargin = {
top?: string | number;
bottom?: string | number;
left?: string | number;
right?: string | number;
}

export interface MqlQuery {
[field: string]: MqlQueryOptions;
}

export type MqlQueryOptions = {
attr?: string | string[] | MqlQuery;
evaluate?: string;
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";
}

export type PdfOptions = {
format?: string;
height?: PixelUnit;
landscape?: string;
margin?: string | PdfMargin;
pageRanges?: string;
scale?: number;
width?: PixelUnit;
}

export type ScreenshotOptions = {
codeScheme?: string;
omitBackground?: boolean;
type?: "jpeg" | "png";
element?: string;
fullPage?: boolean;
overlay?: ScreenshotOverlay
}

export type MicrolinkApiOptions = {
adblock?: boolean;
animations?: boolean;
audio?: boolean;
click?: string | string[];
colorScheme?: ColorScheme;
data?: MqlQuery;
device?: string;
embed?: string;
filename?: string;
filter?: string;
force?: boolean;
function?: string;
headers?: Record<string, string>;
iframe?: boolean | { maxWidth?: number, maxHeight?: number };
insights?: boolean | { lighthouse?: boolean | object, technologies?: boolean };
javascript?: boolean;
mediaType?: string;
meta?: boolean | { logo: { square: boolean } };
modules?: string | string[];
palette?: boolean;
pdf?: boolean | PdfOptions;
ping?: boolean | object;
prerender?: boolean | "auto";
proxy?: string | { countryCode?: string };
retry?: number;
screenshot?: boolean | ScreenshotOptions;
scripts?: string | string[];
scroll?: string;
styles?: string | string[];
staleTtl?: string | number;
timeout?: number;
ttl?: string | number;
video?: boolean;
viewport?: object;
waitForSelector?: string;
waitForTimeout?: number;
waitUntil?: WaitUntilEvent | WaitUntilEvent[];
type MqlResponse = MqlPayload & {
response: {
url: string;
isFromCache?: boolean;
statusCode: number;
headers: { [key: string]: string };
body: MqlPayload
};
}

declare function mql(
url: string,
opts?: MqlOptions & MicrolinkApiOptions,
// TODO: gotOpts could be different depends the environment where the library is being used
// - Under Node.js context, types are from `got`
// https://github.com/microlinkhq/mql/blob/fbb55f4758495fa42d35822867763f95ac5ae960/src/node.js#L5
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/90a4ec8f0a9c76f33fdeeef0118f26c5d3df5cfa/types/got/index.d.ts#L212
//
// - Under brower context, types are from `ky`
// https://github.com/microlinkhq/mql/blob/fbb55f4758495fa42d35822867763f95ac5ae960/src/browser.js#L9
// https://github.com/sindresorhus/ky/blob/main/source/types/options.ts#L30
gotOpts?: object
): Promise<object>;
): Promise<MqlResponse>;

declare namespace mql {
function stream(
url: string,
opts?: MqlOptions & MicrolinkApiOptions,
gotOpts?: object
): NodeJS.ReadableStream;
}

export default mql;
172 changes: 172 additions & 0 deletions lightweight/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
type ColorScheme = | "dark" | "light";

type WaitUntilEvent = "load" | "domcontentloaded" | "networkidle0" | "networkidle2";

type PixelUnit = string | number;

type ScreenshotOverlay = {
background?: string,
browser?: 'dark' | 'light'
}

type PdfMargin = {
top?: string | number;
bottom?: string | number;
left?: string | number;
right?: string | number;
}

type PdfOptions = {
format?: string;
height?: PixelUnit;
landscape?: string;
margin?: string | PdfMargin;
pageRanges?: string;
scale?: number;
width?: PixelUnit;
}

type ScreenshotOptions = {
codeScheme?: string;
omitBackground?: boolean;
type?: "jpeg" | "png";
element?: string;
fullPage?: boolean;
overlay?: ScreenshotOverlay
}

export type MqlOptions = {
endpoint?: string;
apiKey?: string;
retry?: number;
cache?: Map<string, any>;
}

type MqlQuery = {
[field: string]: MqlQueryOptions;
}

type MqlQueryOptions = {
attr?: string | string[] | MqlQuery;
evaluate?: string;
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";
}

export type MicrolinkApiOptions = {
adblock?: boolean;
animations?: boolean;
audio?: boolean;
click?: string | string[];
colorScheme?: ColorScheme;
data?: MqlQuery;
device?: string;
embed?: string;
filename?: string;
filter?: string;
force?: boolean;
function?: string;
headers?: Record<string, string>;
iframe?: boolean | { maxWidth?: number, maxHeight?: number };
insights?: boolean | { lighthouse?: boolean | object, technologies?: boolean };
javascript?: boolean;
mediaType?: string;
meta?: boolean | { logo: { square: boolean } };
modules?: string | string[];
palette?: boolean;
pdf?: boolean | PdfOptions;
ping?: boolean | object;
prerender?: boolean | "auto";
proxy?: string | { countryCode?: string };
retry?: number;
screenshot?: boolean | ScreenshotOptions;
scripts?: string | string[];
scroll?: string;
styles?: string | string[];
staleTtl?: string | number;
timeout?: number;
ttl?: string | number;
video?: boolean;
viewport?: object;
waitForSelector?: string;
waitForTimeout?: number;
waitUntil?: WaitUntilEvent | WaitUntilEvent[];
}

type IframeInfo = {
html: string;
scripts: Record<string, unknown>;
}

type MediaInfo = {
type?: string;
palette?: string[];
background_color?: string;
color?: string;
alternative_color?: string;
width?: number;
height?: number;
duration?: number;
duration_pretty?: string;
}

type MqlResponseData = {
// A human-readable representation of the author's name.
author?: string | null;
// An ISO 8601 representation of the date the article was published.
date?: string | null;
// The publisher's chosen description of the article.
description?: string | null;
// An ISO 639-1 representation of the url content language.
lang?: string | null;
// An image URL that best represents the publisher brand.
logo?: MediaInfo | null;
// A human-readable representation of the publisher's name.
publisher?: string | null;
// The publisher's chosen title of the article.
title?: string | null;
// The URL of the article.
url?: string;
image?: MediaInfo | null;
screenshot?: MediaInfo | null;
video?: MediaInfo | null;
audio?: MediaInfo | null;
iframe?: IframeInfo | null;
function?: MqlFunctionResult;
}

type MqlFunctionResult = {
isFulfilled: boolean;
isRejected: boolean;
value: any;
};

type MqlStatus = "success" | "fail" | "error";

export type MqlPayload = {
status: MqlStatus;
data: MqlResponseData;
statusCode: number;
headers: { [key: string]: string };
more?: string,
code?: string,
url?: string
}

type MqlResponse = MqlPayload & {
response: {
url: string;
statusCode: number;
headers: Headers;
body: MqlPayload
};
}

declare function mql(
url: string,
opts?: MqlOptions & MicrolinkApiOptions,
gotOpts?: object
): Promise<MqlResponse>;

export default mql;
4 changes: 2 additions & 2 deletions lightweight/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"main": "../src/browser.js",
"types": "../index.d.ts"
"main": "../src/lightweight.js",
"types": "index.d.ts"
}
12 changes: 2 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Microlink Query Language. The official HTTP client to interact with Microlink API for Node.js, browsers & Deno.",
"homepage": "https://nicedoc.io/microlinkhq/mql",
"version": "0.10.34",
"browser": "src/browser.js",
"browser": "src/lightweight.js",
"main": "src/node.js",
"author": {
"email": "josefrancisco.verdu@gmail.com",
Expand Down Expand Up @@ -56,22 +56,15 @@
"@rollup/plugin-node-resolve": "latest",
"@rollup/plugin-replace": "latest",
"@rollup/plugin-terser": "latest",
"abort-controller": "latest",
"async-listen": "latest",
"ava": "3",
"beauty-error": "latest",
"c8": "latest",
"chalk": "latest",
"ci-publish": "latest",
"conventional-github-releaser": "latest",
"eachdir": "latest",
"esm": "latest",
"exists-file": "latest",
"git-authors-cli": "latest",
"ky": "latest",
"meow": "latest",
"nano-staged": "latest",
"node-fetch": "2",
"npm-check-updates": "latest",
"prettier-standard": "latest",
"rollup": "latest",
Expand All @@ -82,8 +75,7 @@
"standard-markdown": "latest",
"standard-version": "latest",
"stream-to-promise": "latest",
"tsd": "latest",
"web-streams-polyfill": "latest"
"tsd": "latest"
},
"engines": {
"node": ">= 12"
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const build = ({ format, file }) => {
const compress = file.includes('.min.')

return {
input: './src/browser.js',
input: './src/lightweight.js',
output: {
name: 'mql',
format,
Expand Down
File renamed without changes.
33 changes: 0 additions & 33 deletions test/browser-globals.js

This file was deleted.

Loading