@scalar/snippetz 0.2.7 → 0.2.8

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 (68) hide show
  1. package/dist/_virtual/client.js +133 -0
  2. package/dist/_virtual/client10.js +121 -0
  3. package/dist/_virtual/client11.js +128 -0
  4. package/dist/_virtual/client12.js +64 -0
  5. package/dist/_virtual/client13.js +128 -0
  6. package/dist/_virtual/client14.js +71 -0
  7. package/dist/_virtual/client15.js +81 -0
  8. package/dist/_virtual/client16.js +76 -0
  9. package/dist/_virtual/client17.js +81 -0
  10. package/dist/_virtual/client18.js +87 -0
  11. package/dist/_virtual/client19.js +45 -0
  12. package/dist/_virtual/client2.js +52 -0
  13. package/dist/_virtual/client20.js +44 -0
  14. package/dist/_virtual/client21.js +71 -0
  15. package/dist/_virtual/client22.js +45 -0
  16. package/dist/_virtual/client23.js +71 -0
  17. package/dist/_virtual/client24.js +122 -0
  18. package/dist/_virtual/client25.js +37 -0
  19. package/dist/_virtual/client26.js +154 -0
  20. package/dist/_virtual/client27.js +181 -0
  21. package/dist/_virtual/client28.js +43 -0
  22. package/dist/_virtual/client3.js +113 -0
  23. package/dist/_virtual/client4.js +70 -0
  24. package/dist/_virtual/client5.js +123 -0
  25. package/dist/_virtual/client6.js +166 -0
  26. package/dist/_virtual/client7.js +91 -0
  27. package/dist/_virtual/client8.js +13 -0
  28. package/dist/_virtual/client9.js +13 -0
  29. package/dist/core/utils/convertWithHttpSnippetLite.d.ts +1 -1
  30. package/dist/core/utils/convertWithHttpSnippetLite.d.ts.map +1 -1
  31. package/dist/httpsnippet-lite/dist/esm/helpers/code-builder.js +60 -0
  32. package/dist/httpsnippet-lite/dist/esm/helpers/escape.js +72 -0
  33. package/dist/httpsnippet-lite/dist/esm/helpers/headers.js +20 -0
  34. package/dist/httpsnippet-lite/dist/esm/helpers/shell.js +16 -0
  35. package/dist/httpsnippet-lite/dist/esm/targets/objc/helpers.js +56 -0
  36. package/dist/httpsnippet-lite/dist/esm/targets/php/helpers.js +35 -0
  37. package/dist/httpsnippet-lite/dist/esm/targets/powershell/common.js +51 -0
  38. package/dist/httpsnippet-lite/dist/esm/targets/python/helpers.js +61 -0
  39. package/dist/httpsnippet-lite/dist/esm/targets/swift/helpers.js +74 -0
  40. package/dist/plugins/c/libcurl/libcurl.js +1 -1
  41. package/dist/plugins/clojure/clj_http/clj_http.js +1 -1
  42. package/dist/plugins/csharp/httpclient/httpclient.js +1 -1
  43. package/dist/plugins/csharp/restsharp/restsharp.js +1 -1
  44. package/dist/plugins/go/native/native.js +1 -1
  45. package/dist/plugins/http/http11/http11.js +1 -1
  46. package/dist/plugins/java/asynchttp/asynchttp.js +1 -1
  47. package/dist/plugins/java/nethttp/nethttp.js +1 -1
  48. package/dist/plugins/java/okhttp/okhttp.js +1 -1
  49. package/dist/plugins/java/unirest/unirest.js +1 -1
  50. package/dist/plugins/js/axios/axios.js +1 -1
  51. package/dist/plugins/js/jquery/jquery.js +1 -1
  52. package/dist/plugins/js/xhr/xhr.js +1 -1
  53. package/dist/plugins/kotlin/okhttp/okhttp.js +1 -1
  54. package/dist/plugins/node/axios/axios.js +1 -1
  55. package/dist/plugins/objc/nsurlsession/nsurlsession.js +1 -1
  56. package/dist/plugins/ocaml/cohttp/cohttp.js +1 -1
  57. package/dist/plugins/php/curl/curl.js +1 -1
  58. package/dist/plugins/php/guzzle/guzzle.js +1 -1
  59. package/dist/plugins/powershell/restmethod/restmethod.js +1 -1
  60. package/dist/plugins/powershell/webrequest/webrequest.js +1 -1
  61. package/dist/plugins/python/python3/python3.js +1 -1
  62. package/dist/plugins/python/requests/requests.js +1 -1
  63. package/dist/plugins/r/httr/httr.js +1 -1
  64. package/dist/plugins/ruby/native/native.js +1 -1
  65. package/dist/plugins/shell/httpie/httpie.js +1 -1
  66. package/dist/plugins/shell/wget/wget.js +1 -1
  67. package/dist/plugins/swift/nsurlsession/nsurlsession.js +1 -1
  68. package/package.json +1 -2
@@ -0,0 +1,81 @@
1
+ import stringifyObject from 'stringify-object';
2
+ import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
3
+ import { escapeForSingleQuotes } from '../httpsnippet-lite/dist/esm/helpers/escape.js';
4
+ import { hasHeader, getHeader, getHeaderName } from '../httpsnippet-lite/dist/esm/helpers/headers.js';
5
+
6
+ /**
7
+ * @description
8
+ * HTTP code snippet generator for native XMLHttpRequest
9
+ *
10
+ * @author
11
+ * @AhmadNassri
12
+ *
13
+ * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
14
+ */
15
+ const xhr = {
16
+ info: {
17
+ key: 'xhr',
18
+ title: 'XMLHttpRequest',
19
+ link: 'https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest',
20
+ description: 'W3C Standard API that provides scripted client functionality',
21
+ },
22
+ convert: ({ postData, allHeaders, method, fullUrl }, options) => {
23
+ var _a;
24
+ const opts = {
25
+ indent: ' ',
26
+ cors: true,
27
+ ...options,
28
+ };
29
+ const { blank, push, join } = new CodeBuilder({ indent: opts.indent });
30
+ switch (postData === null || postData === void 0 ? void 0 : postData.mimeType) {
31
+ case 'application/json':
32
+ push(`const data = JSON.stringify(${stringifyObject(postData.jsonObj, {
33
+ indent: opts.indent,
34
+ })});`);
35
+ blank();
36
+ break;
37
+ case 'multipart/form-data':
38
+ if (!postData.params) {
39
+ break;
40
+ }
41
+ push('const data = new FormData();');
42
+ postData.params.forEach(param => {
43
+ push(`data.append('${param.name}', '${param.value || param.fileName || ''}');`);
44
+ });
45
+ // remove the contentType header
46
+ if (hasHeader(allHeaders, 'content-type')) {
47
+ if ((_a = getHeader(allHeaders, 'content-type')) === null || _a === void 0 ? void 0 : _a.includes('boundary')) {
48
+ const headerName = getHeaderName(allHeaders, 'content-type');
49
+ if (headerName) {
50
+ delete allHeaders[headerName];
51
+ }
52
+ }
53
+ }
54
+ blank();
55
+ break;
56
+ default:
57
+ push(`const data = ${(postData === null || postData === void 0 ? void 0 : postData.text) ? `'${postData.text}'` : 'null'};`);
58
+ blank();
59
+ }
60
+ push('const xhr = new XMLHttpRequest();');
61
+ if (opts.cors) {
62
+ push('xhr.withCredentials = true;');
63
+ }
64
+ blank();
65
+ push("xhr.addEventListener('readystatechange', function () {");
66
+ push('if (this.readyState === this.DONE) {', 1);
67
+ push('console.log(this.responseText);', 2);
68
+ push('}', 1);
69
+ push('});');
70
+ blank();
71
+ push(`xhr.open('${method}', '${fullUrl}');`);
72
+ Object.keys(allHeaders).forEach(key => {
73
+ push(`xhr.setRequestHeader('${key}', '${escapeForSingleQuotes(allHeaders[key])}');`);
74
+ });
75
+ blank();
76
+ push('xhr.send(data);');
77
+ return join();
78
+ },
79
+ };
80
+
81
+ export { xhr };
@@ -0,0 +1,76 @@
1
+ import stringifyObject from 'stringify-object';
2
+ import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
3
+
4
+ /**
5
+ * @description
6
+ * HTTP code snippet generator for Javascript & Node.js using Axios.
7
+ *
8
+ * @author
9
+ * @rohit-gohri
10
+ *
11
+ * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
12
+ */
13
+ const axios = {
14
+ info: {
15
+ key: 'axios',
16
+ title: 'Axios',
17
+ link: 'https://github.com/axios/axios',
18
+ description: 'Promise based HTTP client for the browser and node.js',
19
+ },
20
+ convert: ({ method, url, queryObj, allHeaders, postData }, options) => {
21
+ const opts = {
22
+ indent: ' ',
23
+ ...options,
24
+ };
25
+ const { blank, join, push, addPostProcessor } = new CodeBuilder({ indent: opts.indent });
26
+ push("const axios = require('axios').default;");
27
+ const reqOpts = {
28
+ method,
29
+ url,
30
+ };
31
+ if (Object.keys(queryObj).length) {
32
+ reqOpts.params = queryObj;
33
+ }
34
+ if (Object.keys(allHeaders).length) {
35
+ reqOpts.headers = allHeaders;
36
+ }
37
+ switch (postData === null || postData === void 0 ? void 0 : postData.mimeType) {
38
+ case 'application/x-www-form-urlencoded':
39
+ if (postData.params) {
40
+ push("const { URLSearchParams } = require('url');");
41
+ blank();
42
+ push('const encodedParams = new URLSearchParams();');
43
+ postData.params.forEach(param => {
44
+ push(`encodedParams.set('${param.name}', '${param.value}');`);
45
+ });
46
+ blank();
47
+ reqOpts.data = 'encodedParams,';
48
+ addPostProcessor(code => code.replace(/'encodedParams,'/, 'encodedParams,'));
49
+ }
50
+ break;
51
+ case 'application/json':
52
+ blank();
53
+ if (postData.jsonObj) {
54
+ reqOpts.data = postData.jsonObj;
55
+ }
56
+ break;
57
+ default:
58
+ blank();
59
+ if (postData === null || postData === void 0 ? void 0 : postData.text) {
60
+ reqOpts.data = postData.text;
61
+ }
62
+ }
63
+ const stringifiedOptions = stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 });
64
+ push(`const options = ${stringifiedOptions};`);
65
+ blank();
66
+ push('try {');
67
+ push('const { data } = await axios.request(options);', 1);
68
+ push('console.log(data);', 1);
69
+ push('} catch (error) {');
70
+ push('console.error(error);', 1);
71
+ push('}');
72
+ return join();
73
+ },
74
+ };
75
+
76
+ export { axios };
@@ -0,0 +1,81 @@
1
+ import stringifyObject from 'stringify-object';
2
+ import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
3
+ import { hasHeader, getHeader, getHeaderName } from '../httpsnippet-lite/dist/esm/helpers/headers.js';
4
+
5
+ /**
6
+ * @description
7
+ * HTTP code snippet generator for native XMLHttpRequest
8
+ *
9
+ * @author
10
+ * @AhmadNassri
11
+ *
12
+ * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
13
+ */
14
+ const jquery = {
15
+ info: {
16
+ key: 'jquery',
17
+ title: 'jQuery',
18
+ link: 'http://api.jquery.com/jquery.ajax/',
19
+ description: 'Perform an asynchronous HTTP (Ajax) requests with jQuery',
20
+ },
21
+ convert: ({ fullUrl, method, allHeaders, postData }, options) => {
22
+ var _a;
23
+ const opts = {
24
+ indent: ' ',
25
+ ...options,
26
+ };
27
+ const { blank, push, join } = new CodeBuilder({ indent: opts.indent });
28
+ const settings = {
29
+ async: true,
30
+ crossDomain: true,
31
+ url: fullUrl,
32
+ method,
33
+ headers: allHeaders,
34
+ };
35
+ switch (postData === null || postData === void 0 ? void 0 : postData.mimeType) {
36
+ case 'application/x-www-form-urlencoded':
37
+ settings.data = postData.paramsObj ? postData.paramsObj : postData.text;
38
+ break;
39
+ case 'application/json':
40
+ settings.processData = false;
41
+ settings.data = postData.text;
42
+ break;
43
+ case 'multipart/form-data':
44
+ if (!postData.params) {
45
+ break;
46
+ }
47
+ push('const form = new FormData();');
48
+ postData.params.forEach(param => {
49
+ push(`form.append('${param.name}', '${param.value || param.fileName || ''}');`);
50
+ });
51
+ settings.processData = false;
52
+ settings.contentType = false;
53
+ settings.mimeType = 'multipart/form-data';
54
+ settings.data = '[form]';
55
+ // remove the contentType header
56
+ if (hasHeader(allHeaders, 'content-type')) {
57
+ if ((_a = getHeader(allHeaders, 'content-type')) === null || _a === void 0 ? void 0 : _a.includes('boundary')) {
58
+ const headerName = getHeaderName(allHeaders, 'content-type');
59
+ if (headerName) {
60
+ delete settings.headers[headerName];
61
+ }
62
+ }
63
+ }
64
+ blank();
65
+ break;
66
+ default:
67
+ if (postData === null || postData === void 0 ? void 0 : postData.text) {
68
+ settings.data = postData.text;
69
+ }
70
+ }
71
+ const stringifiedSettings = stringifyObject(settings, { indent: opts.indent }).replace("'[form]'", 'form');
72
+ push(`const settings = ${stringifiedSettings};`);
73
+ blank();
74
+ push('$.ajax(settings).done(function (response) {');
75
+ push('console.log(response);', 1);
76
+ push('});');
77
+ return join();
78
+ },
79
+ };
80
+
81
+ export { jquery };
@@ -0,0 +1,87 @@
1
+ import stringifyObject from 'stringify-object';
2
+ import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
3
+
4
+ /**
5
+ * @description
6
+ * HTTP code snippet generator for Javascript & Node.js using Axios.
7
+ *
8
+ * @author
9
+ * @rohit-gohri
10
+ *
11
+ * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
12
+ */
13
+ const axios = {
14
+ info: {
15
+ key: 'axios',
16
+ title: 'Axios',
17
+ link: 'https://github.com/axios/axios',
18
+ description: 'Promise based HTTP client for the browser and node.js',
19
+ },
20
+ convert: ({ allHeaders, method, url, queryObj, postData }, options) => {
21
+ const opts = {
22
+ indent: ' ',
23
+ ...options,
24
+ };
25
+ const { blank, push, join, addPostProcessor } = new CodeBuilder({ indent: opts.indent });
26
+ push("import axios from 'axios';");
27
+ blank();
28
+ const requestOptions = {
29
+ method,
30
+ url,
31
+ };
32
+ if (Object.keys(queryObj).length) {
33
+ requestOptions.params = queryObj;
34
+ }
35
+ if (Object.keys(allHeaders).length) {
36
+ requestOptions.headers = allHeaders;
37
+ }
38
+ switch (postData === null || postData === void 0 ? void 0 : postData.mimeType) {
39
+ case 'application/x-www-form-urlencoded':
40
+ if (postData.params) {
41
+ push('const encodedParams = new URLSearchParams();');
42
+ postData.params.forEach(param => {
43
+ push(`encodedParams.set('${param.name}', '${param.value}');`);
44
+ });
45
+ blank();
46
+ requestOptions.data = 'encodedParams,';
47
+ addPostProcessor(code => code.replace(/'encodedParams,'/, 'encodedParams,'));
48
+ }
49
+ break;
50
+ case 'application/json':
51
+ if (postData.jsonObj) {
52
+ requestOptions.data = postData.jsonObj;
53
+ }
54
+ break;
55
+ case 'multipart/form-data':
56
+ if (!postData.params) {
57
+ break;
58
+ }
59
+ push('const form = new FormData();');
60
+ postData.params.forEach(param => {
61
+ push(`form.append('${param.name}', '${param.value || param.fileName || ''}');`);
62
+ });
63
+ blank();
64
+ requestOptions.data = '[form]';
65
+ break;
66
+ default:
67
+ if (postData === null || postData === void 0 ? void 0 : postData.text) {
68
+ requestOptions.data = postData.text;
69
+ }
70
+ }
71
+ const optionString = stringifyObject(requestOptions, {
72
+ indent: ' ',
73
+ inlineCharacterLimit: 80,
74
+ }).replace('"[form]"', 'form');
75
+ push(`const options = ${optionString};`);
76
+ blank();
77
+ push('try {');
78
+ push('const { data } = await axios.request(options);', 1);
79
+ push('console.log(data);', 1);
80
+ push('} catch (error) {');
81
+ push('console.error(error);', 1);
82
+ push('}');
83
+ return join();
84
+ },
85
+ };
86
+
87
+ export { axios };
@@ -0,0 +1,45 @@
1
+ import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
2
+ import { escapeForDoubleQuotes } from '../httpsnippet-lite/dist/esm/helpers/escape.js';
3
+
4
+ /**
5
+ * @description
6
+ * HTTP code snippet generator for Java using Unirest.
7
+ *
8
+ * @author
9
+ * @shashiranjan84
10
+ *
11
+ * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
12
+ */
13
+ const unirest = {
14
+ info: {
15
+ key: 'unirest',
16
+ title: 'Unirest',
17
+ link: 'http://unirest.io/java.html',
18
+ description: 'Lightweight HTTP Request Client Library',
19
+ },
20
+ convert: ({ method, allHeaders, postData, fullUrl }, options) => {
21
+ const opts = {
22
+ indent: ' ',
23
+ ...options,
24
+ };
25
+ const { join, push } = new CodeBuilder({ indent: opts.indent });
26
+ const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'];
27
+ if (!methods.includes(method.toUpperCase())) {
28
+ push(`HttpResponse<String> response = Unirest.customMethod("${method.toUpperCase()}","${fullUrl}")`);
29
+ }
30
+ else {
31
+ push(`HttpResponse<String> response = Unirest.${method.toLowerCase()}("${fullUrl}")`);
32
+ }
33
+ // Add headers, including the cookies
34
+ Object.keys(allHeaders).forEach(key => {
35
+ push(`.header("${key}", "${escapeForDoubleQuotes(allHeaders[key])}")`, 1);
36
+ });
37
+ if (postData === null || postData === void 0 ? void 0 : postData.text) {
38
+ push(`.body(${JSON.stringify(postData.text)})`, 1);
39
+ }
40
+ push('.asString();', 1);
41
+ return join();
42
+ },
43
+ };
44
+
45
+ export { unirest };
@@ -0,0 +1,52 @@
1
+ import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
2
+ import { quote, escape } from '../httpsnippet-lite/dist/esm/helpers/shell.js';
3
+
4
+ /**
5
+ * @description
6
+ * HTTP code snippet generator for the Shell using Wget.
7
+ *
8
+ * @author
9
+ * @AhmadNassri
10
+ *
11
+ * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
12
+ */
13
+ const wget = {
14
+ info: {
15
+ key: 'wget',
16
+ title: 'Wget',
17
+ link: 'https://www.gnu.org/software/wget/',
18
+ description: 'a free software package for retrieving files using HTTP, HTTPS',
19
+ },
20
+ convert: ({ method, postData, allHeaders, fullUrl }, options) => {
21
+ const opts = {
22
+ indent: ' ',
23
+ short: false,
24
+ verbose: false,
25
+ ...options,
26
+ };
27
+ const { push, join } = new CodeBuilder({
28
+ indent: opts.indent,
29
+ // @ts-expect-error SEEMS LEGIT
30
+ join: opts.indent !== false ? ` \\\n${opts.indent}` : ' ',
31
+ });
32
+ if (opts.verbose) {
33
+ push(`wget ${opts.short ? '-v' : '--verbose'}`);
34
+ }
35
+ else {
36
+ push(`wget ${opts.short ? '-q' : '--quiet'}`);
37
+ }
38
+ push(`--method ${quote(method)}`);
39
+ Object.keys(allHeaders).forEach(key => {
40
+ const header = `${key}: ${allHeaders[key]}`;
41
+ push(`--header ${quote(header)}`);
42
+ });
43
+ if (postData === null || postData === void 0 ? void 0 : postData.text) {
44
+ push(`--body-data ${escape(quote(postData.text))}`);
45
+ }
46
+ push(opts.short ? '-O' : '--output-document');
47
+ push(`- ${quote(fullUrl)}`);
48
+ return join();
49
+ },
50
+ };
51
+
52
+ export { wget };
@@ -0,0 +1,44 @@
1
+ import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
2
+ import { escapeForDoubleQuotes } from '../httpsnippet-lite/dist/esm/helpers/escape.js';
3
+
4
+ /**
5
+ * @description
6
+ * HTTP code snippet generator for Java using java.net.http.
7
+ *
8
+ * @author
9
+ * @wtetsu
10
+ *
11
+ * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
12
+ */
13
+ const nethttp = {
14
+ info: {
15
+ key: 'nethttp',
16
+ title: 'java.net.http',
17
+ link: 'https://openjdk.java.net/groups/net/httpclient/intro.html',
18
+ description: 'Java Standardized HTTP Client API',
19
+ },
20
+ convert: ({ allHeaders, fullUrl, method, postData }, options) => {
21
+ const opts = {
22
+ indent: ' ',
23
+ ...options,
24
+ };
25
+ const { push, join } = new CodeBuilder({ indent: opts.indent });
26
+ push('HttpRequest request = HttpRequest.newBuilder()');
27
+ push(`.uri(URI.create("${fullUrl}"))`, 2);
28
+ Object.keys(allHeaders).forEach(key => {
29
+ push(`.header("${key}", "${escapeForDoubleQuotes(allHeaders[key])}")`, 2);
30
+ });
31
+ if (postData === null || postData === void 0 ? void 0 : postData.text) {
32
+ push(`.method("${method.toUpperCase()}", HttpRequest.BodyPublishers.ofString(${JSON.stringify(postData.text)}))`, 2);
33
+ }
34
+ else {
35
+ push(`.method("${method.toUpperCase()}", HttpRequest.BodyPublishers.noBody())`, 2);
36
+ }
37
+ push('.build();', 2);
38
+ push('HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());');
39
+ push('System.out.println(response.body());');
40
+ return join();
41
+ },
42
+ };
43
+
44
+ export { nethttp };
@@ -0,0 +1,71 @@
1
+ import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
2
+ import { escapeForDoubleQuotes } from '../httpsnippet-lite/dist/esm/helpers/escape.js';
3
+
4
+ /**
5
+ * @description
6
+ * HTTP code snippet generator for Java using OkHttp.
7
+ *
8
+ * @author
9
+ * @shashiranjan84
10
+ *
11
+ * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
12
+ */
13
+ const okhttp = {
14
+ info: {
15
+ key: 'okhttp',
16
+ title: 'OkHttp',
17
+ link: 'http://square.github.io/okhttp/',
18
+ description: 'An HTTP Request Client Library',
19
+ },
20
+ convert: ({ postData, method, fullUrl, allHeaders }, options) => {
21
+ const opts = {
22
+ indent: ' ',
23
+ ...options,
24
+ };
25
+ const { push, blank, join } = new CodeBuilder({ indent: opts.indent });
26
+ const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'];
27
+ const methodsWithBody = ['POST', 'PUT', 'DELETE', 'PATCH'];
28
+ push('OkHttpClient client = new OkHttpClient();');
29
+ blank();
30
+ if (postData === null || postData === void 0 ? void 0 : postData.text) {
31
+ if (postData.boundary) {
32
+ push(`MediaType mediaType = MediaType.parse("${postData.mimeType}; boundary=${postData.boundary}");`);
33
+ }
34
+ else {
35
+ push(`MediaType mediaType = MediaType.parse("${postData.mimeType}");`);
36
+ }
37
+ push(`RequestBody body = RequestBody.create(mediaType, ${JSON.stringify(postData.text)});`);
38
+ }
39
+ push('Request request = new Request.Builder()');
40
+ push(`.url("${fullUrl}")`, 1);
41
+ if (!methods.includes(method.toUpperCase())) {
42
+ if (postData === null || postData === void 0 ? void 0 : postData.text) {
43
+ push(`.method("${method.toUpperCase()}", body)`, 1);
44
+ }
45
+ else {
46
+ push(`.method("${method.toUpperCase()}", null)`, 1);
47
+ }
48
+ }
49
+ else if (methodsWithBody.includes(method.toUpperCase())) {
50
+ if (postData === null || postData === void 0 ? void 0 : postData.text) {
51
+ push(`.${method.toLowerCase()}(body)`, 1);
52
+ }
53
+ else {
54
+ push(`.${method.toLowerCase()}(null)`, 1);
55
+ }
56
+ }
57
+ else {
58
+ push(`.${method.toLowerCase()}()`, 1);
59
+ }
60
+ // Add headers, including the cookies
61
+ Object.keys(allHeaders).forEach(key => {
62
+ push(`.addHeader("${key}", "${escapeForDoubleQuotes(allHeaders[key])}")`, 1);
63
+ });
64
+ push('.build();', 1);
65
+ blank();
66
+ push('Response response = client.newCall(request).execute();');
67
+ return join();
68
+ },
69
+ };
70
+
71
+ export { okhttp };
@@ -0,0 +1,45 @@
1
+ import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
2
+ import { escapeForDoubleQuotes } from '../httpsnippet-lite/dist/esm/helpers/escape.js';
3
+
4
+ /**
5
+ * @description
6
+ * Asynchronous Http and WebSocket Client library for Java
7
+ *
8
+ * @author
9
+ * @windard
10
+ *
11
+ * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
12
+ */
13
+ const asynchttp = {
14
+ info: {
15
+ key: 'asynchttp',
16
+ title: 'AsyncHttp',
17
+ link: 'https://github.com/AsyncHttpClient/async-http-client',
18
+ description: 'Asynchronous Http and WebSocket Client library for Java',
19
+ },
20
+ convert: ({ method, allHeaders, postData, fullUrl }, options) => {
21
+ const opts = {
22
+ indent: ' ',
23
+ ...options,
24
+ };
25
+ const { blank, push, join } = new CodeBuilder({ indent: opts.indent });
26
+ push('AsyncHttpClient client = new DefaultAsyncHttpClient();');
27
+ push(`client.prepare("${method.toUpperCase()}", "${fullUrl}")`);
28
+ // Add headers, including the cookies
29
+ Object.keys(allHeaders).forEach(key => {
30
+ push(`.setHeader("${key}", "${escapeForDoubleQuotes(allHeaders[key])}")`, 1);
31
+ });
32
+ if (postData === null || postData === void 0 ? void 0 : postData.text) {
33
+ push(`.setBody(${JSON.stringify(postData.text)})`, 1);
34
+ }
35
+ push('.execute()', 1);
36
+ push('.toCompletableFuture()', 1);
37
+ push('.thenAccept(System.out::println)', 1);
38
+ push('.join();', 1);
39
+ blank();
40
+ push('client.close();');
41
+ return join();
42
+ },
43
+ };
44
+
45
+ export { asynchttp };
@@ -0,0 +1,71 @@
1
+ import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
2
+
3
+ /**
4
+ * @description
5
+ * HTTP code snippet generator to generate raw HTTP/1.1 request strings,
6
+ * in accordance to the RFC 7230 (and RFC 7231) specifications.
7
+ *
8
+ * @author
9
+ * @irvinlim
10
+ *
11
+ * For any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
12
+ */
13
+ const CRLF = '\r\n';
14
+ /**
15
+ * Request follows the request message format in accordance to RFC 7230, Section 3.
16
+ * Each section is prepended with the RFC and section number.
17
+ * See more at https://tools.ietf.org/html/rfc7230#section-3.
18
+ */
19
+ const http11 = {
20
+ info: {
21
+ key: 'http1.1',
22
+ title: 'HTTP/1.1',
23
+ link: 'https://tools.ietf.org/html/rfc7230',
24
+ description: 'HTTP/1.1 request string in accordance with RFC 7230',
25
+ },
26
+ convert: ({ method, fullUrl, uriObj, httpVersion, allHeaders, postData }, options) => {
27
+ const opts = {
28
+ absoluteURI: false,
29
+ autoContentLength: true,
30
+ autoHost: true,
31
+ ...options,
32
+ };
33
+ // RFC 7230 Section 3. Message Format
34
+ // All lines have no indentation, and should be terminated with CRLF.
35
+ const { blank, push, join } = new CodeBuilder({ indent: '', join: CRLF });
36
+ // RFC 7230 Section 5.3. Request Target
37
+ // Determines if the Request-Line should use 'absolute-form' or 'origin-form'.
38
+ // Basically it means whether the "http://domain.com" will prepend the full url.
39
+ const requestUrl = opts.absoluteURI ? fullUrl : uriObj.path;
40
+ // RFC 7230 Section 3.1.1. Request-Line
41
+ push(`${method} ${requestUrl} ${httpVersion}`);
42
+ const headerKeys = Object.keys(allHeaders);
43
+ // RFC 7231 Section 5. Header Fields
44
+ headerKeys.forEach(key => {
45
+ // Capitalize header keys, even though it's not required by the spec.
46
+ const keyCapitalized = key.toLowerCase().replace(/(^|-)(\w)/g, input => input.toUpperCase());
47
+ push(`${keyCapitalized}: ${allHeaders[key]}`);
48
+ });
49
+ // RFC 7230 Section 5.4. Host
50
+ // Automatically set Host header if option is on and on header already exists.
51
+ if (opts.autoHost && !headerKeys.includes('host')) {
52
+ push(`Host: ${uriObj.host}`);
53
+ }
54
+ // RFC 7230 Section 3.3.3. Message Body Length
55
+ // Automatically set Content-Length header if option is on, postData is present and no header already exists.
56
+ if (opts.autoContentLength && (postData === null || postData === void 0 ? void 0 : postData.text) && !headerKeys.includes('content-length')) {
57
+ push(`Content-Length: ${postData.text.length}`);
58
+ }
59
+ // Add extra line after header section.
60
+ blank();
61
+ // Separate header section and message body section.
62
+ const headerSection = join();
63
+ // RFC 7230 Section 3.3. Message Body
64
+ const messageBody = (postData === null || postData === void 0 ? void 0 : postData.text) || '';
65
+ // RFC 7230 Section 3. Message Format
66
+ // Extra CRLF separating the headers from the body.
67
+ return `${headerSection}${CRLF}${messageBody}`;
68
+ },
69
+ };
70
+
71
+ export { http11 };