1 | [English](./README.md) | 简体中文
|
2 |
|
3 | # electron-request
|
4 |
|
5 | > 在 Electron 或 Node.js 环境下使用的轻量的零依赖的 http 请求库
|
6 |
|
7 | ## 为什么
|
8 |
|
9 | electron-request 在 Electron 环境下使用其内置的 net 模块,在 Node.js 环境下使用其内置的 HTTP 模块。
|
10 |
|
11 | 在 Electron 中使用 net 模块,更好的支持代理、身份验证、流量监控等功能。详细可查看[net 文档](https://www.electronjs.org/docs/api/net)。
|
12 |
|
13 | ## 特性
|
14 |
|
15 | - 零依赖,足够轻量
|
16 | - 快速上手,类似 window.fetch 的使用方式
|
17 | - 不需要引入额外库,支持文件下载进度功能和文件校验功能
|
18 | - 支持在 Electron 或 Node.js 上运行,优先使用 Electron 的 net 模块
|
19 | - 统一的错误处理
|
20 |
|
21 | ## 安装
|
22 |
|
23 | ```bash
|
24 | npm install electron-request --save
|
25 | # or
|
26 | yarn add electron-request
|
27 | ```
|
28 |
|
29 | ## 使用
|
30 |
|
31 | ```ts
|
32 | import request from 'electron-request';
|
33 |
|
34 | void (async () => {
|
35 | const url = 'https://github.com/';
|
36 | const defaultOptions = {
|
37 | method: 'GET',
|
38 | body: null,
|
39 | followRedirect: true,
|
40 | maxRedirectCount: 20,
|
41 | timeout: 0,
|
42 | size: 0,
|
43 | };
|
44 | const response = await request(url, defaultOptions);
|
45 | const text = await response.text();
|
46 | })();
|
47 | ```
|
48 |
|
49 | ## API
|
50 |
|
51 | ### request(url[, options])
|
52 |
|
53 | - url:请求地址
|
54 |
|
55 | - options:配置项
|
56 |
|
57 | ```ts
|
58 | interface Options {
|
59 | /**
|
60 | * Request method
|
61 | * @default 'GET'
|
62 | */
|
63 | method?: string;
|
64 | /**
|
65 | * Request body
|
66 | * @default null
|
67 | */
|
68 | body?: string | null | Buffer | Stream;
|
69 | /**
|
70 | * Request headers
|
71 | */
|
72 | headers?: Record<string, string>;
|
73 | /**
|
74 | * Request query
|
75 | */
|
76 | query?: Record<string, string>;
|
77 | /**
|
78 | * Allow redirect
|
79 | * @default true
|
80 | */
|
81 | followRedirect?: boolean;
|
82 | /**
|
83 | * Maximum redirect count. 0 to not follow redirect
|
84 | * @default 20
|
85 | */
|
86 | maxRedirectCount?: number;
|
87 | /**
|
88 | * Request/Response timeout in ms. 0 to disable
|
89 | * @default 0
|
90 | */
|
91 | timeout?: number;
|
92 | /**
|
93 | * Maximum response body size in bytes. 0 to disable
|
94 | * @default 0
|
95 | */
|
96 | size?: number;
|
97 | /**
|
98 | * Whether to use nodejs native request
|
99 | * @default false
|
100 | */
|
101 | useNative?: boolean;
|
102 |
|
103 | // Docs: https://www.electronjs.org/docs/api/client-request#new-clientrequestoptions
|
104 |
|
105 | /**
|
106 | * Only in Electron. When use authenticated HTTP proxy, username to use to authenticate
|
107 | */
|
108 | username?: string;
|
109 | /**
|
110 | * Only in Electron. When use authenticated HTTP proxy, password to use to authenticate
|
111 | */
|
112 | password?: string;
|
113 | /**
|
114 | * Only in Electron. Whether to send cookies with this request from the provided session
|
115 | */
|
116 | useSessionCookies?: boolean;
|
117 | /**
|
118 | * Only in Electron. The Session instance with which the request is associated
|
119 | * @default electron.session.defaultSession
|
120 | */
|
121 | session?: Session;
|
122 | }
|
123 | ```
|
124 |
|
125 | ### Response
|
126 |
|
127 | ```ts
|
128 | interface Response {
|
129 | /** Whether the response was successful (status in the range 200-299) */
|
130 | ok: boolean;
|
131 | /** Response headers */
|
132 | headers: Record<string, string | string[]>;
|
133 | /** Return origin stream */
|
134 | stream: Stream;
|
135 | /** Decode response as ArrayBuffer */
|
136 | arrayBuffer(): Promise<ArrayBuffer>;
|
137 | /** Decode response as Blob */
|
138 | blob(): Promise<Blob>;
|
139 | /** Decode response as text */
|
140 | text(): Promise<string>;
|
141 | /** Decode response as json */
|
142 | json<T>(): Promise<T>;
|
143 | /** Decode response as buffer */
|
144 | buffer(): Promise<Buffer>;
|
145 | /**
|
146 | * Download file to destination
|
147 | * @param {Writable} destination Writable destination stream
|
148 | * @param {ProgressCallback=} onProgress Download progress callback
|
149 | * @param {ValidateOptions=} validateOptions Validate options
|
150 | */
|
151 | download: (
|
152 | destination: Writable,
|
153 | onProgress?: ProgressCallback,
|
154 | validateOptions?: ValidateOptions,
|
155 | ) => Promise<void>;
|
156 | }
|
157 |
|
158 | /** Download progress information */
|
159 | interface ProgressInfo {
|
160 | /** Total file bytes */
|
161 | total: number;
|
162 | /** Delta file bytes */
|
163 | delta: number;
|
164 | /** Transferred file bytes */
|
165 | transferred: number;
|
166 | /** Transferred percentage */
|
167 | percent: number;
|
168 | /** Bytes transferred per second */
|
169 | bytesPerSecond: number;
|
170 | }
|
171 | ```
|
172 |
|
173 | ## License
|
174 |
|
175 | [MIT License](./LICENSE)
|
176 |
|
177 | ## electron-request vs. the Competition
|
178 |
|
179 | | Package | Size |
|
180 | | --- | --- |
|
181 | | request | [![request package size](https://packagephobia.now.sh/badge?p=request)](https://packagephobia.now.sh/result?p=request) |
|
182 | | axios | [![axios package size](https://packagephobia.now.sh/badge?p=axios)](https://packagephobia.now.sh/result?p=axios) |
|
183 | | node-fetch | [![node-fetch package size](https://packagephobia.now.sh/badge?p=node-fetch)](https://packagephobia.now.sh/result?p=node-fetch) |
|
184 | | request-pure | [![request-pure package size](https://packagephobia.now.sh/badge?p=request-pure)](https://packagephobia.now.sh/result?p=request-pure) |
|
185 | | electron-request | [![electron-request package size](https://packagephobia.now.sh/badge?p=electron-request)](https://packagephobia.now.sh/result?p=electron-request) |
|