UNPKG

5.65 kBHTMLView Raw
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="utf-8">
5 <title>builder.js - Documentation</title>
6
7 <script src="scripts/prettify/prettify.js"></script>
8 <script src="scripts/prettify/lang-css.js"></script>
9 <!--[if lt IE 9]>
10 <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11 <![endif]-->
12 <link type="text/css" rel="stylesheet" href="styles/prettify.css">
13 <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
14 <meta name="viewport" content="width=device-width, initial-scale=1.0">
15</head>
16<body>
17
18<input type="checkbox" id="nav-trigger" class="nav-trigger" />
19<label for="nav-trigger" class="navicon-button x">
20 <div class="navicon"></div>
21</label>
22
23<label for="nav-trigger" class="overlay"></label>
24
25<nav>
26 <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>
27</nav>
28
29<div id="main">
30
31 <h1 class="page-title">builder.js</h1>
32
33
34
35
36
37
38
39 <section>
40 <article>
41 <pre class="prettyprint source linenums"><code>'use strict';
42
43const _ = require('lodash');
44
45const HttpTransportClient = require('./client');
46
47function validatePlugin(plugin) {
48 if (typeof plugin !== 'function') throw new TypeError('Plugin is not a function');
49}
50
51/** @class */
52class HttpTransportBuilder {
53 /**
54 * Configures HttpTransport client
55 * @param {Transport} transport - Transport instance.
56 */
57 constructor(transport) {
58 this._transport = transport;
59 this._defaults = {
60 plugins: []
61 };
62 }
63
64 /**
65 * Sets a default user agent
66 *
67 * @param {string} agent - user agent
68 * @return a HttpTransportBuilder instance
69 * @example
70 * const httpTransport = require('@bbc/http-transport');
71 *
72 * const builder = httpTransport.createBuilder();
73 * builder.userAgent('some-user-agent');
74 */
75 userAgent(userAgent) {
76 _.set(this._defaults, 'ctx.userAgent', userAgent);
77 return this;
78 }
79
80 /**
81 * Set the default number of retries
82 *
83 * @param {integer} retries - number of retry attempts
84 * @return a HttpTransportBuilder instance
85 * @example
86 * const httpTransport = require('@bbc/http-transport');
87 *
88 * const builder = httpTransport.createBuilder();
89 * builder.retries(5);
90 */
91 retries(retries) {
92 _.set(this._defaults, 'ctx.retries', retries);
93 return this;
94 }
95
96 /**
97 * default time delay between retries
98 *
99 * @param {integer} delay - delay time in ms
100 * @return a HttpTransportBuilder instance
101 * @example
102 * const httpTransport = require('@bbc/http-transport');
103 *
104 * const builder = httpTransport.createBuilder();
105 * builder.retryDelay(1000);
106 */
107 retryDelay(delay) {
108 _.set(this._defaults, 'ctx.retryDelay', delay);
109 return this;
110 }
111
112 /**
113 * Registers a global plugin, which is used for all requests
114 *
115 * @param {function} fn - a global plugin
116 * @return a HttpTransportBuilder instance
117 * @example
118 * const toError = require('@bbc/http-transport-errors');
119 * const httpTransport = require('@bbc/http-transport');
120 *
121 * const client = httpTransport.createClient();
122 * client.useGlobal(toError(404));
123 */
124 use(fn) {
125 validatePlugin(fn);
126 this._defaults.plugins.push(fn);
127 return this;
128 }
129
130 /**
131 * Instantiates a HttpTransport
132 *
133 * @return a HttpTransport instance
134 * @example
135 *
136 * const client = httpTransport.createClient();
137 */
138 createClient() {
139 return new HttpTransportClient(this._transport, this._defaults);
140 }
141}
142
143module.exports = HttpTransportBuilder;
144</code></pre>
145 </article>
146 </section>
147
148
149
150
151</div>
152
153<br class="clear">
154
155<footer>
156 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.
157</footer>
158
159<script>prettyPrint();</script>
160<script src="scripts/linenumber.js"></script>
161</body>
162</html>