rekwest 4.3.0 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -175,6 +175,35 @@ The object to fulfill with default [options](#rekwesturl-options)
175
175
 
176
176
  ---
177
177
 
178
+ #### `rekwest.extend(options)`
179
+
180
+ The method to extend default [options](#rekwesturl-options) per instance
181
+
182
+ ```javascript
183
+ import rekwest, { constants } from 'rekwest';
184
+
185
+ const {
186
+ HTTP_STATUS_OK,
187
+ } = constants;
188
+
189
+ const rk = rekwest.extend({
190
+ baseURL: 'https://somewhe.re'
191
+ });
192
+
193
+ const signal = AbortSignal.timeout(1e4);
194
+ const url = '/somewhat/endpoint';
195
+
196
+ const res = await rk(url, {
197
+ signal,
198
+ });
199
+
200
+ console.assert(res.statusCode === HTTP_STATUS_OK);
201
+ console.info(res.headers);
202
+ console.log(res.body);
203
+ ```
204
+
205
+ ---
206
+
178
207
  #### `rekwest.stream(url[, options])`
179
208
 
180
209
  The method with limited functionality to use with streams and/or pipes
package/dist/index.js CHANGED
@@ -85,42 +85,38 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
85
85
  const {
86
86
  HTTP2_HEADER_CONTENT_TYPE
87
87
  } = _nodeHttp2.default.constants;
88
- function rekwest(...args) {
89
- let options = (0, _utils.normalize)(...args);
90
- if (!options.redirected) {
91
- options = (0, _utils.merge)(rekwest.defaults, options);
92
- }
93
- return (0, _utils.transfer)((0, _validation.validation)(options));
88
+ function rekwest(url, options) {
89
+ return (0, _utils.transfer)((0, _validation.validation)((0, _utils.normalize)(url, options)));
94
90
  }
91
+ Reflect.defineProperty(rekwest, 'extend', {
92
+ enumerable: true,
93
+ value(options) {
94
+ return (url, opts) => rekwest(url, (0, _utils.merge)(options, opts));
95
+ }
96
+ });
95
97
  Reflect.defineProperty(rekwest, 'stream', {
96
98
  enumerable: true,
97
- value(...args) {
98
- const options = (0, _preflight.preflight)({
99
- ...(0, _validation.validation)((0, _utils.merge)(rekwest.defaults, {
100
- headers: {
101
- [HTTP2_HEADER_CONTENT_TYPE]: _mediatypes.APPLICATION_OCTET_STREAM
102
- }
103
- }, (0, _utils.normalize)(...args))),
99
+ value(url, options) {
100
+ options = (0, _preflight.preflight)((0, _validation.validation)((0, _utils.normalize)(url, (0, _utils.merge)(options, {
101
+ headers: {
102
+ [HTTP2_HEADER_CONTENT_TYPE]: _mediatypes.APPLICATION_OCTET_STREAM
103
+ },
104
104
  redirect: _constants.requestRedirect.manual
105
- });
106
- const {
107
- h2,
108
- url
109
- } = options;
105
+ }))));
110
106
  let client, req;
111
- if (h2) {
112
- client = _nodeHttp2.default.connect(url.origin, options);
107
+ if (options.h2) {
108
+ client = _nodeHttp2.default.connect(options.url.origin, options);
113
109
  req = client.request(options.headers, options);
114
110
  } else {
115
111
  const {
116
112
  request
117
- } = url.protocol === 'http:' ? _nodeHttp.default : _nodeHttps.default;
118
- req = request(url, options);
113
+ } = options.url.protocol === 'http:' ? _nodeHttp.default : _nodeHttps.default;
114
+ req = request(options.url, options);
119
115
  }
120
116
  (0, _utils.affix)(client, req, options);
121
117
  req.once('response', res => {
122
118
  let headers;
123
- if (h2) {
119
+ if (options.h2) {
124
120
  headers = res;
125
121
  res = req;
126
122
  }
package/dist/utils.js CHANGED
@@ -148,6 +148,9 @@ const merge = (target = {}, ...rest) => {
148
148
  };
149
149
  exports.merge = merge;
150
150
  const normalize = (url, options = {}) => {
151
+ if (!options.redirected) {
152
+ options = merge(_index.default.defaults, options);
153
+ }
151
154
  if (options.trimTrailingSlashes) {
152
155
  url = `${url}`.replace(/(?<!:)\/+/g, '/');
153
156
  }
@@ -171,7 +174,6 @@ async function* tap(value) {
171
174
  const transfer = async options => {
172
175
  const {
173
176
  digest,
174
- h2,
175
177
  redirected,
176
178
  thenable,
177
179
  url
@@ -180,7 +182,7 @@ const transfer = async options => {
180
182
  throw new _errors.RequestError(`Maximum redirect reached at: ${url.href}`);
181
183
  }
182
184
  if (url.protocol === 'https:') {
183
- options = !h2 ? await (0, _ackn.ackn)(options) : {
185
+ options = !options.h2 ? await (0, _ackn.ackn)(options) : {
184
186
  ...options,
185
187
  createConnection: null,
186
188
  protocol: url.protocol
package/package.json CHANGED
@@ -8,13 +8,13 @@
8
8
  "url": "https://github.com/bricss/rekwest/issues"
9
9
  },
10
10
  "devDependencies": {
11
- "@babel/cli": "^7.20.7",
12
- "@babel/core": "^7.20.12",
13
- "@babel/eslint-parser": "^7.19.1",
14
- "@babel/preset-env": "^7.20.2",
15
- "c8": "^7.12.0",
16
- "eslint": "^8.33.0",
17
- "eslint-config-ultra-refined": "^2.10.0",
11
+ "@babel/cli": "^7.21.5",
12
+ "@babel/core": "^7.21.8",
13
+ "@babel/eslint-parser": "^7.21.8",
14
+ "@babel/preset-env": "^7.21.5",
15
+ "c8": "^7.13.0",
16
+ "eslint": "^8.40.0",
17
+ "eslint-config-ultra-refined": "^2.13.0",
18
18
  "mocha": "^10.2.0"
19
19
  },
20
20
  "description": "The robust request library that humanity deserves 🌐",
@@ -67,5 +67,5 @@
67
67
  "test:bail": "mocha --bail",
68
68
  "test:cover": "c8 --include=src --reporter=lcov --reporter=text npm test"
69
69
  },
70
- "version": "4.3.0"
70
+ "version": "4.4.0"
71
71
  }
package/src/index.mjs CHANGED
@@ -31,36 +31,33 @@ const {
31
31
  HTTP2_HEADER_CONTENT_TYPE,
32
32
  } = http2.constants;
33
33
 
34
- export default function rekwest(...args) {
35
- let options = normalize(...args);
36
-
37
- if (!options.redirected) {
38
- options = merge(rekwest.defaults, options);
39
- }
40
-
41
- return transfer(validation(options));
34
+ export default function rekwest(url, options) {
35
+ return transfer(validation(normalize(url, options)));
42
36
  }
43
37
 
38
+ Reflect.defineProperty(rekwest, 'extend', {
39
+ enumerable: true,
40
+ value(options) {
41
+ return (url, opts) => rekwest(url, merge(options, opts));
42
+ },
43
+ });
44
+
44
45
  Reflect.defineProperty(rekwest, 'stream', {
45
46
  enumerable: true,
46
- value(...args) {
47
- const options = preflight({
48
- ...validation(merge(rekwest.defaults, {
49
- headers: { [HTTP2_HEADER_CONTENT_TYPE]: APPLICATION_OCTET_STREAM },
50
- }, normalize(...args))),
47
+ value(url, options) {
48
+ options = preflight(validation(normalize(url, merge(options, {
49
+ headers: { [HTTP2_HEADER_CONTENT_TYPE]: APPLICATION_OCTET_STREAM },
51
50
  redirect: requestRedirect.manual,
52
- });
53
-
54
- const { h2, url } = options;
51
+ }))));
55
52
  let client, req;
56
53
 
57
- if (h2) {
58
- client = http2.connect(url.origin, options);
54
+ if (options.h2) {
55
+ client = http2.connect(options.url.origin, options);
59
56
  req = client.request(options.headers, options);
60
57
  } else {
61
- const { request } = (url.protocol === 'http:' ? http : https);
58
+ const { request } = options.url.protocol === 'http:' ? http : https;
62
59
 
63
- req = request(url, options);
60
+ req = request(options.url, options);
64
61
  }
65
62
 
66
63
  affix(client, req, options);
@@ -68,7 +65,7 @@ Reflect.defineProperty(rekwest, 'stream', {
68
65
  req.once('response', (res) => {
69
66
  let headers;
70
67
 
71
- if (h2) {
68
+ if (options.h2) {
72
69
  headers = res;
73
70
  res = req;
74
71
  }
package/src/utils.mjs CHANGED
@@ -172,6 +172,10 @@ export const merge = (target = {}, ...rest) => {
172
172
  };
173
173
 
174
174
  export const normalize = (url, options = {}) => {
175
+ if (!options.redirected) {
176
+ options = merge(rekwest.defaults, options);
177
+ }
178
+
175
179
  if (options.trimTrailingSlashes) {
176
180
  url = `${ url }`.replace(/(?<!:)\/+/g, '/');
177
181
  }
@@ -194,14 +198,14 @@ export async function* tap(value) {
194
198
  }
195
199
 
196
200
  export const transfer = async (options) => {
197
- const { digest, h2, redirected, thenable, url } = options;
201
+ const { digest, redirected, thenable, url } = options;
198
202
 
199
203
  if (options.follow === 0) {
200
204
  throw new RequestError(`Maximum redirect reached at: ${ url.href }`);
201
205
  }
202
206
 
203
207
  if (url.protocol === 'https:') {
204
- options = !h2 ? await ackn(options) : {
208
+ options = !options.h2 ? await ackn(options) : {
205
209
  ...options,
206
210
  createConnection: null,
207
211
  protocol: url.protocol,
@@ -229,7 +233,7 @@ export const transfer = async (options) => {
229
233
  client = http2.connect(url.origin, options);
230
234
  req = client.request(options.headers, options);
231
235
  } else {
232
- const { request } = (url.protocol === 'http:' ? http : https);
236
+ const { request } = url.protocol === 'http:' ? http : https;
233
237
 
234
238
  req = request(url, options);
235
239
  }