UNPKG

3.64 kBTypeScriptView Raw
1import { BinaryToTextEncoding } from 'crypto';
2import { Stream, Writable } from 'stream';
3
4interface Session {
5 // Docs: https://electronjs.org/docs/api/session
6
7 /**
8 * A `Session` object, the default session object of the app.
9 */
10 defaultSession: Session;
11}
12
13interface Options {
14 /**
15 * Request method
16 * @default 'GET'
17 */
18 method?: string;
19 /**
20 * Request body
21 * @default null
22 */
23 body?: string | null | Buffer | Stream;
24 /**
25 * Request headers
26 */
27 headers?: Record<string, string | string[]>;
28 /**
29 * Request query
30 */
31 query?: Record<string, string>;
32 /**
33 * Allow redirect
34 * @default true
35 */
36 followRedirect?: boolean;
37 /**
38 * Maximum redirect count. 0 to not follow redirect
39 * @default 20
40 */
41 maxRedirectCount?: number;
42 /**
43 * Request/Response timeout in ms. 0 to disable
44 * @default 0
45 */
46 timeout?: number;
47 /**
48 * Maximum response body size in bytes. 0 to disable
49 * @default 0
50 */
51 size?: number;
52 /**
53 * Whether to use nodejs native request
54 * @default false
55 */
56 useNative?: boolean;
57
58 // Docs: https://www.electronjs.org/docs/api/client-request#new-clientrequestoptions
59
60 /**
61 * Only in Electron. When use authenticated HTTP proxy, username to use to authenticate
62 */
63 username?: string;
64 /**
65 * Only in Electron. When use authenticated HTTP proxy, password to use to authenticate
66 */
67 password?: string;
68 /**
69 * Only in Electron. Whether to send cookies with this request from the provided session
70 * @default true
71 */
72 useSessionCookies?: boolean;
73 /**
74 * Only in Electron. The Session instance with which the request is associated
75 * @default electron.session.defaultSession
76 */
77 session?: Session;
78}
79
80interface ProgressInfo {
81 /** Total file bytes */
82 total: number;
83 /** Delta file bytes */
84 delta: number;
85 /** Transferred file bytes */
86 transferred: number;
87 /** Transferred percentage */
88 percent: number;
89 /** Bytes transferred per second */
90 bytesPerSecond: number;
91}
92
93type ProgressCallback = (progressInfo: ProgressInfo) => void;
94
95interface ValidateOptions {
96 /** Expected hash */
97 expected: string;
98 /**
99 * Algorithm: first parameter of crypto.createHash
100 * @default 'md5'
101 */
102 algorithm?: string;
103 /**
104 * Encoding: first parameter of Hash.digest
105 * @default 'base64'
106 */
107 encoding?: BinaryToTextEncoding;
108}
109
110interface Response {
111 /** Whether the response was successful (status in the range 200-299) */
112 ok: boolean;
113 /** Response headers */
114 headers: Record<string, string | string[]>;
115 /** Return origin stream */
116 stream: Stream;
117 /** Decode response as ArrayBuffer */
118 arrayBuffer(): Promise<ArrayBuffer>;
119 /** Decode response as Blob */
120 blob(): Promise<Blob>;
121 /** Decode response as text */
122 text(): Promise<string>;
123 /** Decode response as json */
124 json<T>(): Promise<T>;
125 /** Decode response as buffer */
126 buffer(): Promise<Buffer>;
127 /**
128 * Download file to destination
129 * @param {Writable} destination Writable destination stream
130 * @param {ProgressCallback=} onProgress Download progress callback
131 * @param {ValidateOptions=} validateOptions Validate options
132 */
133 download: (
134 destination: Writable,
135 onProgress?: ProgressCallback,
136 validateOptions?: ValidateOptions,
137 ) => Promise<void>;
138}
139
140interface Blob {
141 size: number;
142 type: string;
143 isClosed: boolean;
144 content: Buffer;
145 slice(start?: number, end?: number, type?: string): Blob;
146 close(): void;
147 toString(): string;
148}
149
150declare const main: (requestURL: string, options?: Options) => Promise<Response>;
151
152export { ProgressInfo, Response, main as default };