index.d.ts
4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import * as http from 'http';
import * as url from 'url';
import * as stream from 'stream';
//tslint:disable:next-line: no-import-side-effect
import 'cheerio';
// cheerio-httpcli本体
declare namespace CheerioHttpcli {
interface FetchResponse extends http.IncomingMessage {
cookies: {[ name: string ]: string};
}
// CheerioStatic拡張
interface CheerioStaticEx extends CheerioStatic {
documentInfo(): { url: string, encoding: string | null, isXml: boolean };
entityHtml(options?: CheerioOptionsInterface): string;
entityHtml(selector: string, options?: CheerioOptionsInterface): string;
entityHtml(element: Cheerio, options?: CheerioOptionsInterface): string;
entityHtml(element: CheerioElement, options?: CheerioOptionsInterface): string;
}
interface FetchResult {
error: Error;
$: CheerioStaticEx;
response: FetchResponse;
body: string;
}
type FetchCallback = (error: Error, $: CheerioStaticEx, response: FetchResponse, body: string) => void;
// やっつけPromise
interface Promise {
then(callback: (result: FetchResult) => void): Promise;
catch(callbck: (error: Error) => void): Promise;
finally(callback: () => void): Promise;
}
namespace Download {
interface Stream extends stream.Stream {
url: url.Url;
type: string;
length: number;
toBuffer(callback: (error: Error, buffer: Buffer) => void): void;
end(): void;
}
interface ErrorEx extends Error {
url: string;
}
export interface Manager {
parallel: number;
state: { queue: number, complete: number, error: number };
clearCache(): void;
on(event: 'ready', handler: ((stream: Stream) => void)): Manager;
on(event: 'error', handler: ((error: ErrorEx) => void)): Manager;
on(event: 'end', handler: (() => void)): Manager;
}
}
type FreeObject = {[ name: string ]: string};
const headers: FreeObject;
const agentOptions: FreeObject;
const timeout: number;
const maxDataSize: number;
const gzip: boolean;
const referer: boolean;
const followMetaRefresh: boolean;
const forceHtml: boolean;
const debug: boolean;
const browser: string;
const iconv: string;
const version: string;
const download: Download.Manager;
function reset(): void;
function set(name: 'browser' | 'iconv', value: string): void;
function set(name: 'timeout' | 'maxDataSize', value: number): void;
function set(name: 'gzip' | 'referer' | 'followMetaRefresh' | 'forceHtml' | 'debug', value: boolean): void;
function set(name: 'headers' | 'agentOptions', value: FreeObject, nomerge?: boolean): void;
function setIconvEngine(icmod: 'iconv' | 'iconv-jp' | 'iconv-lite'): void;
function setBrowser(type:
'ie' | 'edge' | 'chrome' | 'firefox' |
'opera' | 'vivaldi' | 'safari' |
'ipad' | 'iphone' | 'ipod' | 'android' |
'googlebot'): boolean;
function fetch(url: string, param: {[ name: string ]: any}, encode: string, callback: FetchCallback): void;
function fetch(url: string, param: {[ name: string ]: any}, callback: FetchCallback): void;
function fetch(url: string, encode: string, callback: FetchCallback): void;
function fetch(url: string, callback: FetchCallback): void;
function fetch(url: string, param: {[ name: string ]: any}, encode: string): Promise;
function fetch(url: string, param: {[ name: string ]: any}): Promise;
function fetch(url: string, encode: string): Promise;
function fetch(url: string): Promise;
function fetchSync(url: string, param: {[ name: string ]: any}, encode: string): FetchResult;
function fetchSync(url: string, param: {[ name: string ]: any}): FetchResult;
function fetchSync(url: string, encode: string): FetchResult;
function fetchSync(url: string): FetchResult;
}
// cheerio拡張メソッド(オリジナルのinterfaceにマージ)
declare global {
interface Cheerio {
click(callback: CheerioHttpcli.FetchCallback): void;
click(): CheerioHttpcli.Promise;
clickSync(): CheerioHttpcli.FetchResult;
download(srcAttr?: string | string[]): void;
field(): {[ name: string ]: string | number};
field(name: string): string | number;
field(name: string, value: string | (() => string), onNotFound?: 'append' | 'throw'): Cheerio;
field(name: {[ name: string ]: string | number}, onNotFound?: 'append' | 'throw'): Cheerio;
entityHtml(): string;
entityHtml(html: string): Cheerio;
submit(param: {[ name: string ]: string | number}, callback: CheerioHttpcli.FetchCallback): void;
submit(callback: CheerioHttpcli.FetchCallback): void;
submit(param?: {[ name: string ]: string | number}): CheerioHttpcli.Promise;
submitSync(param?: {[ name: string ]: string | number}): CheerioHttpcli.FetchResult;
tick(): Cheerio;
untick(): Cheerio;
url(optFilter: { absolute?: boolean, relative?: boolean, invalid?: boolean }, srcAttrs?: string | string[]): string | string[];
url(srcAttrs?: string | string[]): string | string[];
}
}
export = CheerioHttpcli;