unprint 0.18.9 → 0.18.12

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.
Files changed (3) hide show
  1. package/README.md +1 -0
  2. package/package.json +1 -1
  3. package/src/app.js +31 -11
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.9",
3
+ "version": "0.18.12",
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
@@ -1114,6 +1114,22 @@ function curateHeaders(headers, options) {
1114
1114
  return headers;
1115
1115
  }
1116
1116
 
1117
+ function curateCookies(headers) {
1118
+ const setCookie = typeof headers.get === 'function'
1119
+ ? headers.get('set-cookie')
1120
+ : headers['set-cookie'];
1121
+
1122
+ if (setCookie) {
1123
+ try {
1124
+ return cookie.parseCookie(setCookie);
1125
+ } catch (_error) {
1126
+ // invalid cookie
1127
+ }
1128
+ }
1129
+
1130
+ return null;
1131
+ }
1132
+
1117
1133
  function curateResponse(res, data, options, { url, control, customOptions }) {
1118
1134
  const base = {
1119
1135
  ok: true,
@@ -1121,6 +1137,7 @@ function curateResponse(res, data, options, { url, control, customOptions }) {
1121
1137
  status: res.statusCode || res.status,
1122
1138
  statusText: res.statusText,
1123
1139
  headers: res.headers,
1140
+ cookies: curateCookies(res.headers),
1124
1141
  response: res,
1125
1142
  res,
1126
1143
  control,
@@ -1355,6 +1372,7 @@ async function browserRequest(url, customOptions = {}) {
1355
1372
  status,
1356
1373
  statusText,
1357
1374
  headers,
1375
+ cookies: curateCookies(headers),
1358
1376
  response: res,
1359
1377
  res,
1360
1378
  };
@@ -1380,6 +1398,7 @@ async function browserRequest(url, customOptions = {}) {
1380
1398
  status,
1381
1399
  statusText,
1382
1400
  headers,
1401
+ cookies: curateCookies(headers),
1383
1402
  response: res,
1384
1403
  res,
1385
1404
  };
@@ -1414,21 +1433,21 @@ async function browserRequest(url, customOptions = {}) {
1414
1433
  });
1415
1434
  }
1416
1435
 
1417
- function curateRequestBody(body) {
1436
+ function curateRequestBody(body, options) {
1418
1437
  if (!body) {
1419
1438
  return { body };
1420
1439
  }
1421
1440
 
1422
- if (body instanceof undici.FormData) {
1423
- return {
1424
- body: qs.stringify(body),
1425
- headers: {
1426
- 'content-type': 'application/x-www-form-urlencoded',
1427
- },
1428
- };
1429
- }
1430
-
1431
1441
  if (typeof body === 'object') {
1442
+ if (options.form) {
1443
+ return {
1444
+ body: qs.stringify(body),
1445
+ headers: {
1446
+ 'content-type': 'application/x-www-form-urlencoded',
1447
+ },
1448
+ };
1449
+ }
1450
+
1432
1451
  return {
1433
1452
  body: JSON.stringify(body),
1434
1453
  headers: {
@@ -1466,7 +1485,7 @@ async function request(url, body, customOptions = {}, method = 'GET', redirects
1466
1485
 
1467
1486
  events.emit('requestInit', feedbackBase);
1468
1487
 
1469
- const curatedBody = curateRequestBody(body);
1488
+ const curatedBody = curateRequestBody(body, options);
1470
1489
  const curatedCookie = getCookie(options);
1471
1490
 
1472
1491
  const headers = curateHeaders({
@@ -1521,6 +1540,7 @@ async function request(url, body, customOptions = {}, method = 'GET', redirects
1521
1540
  status,
1522
1541
  statusText: res.statusText,
1523
1542
  headers: res.headers,
1543
+ cookies: curateCookies(res.headers),
1524
1544
  response: res,
1525
1545
  res,
1526
1546
  };