Skip to content

Commit

Permalink
Merge pull request #40 from bbc/modernise
Browse files Browse the repository at this point in the history
Use node-fetch instead of request for default transport
  • Loading branch information
jamierbower authored Sep 22, 2023
2 parents 627d450 + 7272c4f commit 1f6c795
Show file tree
Hide file tree
Showing 26 changed files with 522 additions and 1,134 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ console.log(res.random); // number
```

## Opting Out
If you don't want to type your plugin, simply use `any` as the type. This is not recomemnded though as it means all plugins later in the chain will loose the types too, because they have no idea what changes were made.
If you don't want to type your plugin, simply use `any` as the type. This is not recommended though as it means all plugins later in the chain will lose the types too, because they have no idea what changes were made.

```ts
const myPlugin: Plugin<any> = (context, next) => {};
Expand Down
56 changes: 23 additions & 33 deletions docs.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# HttpTranport
# HttpTransport

> A flexible, modular REST client built for ease-of-use and resilience
Expand Down Expand Up @@ -32,7 +32,7 @@ PATCH, DELETE, HEAD are also supported.

#### Query strings

Make a HTTP GET request specifiying query strings using `.query`
Make a HTTP GET request specifying query strings using `.query`

Single query string
```js
Expand Down Expand Up @@ -62,7 +62,7 @@ Multiple query strings:

#### Headers

Make a HTTP GET request specifiying request headers using `.headers`
Make a HTTP GET request specifying request headers using `.headers`

Add a single header:
```js
Expand Down Expand Up @@ -108,7 +108,7 @@ Convert `Internal Server` responses (500) to errors:
```

`toError` is **only** required if the underlying client does not support HTTP error conversion.
The default transport is `request`, which does **not** convert errors.
The default transport is `node-fetch`, which does **not** convert errors.

#### Retries

Expand Down Expand Up @@ -176,7 +176,7 @@ Middleware are functions that can be executed with before and after a request. M
* Terminate a request early e.g for caching purposes
* Modify the response object e.g format the response body

Middlware can be executed **per request** using the `.use` method:
Middleware can be executed **per request** using the `.use` method:
```js
const exampleMiddleware = require('exampleMiddleware');

Expand All @@ -193,7 +193,7 @@ Middlware can be executed **per request** using the `.use` method:
}
```

Middlware can also be executed **for every request** using the `.use` of the client builder. The client builder is created using the `createBuilder` method:
Middleware can also be executed **for every request** using the `.use` of the client builder. The client builder is created using the `createBuilder` method:

```js
const exampleMiddleware = require('exampleMiddleware');
Expand All @@ -214,7 +214,7 @@ Middlware can also be executed **for every request** using the `.use` of the cli

For writing middleware, see the [offical guide](https://github.com/koajs/koa/blob/master/docs/guide.md)

#### Offical HttpTransport middleware
#### Official HttpTransport middleware
See [Caching](https://github.com/bbc/http-transport-cache)

See [Collapsing](https://github.com/bbc/http-transport-request-collapse)
Expand All @@ -227,37 +227,30 @@ See [Ratelimiting](https://github.com/bbc/http-transport-rate-limiter)

See [xray](https://github.com/bbc/http-transport-xray)

#### Using alternative HTTP clients via transport decorators
#### Callback support
HttpTransport does not support callbacks. However, to integrate with legacy code, use the [callback adapter](https://github.com/bbc/http-transport-callbacks)

Make a HTTP GET request and supply an alternative HTTP transport via `.createClient`
#### Setting default behaviour of underlying http transport

```js
const url = 'http://example.com/';
const HttpTransport = require('@bbc/http-transport');
const OtherTransport = require('some-other-transport');

const res = await HttpTransport.createClient(OtherTransport)
.get(url)
.asResponse();

if (res.statusCode === 200) {
console.log(res.body);
}
```

Make a HTTP GET request by configuring an alternative request instance and supplying it in RequestTransport transport via `.createClient`
Make a HTTP GET request by passing default configuration to RequestTransport, and supplying the configured RequestTransport to `.createClient`

```js
const url = 'http://example.com/';
const HttpTransport = require('@bbc/http-transport');
const request = require('request');

const requestDefaults = {
headers: {
special: 'special value'
const defaultConfig = {
agentOpts: { // Here you can pass in any options for the https agent https://nodejs.org/api/https.html#class-httpsagent
keepAlive: true,
maxSockets: 1000
},
defaults: {
json: true, // parses the response body as json
timeout: 2000 // sets timeout for each request
compress: true // support gzip/deflate content encoding. false to disable
}
};
const requestTransport = new HttpTransport.RequestTransport(request.defaults(requestDefaults));

const requestTransport = new HttpTransport.RequestTransport(defaultConfig);

const res = await HttpTransport.createClient(requestTransport);
.get(url)
Expand All @@ -266,7 +259,4 @@ const res = await HttpTransport.createClient(requestTransport);
if (res.statusCode === 200) {
console.log(res.body);
}
```

#### Callback support
HttpTransport does not support callbacks. However, to integrate with legacy code, use the [callback adapter](https://github.com/bbc/http-transport-callbacks)
```
168 changes: 10 additions & 158 deletions docs/HttpTransportBuilder.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<label for="nav-trigger" class="overlay"></label>

<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="HttpTransportBuilder.html">HttpTransportBuilder</a><ul class='methods'><li data-type='method'><a href="HttpTransportBuilder.html#asCallback">asCallback</a></li><li data-type='method'><a href="HttpTransportBuilder.html#createClient">createClient</a></li><li data-type='method'><a href="HttpTransportBuilder.html#retries">retries</a></li><li data-type='method'><a href="HttpTransportBuilder.html#retryDelay">retryDelay</a></li><li data-type='method'><a href="HttpTransportBuilder.html#use">use</a></li><li data-type='method'><a href="HttpTransportBuilder.html#userAgent">userAgent</a></li></ul></li><li><a href="HttpTransportClient.html">HttpTransportClient</a><ul class='methods'><li data-type='method'><a href="HttpTransportClient.html#asBody">asBody</a></li><li data-type='method'><a href="HttpTransportClient.html#asResponse">asResponse</a></li><li data-type='method'><a href="HttpTransportClient.html#delete">delete</a></li><li data-type='method'><a href="HttpTransportClient.html#get">get</a></li><li data-type='method'><a href="HttpTransportClient.html#head">head</a></li><li data-type='method'><a href="HttpTransportClient.html#headers">headers</a></li><li data-type='method'><a href="HttpTransportClient.html#patch">patch</a></li><li data-type='method'><a href="HttpTransportClient.html#post">post</a></li><li data-type='method'><a href="HttpTransportClient.html#put">put</a></li><li data-type='method'><a href="HttpTransportClient.html#query">query</a></li><li data-type='method'><a href="HttpTransportClient.html#retry">retry</a></li><li data-type='method'><a href="HttpTransportClient.html#retryDelay">retryDelay</a></li><li data-type='method'><a href="HttpTransportClient.html#timeout">timeout</a></li><li data-type='method'><a href="HttpTransportClient.html#use">use</a></li></ul></li></ul>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="HttpTransportBuilder.html">HttpTransportBuilder</a><ul class='methods'><li data-type='method'><a href="HttpTransportBuilder.html#createClient">createClient</a></li><li data-type='method'><a href="HttpTransportBuilder.html#retries">retries</a></li><li data-type='method'><a href="HttpTransportBuilder.html#retryDelay">retryDelay</a></li><li data-type='method'><a href="HttpTransportBuilder.html#use">use</a></li><li data-type='method'><a href="HttpTransportBuilder.html#userAgent">userAgent</a></li></ul></li><li><a href="HttpTransportClient.html">HttpTransportClient</a><ul class='methods'><li data-type='method'><a href="HttpTransportClient.html#asBody">asBody</a></li><li data-type='method'><a href="HttpTransportClient.html#asResponse">asResponse</a></li><li data-type='method'><a href="HttpTransportClient.html#delete">delete</a></li><li data-type='method'><a href="HttpTransportClient.html#get">get</a></li><li data-type='method'><a href="HttpTransportClient.html#head">head</a></li><li data-type='method'><a href="HttpTransportClient.html#headers">headers</a></li><li data-type='method'><a href="HttpTransportClient.html#patch">patch</a></li><li data-type='method'><a href="HttpTransportClient.html#post">post</a></li><li data-type='method'><a href="HttpTransportClient.html#put">put</a></li><li data-type='method'><a href="HttpTransportClient.html#query">query</a></li><li data-type='method'><a href="HttpTransportClient.html#retry">retry</a></li><li data-type='method'><a href="HttpTransportClient.html#retryDelay">retryDelay</a></li><li data-type='method'><a href="HttpTransportClient.html#timeout">timeout</a></li><li data-type='method'><a href="HttpTransportClient.html#use">use</a></li></ul></li></ul>
</nav>

<div id="main">
Expand Down Expand Up @@ -66,7 +66,7 @@ <h4 class="name" id="HttpTransportBuilder"><span class="type-signature"></span>n

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="builder.js.html">builder.js</a>, <a href="builder.js.html#line13">line 13</a>
<a href="builder.js.html">builder.js</a>, <a href="builder.js.html#line12">line 12</a>
</li></ul></dd>


Expand Down Expand Up @@ -205,7 +205,7 @@ <h3 class="subsection-title">Methods</h3>



<h4 class="name" id="asCallback"><span class="type-signature"></span>asCallback<span class="signature">()</span><span class="type-signature"></span></h4>
<h4 class="name" id="createClient"><span class="type-signature"></span>createClient<span class="signature">()</span><span class="type-signature"></span></h4>



Expand All @@ -217,106 +217,7 @@ <h4 class="name" id="asCallback"><span class="type-signature"></span>asCallback<

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="builder.js.html">builder.js</a>, <a href="builder.js.html#line100">line 100</a>
</li></ul></dd>































</dl>





<div class="description">
<p>Callbackify the client</p>
</div>









<h5>Example</h5>

<pre class="prettyprint"><code>const client = httpTransport.asCallback().createClient();</code></pre>
















<h5>Returns:</h5>


<div class="param-desc">
<p>a HttpTransport instance supporting callbacks</p>
</div>











<h4 class="name" id="createClient"><span class="type-signature"></span>createClient<span class="signature">(fn)</span><span class="type-signature"></span></h4>






<dl class="details">


<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="builder.js.html">builder.js</a>, <a href="builder.js.html#line114">line 114</a>
<a href="builder.js.html">builder.js</a>, <a href="builder.js.html#line98">line 98</a>
</li></ul></dd>


Expand Down Expand Up @@ -374,55 +275,6 @@ <h5>Example</h5>



<h5>Parameters:</h5>


<table class="params">
<thead>
<tr>

<th>Name</th>


<th>Type</th>





<th class="last">Description</th>
</tr>
</thead>

<tbody>


<tr>

<td class="name"><code>fn</code></td>


<td class="type">


<span class="param-type">function</span>



</td>





<td class="description last"><p>a global plugin</p></td>
</tr>


</tbody>
</table>





Expand Down Expand Up @@ -464,7 +316,7 @@ <h4 class="name" id="retries"><span class="type-signature"></span>retries<span c

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="builder.js.html">builder.js</a>, <a href="builder.js.html#line53">line 53</a>
<a href="builder.js.html">builder.js</a>, <a href="builder.js.html#line51">line 51</a>
</li></ul></dd>


Expand Down Expand Up @@ -615,7 +467,7 @@ <h4 class="name" id="retryDelay"><span class="type-signature"></span>retryDelay<

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="builder.js.html">builder.js</a>, <a href="builder.js.html#line69">line 69</a>
<a href="builder.js.html">builder.js</a>, <a href="builder.js.html#line67">line 67</a>
</li></ul></dd>


Expand Down Expand Up @@ -766,7 +618,7 @@ <h4 class="name" id="use"><span class="type-signature"></span>use<span class="si

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="builder.js.html">builder.js</a>, <a href="builder.js.html#line86">line 86</a>
<a href="builder.js.html">builder.js</a>, <a href="builder.js.html#line84">line 84</a>
</li></ul></dd>


Expand Down Expand Up @@ -918,7 +770,7 @@ <h4 class="name" id="userAgent"><span class="type-signature"></span>userAgent<sp

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="builder.js.html">builder.js</a>, <a href="builder.js.html#line37">line 37</a>
<a href="builder.js.html">builder.js</a>, <a href="builder.js.html#line35">line 35</a>
</li></ul></dd>


Expand Down Expand Up @@ -1020,7 +872,7 @@ <h5>Parameters:</h5>



<td class="description last"><p>user agant</p></td>
<td class="description last"><p>user agent</p></td>
</tr>


Expand Down Expand Up @@ -1070,7 +922,7 @@ <h5>Returns:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Thu Feb 07 2019 15:28:34 GMT+0000 (Greenwich Mean Time) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.11</a> on Fri Sep 22 2023 15:20:00 GMT+0100 (British Summer Time) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
Loading

0 comments on commit 1f6c795

Please sign in to comment.