UNPKG

7.38 kBMarkdownView Raw
1# go-fetch
2
3[![Circle CI](https://circleci.com/gh/go-fetch-js/go-fetch.svg?style=svg)](https://circleci.com/gh/go-fetch-js/go-fetch)
4
5A pluggable HTTP client for Node.JS.
6
7`go-fetch` boasts a simple API but supports many features through plugins.
8
9**Features:**
10
11- Support for `HTTP` and `HTTPS` protocols
12- Support for streaming
13- Pluggable API with plugins for:
14 - following redirects
15 - compression
16 - authentication
17 - working with JSON
18 - ...and lots more
19
20## Installation
21
22 npm install --save go-fetch
23
24## Usage
25
26### GET
27
28```javascript
29
30const Client = require('go-fetch');
31const json = require('go-fetch-json');
32
33new Client()
34 .use(json())
35 .get('http://httpbin.org/get')
36 .then(res => res.json())
37 .then(json => console.log(res.toString(), json))
38 .catch(err => console.error(err))
39;
40
41```
42
43### POST
44
45```javascript
46
47const Client = require('go-fetch');
48const json = require('go-fetch-json');
49
50new Client()
51 .use(json())
52 .post('http://httpbin.org/post', {msg: 'Go fetch!'})
53 .then(res => res.json())
54 .then(json => console.log(res.toString(), json))
55 .catch(err => console.error(err))
56;
57
58```
59
60## API
61
62### Client
63
64A HTTP client.
65
66
67```
68new Client([options : object])
69```
70
71Create a new `HTTP` client.
72
73**Options:**
74
75```
76.use(plugin : function) : Client
77```
78
79Extend the functionality with a plugin.
80
81**Parameters:**
82
83- `plugin` Required. A plugin function.
84
85**Returns:**
86
87The client.
88
89```
90.before(middleware : function) : Client
91```
92
93Extend the functionality with a middleware function which is run before a request is sent.
94
95**Parameters:**
96
97- `middleware` Required. A middleware function.
98
99**Returns:**
100
101The client.
102
103```
104.after(middleware : function) : Client
105```
106
107Extend the functionality with a middleware function which is run after a request is sent.
108
109**Parameters:**
110
111- `middleware` Required. A middleware function.
112
113**Returns:**
114
115The client.
116
117```
118.get(url : string, [headers : object]) : Promise
119```
120
121Send a `HTTP` `GET` request.
122
123**Parameters:**
124
125- `url` Required. The request URL.
126- `headers` Optional. The request headers. An object containing key-value pairs.
127
128**Returns:**
129
130A `Promise`. Resolves with a `Response`. Rejects with an `Error`.
131
132```
133.post(url : string, [headers : object], [body : *]) : Promise
134```
135
136Send a `HTTP` `POST` request.
137
138**Parameters:**
139
140- `url` Required. The request URL.
141- `headers` Optional. The request headers. An object containing key-value pairs.
142- `body` Optional. The request body. May be a string or a stream.
143
144**Returns:**
145
146A `Promise`. Resolves with a `Response`. Rejects with an `Error`.
147
148```
149.put(url : string, [headers : object], [body : *]) : Promise
150```
151
152Send a `HTTP` `PUT` request.
153
154**Parameters:**
155
156- `url` Required. The request URL.
157- `headers` Optional. The request headers. An object containing key-value pairs.
158- `body` Optional. The request body. May be a string or a stream.
159
160**Returns:**
161
162A `Promise`. Resolves with a `Response`. Rejects with an `Error`.
163
164```
165.delete(url : string, [headers : object]) : Promise
166```
167
168Send a `HTTP` `DELETE` request.
169
170**Parameters:**
171
172- `url` Required. The request URL.
173- `headers` Optional. The request headers. An object containing key-value pairs.
174
175**Returns:**
176
177A `Promise`. Resolves with a `Response`. Rejects with an `Error`.
178
179```
180.request(method : string, url : string, [headers : object], [body : *]) : Promise
181```
182
183Send a `HTTP` request.
184
185**Parameters:**
186
187- `method` Required. The request method.
188- `url` Required. The request URL.
189- `headers` Optional. The request headers. An object containing key-value pairs.
190- `body` Optional. The request body. May be a string or a stream.
191
192**Returns:**
193
194A `Promise`. Resolves with a `Response`. Rejects with an `Error`.
195
196### Request
197
198A HTTP request.
199
200```
201new Request([options : object])
202```
203
204Create a new request.
205
206**Options:**
207
208- `method` Required. The request method.
209- `url` Required. The request URL.
210- `headers` Optional. The request headers. An object containing key-value pairs.
211- `body` Optional. The request body. May be a string or a stream.
212
213```
214.method : string
215```
216
217The request method.
218
219```
220.url : string
221```
222
223The request URL.
224
225```
226.headers : object
227```
228
229The request headers. An object containing key-value pairs.
230
231```
232.body : *
233```
234
235The request body. May be a string or a stream.
236
237### Response
238
239A HTTP response.
240
241```
242new Response([options : object])
243```
244
245Create a new request.
246
247**Options:**
248
249- `status` Required. The request method.
250- `url` Required. The request URL.
251- `headers` Optional. The request headers. An object containing key-value pairs.
252- `body` Optional. The request body. May be a string or a stream.
253
254```
255.status : number
256```
257
258The response stats.
259
260```
261.reason : string
262```
263
264The response reason.
265
266```
267.headers : object
268```
269
270The response headers. An object containing key-value pairs.
271
272```
273.body : *
274```
275
276The response body. May be a string or a stream. Usually a stream.
277
278```
279.text(encoding : string) : Promise
280```
281
282Read the response body into a string.
283
284**Returns:**
285
286A `Promise`. Resolves with a `string`. Rejects with an `Error`.
287
288
289## Plugins and Middleware
290
291Plugin functions are simple functions that take a client instance and do something with it. Plugin functions are called when they are `.use()`d.
292
293Middleware functions are simple functions that take a `Request` or `Response` object and a `next()` callback as parameters, and does something with them. e.g. add helper methods to the `Request` or `Response` objects, modify the headers or body sent or retreived from the server.
294
295### Example
296
297Here's an example plugin that adds a `.error()` method to the `Response` for asserting whether an error occurred with the request.
298
299```javascript
300function plugin(client) {
301 client.after((res, next) => {
302 res.error = () =>
303 this.status >= 400 && this.status < 600
304 ;
305 next(null, res);
306 });
307}
308````
309
310### [prefix-url](https://www.npmjs.com/package/go-fetch-prefix-url)
311
312Prefix each request URL with another URL.
313
314### [content-type](https://www.npmjs.com/package/go-fetch-content-type)
315
316Parse the Content-Type header.
317
318### [parse-body](https://www.npmjs.com/package/go-fetch-parse-body)
319
320Concatenate and parse the response stream.
321
322### [auth](https://www.npmjs.com/package/go-fetch-auth)
323
324Basic HTTP auth.
325
326### [oauth1](https://www.npmjs.com/package/go-fetch-oauth1)
327
328OAuth v1 authentication.
329
330### [follow-redirects](https://www.npmjs.com/package/go-fetch-follow-redirects)
331
332Automatically follow redirects.
333
334### [decompress](https://www.npmjs.com/package/go-fetch-decompress)
335
336Decompress response bodies compressed with gzip.
337
338### [useragent](https://www.npmjs.com/package/go-fetch-useragent)
339
340Add a User-Agent header to every request.
341
342## To do
343
344- Tests
345- Plugins:
346 - Cookie Jar
347- Support for XMLHttpRequest/fetch in the browser
348
349## Changelog
350
351### v3.0.0
352
353Almost a total rewrite.
354
355- break: use promises instead of events and callbacks
356- break: use middleware instead of events for plugins
357- break: use simplified `Request` and `Response` objects
358
359### v2.0.0
360
361 - moved `prefixUrl`, `contentType` and `body` plugins into their own repositories
362 - changed the arguments passed to the `before` and `after` event handlers - handlers now receive a formal event object that allows propagation to be stopped and the request to be prevented
363 - adding some tests
364 - cleaning up documentation
365
366## License
367
368The MIT License (MIT)
369
370Copyright (c) 2016 James Newell
\No newline at end of file