UNPKG

1.09 kBJavaScriptView Raw
1'use strict';
2
3const Request = require('./request');
4const Response = require('./response');
5const packageInfo = require('../package');
6
7const RETRIES = 0;
8const RETRY_DELAY = 100;
9const USER_AGENT = `${packageInfo.name}/${packageInfo.version}`;
10
11class 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
48module.exports = Context;