@readme/httpsnippet 10.1.1 → 11.0.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/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { reducer } from './chunk-KT7MO6Z4.js';
2
- import { targets, getHeaderName } from './chunk-532RKFLR.js';
3
- export { addClientPlugin, addTarget, addTargetClient } from './chunk-532RKFLR.js';
2
+ import { targets, getHeaderName } from './chunk-452Q5GGQ.js';
3
+ export { addClientPlugin, addTarget, addTargetClient } from './chunk-452Q5GGQ.js';
4
4
  import './chunk-Y7NI4MMY.js';
5
5
  import { parse, format } from 'url';
6
6
  import { stringify } from 'qs';
@@ -1,10 +1,10 @@
1
1
  'use strict';
2
2
 
3
- var stringifyObject9 = require('stringify-object');
3
+ var stringifyObject7 = require('stringify-object');
4
4
 
5
5
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
6
6
 
7
- var stringifyObject9__default = /*#__PURE__*/_interopDefault(stringifyObject9);
7
+ var stringifyObject7__default = /*#__PURE__*/_interopDefault(stringifyObject7);
8
8
 
9
9
  // src/helpers/code-builder.ts
10
10
  var DEFAULT_INDENTATION_CHARACTER = "";
@@ -959,7 +959,7 @@ var axios = {
959
959
  requestOptions.data = postData.text;
960
960
  }
961
961
  }
962
- const optionString = stringifyObject9__default.default(requestOptions, {
962
+ const optionString = stringifyObject7__default.default(requestOptions, {
963
963
  indent: " ",
964
964
  inlineCharacterLimit: 80
965
965
  }).replace('"[form]"', "form");
@@ -967,12 +967,8 @@ var axios = {
967
967
  blank();
968
968
  push("axios");
969
969
  push(".request(options)", 1);
970
- push(".then(function (response) {", 1);
971
- push("console.log(response.data);", 2);
972
- push("})", 1);
973
- push(".catch(function (error) {", 1);
974
- push("console.error(error);", 2);
975
- push("});", 1);
970
+ push(".then(res => console.log(res.data))", 1);
971
+ push(".catch(err => console.error(err));", 1);
976
972
  return join();
977
973
  }
978
974
  };
@@ -1032,7 +1028,7 @@ var fetch = {
1032
1028
  delete options.headers;
1033
1029
  }
1034
1030
  push(
1035
- `const options = ${stringifyObject9__default.default(options, {
1031
+ `const options = ${stringifyObject7__default.default(options, {
1036
1032
  indent: opts.indent,
1037
1033
  inlineCharacterLimit: 80,
1038
1034
  // The Fetch API body only accepts string parameters, but stringified JSON can be difficult
@@ -1056,8 +1052,8 @@ var fetch = {
1056
1052
  blank();
1057
1053
  }
1058
1054
  push(`fetch('${fullUrl}', options)`);
1059
- push(".then(response => response.json())", 1);
1060
- push(".then(response => console.log(response))", 1);
1055
+ push(".then(res => res.json())", 1);
1056
+ push(".then(res => console.log(res))", 1);
1061
1057
  push(".catch(err => console.error(err));", 1);
1062
1058
  return join();
1063
1059
  }
@@ -1118,11 +1114,11 @@ var jquery = {
1118
1114
  settings.data = postData.text;
1119
1115
  }
1120
1116
  }
1121
- const stringifiedSettings = stringifyObject9__default.default(settings, { indent: opts.indent }).replace("'[form]'", "form");
1117
+ const stringifiedSettings = stringifyObject7__default.default(settings, { indent: opts.indent }).replace("'[form]'", "form");
1122
1118
  push(`const settings = ${stringifiedSettings};`);
1123
1119
  blank();
1124
- push("$.ajax(settings).done(function (response) {");
1125
- push("console.log(response);", 1);
1120
+ push("$.ajax(settings).done(res => {");
1121
+ push("console.log(res);", 1);
1126
1122
  push("});");
1127
1123
  return join();
1128
1124
  }
@@ -1145,7 +1141,7 @@ var xhr = {
1145
1141
  switch (postData.mimeType) {
1146
1142
  case "application/json":
1147
1143
  push(
1148
- `const data = JSON.stringify(${stringifyObject9__default.default(postData.jsonObj, {
1144
+ `const data = JSON.stringify(${stringifyObject7__default.default(postData.jsonObj, {
1149
1145
  indent: opts.indent
1150
1146
  })});`
1151
1147
  );
@@ -1199,7 +1195,7 @@ var javascript = {
1199
1195
  info: {
1200
1196
  key: "javascript",
1201
1197
  title: "JavaScript",
1202
- default: "xhr"
1198
+ default: "fetch"
1203
1199
  },
1204
1200
  clientsById: {
1205
1201
  xhr,
@@ -1338,7 +1334,7 @@ var axios2 = {
1338
1334
  title: "Axios",
1339
1335
  link: "https://github.com/axios/axios",
1340
1336
  description: "Promise based HTTP client for the browser and node.js",
1341
- extname: ".cjs",
1337
+ extname: ".js",
1342
1338
  installation: "npm install axios --save"
1343
1339
  },
1344
1340
  convert: ({ method, fullUrl, allHeaders, postData }, options) => {
@@ -1347,7 +1343,8 @@ var axios2 = {
1347
1343
  ...options
1348
1344
  };
1349
1345
  const { blank, join, push, addPostProcessor } = new CodeBuilder({ indent: opts.indent });
1350
- push("const axios = require('axios');");
1346
+ push("import axios from 'axios';");
1347
+ blank();
1351
1348
  const reqOpts = {
1352
1349
  method,
1353
1350
  url: fullUrl
@@ -1358,8 +1355,6 @@ var axios2 = {
1358
1355
  switch (postData.mimeType) {
1359
1356
  case "application/x-www-form-urlencoded":
1360
1357
  if (postData.params) {
1361
- push("const { URLSearchParams } = require('url');");
1362
- blank();
1363
1358
  push("const encodedParams = new URLSearchParams();");
1364
1359
  postData.params.forEach((param) => {
1365
1360
  push(`encodedParams.set('${param.name}', '${param.value}');`);
@@ -1370,39 +1365,32 @@ var axios2 = {
1370
1365
  }
1371
1366
  break;
1372
1367
  case "application/json":
1373
- blank();
1374
1368
  if (postData.jsonObj) {
1375
1369
  reqOpts.data = postData.jsonObj;
1376
1370
  }
1377
1371
  break;
1378
1372
  default:
1379
- blank();
1380
1373
  if (postData.text) {
1381
1374
  reqOpts.data = postData.text;
1382
1375
  }
1383
1376
  }
1384
- const stringifiedOptions = stringifyObject9__default.default(reqOpts, { indent: " ", inlineCharacterLimit: 80 });
1377
+ const stringifiedOptions = stringifyObject7__default.default(reqOpts, { indent: " ", inlineCharacterLimit: 80 });
1385
1378
  push(`const options = ${stringifiedOptions};`);
1386
1379
  blank();
1387
1380
  push("axios");
1388
1381
  push(".request(options)", 1);
1389
- push(".then(function (response) {", 1);
1390
- push("console.log(response.data);", 2);
1391
- push("})", 1);
1392
- push(".catch(function (error) {", 1);
1393
- push("console.error(error);", 2);
1394
- push("});", 1);
1382
+ push(".then(res => console.log(res.data))", 1);
1383
+ push(".catch(err => console.error(err));", 1);
1395
1384
  return join();
1396
1385
  }
1397
1386
  };
1398
1387
  var fetch2 = {
1399
1388
  info: {
1400
1389
  key: "fetch",
1401
- title: "Fetch",
1402
- link: "https://github.com/bitinn/node-fetch",
1403
- description: "Simplified HTTP node-fetch client",
1404
- extname: ".cjs",
1405
- installation: "npm install node-fetch@2 --save"
1390
+ title: "fetch",
1391
+ link: "https://nodejs.org/docs/latest/api/globals.html#fetch",
1392
+ description: "Perform asynchronous HTTP requests with the Fetch API",
1393
+ extname: ".js"
1406
1394
  },
1407
1395
  convert: ({ method, fullUrl, postData, headersObj, cookies }, options) => {
1408
1396
  const opts = {
@@ -1411,7 +1399,6 @@ var fetch2 = {
1411
1399
  };
1412
1400
  let includeFS = false;
1413
1401
  const { blank, push, join, unshift } = new CodeBuilder({ indent: opts.indent });
1414
- push("const fetch = require('node-fetch');");
1415
1402
  const url = fullUrl;
1416
1403
  const reqOpts = {
1417
1404
  method
@@ -1421,13 +1408,12 @@ var fetch2 = {
1421
1408
  }
1422
1409
  switch (postData.mimeType) {
1423
1410
  case "application/x-www-form-urlencoded":
1424
- unshift("const { URLSearchParams } = require('url');");
1425
1411
  push("const encodedParams = new URLSearchParams();");
1426
- blank();
1427
1412
  postData.params?.forEach((param) => {
1428
1413
  push(`encodedParams.set('${param.name}', '${param.value}');`);
1429
1414
  });
1430
1415
  reqOpts.body = "encodedParams";
1416
+ blank();
1431
1417
  break;
1432
1418
  case "application/json":
1433
1419
  if (postData.jsonObj) {
@@ -1442,9 +1428,7 @@ var fetch2 = {
1442
1428
  if (contentTypeHeader) {
1443
1429
  delete headersObj[contentTypeHeader];
1444
1430
  }
1445
- unshift("const FormData = require('form-data');");
1446
1431
  push("const formData = new FormData();");
1447
- blank();
1448
1432
  postData.params.forEach((param) => {
1449
1433
  if (!param.fileName && !param.fileName && !param.contentType) {
1450
1434
  push(`formData.append('${param.name}', '${param.value}');`);
@@ -1452,9 +1436,13 @@ var fetch2 = {
1452
1436
  }
1453
1437
  if (param.fileName) {
1454
1438
  includeFS = true;
1455
- push(`formData.append('${param.name}', fs.createReadStream('${param.fileName}'));`);
1439
+ push(
1440
+ `formData.append('${param.name}', await new Response(fs.createReadStream('${param.fileName}')).blob());`
1441
+ );
1456
1442
  }
1457
1443
  });
1444
+ reqOpts.body = "formData";
1445
+ blank();
1458
1446
  break;
1459
1447
  default:
1460
1448
  if (postData.text) {
@@ -1470,12 +1458,11 @@ var fetch2 = {
1470
1458
  reqOpts.headers.cookie = cookiesString;
1471
1459
  }
1472
1460
  }
1473
- blank();
1474
1461
  push(`const url = '${url}';`);
1475
1462
  if (reqOpts.headers && !Object.keys(reqOpts.headers).length) {
1476
1463
  delete reqOpts.headers;
1477
1464
  }
1478
- const stringifiedOptions = stringifyObject9__default.default(reqOpts, {
1465
+ const stringifiedOptions = stringifyObject7__default.default(reqOpts, {
1479
1466
  indent: " ",
1480
1467
  inlineCharacterLimit: 80,
1481
1468
  // The Fetch API body only accepts string parameters, but stringified JSON can be difficult to
@@ -1491,17 +1478,13 @@ var fetch2 = {
1491
1478
  push(`const options = ${stringifiedOptions};`);
1492
1479
  blank();
1493
1480
  if (includeFS) {
1494
- unshift("const fs = require('fs');");
1495
- }
1496
- if (postData.params && postData.mimeType === "multipart/form-data") {
1497
- push("options.body = formData;");
1498
- blank();
1481
+ unshift("import fs from 'fs';\n");
1499
1482
  }
1500
1483
  push("fetch(url, options)");
1501
1484
  push(".then(res => res.json())", 1);
1502
1485
  push(".then(json => console.log(json))", 1);
1503
- push(".catch(err => console.error('error:' + err));", 1);
1504
- return join().replace(/'encodedParams'/, "encodedParams").replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")');
1486
+ push(".catch(err => console.error(err));", 1);
1487
+ return join().replace(/'encodedParams'/, "encodedParams").replace(/'formData'/, "formData");
1505
1488
  }
1506
1489
  };
1507
1490
  var native3 = {
@@ -1524,7 +1507,7 @@ var native3 = {
1524
1507
  };
1525
1508
  push(`const http = require('${uriObj.protocol?.replace(":", "")}');`);
1526
1509
  blank();
1527
- push(`const options = ${stringifyObject9__default.default(reqOpts, { indent })};`);
1510
+ push(`const options = ${stringifyObject7__default.default(reqOpts, { indent })};`);
1528
1511
  blank();
1529
1512
  push("const req = http.request(options, function (res) {");
1530
1513
  push("const chunks = [];", 1);
@@ -1544,7 +1527,7 @@ var native3 = {
1544
1527
  if (postData.paramsObj) {
1545
1528
  unshift("const qs = require('querystring');");
1546
1529
  push(
1547
- `req.write(qs.stringify(${stringifyObject9__default.default(postData.paramsObj, {
1530
+ `req.write(qs.stringify(${stringifyObject7__default.default(postData.paramsObj, {
1548
1531
  indent: " ",
1549
1532
  inlineCharacterLimit: 80
1550
1533
  })}));`
@@ -1554,7 +1537,7 @@ var native3 = {
1554
1537
  case "application/json":
1555
1538
  if (postData.jsonObj) {
1556
1539
  push(
1557
- `req.write(JSON.stringify(${stringifyObject9__default.default(postData.jsonObj, {
1540
+ `req.write(JSON.stringify(${stringifyObject7__default.default(postData.jsonObj, {
1558
1541
  indent: " ",
1559
1542
  inlineCharacterLimit: 80
1560
1543
  })}));`
@@ -1563,208 +1546,24 @@ var native3 = {
1563
1546
  break;
1564
1547
  default:
1565
1548
  if (postData.text) {
1566
- push(`req.write(${stringifyObject9__default.default(postData.text, { indent })});`);
1549
+ push(`req.write(${stringifyObject7__default.default(postData.text, { indent })});`);
1567
1550
  }
1568
1551
  }
1569
1552
  push("req.end();");
1570
1553
  return join();
1571
1554
  }
1572
1555
  };
1573
- var request = {
1574
- info: {
1575
- key: "request",
1576
- title: "Request",
1577
- link: "https://github.com/request/request",
1578
- description: "Simplified HTTP request client",
1579
- extname: ".cjs",
1580
- installation: "npm install request --save"
1581
- },
1582
- convert: ({ method, url, fullUrl, postData, headersObj, cookies }, options) => {
1583
- const opts = {
1584
- indent: " ",
1585
- ...options
1586
- };
1587
- let includeFS = false;
1588
- const { push, blank, join, unshift, addPostProcessor } = new CodeBuilder({ indent: opts.indent });
1589
- push("const request = require('request');");
1590
- blank();
1591
- const reqOpts = {
1592
- method,
1593
- url: fullUrl
1594
- };
1595
- if (Object.keys(headersObj).length) {
1596
- reqOpts.headers = headersObj;
1597
- }
1598
- switch (postData.mimeType) {
1599
- case "application/x-www-form-urlencoded":
1600
- reqOpts.form = postData.paramsObj;
1601
- break;
1602
- case "application/json":
1603
- if (postData.jsonObj) {
1604
- reqOpts.body = postData.jsonObj;
1605
- reqOpts.json = true;
1606
- }
1607
- break;
1608
- case "multipart/form-data":
1609
- if (!postData.params) {
1610
- break;
1611
- }
1612
- reqOpts.formData = {};
1613
- postData.params.forEach((param) => {
1614
- if (!param.fileName && !param.fileName && !param.contentType) {
1615
- reqOpts.formData[param.name] = param.value;
1616
- return;
1617
- }
1618
- let attachment = {};
1619
- if (param.fileName) {
1620
- includeFS = true;
1621
- attachment = {
1622
- value: `fs.createReadStream(${param.fileName})`,
1623
- options: {
1624
- filename: param.fileName,
1625
- contentType: param.contentType ? param.contentType : null
1626
- }
1627
- };
1628
- } else if (param.value) {
1629
- attachment.value = param.value;
1630
- }
1631
- reqOpts.formData[param.name] = attachment;
1632
- });
1633
- addPostProcessor((code) => code.replace(/'fs\.createReadStream\((.*)\)'/, "fs.createReadStream('$1')"));
1634
- break;
1635
- default:
1636
- if (postData.text) {
1637
- reqOpts.body = postData.text;
1638
- }
1639
- }
1640
- if (cookies.length) {
1641
- reqOpts.jar = "JAR";
1642
- push("const jar = request.jar();");
1643
- cookies.forEach(({ name, value }) => {
1644
- push(`jar.setCookie(request.cookie('${encodeURIComponent(name)}=${encodeURIComponent(value)}'), '${url}');`);
1645
- });
1646
- blank();
1647
- addPostProcessor((code) => code.replace(/'JAR'/, "jar"));
1648
- }
1649
- if (includeFS) {
1650
- unshift("const fs = require('fs');");
1651
- }
1652
- push(`const options = ${stringifyObject9__default.default(reqOpts, { indent: " ", inlineCharacterLimit: 80 })};`);
1653
- blank();
1654
- push("request(options, function (error, response, body) {");
1655
- push("if (error) throw new Error(error);", 1);
1656
- blank();
1657
- push("console.log(body);", 1);
1658
- push("});");
1659
- return join();
1660
- }
1661
- };
1662
- var unirest2 = {
1663
- info: {
1664
- key: "unirest",
1665
- title: "Unirest",
1666
- link: "http://unirest.io/nodejs.html",
1667
- description: "Lightweight HTTP Request Client Library",
1668
- extname: ".cjs"
1669
- },
1670
- convert: ({ method, url, cookies, queryObj, postData, headersObj }, options) => {
1671
- const opts = {
1672
- indent: " ",
1673
- ...options
1674
- };
1675
- let includeFS = false;
1676
- const { addPostProcessor, blank, join, push, unshift } = new CodeBuilder({
1677
- indent: opts.indent
1678
- });
1679
- push("const unirest = require('unirest');");
1680
- blank();
1681
- push(`const req = unirest('${method}', '${url}');`);
1682
- blank();
1683
- if (cookies.length) {
1684
- push("const CookieJar = unirest.jar();");
1685
- cookies.forEach((cookie) => {
1686
- push(`CookieJar.add('${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}', '${url}');`);
1687
- });
1688
- push("req.jar(CookieJar);");
1689
- blank();
1690
- }
1691
- if (Object.keys(queryObj).length) {
1692
- push(`req.query(${stringifyObject9__default.default(queryObj, { indent: opts.indent })});`);
1693
- blank();
1694
- }
1695
- if (Object.keys(headersObj).length) {
1696
- push(`req.headers(${stringifyObject9__default.default(headersObj, { indent: opts.indent })});`);
1697
- blank();
1698
- }
1699
- switch (postData.mimeType) {
1700
- case "application/x-www-form-urlencoded":
1701
- if (postData.paramsObj) {
1702
- push(`req.form(${stringifyObject9__default.default(postData.paramsObj, { indent: opts.indent })});`);
1703
- blank();
1704
- }
1705
- break;
1706
- case "application/json":
1707
- if (postData.jsonObj) {
1708
- push("req.type('json');");
1709
- push(`req.send(${stringifyObject9__default.default(postData.jsonObj, { indent: opts.indent })});`);
1710
- blank();
1711
- }
1712
- break;
1713
- case "multipart/form-data": {
1714
- if (!postData.params) {
1715
- break;
1716
- }
1717
- const multipart = [];
1718
- postData.params.forEach((param) => {
1719
- const part = {};
1720
- if (param.fileName && !param.value) {
1721
- includeFS = true;
1722
- part.body = `fs.createReadStream('${param.fileName}')`;
1723
- addPostProcessor((code) => code.replace(/'fs\.createReadStream\(\\'(.+)\\'\)'/, "fs.createReadStream('$1')"));
1724
- } else if (param.value) {
1725
- part.body = param.value;
1726
- }
1727
- if (part.body) {
1728
- if (param.contentType) {
1729
- part["content-type"] = param.contentType;
1730
- }
1731
- multipart.push(part);
1732
- }
1733
- });
1734
- push(`req.multipart(${stringifyObject9__default.default(multipart, { indent: opts.indent })});`);
1735
- blank();
1736
- break;
1737
- }
1738
- default:
1739
- if (postData.text) {
1740
- push(`req.send(${stringifyObject9__default.default(postData.text, { indent: opts.indent })});`);
1741
- blank();
1742
- }
1743
- }
1744
- if (includeFS) {
1745
- unshift("const fs = require('fs');");
1746
- }
1747
- push("req.end(function (res) {");
1748
- push("if (res.error) throw new Error(res.error);", 1);
1749
- blank();
1750
- push("console.log(res.body);", 1);
1751
- push("});");
1752
- return join();
1753
- }
1754
- };
1755
1556
 
1756
1557
  // src/targets/node/target.ts
1757
1558
  var node = {
1758
1559
  info: {
1759
1560
  key: "node",
1760
1561
  title: "Node.js",
1761
- default: "native",
1562
+ default: "fetch",
1762
1563
  cli: "node %s"
1763
1564
  },
1764
1565
  clientsById: {
1765
1566
  native: native3,
1766
- request,
1767
- unirest: unirest2,
1768
1567
  axios: axios2,
1769
1568
  fetch: fetch2
1770
1569
  }
@@ -2715,22 +2514,22 @@ var requests = {
2715
2514
  push("}");
2716
2515
  blank();
2717
2516
  }
2718
- let request2 = builtInMethods.includes(method) ? `response = requests.${method.toLowerCase()}(url` : `response = requests.request("${method}", url`;
2517
+ let request = builtInMethods.includes(method) ? `response = requests.${method.toLowerCase()}(url` : `response = requests.request("${method}", url`;
2719
2518
  if (hasPayload) {
2720
2519
  if (jsonPayload) {
2721
- request2 += ", json=payload";
2520
+ request += ", json=payload";
2722
2521
  } else {
2723
- request2 += ", data=payload";
2522
+ request += ", data=payload";
2724
2523
  }
2725
2524
  }
2726
2525
  if (hasFiles) {
2727
- request2 += ", files=files";
2526
+ request += ", files=files";
2728
2527
  }
2729
2528
  if (headerCount > 0) {
2730
- request2 += ", headers=headers";
2529
+ request += ", headers=headers";
2731
2530
  }
2732
- request2 += ")";
2733
- push(request2);
2531
+ request += ")";
2532
+ push(request);
2734
2533
  blank();
2735
2534
  push("print(response.text)");
2736
2535
  return join();
@@ -2815,22 +2614,22 @@ var httr = {
2815
2614
  const setContentType = `content_type("${escapeForDoubleQuotes(postData.mimeType)}")`;
2816
2615
  const otherHeaders = Object.entries(allHeaders).filter(([key]) => !["cookie", "accept", "content-type"].includes(key.toLowerCase())).map(([key, value]) => `'${key}' = '${escapeForSingleQuotes(value)}'`).join(", ");
2817
2616
  const setHeaders = otherHeaders ? `add_headers(${otherHeaders})` : void 0;
2818
- let request2 = `response <- VERB("${method}", url`;
2617
+ let request = `response <- VERB("${method}", url`;
2819
2618
  if (payload) {
2820
- request2 += ", body = payload";
2619
+ request += ", body = payload";
2821
2620
  }
2822
2621
  if (queryString.length) {
2823
- request2 += ", query = queryString";
2622
+ request += ", query = queryString";
2824
2623
  }
2825
2624
  const headerAdditions = [setHeaders, setContentType, setAccept, setCookies].filter((x) => !!x).join(", ");
2826
2625
  if (headerAdditions) {
2827
- request2 += `, ${headerAdditions}`;
2626
+ request += `, ${headerAdditions}`;
2828
2627
  }
2829
2628
  if (postData.text || postData.jsonObj || postData.params) {
2830
- request2 += ", encode = encode";
2629
+ request += ", encode = encode";
2831
2630
  }
2832
- request2 += ")";
2833
- push(request2);
2631
+ request += ")";
2632
+ push(request);
2834
2633
  blank();
2835
2634
  push('content(response, "text")');
2836
2635
  return join();