1 | 'use strict';
|
2 |
|
3 | const Request = require('./request');
|
4 | const Response = require('./response');
|
5 | const packageInfo = require('../package');
|
6 |
|
7 | const RETRIES = 0;
|
8 | const RETRY_DELAY = 100;
|
9 | const USER_AGENT = `${packageInfo.name}/${packageInfo.version}`;
|
10 |
|
11 | class Context {
|
12 | constructor(defaults) {
|
13 | this.retries = 0;
|
14 | this._retryAttempts = [];
|
15 | this.plugins = [];
|
16 | this.req = Request.create();
|
17 | this.res = Response.create();
|
18 | if (defaults) this._applyDefaults(defaults);
|
19 | }
|
20 |
|
21 | get retryAttempts() {
|
22 | return this._retryAttempts || [];
|
23 | }
|
24 |
|
25 | set retryAttempts(retryAttempts) {
|
26 | if (!Array.isArray(retryAttempts)) {
|
27 | retryAttempts = [];
|
28 | }
|
29 | this._retryAttempts = retryAttempts;
|
30 | }
|
31 |
|
32 | addPlugin(plugin) {
|
33 | this.plugins.push(plugin);
|
34 | return this;
|
35 | }
|
36 |
|
37 | _applyDefaults(defaults) {
|
38 | this.userAgent = defaults.ctx?.userAgent || USER_AGENT;
|
39 | this.retries = defaults.ctx?.retries || RETRIES;
|
40 | this.retryDelay = defaults.ctx?.retryDelay || RETRY_DELAY;
|
41 | }
|
42 |
|
43 | static create(defaults) {
|
44 | return new Context(defaults);
|
45 | }
|
46 | }
|
47 |
|
48 | module.exports = Context;
|