unprint 0.18.11 → 0.18.13

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
@@ -228,6 +228,7 @@ Options
228
228
  * `select`: Pre-query and initialize a specific element on the page.
229
229
  * `selectAll`: Pre-query and initialize multiple specific element on the page.
230
230
  * `interface`: Use undici `fetch` (browser-like, default) or `request` (raw)
231
+ * `form`: Encode POST body as urlencoded form rather than JSON
231
232
  * `userAgent`: The default user agent header
232
233
  * `browserUserAgent`: The default user agent header for browser-like requests (`get` interface `fetch` and `browserRequest`)
233
234
  * `apiUserAgent`: The default user agent header for raw requests (`get` interface `request`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unprint",
3
- "version": "0.18.11",
3
+ "version": "0.18.13",
4
4
  "description": "Simplify common web scraping tasks while staying in control of the data.",
5
5
  "main": "src/app.js",
6
6
  "scripts": {},
package/src/app.js CHANGED
@@ -1115,15 +1115,17 @@ function curateHeaders(headers, options) {
1115
1115
  }
1116
1116
 
1117
1117
  function curateCookies(headers) {
1118
- const setCookie = typeof headers.get === 'function'
1119
- ? headers.get('set-cookie')
1120
- : headers['set-cookie'];
1118
+ if (headers) {
1119
+ const setCookie = typeof headers.get === 'function'
1120
+ ? headers.get('set-cookie')
1121
+ : headers['set-cookie'];
1121
1122
 
1122
- if (setCookie) {
1123
- try {
1124
- return cookie.parseCookie(setCookie);
1125
- } catch (_error) {
1126
- // invalid cookie
1123
+ if (setCookie) {
1124
+ try {
1125
+ return cookie.parseCookie(setCookie);
1126
+ } catch (_error) {
1127
+ // invalid cookie
1128
+ }
1127
1129
  }
1128
1130
  }
1129
1131
 
@@ -1134,6 +1136,7 @@ function curateResponse(res, data, options, { url, control, customOptions }) {
1134
1136
  const base = {
1135
1137
  ok: true,
1136
1138
  data,
1139
+ body: data,
1137
1140
  status: res.statusCode || res.status,
1138
1141
  statusText: res.statusText,
1139
1142
  headers: res.headers,
@@ -1433,21 +1436,21 @@ async function browserRequest(url, customOptions = {}) {
1433
1436
  });
1434
1437
  }
1435
1438
 
1436
- function curateRequestBody(body) {
1439
+ function curateRequestBody(body, options) {
1437
1440
  if (!body) {
1438
1441
  return { body };
1439
1442
  }
1440
1443
 
1441
- if (body instanceof undici.FormData) {
1442
- return {
1443
- body: qs.stringify(body),
1444
- headers: {
1445
- 'content-type': 'application/x-www-form-urlencoded',
1446
- },
1447
- };
1448
- }
1449
-
1450
1444
  if (typeof body === 'object') {
1445
+ if (options.form) {
1446
+ return {
1447
+ body: qs.stringify(body),
1448
+ headers: {
1449
+ 'content-type': 'application/x-www-form-urlencoded',
1450
+ },
1451
+ };
1452
+ }
1453
+
1451
1454
  return {
1452
1455
  body: JSON.stringify(body),
1453
1456
  headers: {
@@ -1485,7 +1488,7 @@ async function request(url, body, customOptions = {}, method = 'GET', redirects
1485
1488
 
1486
1489
  events.emit('requestInit', feedbackBase);
1487
1490
 
1488
- const curatedBody = curateRequestBody(body);
1491
+ const curatedBody = curateRequestBody(body, options);
1489
1492
  const curatedCookie = getCookie(options);
1490
1493
 
1491
1494
  const headers = curateHeaders({
@@ -1522,12 +1525,8 @@ async function request(url, body, customOptions = {}, method = 'GET', redirects
1522
1525
  return request(newUrl, body, options, method, redirects + 1);
1523
1526
  }
1524
1527
 
1525
- const data = options.interface === 'fetch'
1526
- ? await res.text()
1527
- : await res.body.text();
1528
-
1529
1528
  if (!(status >= 200 && status < 300)) {
1530
- handleError(new Error(`HTTP response from ${url} not OK (${status} ${res.statusText}): ${data}`), 'HTTP_NOT_OK');
1529
+ handleError(new Error(`HTTP response from ${url} not OK (${status} ${res.statusText})`), 'HTTP_NOT_OK');
1531
1530
 
1532
1531
  events.emit('requestError', {
1533
1532
  ...feedbackBase,
@@ -1546,6 +1545,10 @@ async function request(url, body, customOptions = {}, method = 'GET', redirects
1546
1545
  };
1547
1546
  }
1548
1547
 
1548
+ const data = options.interface === 'fetch'
1549
+ ? await res.text()
1550
+ : await res.body.text();
1551
+
1549
1552
  events.emit('requestSuccess', {
1550
1553
  ...feedbackBase,
1551
1554
  status,
package/tests/init.js CHANGED
@@ -48,7 +48,7 @@ async function initTest() {
48
48
 
49
49
  const proxyRes = await unprint.get('https://api.ipify.org?format=json', {
50
50
  interface: 'request',
51
- useProxy: true,
51
+ useProxy: false,
52
52
  });
53
53
 
54
54
  console.log('JSON RES', jsonRes);