@readme/httpsnippet 10.1.0 → 10.1.1

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
@@ -109,8 +109,8 @@ console.log(
109
109
  }),
110
110
  );
111
111
 
112
- // generate Node.js: Axios output
113
- console.log(snippet.convert('node', 'axios'));
112
+ // generate Node.js: Unirest output
113
+ console.log(snippet.convert('node', 'unirest'));
114
114
  ```
115
115
 
116
116
  ### addTarget(target)
@@ -1,5 +1,5 @@
1
1
  import { CodeBuilder } from './chunk-Y7NI4MMY.js';
2
- import stringifyObject7 from 'stringify-object';
2
+ import stringifyObject9 from 'stringify-object';
3
3
 
4
4
  // src/helpers/escape.ts
5
5
  function escapeString(rawValue, options = {}) {
@@ -894,7 +894,7 @@ var axios = {
894
894
  requestOptions.data = postData.text;
895
895
  }
896
896
  }
897
- const optionString = stringifyObject7(requestOptions, {
897
+ const optionString = stringifyObject9(requestOptions, {
898
898
  indent: " ",
899
899
  inlineCharacterLimit: 80
900
900
  }).replace('"[form]"', "form");
@@ -902,8 +902,12 @@ var axios = {
902
902
  blank();
903
903
  push("axios");
904
904
  push(".request(options)", 1);
905
- push(".then(res => console.log(res.data))", 1);
906
- push(".catch(err => console.error(err));", 1);
905
+ push(".then(function (response) {", 1);
906
+ push("console.log(response.data);", 2);
907
+ push("})", 1);
908
+ push(".catch(function (error) {", 1);
909
+ push("console.error(error);", 2);
910
+ push("});", 1);
907
911
  return join();
908
912
  }
909
913
  };
@@ -963,7 +967,7 @@ var fetch = {
963
967
  delete options.headers;
964
968
  }
965
969
  push(
966
- `const options = ${stringifyObject7(options, {
970
+ `const options = ${stringifyObject9(options, {
967
971
  indent: opts.indent,
968
972
  inlineCharacterLimit: 80,
969
973
  // The Fetch API body only accepts string parameters, but stringified JSON can be difficult
@@ -987,8 +991,8 @@ var fetch = {
987
991
  blank();
988
992
  }
989
993
  push(`fetch('${fullUrl}', options)`);
990
- push(".then(res => res.json())", 1);
991
- push(".then(res => console.log(res))", 1);
994
+ push(".then(response => response.json())", 1);
995
+ push(".then(response => console.log(response))", 1);
992
996
  push(".catch(err => console.error(err));", 1);
993
997
  return join();
994
998
  }
@@ -1049,11 +1053,11 @@ var jquery = {
1049
1053
  settings.data = postData.text;
1050
1054
  }
1051
1055
  }
1052
- const stringifiedSettings = stringifyObject7(settings, { indent: opts.indent }).replace("'[form]'", "form");
1056
+ const stringifiedSettings = stringifyObject9(settings, { indent: opts.indent }).replace("'[form]'", "form");
1053
1057
  push(`const settings = ${stringifiedSettings};`);
1054
1058
  blank();
1055
- push("$.ajax(settings).done(res => {");
1056
- push("console.log(res);", 1);
1059
+ push("$.ajax(settings).done(function (response) {");
1060
+ push("console.log(response);", 1);
1057
1061
  push("});");
1058
1062
  return join();
1059
1063
  }
@@ -1076,7 +1080,7 @@ var xhr = {
1076
1080
  switch (postData.mimeType) {
1077
1081
  case "application/json":
1078
1082
  push(
1079
- `const data = JSON.stringify(${stringifyObject7(postData.jsonObj, {
1083
+ `const data = JSON.stringify(${stringifyObject9(postData.jsonObj, {
1080
1084
  indent: opts.indent
1081
1085
  })});`
1082
1086
  );
@@ -1130,7 +1134,7 @@ var javascript = {
1130
1134
  info: {
1131
1135
  key: "javascript",
1132
1136
  title: "JavaScript",
1133
- default: "fetch"
1137
+ default: "xhr"
1134
1138
  },
1135
1139
  clientsById: {
1136
1140
  xhr,
@@ -1269,7 +1273,7 @@ var axios2 = {
1269
1273
  title: "Axios",
1270
1274
  link: "https://github.com/axios/axios",
1271
1275
  description: "Promise based HTTP client for the browser and node.js",
1272
- extname: ".js",
1276
+ extname: ".cjs",
1273
1277
  installation: "npm install axios --save"
1274
1278
  },
1275
1279
  convert: ({ method, fullUrl, allHeaders, postData }, options) => {
@@ -1278,8 +1282,7 @@ var axios2 = {
1278
1282
  ...options
1279
1283
  };
1280
1284
  const { blank, join, push, addPostProcessor } = new CodeBuilder({ indent: opts.indent });
1281
- push("import axios from 'axios';");
1282
- blank();
1285
+ push("const axios = require('axios');");
1283
1286
  const reqOpts = {
1284
1287
  method,
1285
1288
  url: fullUrl
@@ -1290,6 +1293,8 @@ var axios2 = {
1290
1293
  switch (postData.mimeType) {
1291
1294
  case "application/x-www-form-urlencoded":
1292
1295
  if (postData.params) {
1296
+ push("const { URLSearchParams } = require('url');");
1297
+ blank();
1293
1298
  push("const encodedParams = new URLSearchParams();");
1294
1299
  postData.params.forEach((param) => {
1295
1300
  push(`encodedParams.set('${param.name}', '${param.value}');`);
@@ -1300,32 +1305,39 @@ var axios2 = {
1300
1305
  }
1301
1306
  break;
1302
1307
  case "application/json":
1308
+ blank();
1303
1309
  if (postData.jsonObj) {
1304
1310
  reqOpts.data = postData.jsonObj;
1305
1311
  }
1306
1312
  break;
1307
1313
  default:
1314
+ blank();
1308
1315
  if (postData.text) {
1309
1316
  reqOpts.data = postData.text;
1310
1317
  }
1311
1318
  }
1312
- const stringifiedOptions = stringifyObject7(reqOpts, { indent: " ", inlineCharacterLimit: 80 });
1319
+ const stringifiedOptions = stringifyObject9(reqOpts, { indent: " ", inlineCharacterLimit: 80 });
1313
1320
  push(`const options = ${stringifiedOptions};`);
1314
1321
  blank();
1315
1322
  push("axios");
1316
1323
  push(".request(options)", 1);
1317
- push(".then(res => console.log(res.data))", 1);
1318
- push(".catch(err => console.error(err));", 1);
1324
+ push(".then(function (response) {", 1);
1325
+ push("console.log(response.data);", 2);
1326
+ push("})", 1);
1327
+ push(".catch(function (error) {", 1);
1328
+ push("console.error(error);", 2);
1329
+ push("});", 1);
1319
1330
  return join();
1320
1331
  }
1321
1332
  };
1322
1333
  var fetch2 = {
1323
1334
  info: {
1324
1335
  key: "fetch",
1325
- title: "fetch",
1326
- link: "https://nodejs.org/docs/latest/api/globals.html#fetch",
1327
- description: "Perform asynchronous HTTP requests with the Fetch API",
1328
- extname: ".js"
1336
+ title: "Fetch",
1337
+ link: "https://github.com/bitinn/node-fetch",
1338
+ description: "Simplified HTTP node-fetch client",
1339
+ extname: ".cjs",
1340
+ installation: "npm install node-fetch@2 --save"
1329
1341
  },
1330
1342
  convert: ({ method, fullUrl, postData, headersObj, cookies }, options) => {
1331
1343
  const opts = {
@@ -1334,6 +1346,7 @@ var fetch2 = {
1334
1346
  };
1335
1347
  let includeFS = false;
1336
1348
  const { blank, push, join, unshift } = new CodeBuilder({ indent: opts.indent });
1349
+ push("const fetch = require('node-fetch');");
1337
1350
  const url = fullUrl;
1338
1351
  const reqOpts = {
1339
1352
  method
@@ -1343,12 +1356,13 @@ var fetch2 = {
1343
1356
  }
1344
1357
  switch (postData.mimeType) {
1345
1358
  case "application/x-www-form-urlencoded":
1359
+ unshift("const { URLSearchParams } = require('url');");
1346
1360
  push("const encodedParams = new URLSearchParams();");
1361
+ blank();
1347
1362
  postData.params?.forEach((param) => {
1348
1363
  push(`encodedParams.set('${param.name}', '${param.value}');`);
1349
1364
  });
1350
1365
  reqOpts.body = "encodedParams";
1351
- blank();
1352
1366
  break;
1353
1367
  case "application/json":
1354
1368
  if (postData.jsonObj) {
@@ -1363,7 +1377,9 @@ var fetch2 = {
1363
1377
  if (contentTypeHeader) {
1364
1378
  delete headersObj[contentTypeHeader];
1365
1379
  }
1380
+ unshift("const FormData = require('form-data');");
1366
1381
  push("const formData = new FormData();");
1382
+ blank();
1367
1383
  postData.params.forEach((param) => {
1368
1384
  if (!param.fileName && !param.fileName && !param.contentType) {
1369
1385
  push(`formData.append('${param.name}', '${param.value}');`);
@@ -1371,13 +1387,9 @@ var fetch2 = {
1371
1387
  }
1372
1388
  if (param.fileName) {
1373
1389
  includeFS = true;
1374
- push(
1375
- `formData.append('${param.name}', await new Response(fs.createReadStream('${param.fileName}')).blob());`
1376
- );
1390
+ push(`formData.append('${param.name}', fs.createReadStream('${param.fileName}'));`);
1377
1391
  }
1378
1392
  });
1379
- reqOpts.body = "formData";
1380
- blank();
1381
1393
  break;
1382
1394
  default:
1383
1395
  if (postData.text) {
@@ -1393,11 +1405,12 @@ var fetch2 = {
1393
1405
  reqOpts.headers.cookie = cookiesString;
1394
1406
  }
1395
1407
  }
1408
+ blank();
1396
1409
  push(`const url = '${url}';`);
1397
1410
  if (reqOpts.headers && !Object.keys(reqOpts.headers).length) {
1398
1411
  delete reqOpts.headers;
1399
1412
  }
1400
- const stringifiedOptions = stringifyObject7(reqOpts, {
1413
+ const stringifiedOptions = stringifyObject9(reqOpts, {
1401
1414
  indent: " ",
1402
1415
  inlineCharacterLimit: 80,
1403
1416
  // The Fetch API body only accepts string parameters, but stringified JSON can be difficult to
@@ -1413,13 +1426,17 @@ var fetch2 = {
1413
1426
  push(`const options = ${stringifiedOptions};`);
1414
1427
  blank();
1415
1428
  if (includeFS) {
1416
- unshift("import fs from 'fs';\n");
1429
+ unshift("const fs = require('fs');");
1430
+ }
1431
+ if (postData.params && postData.mimeType === "multipart/form-data") {
1432
+ push("options.body = formData;");
1433
+ blank();
1417
1434
  }
1418
1435
  push("fetch(url, options)");
1419
1436
  push(".then(res => res.json())", 1);
1420
1437
  push(".then(json => console.log(json))", 1);
1421
- push(".catch(err => console.error(err));", 1);
1422
- return join().replace(/'encodedParams'/, "encodedParams").replace(/'formData'/, "formData");
1438
+ push(".catch(err => console.error('error:' + err));", 1);
1439
+ return join().replace(/'encodedParams'/, "encodedParams").replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")');
1423
1440
  }
1424
1441
  };
1425
1442
  var native3 = {
@@ -1442,7 +1459,7 @@ var native3 = {
1442
1459
  };
1443
1460
  push(`const http = require('${uriObj.protocol?.replace(":", "")}');`);
1444
1461
  blank();
1445
- push(`const options = ${stringifyObject7(reqOpts, { indent })};`);
1462
+ push(`const options = ${stringifyObject9(reqOpts, { indent })};`);
1446
1463
  blank();
1447
1464
  push("const req = http.request(options, function (res) {");
1448
1465
  push("const chunks = [];", 1);
@@ -1462,7 +1479,7 @@ var native3 = {
1462
1479
  if (postData.paramsObj) {
1463
1480
  unshift("const qs = require('querystring');");
1464
1481
  push(
1465
- `req.write(qs.stringify(${stringifyObject7(postData.paramsObj, {
1482
+ `req.write(qs.stringify(${stringifyObject9(postData.paramsObj, {
1466
1483
  indent: " ",
1467
1484
  inlineCharacterLimit: 80
1468
1485
  })}));`
@@ -1472,7 +1489,7 @@ var native3 = {
1472
1489
  case "application/json":
1473
1490
  if (postData.jsonObj) {
1474
1491
  push(
1475
- `req.write(JSON.stringify(${stringifyObject7(postData.jsonObj, {
1492
+ `req.write(JSON.stringify(${stringifyObject9(postData.jsonObj, {
1476
1493
  indent: " ",
1477
1494
  inlineCharacterLimit: 80
1478
1495
  })}));`
@@ -1481,24 +1498,208 @@ var native3 = {
1481
1498
  break;
1482
1499
  default:
1483
1500
  if (postData.text) {
1484
- push(`req.write(${stringifyObject7(postData.text, { indent })});`);
1501
+ push(`req.write(${stringifyObject9(postData.text, { indent })});`);
1485
1502
  }
1486
1503
  }
1487
1504
  push("req.end();");
1488
1505
  return join();
1489
1506
  }
1490
1507
  };
1508
+ var request = {
1509
+ info: {
1510
+ key: "request",
1511
+ title: "Request",
1512
+ link: "https://github.com/request/request",
1513
+ description: "Simplified HTTP request client",
1514
+ extname: ".cjs",
1515
+ installation: "npm install request --save"
1516
+ },
1517
+ convert: ({ method, url, fullUrl, postData, headersObj, cookies }, options) => {
1518
+ const opts = {
1519
+ indent: " ",
1520
+ ...options
1521
+ };
1522
+ let includeFS = false;
1523
+ const { push, blank, join, unshift, addPostProcessor } = new CodeBuilder({ indent: opts.indent });
1524
+ push("const request = require('request');");
1525
+ blank();
1526
+ const reqOpts = {
1527
+ method,
1528
+ url: fullUrl
1529
+ };
1530
+ if (Object.keys(headersObj).length) {
1531
+ reqOpts.headers = headersObj;
1532
+ }
1533
+ switch (postData.mimeType) {
1534
+ case "application/x-www-form-urlencoded":
1535
+ reqOpts.form = postData.paramsObj;
1536
+ break;
1537
+ case "application/json":
1538
+ if (postData.jsonObj) {
1539
+ reqOpts.body = postData.jsonObj;
1540
+ reqOpts.json = true;
1541
+ }
1542
+ break;
1543
+ case "multipart/form-data":
1544
+ if (!postData.params) {
1545
+ break;
1546
+ }
1547
+ reqOpts.formData = {};
1548
+ postData.params.forEach((param) => {
1549
+ if (!param.fileName && !param.fileName && !param.contentType) {
1550
+ reqOpts.formData[param.name] = param.value;
1551
+ return;
1552
+ }
1553
+ let attachment = {};
1554
+ if (param.fileName) {
1555
+ includeFS = true;
1556
+ attachment = {
1557
+ value: `fs.createReadStream(${param.fileName})`,
1558
+ options: {
1559
+ filename: param.fileName,
1560
+ contentType: param.contentType ? param.contentType : null
1561
+ }
1562
+ };
1563
+ } else if (param.value) {
1564
+ attachment.value = param.value;
1565
+ }
1566
+ reqOpts.formData[param.name] = attachment;
1567
+ });
1568
+ addPostProcessor((code) => code.replace(/'fs\.createReadStream\((.*)\)'/, "fs.createReadStream('$1')"));
1569
+ break;
1570
+ default:
1571
+ if (postData.text) {
1572
+ reqOpts.body = postData.text;
1573
+ }
1574
+ }
1575
+ if (cookies.length) {
1576
+ reqOpts.jar = "JAR";
1577
+ push("const jar = request.jar();");
1578
+ cookies.forEach(({ name, value }) => {
1579
+ push(`jar.setCookie(request.cookie('${encodeURIComponent(name)}=${encodeURIComponent(value)}'), '${url}');`);
1580
+ });
1581
+ blank();
1582
+ addPostProcessor((code) => code.replace(/'JAR'/, "jar"));
1583
+ }
1584
+ if (includeFS) {
1585
+ unshift("const fs = require('fs');");
1586
+ }
1587
+ push(`const options = ${stringifyObject9(reqOpts, { indent: " ", inlineCharacterLimit: 80 })};`);
1588
+ blank();
1589
+ push("request(options, function (error, response, body) {");
1590
+ push("if (error) throw new Error(error);", 1);
1591
+ blank();
1592
+ push("console.log(body);", 1);
1593
+ push("});");
1594
+ return join();
1595
+ }
1596
+ };
1597
+ var unirest2 = {
1598
+ info: {
1599
+ key: "unirest",
1600
+ title: "Unirest",
1601
+ link: "http://unirest.io/nodejs.html",
1602
+ description: "Lightweight HTTP Request Client Library",
1603
+ extname: ".cjs"
1604
+ },
1605
+ convert: ({ method, url, cookies, queryObj, postData, headersObj }, options) => {
1606
+ const opts = {
1607
+ indent: " ",
1608
+ ...options
1609
+ };
1610
+ let includeFS = false;
1611
+ const { addPostProcessor, blank, join, push, unshift } = new CodeBuilder({
1612
+ indent: opts.indent
1613
+ });
1614
+ push("const unirest = require('unirest');");
1615
+ blank();
1616
+ push(`const req = unirest('${method}', '${url}');`);
1617
+ blank();
1618
+ if (cookies.length) {
1619
+ push("const CookieJar = unirest.jar();");
1620
+ cookies.forEach((cookie) => {
1621
+ push(`CookieJar.add('${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}', '${url}');`);
1622
+ });
1623
+ push("req.jar(CookieJar);");
1624
+ blank();
1625
+ }
1626
+ if (Object.keys(queryObj).length) {
1627
+ push(`req.query(${stringifyObject9(queryObj, { indent: opts.indent })});`);
1628
+ blank();
1629
+ }
1630
+ if (Object.keys(headersObj).length) {
1631
+ push(`req.headers(${stringifyObject9(headersObj, { indent: opts.indent })});`);
1632
+ blank();
1633
+ }
1634
+ switch (postData.mimeType) {
1635
+ case "application/x-www-form-urlencoded":
1636
+ if (postData.paramsObj) {
1637
+ push(`req.form(${stringifyObject9(postData.paramsObj, { indent: opts.indent })});`);
1638
+ blank();
1639
+ }
1640
+ break;
1641
+ case "application/json":
1642
+ if (postData.jsonObj) {
1643
+ push("req.type('json');");
1644
+ push(`req.send(${stringifyObject9(postData.jsonObj, { indent: opts.indent })});`);
1645
+ blank();
1646
+ }
1647
+ break;
1648
+ case "multipart/form-data": {
1649
+ if (!postData.params) {
1650
+ break;
1651
+ }
1652
+ const multipart = [];
1653
+ postData.params.forEach((param) => {
1654
+ const part = {};
1655
+ if (param.fileName && !param.value) {
1656
+ includeFS = true;
1657
+ part.body = `fs.createReadStream('${param.fileName}')`;
1658
+ addPostProcessor((code) => code.replace(/'fs\.createReadStream\(\\'(.+)\\'\)'/, "fs.createReadStream('$1')"));
1659
+ } else if (param.value) {
1660
+ part.body = param.value;
1661
+ }
1662
+ if (part.body) {
1663
+ if (param.contentType) {
1664
+ part["content-type"] = param.contentType;
1665
+ }
1666
+ multipart.push(part);
1667
+ }
1668
+ });
1669
+ push(`req.multipart(${stringifyObject9(multipart, { indent: opts.indent })});`);
1670
+ blank();
1671
+ break;
1672
+ }
1673
+ default:
1674
+ if (postData.text) {
1675
+ push(`req.send(${stringifyObject9(postData.text, { indent: opts.indent })});`);
1676
+ blank();
1677
+ }
1678
+ }
1679
+ if (includeFS) {
1680
+ unshift("const fs = require('fs');");
1681
+ }
1682
+ push("req.end(function (res) {");
1683
+ push("if (res.error) throw new Error(res.error);", 1);
1684
+ blank();
1685
+ push("console.log(res.body);", 1);
1686
+ push("});");
1687
+ return join();
1688
+ }
1689
+ };
1491
1690
 
1492
1691
  // src/targets/node/target.ts
1493
1692
  var node = {
1494
1693
  info: {
1495
1694
  key: "node",
1496
1695
  title: "Node.js",
1497
- default: "fetch",
1696
+ default: "native",
1498
1697
  cli: "node %s"
1499
1698
  },
1500
1699
  clientsById: {
1501
1700
  native: native3,
1701
+ request,
1702
+ unirest: unirest2,
1502
1703
  axios: axios2,
1503
1704
  fetch: fetch2
1504
1705
  }
@@ -2449,22 +2650,22 @@ var requests = {
2449
2650
  push("}");
2450
2651
  blank();
2451
2652
  }
2452
- let request = builtInMethods.includes(method) ? `response = requests.${method.toLowerCase()}(url` : `response = requests.request("${method}", url`;
2653
+ let request2 = builtInMethods.includes(method) ? `response = requests.${method.toLowerCase()}(url` : `response = requests.request("${method}", url`;
2453
2654
  if (hasPayload) {
2454
2655
  if (jsonPayload) {
2455
- request += ", json=payload";
2656
+ request2 += ", json=payload";
2456
2657
  } else {
2457
- request += ", data=payload";
2658
+ request2 += ", data=payload";
2458
2659
  }
2459
2660
  }
2460
2661
  if (hasFiles) {
2461
- request += ", files=files";
2662
+ request2 += ", files=files";
2462
2663
  }
2463
2664
  if (headerCount > 0) {
2464
- request += ", headers=headers";
2665
+ request2 += ", headers=headers";
2465
2666
  }
2466
- request += ")";
2467
- push(request);
2667
+ request2 += ")";
2668
+ push(request2);
2468
2669
  blank();
2469
2670
  push("print(response.text)");
2470
2671
  return join();
@@ -2549,22 +2750,22 @@ var httr = {
2549
2750
  const setContentType = `content_type("${escapeForDoubleQuotes(postData.mimeType)}")`;
2550
2751
  const otherHeaders = Object.entries(allHeaders).filter(([key]) => !["cookie", "accept", "content-type"].includes(key.toLowerCase())).map(([key, value]) => `'${key}' = '${escapeForSingleQuotes(value)}'`).join(", ");
2551
2752
  const setHeaders = otherHeaders ? `add_headers(${otherHeaders})` : void 0;
2552
- let request = `response <- VERB("${method}", url`;
2753
+ let request2 = `response <- VERB("${method}", url`;
2553
2754
  if (payload) {
2554
- request += ", body = payload";
2755
+ request2 += ", body = payload";
2555
2756
  }
2556
2757
  if (queryString.length) {
2557
- request += ", query = queryString";
2758
+ request2 += ", query = queryString";
2558
2759
  }
2559
2760
  const headerAdditions = [setHeaders, setContentType, setAccept, setCookies].filter((x) => !!x).join(", ");
2560
2761
  if (headerAdditions) {
2561
- request += `, ${headerAdditions}`;
2762
+ request2 += `, ${headerAdditions}`;
2562
2763
  }
2563
2764
  if (postData.text || postData.jsonObj || postData.params) {
2564
- request += ", encode = encode";
2765
+ request2 += ", encode = encode";
2565
2766
  }
2566
- request += ")";
2567
- push(request);
2767
+ request2 += ")";
2768
+ push(request2);
2568
2769
  blank();
2569
2770
  push('content(response, "text")');
2570
2771
  return join();
@@ -3250,5 +3451,5 @@ var addTargetClient = (targetId, client) => {
3250
3451
  };
3251
3452
 
3252
3453
  export { addClientPlugin, addTarget, addTargetClient, getHeaderName, isClient, isTarget, targets };
3253
- //# sourceMappingURL=chunk-452Q5GGQ.js.map
3254
- //# sourceMappingURL=chunk-452Q5GGQ.js.map
3454
+ //# sourceMappingURL=chunk-532RKFLR.js.map
3455
+ //# sourceMappingURL=chunk-532RKFLR.js.map