@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/README.md CHANGED
@@ -109,8 +109,8 @@ console.log(
109
109
  }),
110
110
  );
111
111
 
112
- // generate Node.js: Unirest output
113
- console.log(snippet.convert('node', 'unirest'));
112
+ // generate Node.js: Axios output
113
+ console.log(snippet.convert('node', 'axios'));
114
114
  ```
115
115
 
116
116
  ### addTarget(target)
@@ -1,5 +1,5 @@
1
1
  import { CodeBuilder } from './chunk-Y7NI4MMY.js';
2
- import stringifyObject9 from 'stringify-object';
2
+ import stringifyObject7 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 = stringifyObject9(requestOptions, {
897
+ const optionString = stringifyObject7(requestOptions, {
898
898
  indent: " ",
899
899
  inlineCharacterLimit: 80
900
900
  }).replace('"[form]"', "form");
@@ -902,12 +902,8 @@ var axios = {
902
902
  blank();
903
903
  push("axios");
904
904
  push(".request(options)", 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);
905
+ push(".then(res => console.log(res.data))", 1);
906
+ push(".catch(err => console.error(err));", 1);
911
907
  return join();
912
908
  }
913
909
  };
@@ -967,7 +963,7 @@ var fetch = {
967
963
  delete options.headers;
968
964
  }
969
965
  push(
970
- `const options = ${stringifyObject9(options, {
966
+ `const options = ${stringifyObject7(options, {
971
967
  indent: opts.indent,
972
968
  inlineCharacterLimit: 80,
973
969
  // The Fetch API body only accepts string parameters, but stringified JSON can be difficult
@@ -991,8 +987,8 @@ var fetch = {
991
987
  blank();
992
988
  }
993
989
  push(`fetch('${fullUrl}', options)`);
994
- push(".then(response => response.json())", 1);
995
- push(".then(response => console.log(response))", 1);
990
+ push(".then(res => res.json())", 1);
991
+ push(".then(res => console.log(res))", 1);
996
992
  push(".catch(err => console.error(err));", 1);
997
993
  return join();
998
994
  }
@@ -1053,11 +1049,11 @@ var jquery = {
1053
1049
  settings.data = postData.text;
1054
1050
  }
1055
1051
  }
1056
- const stringifiedSettings = stringifyObject9(settings, { indent: opts.indent }).replace("'[form]'", "form");
1052
+ const stringifiedSettings = stringifyObject7(settings, { indent: opts.indent }).replace("'[form]'", "form");
1057
1053
  push(`const settings = ${stringifiedSettings};`);
1058
1054
  blank();
1059
- push("$.ajax(settings).done(function (response) {");
1060
- push("console.log(response);", 1);
1055
+ push("$.ajax(settings).done(res => {");
1056
+ push("console.log(res);", 1);
1061
1057
  push("});");
1062
1058
  return join();
1063
1059
  }
@@ -1080,7 +1076,7 @@ var xhr = {
1080
1076
  switch (postData.mimeType) {
1081
1077
  case "application/json":
1082
1078
  push(
1083
- `const data = JSON.stringify(${stringifyObject9(postData.jsonObj, {
1079
+ `const data = JSON.stringify(${stringifyObject7(postData.jsonObj, {
1084
1080
  indent: opts.indent
1085
1081
  })});`
1086
1082
  );
@@ -1134,7 +1130,7 @@ var javascript = {
1134
1130
  info: {
1135
1131
  key: "javascript",
1136
1132
  title: "JavaScript",
1137
- default: "xhr"
1133
+ default: "fetch"
1138
1134
  },
1139
1135
  clientsById: {
1140
1136
  xhr,
@@ -1273,7 +1269,7 @@ var axios2 = {
1273
1269
  title: "Axios",
1274
1270
  link: "https://github.com/axios/axios",
1275
1271
  description: "Promise based HTTP client for the browser and node.js",
1276
- extname: ".cjs",
1272
+ extname: ".js",
1277
1273
  installation: "npm install axios --save"
1278
1274
  },
1279
1275
  convert: ({ method, fullUrl, allHeaders, postData }, options) => {
@@ -1282,7 +1278,8 @@ var axios2 = {
1282
1278
  ...options
1283
1279
  };
1284
1280
  const { blank, join, push, addPostProcessor } = new CodeBuilder({ indent: opts.indent });
1285
- push("const axios = require('axios');");
1281
+ push("import axios from 'axios';");
1282
+ blank();
1286
1283
  const reqOpts = {
1287
1284
  method,
1288
1285
  url: fullUrl
@@ -1293,8 +1290,6 @@ var axios2 = {
1293
1290
  switch (postData.mimeType) {
1294
1291
  case "application/x-www-form-urlencoded":
1295
1292
  if (postData.params) {
1296
- push("const { URLSearchParams } = require('url');");
1297
- blank();
1298
1293
  push("const encodedParams = new URLSearchParams();");
1299
1294
  postData.params.forEach((param) => {
1300
1295
  push(`encodedParams.set('${param.name}', '${param.value}');`);
@@ -1305,39 +1300,32 @@ var axios2 = {
1305
1300
  }
1306
1301
  break;
1307
1302
  case "application/json":
1308
- blank();
1309
1303
  if (postData.jsonObj) {
1310
1304
  reqOpts.data = postData.jsonObj;
1311
1305
  }
1312
1306
  break;
1313
1307
  default:
1314
- blank();
1315
1308
  if (postData.text) {
1316
1309
  reqOpts.data = postData.text;
1317
1310
  }
1318
1311
  }
1319
- const stringifiedOptions = stringifyObject9(reqOpts, { indent: " ", inlineCharacterLimit: 80 });
1312
+ const stringifiedOptions = stringifyObject7(reqOpts, { indent: " ", inlineCharacterLimit: 80 });
1320
1313
  push(`const options = ${stringifiedOptions};`);
1321
1314
  blank();
1322
1315
  push("axios");
1323
1316
  push(".request(options)", 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);
1317
+ push(".then(res => console.log(res.data))", 1);
1318
+ push(".catch(err => console.error(err));", 1);
1330
1319
  return join();
1331
1320
  }
1332
1321
  };
1333
1322
  var fetch2 = {
1334
1323
  info: {
1335
1324
  key: "fetch",
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"
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"
1341
1329
  },
1342
1330
  convert: ({ method, fullUrl, postData, headersObj, cookies }, options) => {
1343
1331
  const opts = {
@@ -1346,7 +1334,6 @@ var fetch2 = {
1346
1334
  };
1347
1335
  let includeFS = false;
1348
1336
  const { blank, push, join, unshift } = new CodeBuilder({ indent: opts.indent });
1349
- push("const fetch = require('node-fetch');");
1350
1337
  const url = fullUrl;
1351
1338
  const reqOpts = {
1352
1339
  method
@@ -1356,13 +1343,12 @@ var fetch2 = {
1356
1343
  }
1357
1344
  switch (postData.mimeType) {
1358
1345
  case "application/x-www-form-urlencoded":
1359
- unshift("const { URLSearchParams } = require('url');");
1360
1346
  push("const encodedParams = new URLSearchParams();");
1361
- blank();
1362
1347
  postData.params?.forEach((param) => {
1363
1348
  push(`encodedParams.set('${param.name}', '${param.value}');`);
1364
1349
  });
1365
1350
  reqOpts.body = "encodedParams";
1351
+ blank();
1366
1352
  break;
1367
1353
  case "application/json":
1368
1354
  if (postData.jsonObj) {
@@ -1377,9 +1363,7 @@ var fetch2 = {
1377
1363
  if (contentTypeHeader) {
1378
1364
  delete headersObj[contentTypeHeader];
1379
1365
  }
1380
- unshift("const FormData = require('form-data');");
1381
1366
  push("const formData = new FormData();");
1382
- blank();
1383
1367
  postData.params.forEach((param) => {
1384
1368
  if (!param.fileName && !param.fileName && !param.contentType) {
1385
1369
  push(`formData.append('${param.name}', '${param.value}');`);
@@ -1387,9 +1371,13 @@ var fetch2 = {
1387
1371
  }
1388
1372
  if (param.fileName) {
1389
1373
  includeFS = true;
1390
- push(`formData.append('${param.name}', fs.createReadStream('${param.fileName}'));`);
1374
+ push(
1375
+ `formData.append('${param.name}', await new Response(fs.createReadStream('${param.fileName}')).blob());`
1376
+ );
1391
1377
  }
1392
1378
  });
1379
+ reqOpts.body = "formData";
1380
+ blank();
1393
1381
  break;
1394
1382
  default:
1395
1383
  if (postData.text) {
@@ -1405,12 +1393,11 @@ var fetch2 = {
1405
1393
  reqOpts.headers.cookie = cookiesString;
1406
1394
  }
1407
1395
  }
1408
- blank();
1409
1396
  push(`const url = '${url}';`);
1410
1397
  if (reqOpts.headers && !Object.keys(reqOpts.headers).length) {
1411
1398
  delete reqOpts.headers;
1412
1399
  }
1413
- const stringifiedOptions = stringifyObject9(reqOpts, {
1400
+ const stringifiedOptions = stringifyObject7(reqOpts, {
1414
1401
  indent: " ",
1415
1402
  inlineCharacterLimit: 80,
1416
1403
  // The Fetch API body only accepts string parameters, but stringified JSON can be difficult to
@@ -1426,17 +1413,13 @@ var fetch2 = {
1426
1413
  push(`const options = ${stringifiedOptions};`);
1427
1414
  blank();
1428
1415
  if (includeFS) {
1429
- unshift("const fs = require('fs');");
1430
- }
1431
- if (postData.params && postData.mimeType === "multipart/form-data") {
1432
- push("options.body = formData;");
1433
- blank();
1416
+ unshift("import fs from 'fs';\n");
1434
1417
  }
1435
1418
  push("fetch(url, options)");
1436
1419
  push(".then(res => res.json())", 1);
1437
1420
  push(".then(json => console.log(json))", 1);
1438
- push(".catch(err => console.error('error:' + err));", 1);
1439
- return join().replace(/'encodedParams'/, "encodedParams").replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")');
1421
+ push(".catch(err => console.error(err));", 1);
1422
+ return join().replace(/'encodedParams'/, "encodedParams").replace(/'formData'/, "formData");
1440
1423
  }
1441
1424
  };
1442
1425
  var native3 = {
@@ -1459,7 +1442,7 @@ var native3 = {
1459
1442
  };
1460
1443
  push(`const http = require('${uriObj.protocol?.replace(":", "")}');`);
1461
1444
  blank();
1462
- push(`const options = ${stringifyObject9(reqOpts, { indent })};`);
1445
+ push(`const options = ${stringifyObject7(reqOpts, { indent })};`);
1463
1446
  blank();
1464
1447
  push("const req = http.request(options, function (res) {");
1465
1448
  push("const chunks = [];", 1);
@@ -1479,7 +1462,7 @@ var native3 = {
1479
1462
  if (postData.paramsObj) {
1480
1463
  unshift("const qs = require('querystring');");
1481
1464
  push(
1482
- `req.write(qs.stringify(${stringifyObject9(postData.paramsObj, {
1465
+ `req.write(qs.stringify(${stringifyObject7(postData.paramsObj, {
1483
1466
  indent: " ",
1484
1467
  inlineCharacterLimit: 80
1485
1468
  })}));`
@@ -1489,7 +1472,7 @@ var native3 = {
1489
1472
  case "application/json":
1490
1473
  if (postData.jsonObj) {
1491
1474
  push(
1492
- `req.write(JSON.stringify(${stringifyObject9(postData.jsonObj, {
1475
+ `req.write(JSON.stringify(${stringifyObject7(postData.jsonObj, {
1493
1476
  indent: " ",
1494
1477
  inlineCharacterLimit: 80
1495
1478
  })}));`
@@ -1498,208 +1481,24 @@ var native3 = {
1498
1481
  break;
1499
1482
  default:
1500
1483
  if (postData.text) {
1501
- push(`req.write(${stringifyObject9(postData.text, { indent })});`);
1484
+ push(`req.write(${stringifyObject7(postData.text, { indent })});`);
1502
1485
  }
1503
1486
  }
1504
1487
  push("req.end();");
1505
1488
  return join();
1506
1489
  }
1507
1490
  };
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
- };
1690
1491
 
1691
1492
  // src/targets/node/target.ts
1692
1493
  var node = {
1693
1494
  info: {
1694
1495
  key: "node",
1695
1496
  title: "Node.js",
1696
- default: "native",
1497
+ default: "fetch",
1697
1498
  cli: "node %s"
1698
1499
  },
1699
1500
  clientsById: {
1700
1501
  native: native3,
1701
- request,
1702
- unirest: unirest2,
1703
1502
  axios: axios2,
1704
1503
  fetch: fetch2
1705
1504
  }
@@ -2650,22 +2449,22 @@ var requests = {
2650
2449
  push("}");
2651
2450
  blank();
2652
2451
  }
2653
- let request2 = builtInMethods.includes(method) ? `response = requests.${method.toLowerCase()}(url` : `response = requests.request("${method}", url`;
2452
+ let request = builtInMethods.includes(method) ? `response = requests.${method.toLowerCase()}(url` : `response = requests.request("${method}", url`;
2654
2453
  if (hasPayload) {
2655
2454
  if (jsonPayload) {
2656
- request2 += ", json=payload";
2455
+ request += ", json=payload";
2657
2456
  } else {
2658
- request2 += ", data=payload";
2457
+ request += ", data=payload";
2659
2458
  }
2660
2459
  }
2661
2460
  if (hasFiles) {
2662
- request2 += ", files=files";
2461
+ request += ", files=files";
2663
2462
  }
2664
2463
  if (headerCount > 0) {
2665
- request2 += ", headers=headers";
2464
+ request += ", headers=headers";
2666
2465
  }
2667
- request2 += ")";
2668
- push(request2);
2466
+ request += ")";
2467
+ push(request);
2669
2468
  blank();
2670
2469
  push("print(response.text)");
2671
2470
  return join();
@@ -2750,22 +2549,22 @@ var httr = {
2750
2549
  const setContentType = `content_type("${escapeForDoubleQuotes(postData.mimeType)}")`;
2751
2550
  const otherHeaders = Object.entries(allHeaders).filter(([key]) => !["cookie", "accept", "content-type"].includes(key.toLowerCase())).map(([key, value]) => `'${key}' = '${escapeForSingleQuotes(value)}'`).join(", ");
2752
2551
  const setHeaders = otherHeaders ? `add_headers(${otherHeaders})` : void 0;
2753
- let request2 = `response <- VERB("${method}", url`;
2552
+ let request = `response <- VERB("${method}", url`;
2754
2553
  if (payload) {
2755
- request2 += ", body = payload";
2554
+ request += ", body = payload";
2756
2555
  }
2757
2556
  if (queryString.length) {
2758
- request2 += ", query = queryString";
2557
+ request += ", query = queryString";
2759
2558
  }
2760
2559
  const headerAdditions = [setHeaders, setContentType, setAccept, setCookies].filter((x) => !!x).join(", ");
2761
2560
  if (headerAdditions) {
2762
- request2 += `, ${headerAdditions}`;
2561
+ request += `, ${headerAdditions}`;
2763
2562
  }
2764
2563
  if (postData.text || postData.jsonObj || postData.params) {
2765
- request2 += ", encode = encode";
2564
+ request += ", encode = encode";
2766
2565
  }
2767
- request2 += ")";
2768
- push(request2);
2566
+ request += ")";
2567
+ push(request);
2769
2568
  blank();
2770
2569
  push('content(response, "text")');
2771
2570
  return join();
@@ -3451,5 +3250,5 @@ var addTargetClient = (targetId, client) => {
3451
3250
  };
3452
3251
 
3453
3252
  export { addClientPlugin, addTarget, addTargetClient, getHeaderName, isClient, isTarget, targets };
3454
- //# sourceMappingURL=chunk-532RKFLR.js.map
3455
- //# sourceMappingURL=chunk-532RKFLR.js.map
3253
+ //# sourceMappingURL=chunk-452Q5GGQ.js.map
3254
+ //# sourceMappingURL=chunk-452Q5GGQ.js.map