@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.
- package/dist/_virtual/client.js +133 -0
- package/dist/_virtual/client10.js +121 -0
- package/dist/_virtual/client11.js +128 -0
- package/dist/_virtual/client12.js +64 -0
- package/dist/_virtual/client13.js +128 -0
- package/dist/_virtual/client14.js +71 -0
- package/dist/_virtual/client15.js +81 -0
- package/dist/_virtual/client16.js +76 -0
- package/dist/_virtual/client17.js +81 -0
- package/dist/_virtual/client18.js +87 -0
- package/dist/_virtual/client19.js +45 -0
- package/dist/_virtual/client2.js +52 -0
- package/dist/_virtual/client20.js +44 -0
- package/dist/_virtual/client21.js +71 -0
- package/dist/_virtual/client22.js +45 -0
- package/dist/_virtual/client23.js +71 -0
- package/dist/_virtual/client24.js +122 -0
- package/dist/_virtual/client25.js +37 -0
- package/dist/_virtual/client26.js +154 -0
- package/dist/_virtual/client27.js +181 -0
- package/dist/_virtual/client28.js +43 -0
- package/dist/_virtual/client3.js +113 -0
- package/dist/_virtual/client4.js +70 -0
- package/dist/_virtual/client5.js +123 -0
- package/dist/_virtual/client6.js +166 -0
- package/dist/_virtual/client7.js +91 -0
- package/dist/_virtual/client8.js +13 -0
- package/dist/_virtual/client9.js +13 -0
- package/dist/core/utils/convertWithHttpSnippetLite.d.ts +1 -1
- package/dist/core/utils/convertWithHttpSnippetLite.d.ts.map +1 -1
- package/dist/httpsnippet-lite/dist/esm/helpers/code-builder.js +60 -0
- package/dist/httpsnippet-lite/dist/esm/helpers/escape.js +72 -0
- package/dist/httpsnippet-lite/dist/esm/helpers/headers.js +20 -0
- package/dist/httpsnippet-lite/dist/esm/helpers/shell.js +16 -0
- package/dist/httpsnippet-lite/dist/esm/targets/objc/helpers.js +56 -0
- package/dist/httpsnippet-lite/dist/esm/targets/php/helpers.js +35 -0
- package/dist/httpsnippet-lite/dist/esm/targets/powershell/common.js +51 -0
- package/dist/httpsnippet-lite/dist/esm/targets/python/helpers.js +61 -0
- package/dist/httpsnippet-lite/dist/esm/targets/swift/helpers.js +74 -0
- package/dist/plugins/c/libcurl/libcurl.js +1 -1
- package/dist/plugins/clojure/clj_http/clj_http.js +1 -1
- package/dist/plugins/csharp/httpclient/httpclient.js +1 -1
- package/dist/plugins/csharp/restsharp/restsharp.js +1 -1
- package/dist/plugins/go/native/native.js +1 -1
- package/dist/plugins/http/http11/http11.js +1 -1
- package/dist/plugins/java/asynchttp/asynchttp.js +1 -1
- package/dist/plugins/java/nethttp/nethttp.js +1 -1
- package/dist/plugins/java/okhttp/okhttp.js +1 -1
- package/dist/plugins/java/unirest/unirest.js +1 -1
- package/dist/plugins/js/axios/axios.js +1 -1
- package/dist/plugins/js/jquery/jquery.js +1 -1
- package/dist/plugins/js/xhr/xhr.js +1 -1
- package/dist/plugins/kotlin/okhttp/okhttp.js +1 -1
- package/dist/plugins/node/axios/axios.js +1 -1
- package/dist/plugins/objc/nsurlsession/nsurlsession.js +1 -1
- package/dist/plugins/ocaml/cohttp/cohttp.js +1 -1
- package/dist/plugins/php/curl/curl.js +1 -1
- package/dist/plugins/php/guzzle/guzzle.js +1 -1
- package/dist/plugins/powershell/restmethod/restmethod.js +1 -1
- package/dist/plugins/powershell/webrequest/webrequest.js +1 -1
- package/dist/plugins/python/python3/python3.js +1 -1
- package/dist/plugins/python/requests/requests.js +1 -1
- package/dist/plugins/r/httr/httr.js +1 -1
- package/dist/plugins/ruby/native/native.js +1 -1
- package/dist/plugins/shell/httpie/httpie.js +1 -1
- package/dist/plugins/shell/wget/wget.js +1 -1
- package/dist/plugins/swift/nsurlsession/nsurlsession.js +1 -1
- package/package.json +1 -2
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
|
|
2
|
+
import { escapeForSingleQuotes } from '../httpsnippet-lite/dist/esm/helpers/escape.js';
|
|
3
|
+
|
|
4
|
+
const native = {
|
|
5
|
+
info: {
|
|
6
|
+
key: 'native',
|
|
7
|
+
title: 'net::http',
|
|
8
|
+
link: 'http://ruby-doc.org/stdlib-2.2.1/libdoc/net/http/rdoc/Net/HTTP.html',
|
|
9
|
+
description: 'Ruby HTTP client',
|
|
10
|
+
},
|
|
11
|
+
convert: ({ uriObj, method: rawMethod, fullUrl, postData, allHeaders }, options = {}) => {
|
|
12
|
+
const { insecureSkipVerify = false } = options;
|
|
13
|
+
const { push, blank, join } = new CodeBuilder();
|
|
14
|
+
push("require 'uri'");
|
|
15
|
+
push("require 'net/http'");
|
|
16
|
+
blank();
|
|
17
|
+
// To support custom methods we check for the supported methods
|
|
18
|
+
// and if doesn't exist then we build a custom class for it
|
|
19
|
+
const method = rawMethod.toUpperCase();
|
|
20
|
+
const methods = [
|
|
21
|
+
'GET',
|
|
22
|
+
'POST',
|
|
23
|
+
'HEAD',
|
|
24
|
+
'DELETE',
|
|
25
|
+
'PATCH',
|
|
26
|
+
'PUT',
|
|
27
|
+
'OPTIONS',
|
|
28
|
+
'COPY',
|
|
29
|
+
'LOCK',
|
|
30
|
+
'UNLOCK',
|
|
31
|
+
'MOVE',
|
|
32
|
+
'TRACE',
|
|
33
|
+
];
|
|
34
|
+
const capMethod = method.charAt(0) + method.substring(1).toLowerCase();
|
|
35
|
+
if (!methods.includes(method)) {
|
|
36
|
+
push(`class Net::HTTP::${capMethod} < Net::HTTPRequest`);
|
|
37
|
+
push(` METHOD = '${method.toUpperCase()}'`);
|
|
38
|
+
push(` REQUEST_HAS_BODY = '${(postData === null || postData === void 0 ? void 0 : postData.text) ? 'true' : 'false'}'`);
|
|
39
|
+
push(' RESPONSE_HAS_BODY = true');
|
|
40
|
+
push('end');
|
|
41
|
+
blank();
|
|
42
|
+
}
|
|
43
|
+
push(`url = URI("${fullUrl}")`);
|
|
44
|
+
blank();
|
|
45
|
+
push('http = Net::HTTP.new(url.host, url.port)');
|
|
46
|
+
if (uriObj.protocol === 'https:') {
|
|
47
|
+
push('http.use_ssl = true');
|
|
48
|
+
if (insecureSkipVerify) {
|
|
49
|
+
push('http.verify_mode = OpenSSL::SSL::VERIFY_NONE');
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
blank();
|
|
53
|
+
push(`request = Net::HTTP::${capMethod}.new(url)`);
|
|
54
|
+
const headers = Object.keys(allHeaders);
|
|
55
|
+
if (headers.length) {
|
|
56
|
+
headers.forEach(key => {
|
|
57
|
+
push(`request["${key}"] = '${escapeForSingleQuotes(allHeaders[key])}'`);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
if (postData === null || postData === void 0 ? void 0 : postData.text) {
|
|
61
|
+
push(`request.body = ${JSON.stringify(postData.text)}`);
|
|
62
|
+
}
|
|
63
|
+
blank();
|
|
64
|
+
push('response = http.request(request)');
|
|
65
|
+
push('puts response.read_body');
|
|
66
|
+
return join();
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export { native };
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
|
|
2
|
+
import { escapeForDoubleQuotes, escapeForSingleQuotes } from '../httpsnippet-lite/dist/esm/helpers/escape.js';
|
|
3
|
+
import { getHeader } from '../httpsnippet-lite/dist/esm/helpers/headers.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* HTTP code snippet generator for R using httr
|
|
8
|
+
*
|
|
9
|
+
* @author
|
|
10
|
+
* @gabrielakoreeda
|
|
11
|
+
*
|
|
12
|
+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
13
|
+
*/
|
|
14
|
+
const httr = {
|
|
15
|
+
info: {
|
|
16
|
+
key: 'httr',
|
|
17
|
+
title: 'httr',
|
|
18
|
+
link: 'https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html',
|
|
19
|
+
description: 'httr: Tools for Working with URLs and HTTP',
|
|
20
|
+
},
|
|
21
|
+
convert: ({ url, queryObj, queryString, postData, allHeaders, method }, options = {}) => {
|
|
22
|
+
var _a, _b;
|
|
23
|
+
// Start snippet
|
|
24
|
+
const { push, blank, join } = new CodeBuilder({
|
|
25
|
+
indent: (_a = options.indent) !== null && _a !== void 0 ? _a : ' ',
|
|
26
|
+
});
|
|
27
|
+
// Import httr
|
|
28
|
+
push('library(httr)');
|
|
29
|
+
blank();
|
|
30
|
+
// Set URL
|
|
31
|
+
push(`url <- "${url}"`);
|
|
32
|
+
blank();
|
|
33
|
+
// Construct query string
|
|
34
|
+
const qs = queryObj;
|
|
35
|
+
delete queryObj.key;
|
|
36
|
+
const entries = Object.entries(qs);
|
|
37
|
+
const entriesCount = entries.length;
|
|
38
|
+
if (entriesCount === 1) {
|
|
39
|
+
const entry = entries[0];
|
|
40
|
+
push(`queryString <- list(${entry[0]} = "${entry[1]}")`);
|
|
41
|
+
blank();
|
|
42
|
+
}
|
|
43
|
+
else if (entriesCount > 1) {
|
|
44
|
+
push('queryString <- list(');
|
|
45
|
+
entries.forEach(([key, value], i) => {
|
|
46
|
+
const isLastItem = i !== entriesCount - 1;
|
|
47
|
+
const maybeComma = isLastItem ? ',' : '';
|
|
48
|
+
push(`${key} = "${value}"${maybeComma}`, 1);
|
|
49
|
+
});
|
|
50
|
+
push(')');
|
|
51
|
+
blank();
|
|
52
|
+
}
|
|
53
|
+
// Construct payload
|
|
54
|
+
const payload = JSON.stringify(postData === null || postData === void 0 ? void 0 : postData.text);
|
|
55
|
+
if (payload) {
|
|
56
|
+
push(`payload <- ${payload}`);
|
|
57
|
+
blank();
|
|
58
|
+
}
|
|
59
|
+
// Define encode
|
|
60
|
+
if (postData && (postData.text || postData.jsonObj || postData.params)) {
|
|
61
|
+
switch (postData.mimeType) {
|
|
62
|
+
case 'application/x-www-form-urlencoded':
|
|
63
|
+
push('encode <- "form"');
|
|
64
|
+
blank();
|
|
65
|
+
break;
|
|
66
|
+
case 'application/json':
|
|
67
|
+
push('encode <- "json"');
|
|
68
|
+
blank();
|
|
69
|
+
break;
|
|
70
|
+
case 'multipart/form-data':
|
|
71
|
+
push('encode <- "multipart"');
|
|
72
|
+
blank();
|
|
73
|
+
break;
|
|
74
|
+
default:
|
|
75
|
+
push('encode <- "raw"');
|
|
76
|
+
blank();
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// Construct headers
|
|
81
|
+
const cookieHeader = getHeader(allHeaders, 'cookie');
|
|
82
|
+
const acceptHeader = getHeader(allHeaders, 'accept');
|
|
83
|
+
const setCookies = cookieHeader
|
|
84
|
+
? `set_cookies(\`${String(cookieHeader)
|
|
85
|
+
.replace(/;/g, '", `')
|
|
86
|
+
.replace(/` /g, '`')
|
|
87
|
+
.replace(/[=]/g, '` = "')}")`
|
|
88
|
+
: undefined;
|
|
89
|
+
const setAccept = acceptHeader ? `accept("${escapeForDoubleQuotes(acceptHeader)}")` : undefined;
|
|
90
|
+
const setContentType = `content_type("${escapeForDoubleQuotes((_b = postData === null || postData === void 0 ? void 0 : postData.mimeType) !== null && _b !== void 0 ? _b : 'application/octet-stream')}")`;
|
|
91
|
+
const otherHeaders = Object.entries(allHeaders)
|
|
92
|
+
// These headers are all handled separately:
|
|
93
|
+
.filter(([key]) => !['cookie', 'accept', 'content-type'].includes(key.toLowerCase()))
|
|
94
|
+
.map(([key, value]) => `'${key}' = '${escapeForSingleQuotes(value)}'`)
|
|
95
|
+
.join(', ');
|
|
96
|
+
const setHeaders = otherHeaders ? `add_headers(${otherHeaders})` : undefined;
|
|
97
|
+
// Construct request
|
|
98
|
+
let request = `response <- VERB("${method}", url`;
|
|
99
|
+
if (payload) {
|
|
100
|
+
request += ', body = payload';
|
|
101
|
+
}
|
|
102
|
+
if (queryString.length) {
|
|
103
|
+
request += ', query = queryString';
|
|
104
|
+
}
|
|
105
|
+
const headerAdditions = [setHeaders, setContentType, setAccept, setCookies]
|
|
106
|
+
.filter(x => !!x)
|
|
107
|
+
.join(', ');
|
|
108
|
+
if (headerAdditions) {
|
|
109
|
+
request += `, ${headerAdditions}`;
|
|
110
|
+
}
|
|
111
|
+
if (postData && (postData.text || postData.jsonObj || postData.params)) {
|
|
112
|
+
request += ', encode = encode';
|
|
113
|
+
}
|
|
114
|
+
request += ')';
|
|
115
|
+
push(request);
|
|
116
|
+
blank();
|
|
117
|
+
// Print response
|
|
118
|
+
push('content(response, "text")');
|
|
119
|
+
return join();
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export { httr };
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
|
|
2
|
+
import { escapeForDoubleQuotes } from '../httpsnippet-lite/dist/esm/helpers/escape.js';
|
|
3
|
+
import { getHeaderName } from '../httpsnippet-lite/dist/esm/helpers/headers.js';
|
|
4
|
+
import { literalRepresentation } from '../httpsnippet-lite/dist/esm/targets/python/helpers.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @description
|
|
8
|
+
* HTTP code snippet generator for Python using Requests
|
|
9
|
+
*
|
|
10
|
+
* @author
|
|
11
|
+
* @montanaflynn
|
|
12
|
+
*
|
|
13
|
+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
14
|
+
*/
|
|
15
|
+
const builtInMethods = ['HEAD', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'];
|
|
16
|
+
const requests = {
|
|
17
|
+
info: {
|
|
18
|
+
key: 'requests',
|
|
19
|
+
title: 'Requests',
|
|
20
|
+
link: 'http://docs.python-requests.org/en/latest/api/#requests.request',
|
|
21
|
+
description: 'Requests HTTP library',
|
|
22
|
+
},
|
|
23
|
+
convert: ({ queryObj, url, postData, allHeaders, method }, options) => {
|
|
24
|
+
const opts = {
|
|
25
|
+
indent: ' ',
|
|
26
|
+
pretty: true,
|
|
27
|
+
...options,
|
|
28
|
+
};
|
|
29
|
+
// Start snippet
|
|
30
|
+
const { push, blank, join } = new CodeBuilder({ indent: opts.indent });
|
|
31
|
+
// Import requests
|
|
32
|
+
push('import requests');
|
|
33
|
+
blank();
|
|
34
|
+
// Set URL
|
|
35
|
+
push(`url = "${url}"`);
|
|
36
|
+
blank();
|
|
37
|
+
// Construct query string
|
|
38
|
+
let qs;
|
|
39
|
+
if (Object.keys(queryObj).length) {
|
|
40
|
+
qs = `querystring = ${JSON.stringify(queryObj)}`;
|
|
41
|
+
push(qs);
|
|
42
|
+
blank();
|
|
43
|
+
}
|
|
44
|
+
const headers = allHeaders;
|
|
45
|
+
// Construct payload
|
|
46
|
+
let payload = {};
|
|
47
|
+
const files = {};
|
|
48
|
+
let hasFiles = false;
|
|
49
|
+
let hasPayload = false;
|
|
50
|
+
let jsonPayload = false;
|
|
51
|
+
switch (postData === null || postData === void 0 ? void 0 : postData.mimeType) {
|
|
52
|
+
case 'application/json':
|
|
53
|
+
if (postData.jsonObj) {
|
|
54
|
+
push(`payload = ${literalRepresentation(postData.jsonObj, opts)}`);
|
|
55
|
+
jsonPayload = true;
|
|
56
|
+
hasPayload = true;
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
case 'multipart/form-data':
|
|
60
|
+
if (!postData.params) {
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
payload = {};
|
|
64
|
+
postData.params.forEach(p => {
|
|
65
|
+
if (p.fileName) {
|
|
66
|
+
files[p.name] = `open('${p.fileName}', 'rb')`;
|
|
67
|
+
hasFiles = true;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
payload[p.name] = p.value;
|
|
71
|
+
hasPayload = true;
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
if (hasFiles) {
|
|
75
|
+
push(`files = ${literalRepresentation(files, opts)}`);
|
|
76
|
+
if (hasPayload) {
|
|
77
|
+
push(`payload = ${literalRepresentation(payload, opts)}`);
|
|
78
|
+
}
|
|
79
|
+
// The requests library will only automatically add a `multipart/form-data` header if there are files being sent. If we're **only** sending form data we still need to send the boundary ourselves.
|
|
80
|
+
const headerName = getHeaderName(headers, 'content-type');
|
|
81
|
+
if (headerName) {
|
|
82
|
+
delete headers[headerName];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
const nonFilePayload = JSON.stringify(postData.text);
|
|
87
|
+
if (nonFilePayload) {
|
|
88
|
+
push(`payload = ${nonFilePayload}`);
|
|
89
|
+
hasPayload = true;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
default: {
|
|
94
|
+
if (!postData) {
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
if (postData.mimeType === 'application/x-www-form-urlencoded' && postData.paramsObj) {
|
|
98
|
+
push(`payload = ${literalRepresentation(postData.paramsObj, opts)}`);
|
|
99
|
+
hasPayload = true;
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
const payload = JSON.stringify(postData.text);
|
|
103
|
+
if (payload) {
|
|
104
|
+
push(`payload = ${payload}`);
|
|
105
|
+
hasPayload = true;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// Construct headers
|
|
110
|
+
const headerCount = Object.keys(headers).length;
|
|
111
|
+
if (headerCount === 0 && (hasPayload || hasFiles)) {
|
|
112
|
+
// If we don't have any heads but we do have a payload we should put a blank line here between that payload consturction and our execution of the requests library.
|
|
113
|
+
blank();
|
|
114
|
+
}
|
|
115
|
+
else if (headerCount === 1) {
|
|
116
|
+
for (const header in headers) {
|
|
117
|
+
push(`headers = {"${header}": "${escapeForDoubleQuotes(headers[header])}"}`);
|
|
118
|
+
blank();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
else if (headerCount > 1) {
|
|
122
|
+
let count = 1;
|
|
123
|
+
push('headers = {');
|
|
124
|
+
for (const header in headers) {
|
|
125
|
+
if (count !== headerCount) {
|
|
126
|
+
push(`"${header}": "${escapeForDoubleQuotes(headers[header])}",`, 1);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
push(`"${header}": "${escapeForDoubleQuotes(headers[header])}"`, 1);
|
|
130
|
+
}
|
|
131
|
+
count += 1;
|
|
132
|
+
}
|
|
133
|
+
push('}');
|
|
134
|
+
blank();
|
|
135
|
+
}
|
|
136
|
+
// Construct request
|
|
137
|
+
let request = builtInMethods.includes(method)
|
|
138
|
+
? `response = requests.${method.toLowerCase()}(url`
|
|
139
|
+
: `response = requests.request("${method}", url`;
|
|
140
|
+
if (hasPayload) {
|
|
141
|
+
if (jsonPayload) {
|
|
142
|
+
request += ', json=payload';
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
request += ', data=payload';
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (hasFiles) {
|
|
149
|
+
request += ', files=files';
|
|
150
|
+
}
|
|
151
|
+
if (headerCount > 0) {
|
|
152
|
+
request += ', headers=headers';
|
|
153
|
+
}
|
|
154
|
+
if (qs) {
|
|
155
|
+
request += ', params=querystring';
|
|
156
|
+
}
|
|
157
|
+
request += ')';
|
|
158
|
+
push(request);
|
|
159
|
+
blank();
|
|
160
|
+
// Print response
|
|
161
|
+
push('print(response.json())');
|
|
162
|
+
return join();
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
export { requests };
|
|
@@ -0,0 +1,91 @@
|
|
|
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 native Python3.
|
|
7
|
+
*
|
|
8
|
+
* @author
|
|
9
|
+
* @montanaflynn
|
|
10
|
+
*
|
|
11
|
+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
12
|
+
*/
|
|
13
|
+
const python3 = {
|
|
14
|
+
info: {
|
|
15
|
+
key: 'python3',
|
|
16
|
+
title: 'http.client',
|
|
17
|
+
link: 'https://docs.python.org/3/library/http.client.html',
|
|
18
|
+
description: 'Python3 HTTP Client',
|
|
19
|
+
},
|
|
20
|
+
convert: ({ uriObj: { path, protocol, host }, postData, allHeaders, method }, options = {}) => {
|
|
21
|
+
const { insecureSkipVerify = false } = options;
|
|
22
|
+
const { push, blank, join } = new CodeBuilder();
|
|
23
|
+
// Start Request
|
|
24
|
+
push('import http.client');
|
|
25
|
+
if (insecureSkipVerify) {
|
|
26
|
+
push('import ssl');
|
|
27
|
+
}
|
|
28
|
+
blank();
|
|
29
|
+
// Check which protocol to be used for the client connection
|
|
30
|
+
if (protocol === 'https:') {
|
|
31
|
+
const sslContext = insecureSkipVerify ? ', context = ssl._create_unverified_context()' : '';
|
|
32
|
+
push(`conn = http.client.HTTPSConnection("${host}"${sslContext})`);
|
|
33
|
+
blank();
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
push(`conn = http.client.HTTPConnection("${host}")`);
|
|
37
|
+
blank();
|
|
38
|
+
}
|
|
39
|
+
// Create payload string if it exists
|
|
40
|
+
const payload = JSON.stringify(postData === null || postData === void 0 ? void 0 : postData.text);
|
|
41
|
+
if (payload) {
|
|
42
|
+
push(`payload = ${payload}`);
|
|
43
|
+
blank();
|
|
44
|
+
}
|
|
45
|
+
// Create Headers
|
|
46
|
+
const headers = allHeaders;
|
|
47
|
+
const headerCount = Object.keys(headers).length;
|
|
48
|
+
if (headerCount === 1) {
|
|
49
|
+
for (const header in headers) {
|
|
50
|
+
push(`headers = { '${header}': "${escapeForDoubleQuotes(headers[header])}" }`);
|
|
51
|
+
blank();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else if (headerCount > 1) {
|
|
55
|
+
let count = 1;
|
|
56
|
+
push('headers = {');
|
|
57
|
+
for (const header in headers) {
|
|
58
|
+
if (count++ !== headerCount) {
|
|
59
|
+
push(` '${header}': "${escapeForDoubleQuotes(headers[header])}",`);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
push(` '${header}': "${escapeForDoubleQuotes(headers[header])}"`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
push('}');
|
|
66
|
+
blank();
|
|
67
|
+
}
|
|
68
|
+
// Make Request
|
|
69
|
+
if (payload && headerCount) {
|
|
70
|
+
push(`conn.request("${method}", "${path}", payload, headers)`);
|
|
71
|
+
}
|
|
72
|
+
else if (payload && !headerCount) {
|
|
73
|
+
push(`conn.request("${method}", "${path}", payload)`);
|
|
74
|
+
}
|
|
75
|
+
else if (!payload && headerCount) {
|
|
76
|
+
push(`conn.request("${method}", "${path}", headers=headers)`);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
push(`conn.request("${method}", "${path}")`);
|
|
80
|
+
}
|
|
81
|
+
// Get Response
|
|
82
|
+
blank();
|
|
83
|
+
push('res = conn.getresponse()');
|
|
84
|
+
push('data = res.read()');
|
|
85
|
+
blank();
|
|
86
|
+
push('print(data.decode("utf-8"))');
|
|
87
|
+
return join();
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export { python3 };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { generatePowershellConvert } from '../httpsnippet-lite/dist/esm/targets/powershell/common.js';
|
|
2
|
+
|
|
3
|
+
const webrequest = {
|
|
4
|
+
info: {
|
|
5
|
+
key: 'webrequest',
|
|
6
|
+
title: 'Invoke-WebRequest',
|
|
7
|
+
link: 'https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Invoke-WebRequest',
|
|
8
|
+
description: 'Powershell Invoke-WebRequest client',
|
|
9
|
+
},
|
|
10
|
+
convert: generatePowershellConvert('Invoke-WebRequest'),
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { webrequest };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { generatePowershellConvert } from '../httpsnippet-lite/dist/esm/targets/powershell/common.js';
|
|
2
|
+
|
|
3
|
+
const restmethod = {
|
|
4
|
+
info: {
|
|
5
|
+
key: 'restmethod',
|
|
6
|
+
title: 'Invoke-RestMethod',
|
|
7
|
+
link: 'https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Invoke-RestMethod',
|
|
8
|
+
description: 'Powershell Invoke-RestMethod client',
|
|
9
|
+
},
|
|
10
|
+
convert: generatePowershellConvert('Invoke-RestMethod'),
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { restmethod };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import type { Client } from '@/httpsnippet-lite/dist/types/targets/targets';
|
|
1
2
|
import type { Request } from 'har-format';
|
|
2
|
-
import type { Client } from '../../../node_modules/httpsnippet-lite/dist/types/targets/targets';
|
|
3
3
|
/**
|
|
4
4
|
* Takes a httpsnippet-lite client and converts the given request to a code example with it.
|
|
5
5
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convertWithHttpSnippetLite.d.ts","sourceRoot":"","sources":["../../../src/core/utils/convertWithHttpSnippetLite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"convertWithHttpSnippetLite.d.ts","sourceRoot":"","sources":["../../../src/core/utils/convertWithHttpSnippetLite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,+CAA+C,CAAA;AAC3E,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAEzC;;GAEG;AACH,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,EACtB,cAAc,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAChC,MAAM,CAiGR"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const DEFAULT_INDENTATION_CHARACTER = '';
|
|
2
|
+
const DEFAULT_LINE_JOIN = '\n';
|
|
3
|
+
class CodeBuilder {
|
|
4
|
+
/**
|
|
5
|
+
* Helper object to format and aggragate lines of code.
|
|
6
|
+
* Lines are aggregated in a `code` array, and need to be joined to obtain a proper code snippet.
|
|
7
|
+
*/
|
|
8
|
+
constructor({ indent, join } = {}) {
|
|
9
|
+
this.postProcessors = [];
|
|
10
|
+
this.code = [];
|
|
11
|
+
this.indentationCharacter = DEFAULT_INDENTATION_CHARACTER;
|
|
12
|
+
this.lineJoin = DEFAULT_LINE_JOIN;
|
|
13
|
+
/**
|
|
14
|
+
* Add given indentation level to given line of code
|
|
15
|
+
*/
|
|
16
|
+
this.indentLine = (line, indentationLevel = 0) => {
|
|
17
|
+
const indent = this.indentationCharacter.repeat(indentationLevel);
|
|
18
|
+
return `${indent}${line}`;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Add the line at the beginning of the current lines
|
|
22
|
+
*/
|
|
23
|
+
this.unshift = (line, indentationLevel) => {
|
|
24
|
+
const newLine = this.indentLine(line, indentationLevel);
|
|
25
|
+
this.code.unshift(newLine);
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Add the line at the end of the current lines
|
|
29
|
+
*/
|
|
30
|
+
this.push = (line, indentationLevel) => {
|
|
31
|
+
const newLine = this.indentLine(line, indentationLevel);
|
|
32
|
+
this.code.push(newLine);
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Add an empty line at the end of current lines
|
|
36
|
+
*/
|
|
37
|
+
this.blank = () => {
|
|
38
|
+
this.code.push('');
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Concatenate all current lines using the given lineJoin, then apply any replacers that may have been added
|
|
42
|
+
*/
|
|
43
|
+
this.join = () => {
|
|
44
|
+
const unreplacedCode = this.code.join(this.lineJoin);
|
|
45
|
+
const replacedOutput = this.postProcessors.reduce((accumulator, replacer) => replacer(accumulator), unreplacedCode);
|
|
46
|
+
return replacedOutput;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Often when writing modules you may wish to add a literal tag or bit of metadata that you wish to transform after other processing as a final step.
|
|
50
|
+
* To do so, you can provide a PostProcessor function and it will be run automatically for you when you call `join()` later on.
|
|
51
|
+
*/
|
|
52
|
+
this.addPostProcessor = (postProcessor) => {
|
|
53
|
+
this.postProcessors = [...this.postProcessors, postProcessor];
|
|
54
|
+
};
|
|
55
|
+
this.indentationCharacter = indent || DEFAULT_INDENTATION_CHARACTER;
|
|
56
|
+
this.lineJoin = join !== null && join !== void 0 ? join : DEFAULT_LINE_JOIN;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { CodeBuilder };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escape characters within a value to make it safe to insert directly into a
|
|
3
|
+
* snippet. Takes options which define the escape requirements.
|
|
4
|
+
*
|
|
5
|
+
* This is closely based on the JSON-stringify string serialization algorithm,
|
|
6
|
+
* but generalized for other string delimiters (e.g. " or ') and different escape
|
|
7
|
+
* characters (e.g. Powershell uses `)
|
|
8
|
+
*
|
|
9
|
+
* See https://tc39.es/ecma262/multipage/structured-data.html#sec-quotejsonstring
|
|
10
|
+
* for the complete original algorithm.
|
|
11
|
+
*/
|
|
12
|
+
function escapeString(rawValue, options = {}) {
|
|
13
|
+
const { delimiter = '"', escapeChar = '\\', escapeNewlines = true } = options;
|
|
14
|
+
const stringValue = rawValue.toString();
|
|
15
|
+
return [...stringValue]
|
|
16
|
+
.map(c => {
|
|
17
|
+
if (c === '\b') {
|
|
18
|
+
return `${escapeChar}b`;
|
|
19
|
+
}
|
|
20
|
+
else if (c === '\t') {
|
|
21
|
+
return `${escapeChar}t`;
|
|
22
|
+
}
|
|
23
|
+
else if (c === '\n') {
|
|
24
|
+
if (escapeNewlines) {
|
|
25
|
+
return `${escapeChar}n`;
|
|
26
|
+
}
|
|
27
|
+
return c; // Don't just continue, or this is caught by < \u0020
|
|
28
|
+
}
|
|
29
|
+
else if (c === '\f') {
|
|
30
|
+
return `${escapeChar}f`;
|
|
31
|
+
}
|
|
32
|
+
else if (c === '\r') {
|
|
33
|
+
if (escapeNewlines) {
|
|
34
|
+
return `${escapeChar}r`;
|
|
35
|
+
}
|
|
36
|
+
return c; // Don't just continue, or this is caught by < \u0020
|
|
37
|
+
}
|
|
38
|
+
else if (c === escapeChar) {
|
|
39
|
+
return escapeChar + escapeChar;
|
|
40
|
+
}
|
|
41
|
+
else if (c === delimiter) {
|
|
42
|
+
return escapeChar + delimiter;
|
|
43
|
+
}
|
|
44
|
+
else if (c < '\u0020' || c > '\u007E') {
|
|
45
|
+
// Delegate the trickier non-ASCII cases to the normal algorithm. Some of these
|
|
46
|
+
// are escaped as \uXXXX, whilst others are represented literally. Since we're
|
|
47
|
+
// using this primarily for header values that are generally (though not 100%
|
|
48
|
+
// strictly?) ASCII-only, this should almost never happen.
|
|
49
|
+
return JSON.stringify(c).slice(1, -1);
|
|
50
|
+
}
|
|
51
|
+
return c;
|
|
52
|
+
})
|
|
53
|
+
.join('');
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Make a string value safe to insert literally into a snippet within single quotes,
|
|
57
|
+
* by escaping problematic characters, including single quotes inside the string,
|
|
58
|
+
* backslashes, newlines, and other special characters.
|
|
59
|
+
*
|
|
60
|
+
* If value is not a string, it will be stringified with .toString() first.
|
|
61
|
+
*/
|
|
62
|
+
const escapeForSingleQuotes = (value) => escapeString(value, { delimiter: "'" });
|
|
63
|
+
/**
|
|
64
|
+
* Make a string value safe to insert literally into a snippet within double quotes,
|
|
65
|
+
* by escaping problematic characters, including double quotes inside the string,
|
|
66
|
+
* backslashes, newlines, and other special characters.
|
|
67
|
+
*
|
|
68
|
+
* If value is not a string, it will be stringified with .toString() first.
|
|
69
|
+
*/
|
|
70
|
+
const escapeForDoubleQuotes = (value) => escapeString(value, { delimiter: '"' });
|
|
71
|
+
|
|
72
|
+
export { escapeForDoubleQuotes, escapeForSingleQuotes, escapeString };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Given a headers object retrieve a specific header out of it via a case-insensitive key.
|
|
3
|
+
*/
|
|
4
|
+
const getHeaderName = (headers, name) => Object.keys(headers).find(header => header.toLowerCase() === name.toLowerCase());
|
|
5
|
+
/**
|
|
6
|
+
* Given a headers object retrieve the contents of a header out of it via a case-insensitive key.
|
|
7
|
+
*/
|
|
8
|
+
const getHeader = (headers, name) => {
|
|
9
|
+
const headerName = getHeaderName(headers, name);
|
|
10
|
+
if (!headerName) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
return headers[headerName];
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Determine if a given case-insensitive header exists within a header object.
|
|
17
|
+
*/
|
|
18
|
+
const hasHeader = (headers, name) => Boolean(getHeaderName(headers, name));
|
|
19
|
+
|
|
20
|
+
export { getHeader, getHeaderName, hasHeader };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Use 'strong quoting' using single quotes so that we only need to deal with nested single quote characters.
|
|
3
|
+
* see: http://wiki.bash-hackers.org/syntax/quoting#strong_quoting
|
|
4
|
+
*/
|
|
5
|
+
const quote = (value = '') => {
|
|
6
|
+
const safe = /^[a-z0-9-_/.@%^=:]+$/i;
|
|
7
|
+
const isShellSafe = safe.test(value);
|
|
8
|
+
if (isShellSafe) {
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
// if the value is not shell safe, then quote it
|
|
12
|
+
return `'${value.replace(/'/g, "'\\''")}'`;
|
|
13
|
+
};
|
|
14
|
+
const escape = (value) => value.replace(/\r/g, '\\r').replace(/\n/g, '\\n');
|
|
15
|
+
|
|
16
|
+
export { escape, quote };
|