UNPKG

3.81 kBMarkdownView Raw
1# request-pure
2
3> Zero-dependency, lightweight http request client for nodejs
4
5## Install
6
7```bash
8npm install request-pure --save
9# or
10yarn add request-pure
11```
12
13## Usage
14
15```ts
16import request from 'request-pure';
17
18void (async () => {
19 const url = 'https://github.com/';
20 const defaultOptions = {
21 method: 'GET',
22 body: null,
23 followRedirect: true,
24 maxRedirectCount: 20,
25 timeout: 0,
26 size: 0,
27 };
28 const response = await request(url, defaultOptions);
29 const text = await response.text();
30})();
31```
32
33## API
34
35### request(url[, options])
36
37- url: Request URL
38
39- options: Options
40
41 ```ts
42 interface Options {
43 /**
44 * Request method
45 * @default 'GET'
46 */
47 method?: string;
48 /**
49 * Request body
50 * @default null
51 */
52 body?: string | null | Buffer | Stream;
53 /**
54 * Request headers
55 */
56 headers?: Record<string, string>;
57 /**
58 * Request query
59 */
60 query?: Record<string, string>;
61 /**
62 * Allow redirect
63 * @default true
64 */
65 followRedirect?: boolean;
66 /**
67 * Maximum redirect count. 0 to not follow redirect
68 * @default 20
69 */
70 maxRedirectCount?: number;
71 /**
72 * Request/Response timeout in ms. 0 to disable
73 * @default 0
74 */
75 timeout?: number;
76 /**
77 * Maximum response body size in bytes. 0 to disable
78 * @default 0
79 */
80 size?: number;
81 }
82 ```
83
84### Response
85
86```ts
87interface Response {
88 /** Convenience property representing if the request ended normally */
89 ok: boolean;
90 /** Return origin stream */
91 stream: Stream;
92 /**
93 * Download file to destination
94 * @param {WriteStream} dest Download write stream
95 * @param {ProgressCallback=} onProgress Download progress callback
96 */
97 download: (dest: WriteStream, onProgress?: ProgressCallback) => Promise<void>;
98 /** Decode response as ArrayBuffer */
99 arrayBuffer(): Promise<ArrayBuffer>;
100 /** Decode response as Blob */
101 blob(): Promise<Blob>;
102 /** Decode response as text */
103 text(): Promise<string>;
104 /** Decode response as json */
105 json<T>(): Promise<T>;
106 /** Decode response as buffer */
107 buffer(): Promise<Buffer>;
108}
109
110/** Download progress information */
111interface ProgressInfo {
112 /** Total file bytes */
113 total: number;
114 /** Delta file bytes */
115 delta: number;
116 /** Transferred file bytes */
117 transferred: number;
118 /** Transferred percentage */
119 percent: number;
120 /** Bytes transferred per second */
121 bytesPerSecond: number;
122}
123```
124
125## License
126
127[MIT License](./LICENSE)
128
129## request-pure vs. the Competition
130
131| Package | Size |
132| --- | --- |
133| request | [![request package size](https://packagephobia.now.sh/badge?p=request)](https://packagephobia.now.sh/result?p=request) |
134| superagent | [![superagent package size](https://packagephobia.now.sh/badge?p=superagent)](https://packagephobia.now.sh/result?p=superagent) |
135| got | [![got package size](https://packagephobia.now.sh/badge?p=got)](https://packagephobia.now.sh/result?p=got) |
136| axios | [![axios package size](https://packagephobia.now.sh/badge?p=axios)](https://packagephobia.now.sh/result?p=axios) |
137| isomorphic-fetch | [![isomorphic-fetch package size](https://packagephobia.now.sh/badge?p=isomorphic-fetch)](https://packagephobia.now.sh/result?p=isomorphic-fetch) |
138| r2 | [![r2 package size](https://packagephobia.now.sh/badge?p=r2)](https://packagephobia.now.sh/result?p=r2) |
139| node-fetch | [![node-fetch package size](https://packagephobia.now.sh/badge?p=node-fetch)](https://packagephobia.now.sh/result?p=node-fetch) |
140| phin | [![phin package size](https://packagephobia.now.sh/badge?p=phin)](https://packagephobia.now.sh/result?p=phin) |
141| request-pure | [![request-pure package size](https://packagephobia.now.sh/badge?p=request-pure)](https://packagephobia.now.sh/result?p=request-pure) |