Skip to content

Commit

Permalink
test: add linter for types
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Sep 1, 2023
1 parent 64e4aff commit a85928c
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 226 deletions.
328 changes: 103 additions & 225 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,236 +1,114 @@
/// <reference types="node" />
export type ColorScheme = | "dark" | "light";

declare module "@microlink/mql" {
export type WaitUntilEvent = "load" | "domcontentloaded" | "networkidle0" | "networkidle2";
export type WaitUntilEvent = "load" | "domcontentloaded" | "networkidle0" | "networkidle2";

export type AssetOptions = Partial<{
click: string | string[];
disableAnimations: boolean;
filename: string;
hide: string | string[];
scrollTo: string;
viewport: object;
waitFor: number | string;
waitForSelector: string;
waitForTimeout: number;
waitUntil: WaitUntilEvent | WaitUntilEvent[];
}>;
export type PixelUnit = string | number;

export type ScreenshotOptions = AssetOptions & Partial<{
background: string;
browser: "light" | "dark";
element: string;
fullPage: boolean;
omitBackground: object;
overlay: object;
type: "jpeg" | "png";
}>;

export type PdfOptions = AssetOptions & Partial<{
format: string;
height: number;
hide: string | string[];
landscape: string;
margin: string | object;
pageRanges: string;
scale: number;
width: number;
}>;

export type MqlQueryResponseType =
| "audio"
| "author"
| "auto"
| "boolean"
| "date"
| "description"
| "email"
| "image"
| "ip"
| "lang"
| "lang"
| "logo"
| "number"
| "object"
| "publisher"
| "publisher"
| "regexp"
| "string"
| "title"
| "url"
| "video";

export type MqlQueryOptions = Partial<{
attr: string | string[] | MqlQuery;
evaluate: string | (() => string)
selector: string | string[];
selectorAll: string | string[];
type: MqlQueryResponseType;
}>;

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

export type MicrolinkApiOptions = Partial<{
adblock: boolean;
animations: boolean;
audio: boolean;
click: string | string[];
codeScheme: string;
colorScheme: "dark" | "light";
data: MqlQuery;
device: string;
embed: string;
filter: string;
force: boolean;
function: string;
headers: Record<string, unknown>;
iframe: boolean | Record<"maxwidth" | "maxheight", number>;
insights: boolean | Partial<{ lighthouse: boolean | object, technologies: boolean }>;
javascript: boolean;
mediaType: string;
meta: boolean;
modules: string | string[];
palette: boolean;
pdf: boolean | PdfOptions;
ping: boolean | object;
prerender: boolean | "auto";
proxy: string;
remove: string | string[];
retry: number;
screenshot: boolean | ScreenshotOptions;
scripts: string | string[];
scroll: string;
staleTtl: string | number;
styles: string | string[];
timeout: number;
ttl: string | number;
url: string;
video: boolean;
}>;

export type MqlOptions = Partial<{
endpoint: string;
apiKey: string;
retry: number;
cache: Map<string, any>;
timeout: number;
} & AssetOptions>;

export interface BaseMediaInfo {
url: string;
// file type extension.
type: string;
// file size in bytes.
size: number;
// file size in a human readable format.
size_pretty: string;
}

export interface PlayableMediaInfo {
// source duration in seconds.
duration: number;
// source duration in a human readable format.
duration_pretty: string;
}

export interface VisualMediaInfo {
// file width in pixels.
width: number;
// file height in pixels.
height: number;
}

export interface AudioInfo extends BaseMediaInfo, PlayableMediaInfo {
// TODO make this a complete type
type: "mp3" | string;
}

export interface ImageInfo extends BaseMediaInfo, VisualMediaInfo {
// TODO make this a complete type
type: "png" | "jpg" | string;
palette: string[];
background_color: string;
color: string;
alternative_color: string;
}

export interface VideoInfo extends BaseMediaInfo, PlayableMediaInfo, VisualMediaInfo {
// TODO make this a complete type
type: "mp4" | string;
}

export interface IframeInfo {
html: string;
scripts: Record<string, unknown>;
}
export type ScreenshotOverlay = {
/**
* @docs https://microlink.io/docs/api/parameters/screenshot/overlay
*/
background?: string,
browser?: 'dark' | 'light'
}

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

export type MqlResponseData = Partial<{
// 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: ImageInfo | 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: ImageInfo | null;
screenshot: ImageInfo | null;
video: VideoInfo | null;
audio: AudioInfo | null;
iframe: IframeInfo | null;
function: MqlFunctionResult;
}>;
export type PdfMargin = {
top?: string | number;
bottom?: string | number;
left?: string | number;
right?: string | number;
}

export type MqlStatus = "success" | "fail";
export interface MqlQuery {
[field: string]: MqlQueryOptions;
}

export interface MqlResponse {
status: MqlStatus;
data: MqlResponseData;
// TODO: The response object depends on the context
// - Under Node.js context
// import { ServerResponse} from 'http';
// - Under browser, It will be global `Response`
response: {
headers: { [key: string]: string };
body?: {
statusCode?: number;
};
};
}
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";
}

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<MqlResponse>;
export type PdfOptions = {
format?: string;
height?: PixelUnit;
landscape?: string;
margin?: string | PdfMargin;
pageRanges?: string;
scale?: number;
width?: PixelUnit;
}

declare namespace mql {
export class MicrolinkError extends Error {}
}
export type ScreenshotOptions = {
codeScheme?: string;
omitBackground?: boolean;
type?: "jpeg" | "png";
element?: string;
fullPage?: boolean;
overlay?: ScreenshotOverlay
}

export = mql;
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[];
}

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>;

export default mql;
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"standard-markdown": "latest",
"standard-version": "latest",
"stream-to-promise": "latest",
"tsd": "latest",
"web-streams-polyfill": "latest"
},
"engines": {
Expand All @@ -100,7 +101,7 @@
"clean:build": "rm -rf dist",
"contributors": "(npx git-authors-cli && npx finepack --sort-ignore-object-at ava && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
"dev": "npm run build -- -w",
"lint": "standard",
"lint": "standard && tsd",
"postrelease": "npm run release:tags && npm run release:github && (ci-publish || npm publish --access=public)",
"prebuild": "npm run clean:build && npm run build:kys",
"prerelease": "npm run update:check",
Expand Down Expand Up @@ -156,6 +157,9 @@
"postbump": "npm run build"
}
},
"tsd": {
"directory": "test"
},
"types": "index.d.ts",
"umd:main": "dist/mql.js",
"unpkg": "dist/mql.js"
Expand Down
Loading

0 comments on commit a85928c

Please sign in to comment.