@scalar/snippetz 0.7.4 → 0.7.6
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/clients/index.js +168 -169
- package/dist/httpsnippet-lite/helpers/code-builder.js +86 -74
- package/dist/httpsnippet-lite/helpers/escape.js +69 -44
- package/dist/httpsnippet-lite/helpers/headers.js +17 -14
- package/dist/httpsnippet-lite/helpers/reducer.js +1 -1
- package/dist/httpsnippet-lite/helpers/shell.js +13 -13
- package/dist/httpsnippet-lite/helpers/snippet-request.js +1 -1
- package/dist/httpsnippet-lite/helpers/url.js +4 -8
- package/dist/httpsnippet-lite/targets/c/libcurl/client.js +69 -67
- package/dist/httpsnippet-lite/targets/clojure/clj_http/client.js +165 -154
- package/dist/httpsnippet-lite/targets/csharp/restsharp/client.js +35 -38
- package/dist/httpsnippet-lite/targets/go/native/client.js +151 -147
- package/dist/httpsnippet-lite/targets/java/asynchttp/client.js +32 -35
- package/dist/httpsnippet-lite/targets/java/nethttp/client.js +40 -39
- package/dist/httpsnippet-lite/targets/java/okhttp/client.js +83 -73
- package/dist/httpsnippet-lite/targets/java/unirest/client.js +41 -34
- package/dist/httpsnippet-lite/targets/javascript/axios/client.js +81 -76
- package/dist/httpsnippet-lite/targets/javascript/jquery/client.js +77 -71
- package/dist/httpsnippet-lite/targets/javascript/xhr/client.js +73 -71
- package/dist/httpsnippet-lite/targets/kotlin/okhttp/client.js +83 -73
- package/dist/httpsnippet-lite/targets/node/axios/client.js +74 -69
- package/dist/httpsnippet-lite/targets/objc/helpers.js +52 -33
- package/dist/httpsnippet-lite/targets/objc/nsurlsession/client.js +123 -121
- package/dist/httpsnippet-lite/targets/ocaml/cohttp/client.js +60 -49
- package/dist/httpsnippet-lite/targets/powershell/common.js +46 -56
- package/dist/httpsnippet-lite/targets/powershell/restmethod/client.js +9 -13
- package/dist/httpsnippet-lite/targets/powershell/webrequest/client.js +9 -13
- package/dist/httpsnippet-lite/targets/python/python3/client.js +85 -68
- package/dist/httpsnippet-lite/targets/r/httr/client.js +114 -95
- package/dist/httpsnippet-lite/targets/ruby/native/client.js +66 -70
- package/dist/httpsnippet-lite/targets/shell/httpie/client.js +121 -107
- package/dist/httpsnippet-lite/targets/shell/wget/client.js +48 -43
- package/dist/httpsnippet-lite/targets/swift/helpers.js +62 -44
- package/dist/httpsnippet-lite/targets/swift/nsurlsession/client.js +126 -112
- package/dist/httpsnippet-lite/targets/target.js +1 -1
- package/dist/index.js +2 -9
- package/dist/libs/http.js +68 -56
- package/dist/libs/javascript.js +77 -60
- package/dist/libs/php.js +59 -49
- package/dist/libs/rust.js +60 -33
- package/dist/libs/shell.js +14 -5
- package/dist/plugins/c/libcurl/index.js +1 -5
- package/dist/plugins/c/libcurl/libcurl.js +13 -13
- package/dist/plugins/clojure/clj_http/clj_http.js +13 -13
- package/dist/plugins/clojure/clj_http/index.js +1 -5
- package/dist/plugins/csharp/httpclient/httpclient.js +206 -150
- package/dist/plugins/csharp/httpclient/index.js +1 -5
- package/dist/plugins/csharp/restsharp/index.js +1 -5
- package/dist/plugins/csharp/restsharp/restsharp.js +13 -13
- package/dist/plugins/dart/http/http.js +95 -93
- package/dist/plugins/dart/http/index.js +1 -5
- package/dist/plugins/fsharp/httpclient/httpclient.js +169 -122
- package/dist/plugins/fsharp/httpclient/index.js +1 -5
- package/dist/plugins/go/native/index.js +1 -5
- package/dist/plugins/go/native/native.js +13 -13
- package/dist/plugins/http/http11/http11.d.ts.map +1 -1
- package/dist/plugins/http/http11/http11.js +104 -85
- package/dist/plugins/http/http11/index.js +1 -5
- package/dist/plugins/java/asynchttp/asynchttp.js +13 -13
- package/dist/plugins/java/asynchttp/index.js +1 -5
- package/dist/plugins/java/nethttp/index.js +1 -5
- package/dist/plugins/java/nethttp/nethttp.js +13 -13
- package/dist/plugins/java/okhttp/index.js +1 -5
- package/dist/plugins/java/okhttp/okhttp.js +13 -13
- package/dist/plugins/java/unirest/index.js +1 -5
- package/dist/plugins/java/unirest/unirest.js +13 -13
- package/dist/plugins/js/axios/axios.js +13 -13
- package/dist/plugins/js/axios/index.js +1 -5
- package/dist/plugins/js/fetch/fetch.js +82 -68
- package/dist/plugins/js/fetch/index.js +1 -5
- package/dist/plugins/js/jquery/index.js +1 -5
- package/dist/plugins/js/jquery/jquery.js +13 -13
- package/dist/plugins/js/ofetch/index.js +1 -5
- package/dist/plugins/js/ofetch/ofetch.js +61 -48
- package/dist/plugins/js/xhr/index.js +1 -5
- package/dist/plugins/js/xhr/xhr.js +13 -13
- package/dist/plugins/kotlin/okhttp/index.js +1 -5
- package/dist/plugins/kotlin/okhttp/okhttp.js +13 -13
- package/dist/plugins/node/axios/axios.js +13 -13
- package/dist/plugins/node/axios/index.js +1 -5
- package/dist/plugins/node/fetch/fetch.js +86 -74
- package/dist/plugins/node/fetch/index.js +1 -5
- package/dist/plugins/node/ofetch/index.js +1 -5
- package/dist/plugins/node/ofetch/ofetch.js +61 -48
- package/dist/plugins/node/undici/index.js +1 -5
- package/dist/plugins/node/undici/undici.js +60 -47
- package/dist/plugins/objc/nsurlsession/index.js +1 -5
- package/dist/plugins/objc/nsurlsession/nsurlsession.js +13 -13
- package/dist/plugins/ocaml/cohttp/cohttp.js +13 -13
- package/dist/plugins/ocaml/cohttp/index.js +1 -5
- package/dist/plugins/php/curl/curl.js +143 -101
- package/dist/plugins/php/curl/index.js +1 -5
- package/dist/plugins/php/guzzle/guzzle.d.ts.map +1 -1
- package/dist/plugins/php/guzzle/guzzle.js +109 -83
- package/dist/plugins/php/guzzle/index.js +1 -5
- package/dist/plugins/powershell/restmethod/index.js +1 -5
- package/dist/plugins/powershell/restmethod/restmethod.js +13 -13
- package/dist/plugins/powershell/webrequest/index.js +1 -5
- package/dist/plugins/powershell/webrequest/webrequest.js +13 -13
- package/dist/plugins/python/httpx/async.js +17 -15
- package/dist/plugins/python/httpx/index.js +2 -7
- package/dist/plugins/python/httpx/sync.js +11 -12
- package/dist/plugins/python/python3/index.js +1 -5
- package/dist/plugins/python/python3/python3.js +13 -13
- package/dist/plugins/python/requests/index.js +1 -5
- package/dist/plugins/python/requests/requests.js +11 -12
- package/dist/plugins/python/requestsLike.d.ts.map +1 -1
- package/dist/plugins/python/requestsLike.js +145 -112
- package/dist/plugins/r/httr/httr.js +13 -13
- package/dist/plugins/r/httr/index.js +1 -5
- package/dist/plugins/ruby/native/index.js +1 -5
- package/dist/plugins/ruby/native/native.js +13 -13
- package/dist/plugins/rust/reqwest/index.js +1 -5
- package/dist/plugins/rust/reqwest/reqwest.js +126 -83
- package/dist/plugins/shell/curl/curl.d.ts.map +1 -1
- package/dist/plugins/shell/curl/curl.js +129 -97
- package/dist/plugins/shell/curl/index.js +1 -5
- package/dist/plugins/shell/httpie/httpie.js +13 -13
- package/dist/plugins/shell/httpie/index.js +1 -5
- package/dist/plugins/shell/wget/index.js +1 -5
- package/dist/plugins/shell/wget/wget.js +13 -13
- package/dist/plugins/swift/nsurlsession/index.js +1 -5
- package/dist/plugins/swift/nsurlsession/nsurlsession.js +13 -13
- package/dist/snippetz.js +25 -28
- package/dist/utils/convertWithHttpSnippetLite.js +100 -96
- package/dist/utils/index.js +1 -5
- package/package.json +8 -12
- package/dist/clients/index.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/code-builder.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/escape.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/headers.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/reducer.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/shell.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/snippet-request.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/url.js.map +0 -7
- package/dist/httpsnippet-lite/targets/c/libcurl/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/clojure/clj_http/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/csharp/restsharp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/go/native/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/java/asynchttp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/java/nethttp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/java/okhttp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/java/unirest/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/javascript/axios/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/javascript/jquery/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/javascript/xhr/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/kotlin/okhttp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/node/axios/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/objc/helpers.js.map +0 -7
- package/dist/httpsnippet-lite/targets/objc/nsurlsession/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/ocaml/cohttp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/powershell/common.js.map +0 -7
- package/dist/httpsnippet-lite/targets/powershell/restmethod/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/powershell/webrequest/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/python/python3/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/r/httr/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/ruby/native/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/shell/httpie/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/shell/wget/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/swift/helpers.js.map +0 -7
- package/dist/httpsnippet-lite/targets/swift/nsurlsession/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/target.js.map +0 -7
- package/dist/index.js.map +0 -7
- package/dist/libs/http.js.map +0 -7
- package/dist/libs/javascript.js.map +0 -7
- package/dist/libs/php.js.map +0 -7
- package/dist/libs/rust.js.map +0 -7
- package/dist/libs/shell.js.map +0 -7
- package/dist/plugins/c/libcurl/index.js.map +0 -7
- package/dist/plugins/c/libcurl/libcurl.js.map +0 -7
- package/dist/plugins/clojure/clj_http/clj_http.js.map +0 -7
- package/dist/plugins/clojure/clj_http/index.js.map +0 -7
- package/dist/plugins/csharp/httpclient/httpclient.js.map +0 -7
- package/dist/plugins/csharp/httpclient/index.js.map +0 -7
- package/dist/plugins/csharp/restsharp/index.js.map +0 -7
- package/dist/plugins/csharp/restsharp/restsharp.js.map +0 -7
- package/dist/plugins/dart/http/http.js.map +0 -7
- package/dist/plugins/dart/http/index.js.map +0 -7
- package/dist/plugins/fsharp/httpclient/httpclient.js.map +0 -7
- package/dist/plugins/fsharp/httpclient/index.js.map +0 -7
- package/dist/plugins/go/native/index.js.map +0 -7
- package/dist/plugins/go/native/native.js.map +0 -7
- package/dist/plugins/http/http11/http11.js.map +0 -7
- package/dist/plugins/http/http11/index.js.map +0 -7
- package/dist/plugins/java/asynchttp/asynchttp.js.map +0 -7
- package/dist/plugins/java/asynchttp/index.js.map +0 -7
- package/dist/plugins/java/nethttp/index.js.map +0 -7
- package/dist/plugins/java/nethttp/nethttp.js.map +0 -7
- package/dist/plugins/java/okhttp/index.js.map +0 -7
- package/dist/plugins/java/okhttp/okhttp.js.map +0 -7
- package/dist/plugins/java/unirest/index.js.map +0 -7
- package/dist/plugins/java/unirest/unirest.js.map +0 -7
- package/dist/plugins/js/axios/axios.js.map +0 -7
- package/dist/plugins/js/axios/index.js.map +0 -7
- package/dist/plugins/js/fetch/fetch.js.map +0 -7
- package/dist/plugins/js/fetch/index.js.map +0 -7
- package/dist/plugins/js/jquery/index.js.map +0 -7
- package/dist/plugins/js/jquery/jquery.js.map +0 -7
- package/dist/plugins/js/ofetch/index.js.map +0 -7
- package/dist/plugins/js/ofetch/ofetch.js.map +0 -7
- package/dist/plugins/js/xhr/index.js.map +0 -7
- package/dist/plugins/js/xhr/xhr.js.map +0 -7
- package/dist/plugins/kotlin/okhttp/index.js.map +0 -7
- package/dist/plugins/kotlin/okhttp/okhttp.js.map +0 -7
- package/dist/plugins/node/axios/axios.js.map +0 -7
- package/dist/plugins/node/axios/index.js.map +0 -7
- package/dist/plugins/node/fetch/fetch.js.map +0 -7
- package/dist/plugins/node/fetch/index.js.map +0 -7
- package/dist/plugins/node/ofetch/index.js.map +0 -7
- package/dist/plugins/node/ofetch/ofetch.js.map +0 -7
- package/dist/plugins/node/undici/index.js.map +0 -7
- package/dist/plugins/node/undici/undici.js.map +0 -7
- package/dist/plugins/objc/nsurlsession/index.js.map +0 -7
- package/dist/plugins/objc/nsurlsession/nsurlsession.js.map +0 -7
- package/dist/plugins/ocaml/cohttp/cohttp.js.map +0 -7
- package/dist/plugins/ocaml/cohttp/index.js.map +0 -7
- package/dist/plugins/php/curl/curl.js.map +0 -7
- package/dist/plugins/php/curl/index.js.map +0 -7
- package/dist/plugins/php/guzzle/guzzle.js.map +0 -7
- package/dist/plugins/php/guzzle/index.js.map +0 -7
- package/dist/plugins/powershell/restmethod/index.js.map +0 -7
- package/dist/plugins/powershell/restmethod/restmethod.js.map +0 -7
- package/dist/plugins/powershell/webrequest/index.js.map +0 -7
- package/dist/plugins/powershell/webrequest/webrequest.js.map +0 -7
- package/dist/plugins/python/httpx/async.js.map +0 -7
- package/dist/plugins/python/httpx/index.js.map +0 -7
- package/dist/plugins/python/httpx/sync.js.map +0 -7
- package/dist/plugins/python/python3/index.js.map +0 -7
- package/dist/plugins/python/python3/python3.js.map +0 -7
- package/dist/plugins/python/requests/index.js.map +0 -7
- package/dist/plugins/python/requests/requests.js.map +0 -7
- package/dist/plugins/python/requestsLike.js.map +0 -7
- package/dist/plugins/r/httr/httr.js.map +0 -7
- package/dist/plugins/r/httr/index.js.map +0 -7
- package/dist/plugins/ruby/native/index.js.map +0 -7
- package/dist/plugins/ruby/native/native.js.map +0 -7
- package/dist/plugins/rust/reqwest/index.js.map +0 -7
- package/dist/plugins/rust/reqwest/reqwest.js.map +0 -7
- package/dist/plugins/shell/curl/curl.js.map +0 -7
- package/dist/plugins/shell/curl/index.js.map +0 -7
- package/dist/plugins/shell/httpie/httpie.js.map +0 -7
- package/dist/plugins/shell/httpie/index.js.map +0 -7
- package/dist/plugins/shell/wget/index.js.map +0 -7
- package/dist/plugins/shell/wget/wget.js.map +0 -7
- package/dist/plugins/swift/nsurlsession/index.js.map +0 -7
- package/dist/plugins/swift/nsurlsession/nsurlsession.js.map +0 -7
- package/dist/snippetz.js.map +0 -7
- package/dist/utils/convertWithHttpSnippetLite.js.map +0 -7
- package/dist/utils/index.js.map +0 -7
|
@@ -1,167 +1,223 @@
|
|
|
1
|
-
import { encode } from
|
|
2
|
-
import { createSearchParams } from
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
1
|
+
import { encode } from 'js-base64';
|
|
2
|
+
import { createSearchParams } from '../../../libs/http.js';
|
|
3
|
+
/**
|
|
4
|
+
* csharp/httpclient
|
|
5
|
+
*/
|
|
6
|
+
export const csharpHttpclient = {
|
|
7
|
+
target: 'csharp',
|
|
8
|
+
client: 'httpclient',
|
|
9
|
+
title: 'HttpClient',
|
|
10
|
+
generate(request, configuration) {
|
|
11
|
+
// Defaults
|
|
12
|
+
const normalizedRequest = {
|
|
13
|
+
method: 'GET',
|
|
14
|
+
url: '',
|
|
15
|
+
...request,
|
|
16
|
+
};
|
|
17
|
+
// Normalization
|
|
18
|
+
normalizedRequest.method = normalizedRequest.method.toUpperCase();
|
|
19
|
+
// Build URL with query string
|
|
20
|
+
const searchParams = createSearchParams(normalizedRequest.queryString);
|
|
21
|
+
const queryString = searchParams.size ? `?${searchParams.toString()}` : '';
|
|
22
|
+
const url = `${normalizedRequest.url}${queryString}`;
|
|
23
|
+
// Start building the snippet
|
|
24
|
+
const lines = [];
|
|
25
|
+
// HttpClient declaration
|
|
26
|
+
lines.push('using var client = new HttpClient();');
|
|
27
|
+
lines.push('');
|
|
28
|
+
// HttpRequestMessage
|
|
29
|
+
const httpMethod = getHttpMethod(normalizedRequest.method);
|
|
30
|
+
lines.push(`var request = new HttpRequestMessage(${httpMethod}, "${url}");`);
|
|
31
|
+
// Headers and auth
|
|
32
|
+
addHeadersAndAuth(lines, normalizedRequest, configuration);
|
|
33
|
+
// Body content
|
|
34
|
+
addBodyContent(lines, normalizedRequest);
|
|
35
|
+
// Send request and read response
|
|
36
|
+
lines.push('');
|
|
37
|
+
lines.push('using var response = await client.SendAsync(request);');
|
|
38
|
+
return lines.join('\n');
|
|
39
|
+
},
|
|
28
40
|
};
|
|
41
|
+
/**
|
|
42
|
+
* Convert HTTP method to HttpMethod constant
|
|
43
|
+
*/
|
|
29
44
|
function getHttpMethod(method) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
switch (method) {
|
|
46
|
+
case 'GET':
|
|
47
|
+
return 'HttpMethod.Get';
|
|
48
|
+
case 'POST':
|
|
49
|
+
return 'HttpMethod.Post';
|
|
50
|
+
case 'PUT':
|
|
51
|
+
return 'HttpMethod.Put';
|
|
52
|
+
case 'DELETE':
|
|
53
|
+
return 'HttpMethod.Delete';
|
|
54
|
+
case 'PATCH':
|
|
55
|
+
return 'HttpMethod.Patch';
|
|
56
|
+
case 'HEAD':
|
|
57
|
+
return 'HttpMethod.Head';
|
|
58
|
+
case 'OPTIONS':
|
|
59
|
+
return 'HttpMethod.Options';
|
|
60
|
+
default:
|
|
61
|
+
return `new HttpMethod("${method}")`;
|
|
62
|
+
}
|
|
48
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Add headers and authentication to the request
|
|
66
|
+
*/
|
|
49
67
|
function addHeadersAndAuth(lines, request, configuration) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
68
|
+
const headers = request.headers || [];
|
|
69
|
+
const cookies = request.cookies || [];
|
|
70
|
+
// Check for explicit Authorization header first
|
|
71
|
+
const authHeader = headers.find((h) => h.name.toLowerCase() === 'authorization');
|
|
72
|
+
if (authHeader) {
|
|
73
|
+
const [scheme, parameter] = authHeader.value.split(' ', 2);
|
|
74
|
+
if (scheme && parameter) {
|
|
75
|
+
lines.push(`request.Headers.Authorization = new AuthenticationHeaderValue("${scheme}", "${parameter}");`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
else if (configuration?.auth?.username && configuration?.auth?.password) {
|
|
79
|
+
// Use configuration auth if no explicit header
|
|
80
|
+
const credentials = encode(`${configuration.auth.username}:${configuration.auth.password}`);
|
|
81
|
+
lines.push(`request.Headers.Authorization = new AuthenticationHeaderValue("Basic", "${credentials}");`);
|
|
82
|
+
}
|
|
83
|
+
// Process other headers (keep only the last value for duplicates)
|
|
84
|
+
const processedHeaders = new Map();
|
|
85
|
+
for (const header of headers) {
|
|
86
|
+
const name = header.name;
|
|
87
|
+
const value = header.value;
|
|
88
|
+
if (name.toLowerCase() === 'authorization') {
|
|
89
|
+
// Already handled above
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
processedHeaders.set(name, value);
|
|
57
93
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
94
|
+
for (const [name, value] of processedHeaders) {
|
|
95
|
+
if (name.toLowerCase() === 'accept' && isMediaType(value)) {
|
|
96
|
+
lines.push(`request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("${value}"));`);
|
|
97
|
+
}
|
|
98
|
+
else if (name.toLowerCase() === 'content-type' && request.postData) {
|
|
99
|
+
// Content-Type will be set on content object
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
lines.push(`request.Headers.TryAddWithoutValidation("${name}", "${value}");`);
|
|
104
|
+
}
|
|
68
105
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
lines.push(`request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("${value}"));`);
|
|
74
|
-
} else if (name.toLowerCase() === "content-type" && request.postData) {
|
|
75
|
-
continue;
|
|
76
|
-
} else {
|
|
77
|
-
lines.push(`request.Headers.TryAddWithoutValidation("${name}", "${value}");`);
|
|
106
|
+
// Add cookies
|
|
107
|
+
if (cookies.length > 0) {
|
|
108
|
+
const cookieString = cookies.map((cookie) => `${cookie.name}=${cookie.value}`).join('; ');
|
|
109
|
+
lines.push(`request.Headers.TryAddWithoutValidation("Cookie", "${cookieString}");`);
|
|
78
110
|
}
|
|
79
|
-
}
|
|
80
|
-
if (cookies.length > 0) {
|
|
81
|
-
const cookieString = cookies.map((cookie) => `${cookie.name}=${cookie.value}`).join("; ");
|
|
82
|
-
lines.push(`request.Headers.TryAddWithoutValidation("Cookie", "${cookieString}");`);
|
|
83
|
-
}
|
|
84
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Add body content to the request
|
|
114
|
+
*/
|
|
85
115
|
function addBodyContent(lines, request) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
116
|
+
if (!request.postData) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const { mimeType, text, params } = request.postData;
|
|
120
|
+
if (mimeType === 'application/json' && text) {
|
|
121
|
+
try {
|
|
122
|
+
const jsonData = JSON.parse(text);
|
|
123
|
+
const prettyJson = JSON.stringify(jsonData, null, 2);
|
|
124
|
+
const rawStringLiteral = createRawStringLiteral(prettyJson);
|
|
125
|
+
lines.push('request.Content = new StringContent(');
|
|
126
|
+
lines.push(`${rawStringLiteral},`);
|
|
127
|
+
lines.push('System.Text.Encoding.UTF8, "application/json");');
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
const rawStringLiteral = createRawStringLiteral(text);
|
|
131
|
+
lines.push('request.Content = new StringContent(');
|
|
132
|
+
lines.push(`${rawStringLiteral},`);
|
|
133
|
+
lines.push('System.Text.Encoding.UTF8, "application/json");');
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else if (mimeType === 'application/x-www-form-urlencoded' && params) {
|
|
137
|
+
// Check for duplicate field names
|
|
138
|
+
const fieldNames = params.map((p) => p.name);
|
|
139
|
+
const hasDuplicates = fieldNames.length !== new Set(fieldNames).size;
|
|
140
|
+
if (hasDuplicates) {
|
|
141
|
+
// Use List<KeyValuePair> for duplicates
|
|
142
|
+
lines.push('var formParams = new List<KeyValuePair<string, string>>');
|
|
143
|
+
lines.push('{');
|
|
144
|
+
for (const param of params) {
|
|
145
|
+
lines.push(` new("${param.name}", "${param.value}"),`);
|
|
146
|
+
}
|
|
147
|
+
lines.push('};');
|
|
148
|
+
lines.push('request.Content = new FormUrlEncodedContent(formParams);');
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
// Use Dictionary for clean syntax
|
|
152
|
+
lines.push('var formParams = new Dictionary<string, string>');
|
|
153
|
+
lines.push('{');
|
|
154
|
+
for (const param of params) {
|
|
155
|
+
lines.push(` ["${param.name}"] = "${param.value}",`);
|
|
156
|
+
}
|
|
157
|
+
lines.push('};');
|
|
158
|
+
lines.push('request.Content = new FormUrlEncodedContent(formParams);');
|
|
159
|
+
}
|
|
103
160
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
161
|
+
else if (mimeType === 'multipart/form-data' && params) {
|
|
162
|
+
lines.push('var content = new MultipartFormDataContent();');
|
|
163
|
+
let multipartContentIndex = 0;
|
|
164
|
+
for (const param of params) {
|
|
165
|
+
if (param.fileName !== undefined) {
|
|
166
|
+
if (param.contentType) {
|
|
167
|
+
const contentName = `fileContent${multipartContentIndex++}`;
|
|
168
|
+
lines.push(`var ${contentName} = new StreamContent(File.OpenRead("${escapeCSharpString(param.fileName)}"));`);
|
|
169
|
+
lines.push(`${contentName}.Headers.ContentType = new MediaTypeHeaderValue("${param.contentType}");`);
|
|
170
|
+
lines.push(`content.Add(${contentName}, "${escapeCSharpString(param.name)}", "${escapeCSharpString(param.fileName)}");`);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
lines.push(`content.Add(new StreamContent(File.OpenRead("${escapeCSharpString(param.fileName)}")), "${escapeCSharpString(param.name)}", "${escapeCSharpString(param.fileName)}");`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
if (param.contentType) {
|
|
178
|
+
const contentName = `stringContent${multipartContentIndex++}`;
|
|
179
|
+
lines.push(`var ${contentName} = new StringContent("${escapeCSharpString(param.value ?? '')}");`);
|
|
180
|
+
lines.push(`${contentName}.Headers.ContentType = new MediaTypeHeaderValue("${param.contentType}");`);
|
|
181
|
+
lines.push(`content.Add(${contentName}, "${escapeCSharpString(param.name)}");`);
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
lines.push(`content.Add(new StringContent("${escapeCSharpString(param.value ?? '')}"), "${escapeCSharpString(param.name)}");`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
lines.push('request.Content = content;');
|
|
123
189
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
lines.push(
|
|
133
|
-
|
|
190
|
+
else if (mimeType === 'application/octet-stream' && text) {
|
|
191
|
+
lines.push('var content = new ByteArrayContent(System.Text.Encoding.UTF8.GetBytes("' + text.replace(/"/g, '\\"') + '"));');
|
|
192
|
+
lines.push('content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");');
|
|
193
|
+
lines.push('request.Content = content;');
|
|
194
|
+
}
|
|
195
|
+
else if (text) {
|
|
196
|
+
// Fallback for other content types
|
|
197
|
+
const rawStringLiteral = createRawStringLiteral(text);
|
|
198
|
+
lines.push('request.Content = new StringContent(');
|
|
199
|
+
lines.push(`${rawStringLiteral},`);
|
|
200
|
+
lines.push(`System.Text.Encoding.UTF8, "${mimeType}");`);
|
|
134
201
|
}
|
|
135
|
-
lines.push("request.Content = content;");
|
|
136
|
-
} else if (mimeType === "application/octet-stream" && text) {
|
|
137
|
-
lines.push(
|
|
138
|
-
'var content = new ByteArrayContent(System.Text.Encoding.UTF8.GetBytes("' + text.replace(/"/g, '\\"') + '"));'
|
|
139
|
-
);
|
|
140
|
-
lines.push('content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");');
|
|
141
|
-
lines.push("request.Content = content;");
|
|
142
|
-
} else if (text) {
|
|
143
|
-
const rawStringLiteral = createRawStringLiteral(text);
|
|
144
|
-
lines.push("request.Content = new StringContent(");
|
|
145
|
-
lines.push(`${rawStringLiteral},`);
|
|
146
|
-
lines.push(`System.Text.Encoding.UTF8, "${mimeType}");`);
|
|
147
|
-
}
|
|
148
202
|
}
|
|
203
|
+
/**
|
|
204
|
+
* Create a C# raw string literal with minimal quote count
|
|
205
|
+
*/
|
|
149
206
|
function createRawStringLiteral(text) {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
quoteCount
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
${text}
|
|
157
|
-
${quotes}`;
|
|
207
|
+
// Find the minimum number of quotes needed
|
|
208
|
+
let quoteCount = 3;
|
|
209
|
+
while (text.includes('"'.repeat(quoteCount))) {
|
|
210
|
+
quoteCount++;
|
|
211
|
+
}
|
|
212
|
+
const quotes = '"'.repeat(quoteCount);
|
|
213
|
+
return `${quotes}\n${text}\n${quotes}`;
|
|
158
214
|
}
|
|
215
|
+
function escapeCSharpString(text) {
|
|
216
|
+
return text.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Check if a value looks like a media type
|
|
220
|
+
*/
|
|
159
221
|
function isMediaType(value) {
|
|
160
|
-
|
|
161
|
-
value
|
|
162
|
-
);
|
|
222
|
+
return /^[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_]*\/[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_]*(\s*;\s*[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_]*=.*)?$/.test(value);
|
|
163
223
|
}
|
|
164
|
-
export {
|
|
165
|
-
csharpHttpclient
|
|
166
|
-
};
|
|
167
|
-
//# sourceMappingURL=httpclient.js.map
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { restsharp } from
|
|
2
|
-
import { convertWithHttpSnippetLite } from
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { restsharp } from '../../../httpsnippet-lite/targets/csharp/restsharp/client.js';
|
|
2
|
+
import { convertWithHttpSnippetLite } from '../../../utils/convertWithHttpSnippetLite.js';
|
|
3
|
+
/**
|
|
4
|
+
* csharp/restsharp
|
|
5
|
+
*/
|
|
6
|
+
export const csharpRestsharp = {
|
|
7
|
+
target: 'csharp',
|
|
8
|
+
client: 'restsharp',
|
|
9
|
+
title: 'RestSharp',
|
|
10
|
+
generate(request) {
|
|
11
|
+
// TODO: Write an own converter
|
|
12
|
+
return convertWithHttpSnippetLite(restsharp, request);
|
|
13
|
+
},
|
|
10
14
|
};
|
|
11
|
-
export {
|
|
12
|
-
csharpRestsharp
|
|
13
|
-
};
|
|
14
|
-
//# sourceMappingURL=restsharp.js.map
|
|
@@ -1,97 +1,99 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
1
|
+
/**
|
|
2
|
+
* dart/http
|
|
3
|
+
*/
|
|
4
|
+
export const dartHttp = {
|
|
5
|
+
target: 'dart',
|
|
6
|
+
client: 'http',
|
|
7
|
+
title: 'Http',
|
|
8
|
+
generate(request, options) {
|
|
9
|
+
// Defaults
|
|
10
|
+
const normalizedRequest = {
|
|
11
|
+
method: 'GET',
|
|
12
|
+
...request,
|
|
13
|
+
};
|
|
14
|
+
// Normalize method to uppercase
|
|
15
|
+
normalizedRequest.method = normalizedRequest.method.toUpperCase();
|
|
16
|
+
// Start building the Dart code
|
|
17
|
+
let code = `import 'package:http/http.dart' as http;\n\nvoid main() async {\n`;
|
|
18
|
+
// Handle cookies
|
|
19
|
+
let cookieHeader = '';
|
|
20
|
+
let cookieString = '';
|
|
21
|
+
if (normalizedRequest.cookies && normalizedRequest.cookies.length > 0) {
|
|
22
|
+
cookieString = normalizedRequest.cookies
|
|
23
|
+
.map((cookie) => `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`)
|
|
24
|
+
.join('; ');
|
|
25
|
+
cookieHeader = ` "Cookie": "${cookieString}",\n`;
|
|
26
26
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (Object.keys(headers).length > 0) {
|
|
42
|
-
code += " final headers = <String,String>{\n";
|
|
43
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
44
|
-
if (value.includes("utf8.encode")) {
|
|
45
|
-
code += ` '${key}': ${value},
|
|
46
|
-
`;
|
|
47
|
-
} else {
|
|
48
|
-
code += ` '${key}': '${value}',
|
|
49
|
-
`;
|
|
27
|
+
// Handle headers
|
|
28
|
+
const headers = normalizedRequest.headers?.reduce((acc, header) => {
|
|
29
|
+
if (header.value && !/[; ]/.test(header.name)) {
|
|
30
|
+
acc[header.name] = header.value;
|
|
31
|
+
}
|
|
32
|
+
return acc;
|
|
33
|
+
}, {}) || {};
|
|
34
|
+
// Add Authorization header if credentials are provided
|
|
35
|
+
if (options?.auth) {
|
|
36
|
+
const { username, password } = options.auth;
|
|
37
|
+
if (username && password) {
|
|
38
|
+
const credentials = `${username}:${password}`;
|
|
39
|
+
headers['Authorization'] = `'Basic ' + base64Encode(utf8.encode('${credentials}'))`;
|
|
40
|
+
}
|
|
50
41
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
const queryString = normalizedRequest.queryString?.length ? "?" + normalizedRequest.queryString.map((param) => `${encodeURIComponent(param.name)}=${encodeURIComponent(param.value)}`).join("&") : "";
|
|
55
|
-
const url = `${normalizedRequest.url}${queryString}`;
|
|
56
|
-
let body = "";
|
|
57
|
-
if (normalizedRequest.postData) {
|
|
58
|
-
if (normalizedRequest.postData.mimeType === "application/json") {
|
|
59
|
-
body = ` final body = r'${normalizedRequest.postData.text}';
|
|
60
|
-
|
|
61
|
-
`;
|
|
62
|
-
} else if (normalizedRequest.postData.mimeType === "application/x-www-form-urlencoded") {
|
|
63
|
-
body = ` final body = '${normalizedRequest.postData.params?.map((param) => `${encodeURIComponent(param.name)}=${encodeURIComponent(param.value ?? "")}`).join("&") || ""}';
|
|
64
|
-
|
|
65
|
-
`;
|
|
66
|
-
} else if (normalizedRequest.postData.mimeType === "multipart/form-data") {
|
|
67
|
-
body = " final body = <String,String>{\n";
|
|
68
|
-
for (const param of normalizedRequest.postData.params || []) {
|
|
69
|
-
const value = param.value || "";
|
|
70
|
-
const fileName = param.fileName || "";
|
|
71
|
-
body += ` '${param.name}': '${fileName || value}',
|
|
72
|
-
`;
|
|
42
|
+
if (cookieHeader) {
|
|
43
|
+
headers['Cookie'] = cookieString;
|
|
73
44
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
`;
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
45
|
+
if (Object.keys(headers).length > 0) {
|
|
46
|
+
code += ' final headers = <String,String>{\n';
|
|
47
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
48
|
+
if (value.includes('utf8.encode')) {
|
|
49
|
+
code += ` '${key}': ${value},\n`;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
code += ` '${key}': '${value}',\n`;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
code += ' };\n\n';
|
|
56
|
+
}
|
|
57
|
+
// Handle query string
|
|
58
|
+
const queryString = normalizedRequest.queryString?.length
|
|
59
|
+
? '?' +
|
|
60
|
+
normalizedRequest.queryString
|
|
61
|
+
.map((param) => `${encodeURIComponent(param.name)}=${encodeURIComponent(param.value)}`)
|
|
62
|
+
.join('&')
|
|
63
|
+
: '';
|
|
64
|
+
const url = `${normalizedRequest.url}${queryString}`;
|
|
65
|
+
// Handle body
|
|
66
|
+
let body = '';
|
|
67
|
+
if (normalizedRequest.postData) {
|
|
68
|
+
if (normalizedRequest.postData.mimeType === 'application/json') {
|
|
69
|
+
body = ` final body = r'${normalizedRequest.postData.text}';\n\n`;
|
|
70
|
+
}
|
|
71
|
+
else if (normalizedRequest.postData.mimeType === 'application/x-www-form-urlencoded') {
|
|
72
|
+
body = ` final body = '${normalizedRequest.postData.params?.map((param) => `${encodeURIComponent(param.name)}=${encodeURIComponent(param.value ?? '')}`).join('&') || ''}';\n\n`;
|
|
73
|
+
}
|
|
74
|
+
else if (normalizedRequest.postData.mimeType === 'multipart/form-data') {
|
|
75
|
+
body = ' final body = <String,String>{\n';
|
|
76
|
+
for (const param of normalizedRequest.postData.params || []) {
|
|
77
|
+
const value = param.value || '';
|
|
78
|
+
const fileName = param.fileName || '';
|
|
79
|
+
body += ` '${param.name}': '${fileName || value}',\n`;
|
|
80
|
+
}
|
|
81
|
+
body += ' };\n\n';
|
|
82
|
+
}
|
|
83
|
+
else if (normalizedRequest.postData.mimeType === 'application/octet-stream') {
|
|
84
|
+
body = ` final body = '${normalizedRequest.postData.text}';\n\n`;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (body) {
|
|
88
|
+
code += body;
|
|
89
|
+
}
|
|
90
|
+
// Handle method and request
|
|
91
|
+
const method = normalizedRequest.method.toLowerCase();
|
|
92
|
+
const headersPart = Object.keys(headers).length > 0 ? ', headers: headers' : '';
|
|
93
|
+
const bodyPart = body ? ', body: body' : '';
|
|
94
|
+
code += ` final response = await http.${method}(Uri.parse('${url}')${headersPart}${bodyPart});\n`;
|
|
95
|
+
code += ' print(response.body);\n';
|
|
96
|
+
code += '}';
|
|
97
|
+
return code;
|
|
98
|
+
},
|
|
96
99
|
};
|
|
97
|
-
//# sourceMappingURL=http.js.map
|