UNPKG

3.62 kBMarkdownView Raw
1# HttpTranport
2
3> A flexible rest client that can be easy extended using plugins
4
5## Common examples
6
7#### Supported HTTP methods
8
9Make a HTTP GET request using `.get`
10
11```js
12 const url = 'http://example.com/';
13 await HttpTransport.createClient()
14 .get(url)
15 .asResponse()
16
17 console.log(res);
18```
19
20Make a HTTP POST request using `.post`
21
22```js
23 const url = 'http://example.com/';
24 await HttpTransport.createClient()
25 .post(url, requestBody)
26 .asResponse()
27
28 console.log(res);
29```
30
31PATCH, DELETE, HEAD are also supported.
32
33#### Query strings
34
35Make a HTTP GET request specifiying query strings using `.query`
36
37Single query string
38```js
39 const url = 'http://example.com/';
40 const res = await HttpTransport.createClient()
41 .get(url)
42 .query('example', 'true')
43 .asResponse();
44
45 console.log(res);
46```
47
48Multiple query strings:
49```js
50 const url = 'http://example.com/';
51 const res = await HttpTransport.createClient()
52 .get(url)
53 .query({
54 example1: true
55 example2: false
56 })
57 .asResponse();
58
59 console.log(res);
60```
61
62
63#### Headers
64
65Make a HTTP GET request specifiying request headers using `.headers`
66
67Add a single header:
68```js
69 await HttpTransport.createClient()
70 .get(url)
71 .headers('someHeader1', 'someValue1')
72 .asResponse();
73
74 console.log(res);
75```
76
77Add multiple headers:
78```js
79 await HttpTransport.createClient()
80 .get(url)
81 .headers({
82 'someHeader1' : 'someValue1'
83 'someHeader2' : 'someValue2'
84 })
85 .asResponse();
86
87 console.log(res);
88```
89
90#### Handling errors
91
92Convert `Internal Server` responses (500) to errors:
93
94```js
95 const toError = require('@bbc/http-transport-to-error');
96
97 const url = 'http://example.com/';
98 const client = HttpTransport.createBuilder()
99 .use(toError())
100 .createClient(); // for all requests
101
102 await client.get(url)
103 .asResponse();
104
105 console.error(err);
106```
107
108`toError` is only required if the underlying client does not support HTTP error conversion.
109The default transport is `request`, which does not convert errors.
110
111#### Retries
112
113Make a HTTP GET and retry twice on error `.retry`
114
115```js
116const toError = require('@bbc/http-transport-to-error');
117
118const client = HttpTransport.createBuilder()
119 .use(toError())
120 .createClient();
121
122 const res = await client.get('http://example.com/')
123 .retry(2)
124 .asResponse();
125
126 console.log(res);
127```
128
129#### Timeouts
130
131Make a HTTP GET and timeout after 50ms `.query`
132
133```js
134const body = await HttpTransport.createClient()
135 .get(url)
136 .timeout(50)
137 .asBody();
138```
139
140#### Using alternative HTTP clients
141
142Make a HTTP GET request and supply a alternative HTTP transport via `.createClient`
143
144```js
145const url = 'http://example.com/';
146const HttpTransport = require('@bbc/http-transport');
147const OtherTranport = require('some-other-transport');
148
149const res = await HttpTransport.createClient(OtherTranport)
150 .get(url)
151 .asResponse();
152
153 if (res.statusCode === 200) {
154 console.log(res.body);
155 }
156});
157```
158
159#### Offical plugins
160See [Caching](https://github.com/bbc/http-transport-cache)
161
162See [Collapsing](https://github.com/bbc/http-transport-request-collapse)
163
164See [Errors](https://github.com/bbc/http-transport-to-error)
165
166See [Stats](https://github.com/bbc/http-transport-statsd)
167
168See [Ratelimiting](https://github.com/bbc/http-transport-rate-limiter)
169
170#### Offical transport decorators
171See [Callbacks](https://github.com/bbc/http-transport-callbacks)
\No newline at end of file