Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use node-fetch instead of request for default transport #40

Merged
merged 69 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
b76a152
chore: use node-fetch wip
Simon2828 Aug 15, 2023
eddb7a9
get request working with node fetch
jamierbower Aug 15, 2023
19e6f71
chore: remove customrequest
Simon2828 Aug 16, 2023
31701fc
add https agent
jamierbower Aug 16, 2023
1bd71cc
rename _request prop to _fetch
jamierbower Aug 21, 2023
3765d7d
get cert opts
jamierbower Aug 21, 2023
d99dff6
chore: wip replicating json body behaviour from request in node-fetch
vaughan-rich Aug 22, 2023
e8dffe5
feat: convert response body to json/text accordingly
vaughan-rich Aug 22, 2023
9b79c5c
feat: add AbortController signal opt
vaughan-rich Aug 22, 2023
644041e
WIP handle errors correctly
jamierbower Aug 23, 2023
e0976c7
feat: custom error message for timed out requests
vaughan-rich Aug 23, 2023
20ac4b3
feat: remove elapsedTime from response
vaughan-rich Aug 23, 2023
4c286cf
reads agent options from agentOpts
jamierbower Aug 23, 2023
3ed1120
WIP use http proxy agent
jamierbower Aug 23, 2023
af4c98c
remove proxy agent stuff
jamierbower Aug 24, 2023
b904a2c
uninstall proxy agent
jamierbower Aug 24, 2023
082b4f3
remove unnecessary override of dns lookup
jamierbower Aug 24, 2023
cf4b233
get test working
jamierbower Aug 29, 2023
2b6b2e5
get first retry test working
jamierbower Aug 29, 2023
cfb0707
fix more timeout tests
jamierbower Aug 29, 2023
657f2df
chore: wip tests
onlyonehas Aug 29, 2023
7ffcb88
WIP fix query string test
jamierbower Aug 29, 2023
1f34bf5
client test file working
jamierbower Aug 31, 2023
c86bb8d
chore: fixing linting + tests
onlyonehas Aug 31, 2023
42b7262
adds elapsedTime to response
jamierbower Sep 5, 2023
39eb8e2
change status to statusCode on response
jamierbower Sep 5, 2023
1798742
fix linting errors in request
jamierbower Sep 5, 2023
4b661b5
whitespace
jamierbower Sep 5, 2023
1547124
add elapsedTime back to type def file
jamierbower Sep 5, 2023
616f1bd
whitespace
jamierbower Sep 5, 2023
b76ee49
tidy up response
jamierbower Sep 5, 2023
ecc7acb
further tidy up
jamierbower Sep 5, 2023
b363f49
fix: correcting typos
simongregory Sep 5, 2023
1ff8ac9
fix: correcting typos
simongregory Sep 5, 2023
6d25604
removes all mention of the callbackifying stuff from code
jamierbower Sep 5, 2023
8260c0b
removes statusText
jamierbower Sep 5, 2023
4dc1e4f
remove bluebird dependency
jamierbower Sep 6, 2023
50118cd
remove some uses of lodash
jamierbower Sep 6, 2023
1fda64b
renames RequestTransport to FetchTransport
jamierbower Sep 7, 2023
c010147
rename class
jamierbower Sep 8, 2023
b674b92
enables setting timeout by default
jamierbower Sep 8, 2023
06d9638
updates type index
jamierbower Sep 8, 2023
57fad88
passes through compress from default config, makes agentOpts optional…
jamierbower Sep 11, 2023
689c8dd
docs: update js docs with one section to todo
vaughan-rich Sep 11, 2023
18e5716
chore: remove request dependencies
vaughan-rich Sep 11, 2023
84767dd
3.5.7-0
vaughan-rich Sep 11, 2023
7b10791
chore: remove request types
vaughan-rich Sep 11, 2023
f2075a7
docs: fix js docs
vaughan-rich Sep 12, 2023
5ca8f60
add section to docs about setting default behaviour of node-fetch
jamierbower Sep 12, 2023
459028c
docs: update readme
vaughan-rich Sep 12, 2023
d316b08
feat: adds http support
vaughan-rich Sep 14, 2023
411d93d
test: wip httpsAgent unit test
vaughan-rich Sep 14, 2023
5f8b5f4
convert module to ESM
jamierbower Sep 18, 2023
2a7bc68
allows disbling timing via setContextProperty
jamierbower Sep 18, 2023
212da7d
tests: check agent is using correct protocol for http/https
onlyonehas Sep 18, 2023
af933d3
test: test correct agent protocol is added
vaughan-rich Sep 18, 2023
612c126
remove unecessary import|
jamierbower Sep 18, 2023
16785cc
3.5.7-1
jamierbower Sep 18, 2023
2c474dc
revert ESM conversion
jamierbower Sep 18, 2023
df7a86e
3.5.7-2
jamierbower Sep 18, 2023
5d682f5
converts headers from iterable to serializable object, fixes leak in …
jamierbower Sep 19, 2023
7cb5ac0
3.5.7-3
jamierbower Sep 19, 2023
27c5bf3
default to from.text() if content type is not json
jamierbower Sep 20, 2023
a411bcf
4.0.0-0
jamierbower Sep 20, 2023
2f68d8e
parse body as json if json default is set
jamierbower Sep 21, 2023
9b3c6e4
4.0.0-1
jamierbower Sep 21, 2023
a72a892
catches error if JSON parsing fails, and defaults response to text
jamierbower Sep 21, 2023
df2c4c5
4.0.0-2
jamierbower Sep 21, 2023
7272c4f
update docs
jamierbower Sep 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
55 changes: 22 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,29 @@ 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: {
timeout: 2000
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 +258,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 Tue Sep 12 2023 10:54:32 GMT+0100 (British Summer Time) using the <a href="/~https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

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