@scalar/snippetz 0.9.30 → 0.9.32
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/libs/browser-request.d.ts +14 -0
- package/dist/libs/browser-request.d.ts.map +1 -0
- package/dist/libs/browser-request.js +64 -0
- package/dist/libs/http.d.ts.map +1 -1
- package/dist/libs/java.d.ts +6 -0
- package/dist/libs/java.d.ts.map +1 -0
- package/dist/libs/java.js +21 -0
- package/dist/libs/julia.d.ts.map +1 -1
- package/dist/libs/julia.js +2 -1
- package/dist/libs/powershell.d.ts +4 -0
- package/dist/libs/powershell.d.ts.map +1 -0
- package/dist/libs/powershell.js +38 -0
- package/dist/libs/prepare-request.d.ts +21 -0
- package/dist/libs/prepare-request.d.ts.map +1 -0
- package/dist/libs/prepare-request.js +58 -0
- package/dist/plugins/clojure/clj_http/clj_http.d.ts.map +1 -1
- package/dist/plugins/clojure/clj_http/clj_http.js +2 -1
- package/dist/plugins/csharp/restsharp/restsharp.js +2 -2
- package/dist/plugins/dart/http/http.d.ts.map +1 -1
- package/dist/plugins/java/asynchttp/asynchttp.d.ts +1 -3
- package/dist/plugins/java/asynchttp/asynchttp.d.ts.map +1 -1
- package/dist/plugins/java/asynchttp/asynchttp.js +16 -8
- package/dist/plugins/java/nethttp/nethttp.d.ts +1 -3
- package/dist/plugins/java/nethttp/nethttp.d.ts.map +1 -1
- package/dist/plugins/java/nethttp/nethttp.js +17 -8
- package/dist/plugins/java/okhttp/okhttp.d.ts +1 -3
- package/dist/plugins/java/okhttp/okhttp.d.ts.map +1 -1
- package/dist/plugins/java/okhttp/okhttp.js +50 -8
- package/dist/plugins/java/unirest/unirest.d.ts +1 -3
- package/dist/plugins/java/unirest/unirest.d.ts.map +1 -1
- package/dist/plugins/java/unirest/unirest.js +13 -8
- package/dist/plugins/js/jquery/jquery.d.ts +1 -3
- package/dist/plugins/js/jquery/jquery.d.ts.map +1 -1
- package/dist/plugins/js/jquery/jquery.js +22 -8
- package/dist/plugins/js/xhr/xhr.d.ts +1 -3
- package/dist/plugins/js/xhr/xhr.d.ts.map +1 -1
- package/dist/plugins/js/xhr/xhr.js +13 -8
- package/dist/plugins/kotlin/okhttp/okhttp.d.ts.map +1 -1
- package/dist/plugins/kotlin/okhttp/okhttp.js +15 -9
- package/dist/plugins/ocaml/cohttp/cohttp.d.ts +1 -3
- package/dist/plugins/ocaml/cohttp/cohttp.d.ts.map +1 -1
- package/dist/plugins/ocaml/cohttp/cohttp.js +32 -8
- package/dist/plugins/php/laravel/laravel.d.ts.map +1 -1
- package/dist/plugins/php/laravel/laravel.js +2 -1
- package/dist/plugins/powershell/restmethod/restmethod.d.ts +1 -3
- package/dist/plugins/powershell/restmethod/restmethod.d.ts.map +1 -1
- package/dist/plugins/powershell/restmethod/restmethod.js +4 -8
- package/dist/plugins/powershell/webrequest/webrequest.d.ts +1 -3
- package/dist/plugins/powershell/webrequest/webrequest.d.ts.map +1 -1
- package/dist/plugins/powershell/webrequest/webrequest.js +4 -8
- package/dist/plugins/python/requestsLike.d.ts.map +1 -1
- package/dist/plugins/r/httr2/httr2.d.ts.map +1 -1
- package/dist/plugins/r/httr2/httr2.js +2 -1
- package/dist/plugins/shared/axios.d.ts.map +1 -1
- package/dist/plugins/shell/curl/curl.d.ts.map +1 -1
- package/dist/plugins/shell/curl/curl.js +12 -1
- package/dist/plugins/shell/httpie/httpie.d.ts +1 -3
- package/dist/plugins/shell/httpie/httpie.d.ts.map +1 -1
- package/dist/plugins/shell/httpie/httpie.js +34 -8
- package/package.json +4 -11
- package/dist/httpsnippet-lite/helpers/code-builder.d.ts +0 -51
- package/dist/httpsnippet-lite/helpers/code-builder.d.ts.map +0 -1
- package/dist/httpsnippet-lite/helpers/code-builder.js +0 -87
- package/dist/httpsnippet-lite/helpers/escape.d.ts +0 -48
- package/dist/httpsnippet-lite/helpers/escape.d.ts.map +0 -1
- package/dist/httpsnippet-lite/helpers/escape.js +0 -70
- package/dist/httpsnippet-lite/helpers/headers.d.ts +0 -15
- package/dist/httpsnippet-lite/helpers/headers.d.ts.map +0 -1
- package/dist/httpsnippet-lite/helpers/headers.js +0 -18
- package/dist/httpsnippet-lite/helpers/reducer.d.ts +0 -2
- package/dist/httpsnippet-lite/helpers/reducer.d.ts.map +0 -1
- package/dist/httpsnippet-lite/helpers/reducer.js +0 -1
- package/dist/httpsnippet-lite/helpers/shell.d.ts +0 -6
- package/dist/httpsnippet-lite/helpers/shell.d.ts.map +0 -1
- package/dist/httpsnippet-lite/helpers/shell.js +0 -13
- package/dist/httpsnippet-lite/helpers/snippet-request.d.ts +0 -33
- package/dist/httpsnippet-lite/helpers/snippet-request.d.ts.map +0 -1
- package/dist/httpsnippet-lite/helpers/snippet-request.js +0 -1
- package/dist/httpsnippet-lite/helpers/url.d.ts +0 -4
- package/dist/httpsnippet-lite/helpers/url.d.ts.map +0 -1
- package/dist/httpsnippet-lite/helpers/url.js +0 -5
- package/dist/httpsnippet-lite/targets/java/asynchttp/client.d.ts +0 -12
- package/dist/httpsnippet-lite/targets/java/asynchttp/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/java/asynchttp/client.js +0 -33
- package/dist/httpsnippet-lite/targets/java/nethttp/client.d.ts +0 -3
- package/dist/httpsnippet-lite/targets/java/nethttp/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/java/nethttp/client.js +0 -41
- package/dist/httpsnippet-lite/targets/java/okhttp/client.d.ts +0 -12
- package/dist/httpsnippet-lite/targets/java/okhttp/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/java/okhttp/client.js +0 -87
- package/dist/httpsnippet-lite/targets/java/unirest/client.d.ts +0 -12
- package/dist/httpsnippet-lite/targets/java/unirest/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/java/unirest/client.js +0 -42
- package/dist/httpsnippet-lite/targets/javascript/jquery/client.d.ts +0 -3
- package/dist/httpsnippet-lite/targets/javascript/jquery/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/javascript/jquery/client.js +0 -80
- package/dist/httpsnippet-lite/targets/javascript/xhr/client.d.ts +0 -3
- package/dist/httpsnippet-lite/targets/javascript/xhr/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/javascript/xhr/client.js +0 -78
- package/dist/httpsnippet-lite/targets/ocaml/cohttp/client.d.ts +0 -3
- package/dist/httpsnippet-lite/targets/ocaml/cohttp/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/ocaml/cohttp/client.js +0 -61
- package/dist/httpsnippet-lite/targets/powershell/common.d.ts +0 -3
- package/dist/httpsnippet-lite/targets/powershell/common.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/powershell/common.js +0 -48
- package/dist/httpsnippet-lite/targets/powershell/restmethod/client.d.ts +0 -3
- package/dist/httpsnippet-lite/targets/powershell/restmethod/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/powershell/restmethod/client.js +0 -10
- package/dist/httpsnippet-lite/targets/powershell/webrequest/client.d.ts +0 -3
- package/dist/httpsnippet-lite/targets/powershell/webrequest/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/powershell/webrequest/client.js +0 -10
- package/dist/httpsnippet-lite/targets/shell/httpie/client.d.ts +0 -12
- package/dist/httpsnippet-lite/targets/shell/httpie/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/shell/httpie/client.js +0 -124
- package/dist/httpsnippet-lite/targets/target.d.ts +0 -19
- package/dist/httpsnippet-lite/targets/target.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/target.js +0 -1
- package/dist/utils/convertWithHttpSnippetLite.d.ts +0 -8
- package/dist/utils/convertWithHttpSnippetLite.d.ts.map +0 -1
- package/dist/utils/convertWithHttpSnippetLite.js +0 -102
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/index.js +0 -1
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { HarRequest, PluginConfiguration } from '@scalar/types/snippetz';
|
|
2
|
+
/** Builds browser request values without collapsing repeated headers or form fields. */
|
|
3
|
+
export declare const prepareBrowserRequest: (request?: Partial<HarRequest>, configuration?: PluginConfiguration) => {
|
|
4
|
+
url: string;
|
|
5
|
+
method: string;
|
|
6
|
+
headers: {
|
|
7
|
+
name: string;
|
|
8
|
+
value: string;
|
|
9
|
+
}[];
|
|
10
|
+
setup: string[];
|
|
11
|
+
body: string;
|
|
12
|
+
withCredentials: boolean;
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=browser-request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-request.d.ts","sourceRoot":"","sources":["../../src/libs/browser-request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AAK7E,wFAAwF;AACxF,eAAO,MAAM,qBAAqB,GAChC,UAAS,OAAO,CAAC,UAAU,CAAM,EACjC,gBAAgB,mBAAmB,KAClC;IACD,GAAG,EAAE,MAAM,CAAA;IACX,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,EAAE,CAAA;IAC1C,KAAK,EAAE,MAAM,EAAE,CAAA;IACf,IAAI,EAAE,MAAM,CAAA;IACZ,eAAe,EAAE,OAAO,CAAA;CA8DzB,CAAA"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Base64 } from 'js-base64';
|
|
2
|
+
import { joinUrlAndQuery, normalizeMethod } from './http.js';
|
|
3
|
+
/** Builds browser request values without collapsing repeated headers or form fields. */
|
|
4
|
+
export const prepareBrowserRequest = (request = {}, configuration) => {
|
|
5
|
+
const headers = [...(request.headers ?? [])];
|
|
6
|
+
if (configuration?.auth?.username && configuration.auth.password) {
|
|
7
|
+
headers.push({
|
|
8
|
+
name: 'Authorization',
|
|
9
|
+
value: `Basic ${Base64.encode(`${configuration.auth.username}:${configuration.auth.password}`)}`,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
const setup = [];
|
|
13
|
+
if (request.cookies?.length) {
|
|
14
|
+
setup.push('// Run on the request origin to set these cookies in the browser.');
|
|
15
|
+
for (const { name, value } of request.cookies) {
|
|
16
|
+
setup.push(`document.cookie = ${JSON.stringify(`${encodeURIComponent(name)}=${encodeURIComponent(value)}; path=/`)};`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const postData = request.postData;
|
|
20
|
+
const multipart = postData?.mimeType === 'multipart/form-data' && postData.params;
|
|
21
|
+
const form = postData?.mimeType === 'application/x-www-form-urlencoded' && postData.params;
|
|
22
|
+
if (multipart) {
|
|
23
|
+
setup.push('const body = new FormData();');
|
|
24
|
+
const uploads = multipart.filter((param) => param.fileName !== undefined && param.value === undefined);
|
|
25
|
+
if (uploads.length) {
|
|
26
|
+
setup.push('// Select upload files with an <input type="file" multiple> element first.');
|
|
27
|
+
setup.push(`const files = document.querySelector('input[type="file"]').files;`);
|
|
28
|
+
}
|
|
29
|
+
for (const param of multipart) {
|
|
30
|
+
const name = JSON.stringify(param.name);
|
|
31
|
+
const value = JSON.stringify(param.value ?? '');
|
|
32
|
+
if (param.fileName !== undefined) {
|
|
33
|
+
const contents = param.value === undefined ? `files[${uploads.indexOf(param)}]` : value;
|
|
34
|
+
setup.push(`body.append(${name}, new File([${contents}], ${JSON.stringify(param.fileName)}, { type: ${JSON.stringify(param.contentType ?? 'application/octet-stream')} }));`);
|
|
35
|
+
}
|
|
36
|
+
else if (param.contentType) {
|
|
37
|
+
setup.push(`body.append(${name}, new Blob([${value}], { type: ${JSON.stringify(param.contentType)} }));`);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
setup.push(`body.append(${name}, ${value});`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
else if (form) {
|
|
45
|
+
setup.push('const body = new URLSearchParams();');
|
|
46
|
+
for (const param of form) {
|
|
47
|
+
setup.push(`body.append(${JSON.stringify(param.name)}, ${JSON.stringify(param.value ?? '')});`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (postData?.mimeType && !multipart && !headers.some(({ name }) => name.toLowerCase() === 'content-type')) {
|
|
51
|
+
headers.push({ name: 'Content-Type', value: postData.mimeType });
|
|
52
|
+
}
|
|
53
|
+
const textBody = postData ? JSON.stringify(postData.text ?? '') : 'null';
|
|
54
|
+
const formBody = form ? 'body.toString()' : textBody;
|
|
55
|
+
return {
|
|
56
|
+
url: joinUrlAndQuery(request.url ?? '', request.queryString),
|
|
57
|
+
method: normalizeMethod(request.method),
|
|
58
|
+
// The browser must supply the boundary matching its FormData serialization.
|
|
59
|
+
headers: multipart ? headers.filter(({ name }) => name.toLowerCase() !== 'content-type') : headers,
|
|
60
|
+
setup,
|
|
61
|
+
withCredentials: Boolean(request.cookies?.length),
|
|
62
|
+
body: multipart ? 'body' : formBody,
|
|
63
|
+
};
|
|
64
|
+
};
|
package/dist/libs/http.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/libs/http.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AAExD,KAAK,UAAU,GAAG;IAChB,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;CACd,CAAA;AAED,KAAK,aAAa,GAAG;IACnB,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;CACd,CAAA;AAED,KAAK,qBAAqB,GAAG;IAC3B,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,CAAC,EAAE,MAAM,CAAA;CACf,CAAA;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,UAAU,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CAKvG;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,WAAW,CAAC,EAAE,KAAK,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC,GAAG,MAAM,CAO7F;AAED;;GAEG;AACH,eAAO,MAAM,eAAe,GAAI,SAAS,MAAM,KAAG,MAAyC,CAAA;AAE3F;;GAEG;AACH,eAAO,MAAM,YAAY,GAAI,KAAK,MAAM,KAAG,MAgB1C,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,eAAe,GAAI,KAAK,MAAM,EAAE,cAAc,aAAa,EAAE,KAAG,MAY5E,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,UAAU,qBAAqB,EAAE,EAAE,UAAU,aAAa,EAAE,KAAG,UAAU,EAiBvG,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,uBAAuB,GAAI,MAAM,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,MAAM,EAAE,OAAO,MAAM,KAAG,IAU9G,CAAA;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,GAAE,UAAU,CAAC,aAAa,CAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/libs/http.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AAExD,KAAK,UAAU,GAAG;IAChB,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;CACd,CAAA;AAED,KAAK,aAAa,GAAG;IACnB,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;CACd,CAAA;AAED,KAAK,qBAAqB,GAAG;IAC3B,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,CAAC,EAAE,MAAM,CAAA;CACf,CAAA;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,UAAU,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CAKvG;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,WAAW,CAAC,EAAE,KAAK,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC,GAAG,MAAM,CAO7F;AAED;;GAEG;AACH,eAAO,MAAM,eAAe,GAAI,SAAS,MAAM,KAAG,MAAyC,CAAA;AAE3F;;GAEG;AACH,eAAO,MAAM,YAAY,GAAI,KAAK,MAAM,KAAG,MAgB1C,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,eAAe,GAAI,KAAK,MAAM,EAAE,cAAc,aAAa,EAAE,KAAG,MAY5E,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,UAAU,qBAAqB,EAAE,EAAE,UAAU,aAAa,EAAE,KAAG,UAAU,EAiBvG,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,uBAAuB,GAAI,MAAM,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,MAAM,EAAE,OAAO,MAAM,KAAG,IAU9G,CAAA;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,GAAE,UAAU,CAAC,aAAa,CAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,CAK1G;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,CAErE;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,CAAC,UAAU,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAqBnF"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { BodySegment } from './prepare-request.js';
|
|
2
|
+
/** Java literals need octal escapes for controls that Java processes before tokenization. */
|
|
3
|
+
export declare const quoteJava: (value: string) => string;
|
|
4
|
+
/** Build a byte array while preserving binary file contents in multipart requests. */
|
|
5
|
+
export declare const javaBody: (body: BodySegment[] | undefined) => string[];
|
|
6
|
+
//# sourceMappingURL=java.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"java.d.ts","sourceRoot":"","sources":["../../src/libs/java.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAEpD,6FAA6F;AAC7F,eAAO,MAAM,SAAS,GAAI,OAAO,MAAM,KAAG,MAO1B,CAAA;AAEhB,sFAAsF;AACtF,eAAO,MAAM,QAAQ,GAAI,MAAM,WAAW,EAAE,GAAG,SAAS,KAAG,MAAM,EAahE,CAAA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Java literals need octal escapes for controls that Java processes before tokenization. */
|
|
2
|
+
export const quoteJava = (value) => `"${Array.from(value, (character) => {
|
|
3
|
+
const code = character.charCodeAt(0);
|
|
4
|
+
if (code < 32 && !['\n', '\r', '\t', '\b', '\f'].includes(character)) {
|
|
5
|
+
return `\\${code.toString(8).padStart(3, '0')}`;
|
|
6
|
+
}
|
|
7
|
+
return JSON.stringify(character).slice(1, -1);
|
|
8
|
+
}).join('')}"`;
|
|
9
|
+
/** Build a byte array while preserving binary file contents in multipart requests. */
|
|
10
|
+
export const javaBody = (body) => {
|
|
11
|
+
if (!body) {
|
|
12
|
+
return [];
|
|
13
|
+
}
|
|
14
|
+
return [
|
|
15
|
+
'java.io.ByteArrayOutputStream body = new java.io.ByteArrayOutputStream();',
|
|
16
|
+
...body.map((segment) => 'file' in segment
|
|
17
|
+
? `body.write(java.nio.file.Files.readAllBytes(java.nio.file.Path.of(${quoteJava(segment.file)})));`
|
|
18
|
+
: `body.write(${quoteJava(segment.text)}.getBytes(java.nio.charset.StandardCharsets.UTF_8));`),
|
|
19
|
+
'',
|
|
20
|
+
];
|
|
21
|
+
};
|
package/dist/libs/julia.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"julia.d.ts","sourceRoot":"","sources":["../../src/libs/julia.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"julia.d.ts","sourceRoot":"","sources":["../../src/libs/julia.ts"],"names":[],"mappings":"AAiBA;;GAEG;AACH,eAAO,MAAM,kBAAkB,GAAI,KAAK,MAAM,KAAG,MAAkC,CAAA;AAEnF;;GAEG;AACH,eAAO,MAAM,MAAM,GAAI,OAAO,MAAM,EAAE,MAAM,MAAM,KAAG,MAA2C,CAAA;AAEhG;;;;;;;;;;GAUG;AACH,eAAO,MAAM,gBAAgB,GAAI,SAAS,MAAM,EAAE,EAAE,MAAM,MAAM,EAAE,OAAO,MAAM,EAAE,OAAO,MAAM,KAAG,MAchG,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,gBAAgB,GAAI,OAAO;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,EAAE,EAAE,OAAO,MAAM,KAAG,MAMxF,CAAA;AAEH;;GAEG;AACH,eAAO,MAAM,UAAU,GAAI,OAAO;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,EAAE,EAAE,OAAO,MAAM,KAAG,MAMlF,CAAA;AAEH;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,OAAO,OAAO,EAAE,OAAO,MAAM,KAAG,MAoC3D,CAAA"}
|
package/dist/libs/julia.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isObjectLike } from '@scalar/helpers/object/is-object';
|
|
1
2
|
/**
|
|
2
3
|
* Escapes a string for use in a Julia double-quoted string literal.
|
|
3
4
|
*
|
|
@@ -72,7 +73,7 @@ export const formatValue = (value, level) => {
|
|
|
72
73
|
if (Array.isArray(value)) {
|
|
73
74
|
return formatCollection(value.map((item) => formatValue(item, level + 1)), '[', ']', level);
|
|
74
75
|
}
|
|
75
|
-
if (
|
|
76
|
+
if (isObjectLike(value)) {
|
|
76
77
|
return formatCollection(Object.entries(value).map(([key, item]) => `${wrapInDoubleQuotes(key)} => ${formatValue(item, level + 1)}`), 'Dict(', ')', level);
|
|
77
78
|
}
|
|
78
79
|
return wrapInDoubleQuotes(String(value));
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { HarRequest, PluginConfiguration } from '@scalar/types/snippetz';
|
|
2
|
+
/** Generate the common request arguments for PowerShell 7 HTTP cmdlets. */
|
|
3
|
+
export declare const generatePowershell: (command: "Invoke-RestMethod" | "Invoke-WebRequest", request?: Partial<HarRequest>, configuration?: PluginConfiguration) => string;
|
|
4
|
+
//# sourceMappingURL=powershell.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"powershell.d.ts","sourceRoot":"","sources":["../../src/libs/powershell.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AAO7E,2EAA2E;AAC3E,eAAO,MAAM,kBAAkB,GAC7B,SAAS,mBAAmB,GAAG,mBAAmB,EAClD,UAAS,OAAO,CAAC,UAAU,CAAM,EACjC,gBAAgB,mBAAmB,KAClC,MAqCF,CAAA"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { prepareRequest } from './prepare-request.js';
|
|
2
|
+
/** Double every PowerShell single-quote delimiter, including smart apostrophes, to keep values literal. */
|
|
3
|
+
const quote = (value) => `'${value.replace(/['\u2018-\u201b]/g, (quote) => quote + quote)}'`;
|
|
4
|
+
/** Generate the common request arguments for PowerShell 7 HTTP cmdlets. */
|
|
5
|
+
export const generatePowershell = (command, request = {}, configuration) => {
|
|
6
|
+
const { url, method, headers, body } = prepareRequest(request, configuration);
|
|
7
|
+
const lines = [];
|
|
8
|
+
const headerNames = [...new Set(headers.map(({ name }) => name.toLowerCase()))];
|
|
9
|
+
if (headers.length) {
|
|
10
|
+
lines.push('$headers = @{');
|
|
11
|
+
for (const name of headerNames) {
|
|
12
|
+
const matches = headers.filter((header) => header.name.toLowerCase() === name);
|
|
13
|
+
// HTTP cmdlets stringify header values; arrays would be sent as System.Object[].
|
|
14
|
+
const value = matches.map((header) => header.value).join(name === 'cookie' ? '; ' : ', ');
|
|
15
|
+
lines.push(` ${quote(matches[0]?.name ?? name)} = ${quote(value)}`);
|
|
16
|
+
}
|
|
17
|
+
lines.push('}', '');
|
|
18
|
+
}
|
|
19
|
+
if (body) {
|
|
20
|
+
lines.push('$body = [System.IO.MemoryStream]::new()');
|
|
21
|
+
for (const segment of body) {
|
|
22
|
+
lines.push('file' in segment
|
|
23
|
+
? `$bytes = [System.IO.File]::ReadAllBytes(${quote(segment.file)})`
|
|
24
|
+
: `$bytes = [System.Text.Encoding]::UTF8.GetBytes(${quote(segment.text)})`);
|
|
25
|
+
lines.push('$body.Write($bytes, 0, $bytes.Length)');
|
|
26
|
+
}
|
|
27
|
+
lines.push('');
|
|
28
|
+
}
|
|
29
|
+
const methodOption = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'TRACE', 'OPTIONS', 'MERGE', 'PATCH'].includes(method)
|
|
30
|
+
? '-Method'
|
|
31
|
+
: '-CustomMethod';
|
|
32
|
+
lines.push(`$response = ${command} -Uri ${quote(url)} ${methodOption} ${quote(method)}${headers.length ? ' -Headers $headers' : ''}${body ? ' -Body $body.ToArray()' : ''}`);
|
|
33
|
+
if (body) {
|
|
34
|
+
lines.push('$body.Dispose()');
|
|
35
|
+
}
|
|
36
|
+
lines.push('$response');
|
|
37
|
+
return lines.join('\n');
|
|
38
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { HarRequest, PluginConfiguration } from '@scalar/types/snippetz';
|
|
2
|
+
/** A body segment is either literal UTF-8 text or a file read when the snippet runs. */
|
|
3
|
+
export type BodySegment = {
|
|
4
|
+
text: string;
|
|
5
|
+
} | {
|
|
6
|
+
file: string;
|
|
7
|
+
};
|
|
8
|
+
/** Request values shared by generators that serialize a body explicitly. */
|
|
9
|
+
type PreparedRequest = {
|
|
10
|
+
url: string;
|
|
11
|
+
method: string;
|
|
12
|
+
headers: {
|
|
13
|
+
name: string;
|
|
14
|
+
value: string;
|
|
15
|
+
}[];
|
|
16
|
+
body: BodySegment[] | undefined;
|
|
17
|
+
};
|
|
18
|
+
/** Normalize HAR values without dropping duplicate headers, cookies, query parameters, or empty bodies. */
|
|
19
|
+
export declare const prepareRequest: (request?: Partial<HarRequest>, configuration?: PluginConfiguration) => PreparedRequest;
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=prepare-request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prepare-request.d.ts","sourceRoot":"","sources":["../../src/libs/prepare-request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AAK7E,wFAAwF;AACxF,MAAM,MAAM,WAAW,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAA;AAE7D,4EAA4E;AAC5E,KAAK,eAAe,GAAG;IACrB,GAAG,EAAE,MAAM,CAAA;IACX,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,EAAE,CAAA;IAC1C,IAAI,EAAE,WAAW,EAAE,GAAG,SAAS,CAAA;CAChC,CAAA;AAiCD,2GAA2G;AAC3G,eAAO,MAAM,cAAc,GACzB,UAAS,OAAO,CAAC,UAAU,CAAM,EACjC,gBAAgB,mBAAmB,KAClC,eAkCF,CAAA"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Base64 } from 'js-base64';
|
|
2
|
+
import { collectHeaders, joinUrlAndQuery, normalizeMethod, normalizeUrl } from './http.js';
|
|
3
|
+
/** Prevent names and filenames from terminating a multipart disposition parameter. */
|
|
4
|
+
const dispositionValue = (value) => value.replaceAll('\r', '%0D').replaceAll('\n', '%0A').replaceAll('"', '%22');
|
|
5
|
+
/** Preserve repeated fields, file uploads, and part media types in a multipart body. */
|
|
6
|
+
const multipartBody = (params) => {
|
|
7
|
+
// Keep snippets deterministic while avoiding boundaries present in the supplied values.
|
|
8
|
+
const values = JSON.stringify(params);
|
|
9
|
+
let boundary = 'scalar-boundary';
|
|
10
|
+
while (values.includes(boundary)) {
|
|
11
|
+
boundary += '-';
|
|
12
|
+
}
|
|
13
|
+
const body = [];
|
|
14
|
+
for (const param of params) {
|
|
15
|
+
const filename = param.fileName !== undefined ? `; filename="${dispositionValue(param.fileName)}"` : '';
|
|
16
|
+
const contentType = param.contentType ?? (param.fileName !== undefined ? 'application/octet-stream' : undefined);
|
|
17
|
+
body.push({
|
|
18
|
+
text: `--${boundary}\r\nContent-Disposition: form-data; name="${dispositionValue(param.name)}"${filename}\r\n${contentType ? `Content-Type: ${contentType}\r\n` : ''}\r\n`,
|
|
19
|
+
});
|
|
20
|
+
body.push(param.fileName !== undefined ? { file: param.fileName } : { text: param.value ?? '' });
|
|
21
|
+
body.push({ text: '\r\n' });
|
|
22
|
+
}
|
|
23
|
+
body.push({ text: `--${boundary}--\r\n` });
|
|
24
|
+
return { contentType: `multipart/form-data; boundary=${boundary}`, body };
|
|
25
|
+
};
|
|
26
|
+
/** Normalize HAR values without dropping duplicate headers, cookies, query parameters, or empty bodies. */
|
|
27
|
+
export const prepareRequest = (request = {}, configuration) => {
|
|
28
|
+
const headers = [...(request.headers ?? []), ...collectHeaders([], request.cookies)];
|
|
29
|
+
if (configuration?.auth?.username && configuration.auth.password) {
|
|
30
|
+
headers.push({
|
|
31
|
+
name: 'Authorization',
|
|
32
|
+
value: `Basic ${Base64.encode(`${configuration.auth.username}:${configuration.auth.password}`)}`,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
const postData = request.postData;
|
|
36
|
+
const multipart = postData?.mimeType === 'multipart/form-data' && postData.params ? multipartBody(postData.params) : undefined;
|
|
37
|
+
const body = multipart?.body ??
|
|
38
|
+
(postData
|
|
39
|
+
? [
|
|
40
|
+
{
|
|
41
|
+
text: postData.mimeType === 'application/x-www-form-urlencoded' && postData.params
|
|
42
|
+
? new URLSearchParams(postData.params.map(({ name, value }) => [name, value ?? ''])).toString()
|
|
43
|
+
: (postData.text ?? ''),
|
|
44
|
+
},
|
|
45
|
+
]
|
|
46
|
+
: undefined);
|
|
47
|
+
const contentType = multipart?.contentType ?? postData?.mimeType;
|
|
48
|
+
const bodyHeaders = multipart ? headers.filter(({ name }) => name.toLowerCase() !== 'content-type') : headers;
|
|
49
|
+
if (contentType && !bodyHeaders.some(({ name }) => name.toLowerCase() === 'content-type')) {
|
|
50
|
+
bodyHeaders.push({ name: 'Content-Type', value: contentType });
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
url: normalizeUrl(joinUrlAndQuery(request.url ?? '', request.queryString)),
|
|
54
|
+
method: normalizeMethod(request.method),
|
|
55
|
+
headers: bodyHeaders,
|
|
56
|
+
body,
|
|
57
|
+
};
|
|
58
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clj_http.d.ts","sourceRoot":"","sources":["../../../../src/plugins/clojure/clj_http/clj_http.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"clj_http.d.ts","sourceRoot":"","sources":["../../../../src/plugins/clojure/clj_http/clj_http.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAsGpD;;GAEG;AACH,eAAO,MAAM,cAAc,EAAE,MA2H5B,CAAA"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isObjectLike } from '@scalar/helpers/object/is-object';
|
|
1
2
|
import { normalizeMethod, reduceQueryParams } from '../../../libs/http.js';
|
|
2
3
|
/**
|
|
3
4
|
* Escapes a string so it stays a valid EDN string literal. Backslashes are
|
|
@@ -68,7 +69,7 @@ const jsToEdn = (value) => {
|
|
|
68
69
|
const body = value.reduce((accumulator, item) => `${accumulator} ${jsToEdn(item)}`, '').trim();
|
|
69
70
|
return `[${padBlock(1, body)}]`;
|
|
70
71
|
}
|
|
71
|
-
if (
|
|
72
|
+
if (isObjectLike(value)) {
|
|
72
73
|
// Simple vertical format, one key per line.
|
|
73
74
|
const body = Object.keys(value)
|
|
74
75
|
.reduce((accumulator, key) => {
|
|
@@ -18,7 +18,7 @@ const isJsonContentType = (value) => {
|
|
|
18
18
|
* PascalCase members (`Method.Get`, `Method.Post`, ...), so we title-case the
|
|
19
19
|
* method name to cover both the well-known verbs and any custom ones.
|
|
20
20
|
*/
|
|
21
|
-
const
|
|
21
|
+
const getRestSharpMethod = (method) => {
|
|
22
22
|
const titleCased = method.charAt(0).toUpperCase() + method.slice(1).toLowerCase();
|
|
23
23
|
return `Method.${titleCased}`;
|
|
24
24
|
};
|
|
@@ -72,7 +72,7 @@ export const csharpRestsharp = {
|
|
|
72
72
|
const lines = [];
|
|
73
73
|
// Client and request
|
|
74
74
|
lines.push(`var client = new RestClient("${escapeCSharpString(url)}");`);
|
|
75
|
-
lines.push(`var request = new RestRequest("", ${
|
|
75
|
+
lines.push(`var request = new RestRequest("", ${getRestSharpMethod(normalizedRequest.method)});`);
|
|
76
76
|
// Basic Auth (added as an Authorization header so the client stays request-scoped)
|
|
77
77
|
const { username, password } = configuration?.auth ?? {};
|
|
78
78
|
const hasBasicAuth = Boolean(username && password);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../../../src/plugins/dart/http/http.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAEpD;;GAEG;AACH,eAAO,MAAM,QAAQ,EAAE,
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../../../src/plugins/dart/http/http.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAEpD;;GAEG;AACH,eAAO,MAAM,QAAQ,EAAE,MAsGtB,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asynchttp.d.ts","sourceRoot":"","sources":["../../../../src/plugins/java/asynchttp/asynchttp.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD
|
|
1
|
+
{"version":3,"file":"asynchttp.d.ts","sourceRoot":"","sources":["../../../../src/plugins/java/asynchttp/asynchttp.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD,wFAAwF;AACxF,eAAO,MAAM,aAAa,EAAE,MAkB3B,CAAA"}
|
|
@@ -1,14 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
/**
|
|
4
|
-
* java/asynchttp
|
|
5
|
-
*/
|
|
1
|
+
import { javaBody, quoteJava } from '../../../libs/java.js';
|
|
2
|
+
import { prepareRequest } from '../../../libs/prepare-request.js';
|
|
3
|
+
/** Generates a request using AsyncHttpClient and closes the client after completion. */
|
|
6
4
|
export const javaAsynchttp = {
|
|
7
5
|
target: 'java',
|
|
8
6
|
client: 'asynchttp',
|
|
9
7
|
title: 'AsyncHttp',
|
|
10
|
-
generate(request) {
|
|
11
|
-
|
|
12
|
-
return
|
|
8
|
+
generate(request, configuration) {
|
|
9
|
+
const { url, method, headers, body } = prepareRequest(request, configuration);
|
|
10
|
+
return [
|
|
11
|
+
...javaBody(body),
|
|
12
|
+
'try (AsyncHttpClient client = new DefaultAsyncHttpClient()) {',
|
|
13
|
+
` Response response = client.prepare(${quoteJava(method)}, ${quoteJava(url)})`,
|
|
14
|
+
...headers.map(({ name, value }) => ` .addHeader(${quoteJava(name)}, ${quoteJava(value)})`),
|
|
15
|
+
...(body ? [' .setBody(body.toByteArray())'] : []),
|
|
16
|
+
' .execute()',
|
|
17
|
+
' .get();',
|
|
18
|
+
' System.out.println(response.getResponseBody());',
|
|
19
|
+
'}',
|
|
20
|
+
].join('\n');
|
|
13
21
|
},
|
|
14
22
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nethttp.d.ts","sourceRoot":"","sources":["../../../../src/plugins/java/nethttp/nethttp.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD
|
|
1
|
+
{"version":3,"file":"nethttp.d.ts","sourceRoot":"","sources":["../../../../src/plugins/java/nethttp/nethttp.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD,qDAAqD;AACrD,eAAO,MAAM,WAAW,EAAE,MAmBzB,CAAA"}
|
|
@@ -1,14 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
/**
|
|
4
|
-
* java/nethttp
|
|
5
|
-
*/
|
|
1
|
+
import { javaBody, quoteJava } from '../../../libs/java.js';
|
|
2
|
+
import { prepareRequest } from '../../../libs/prepare-request.js';
|
|
3
|
+
/** Generates a request using the JDK HTTP client. */
|
|
6
4
|
export const javaNethttp = {
|
|
7
5
|
target: 'java',
|
|
8
6
|
client: 'nethttp',
|
|
9
7
|
title: 'java.net.http',
|
|
10
|
-
generate(request) {
|
|
11
|
-
|
|
12
|
-
return
|
|
8
|
+
generate(request, configuration) {
|
|
9
|
+
const { url, method, headers, body } = prepareRequest(request, configuration);
|
|
10
|
+
return [
|
|
11
|
+
...javaBody(body),
|
|
12
|
+
'HttpClient client = HttpClient.newHttpClient();',
|
|
13
|
+
'HttpRequest request = HttpRequest.newBuilder()',
|
|
14
|
+
` .uri(java.net.URI.create(${quoteJava(url)}))`,
|
|
15
|
+
...headers.map(({ name, value }) => ` .header(${quoteJava(name)}, ${quoteJava(value)})`),
|
|
16
|
+
` .method(${quoteJava(method)}, HttpRequest.BodyPublishers.${body ? 'ofByteArray(body.toByteArray())' : 'noBody()'})`,
|
|
17
|
+
' .build();',
|
|
18
|
+
'',
|
|
19
|
+
'HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());',
|
|
20
|
+
'System.out.println(response.body());',
|
|
21
|
+
].join('\n');
|
|
13
22
|
},
|
|
14
23
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"okhttp.d.ts","sourceRoot":"","sources":["../../../../src/plugins/java/okhttp/okhttp.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD
|
|
1
|
+
{"version":3,"file":"okhttp.d.ts","sourceRoot":"","sources":["../../../../src/plugins/java/okhttp/okhttp.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD,2EAA2E;AAC3E,eAAO,MAAM,UAAU,EAAE,MAiExB,CAAA"}
|
|
@@ -1,14 +1,56 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
/**
|
|
4
|
-
* java/okhttp
|
|
5
|
-
*/
|
|
1
|
+
import { quoteJava } from '../../../libs/java.js';
|
|
2
|
+
import { prepareRequest } from '../../../libs/prepare-request.js';
|
|
3
|
+
/** Generates an OkHttp request with native form and multipart builders. */
|
|
6
4
|
export const javaOkhttp = {
|
|
7
5
|
target: 'java',
|
|
8
6
|
client: 'okhttp',
|
|
9
7
|
title: 'OkHttp',
|
|
10
|
-
generate(request) {
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
generate(request, configuration) {
|
|
9
|
+
const { url, method, headers } = prepareRequest(request, configuration);
|
|
10
|
+
// OkHttp rejects every non-null body for GET and HEAD, including an empty one.
|
|
11
|
+
const postData = ['GET', 'HEAD'].includes(method) ? undefined : request?.postData;
|
|
12
|
+
const multipart = postData?.mimeType === 'multipart/form-data' && postData.params;
|
|
13
|
+
const form = postData?.mimeType === 'application/x-www-form-urlencoded' && postData.params;
|
|
14
|
+
const lines = ['OkHttpClient client = new OkHttpClient();', ''];
|
|
15
|
+
if (multipart) {
|
|
16
|
+
lines.push('RequestBody body = new MultipartBody.Builder()', ' .setType(MultipartBody.FORM)');
|
|
17
|
+
for (const param of multipart) {
|
|
18
|
+
const name = quoteJava(param.name);
|
|
19
|
+
const value = quoteJava(param.value ?? '');
|
|
20
|
+
if (param.fileName !== undefined) {
|
|
21
|
+
lines.push(` .addFormDataPart(${name}, ${quoteJava(param.fileName)}, RequestBody.create(MediaType.parse(${quoteJava(param.contentType ?? 'application/octet-stream')}), new java.io.File(${quoteJava(param.fileName)})))`);
|
|
22
|
+
}
|
|
23
|
+
else if (param.contentType) {
|
|
24
|
+
lines.push(` .addFormDataPart(${name}, null, RequestBody.create(MediaType.parse(${quoteJava(param.contentType)}), ${value}))`);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
lines.push(` .addFormDataPart(${name}, ${value})`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
lines.push(' .build();', '');
|
|
31
|
+
}
|
|
32
|
+
else if (form) {
|
|
33
|
+
lines.push('RequestBody body = new FormBody.Builder()');
|
|
34
|
+
for (const param of form) {
|
|
35
|
+
lines.push(` .add(${quoteJava(param.name)}, ${quoteJava(param.value ?? '')})`);
|
|
36
|
+
}
|
|
37
|
+
lines.push(' .build();', '');
|
|
38
|
+
}
|
|
39
|
+
else if (postData) {
|
|
40
|
+
lines.push(`RequestBody body = RequestBody.create(${postData.mimeType ? `MediaType.parse(${quoteJava(postData.mimeType)})` : 'null'}, ${quoteJava(postData.text ?? '')});`, '');
|
|
41
|
+
}
|
|
42
|
+
const emptyBody = ['POST', 'PUT', 'PATCH', 'PROPPATCH', 'REPORT'].includes(method)
|
|
43
|
+
? 'RequestBody.create(null, "")'
|
|
44
|
+
: 'null';
|
|
45
|
+
const body = postData ? 'body' : emptyBody;
|
|
46
|
+
lines.push('Request request = new Request.Builder()', ` .url(${quoteJava(url)})`, ` .method(${quoteJava(method)}, ${body})`);
|
|
47
|
+
for (const { name, value } of headers) {
|
|
48
|
+
// OkHttp sets the multipart boundary when it writes the body.
|
|
49
|
+
if (!multipart || name.toLowerCase() !== 'content-type') {
|
|
50
|
+
lines.push(` .addHeader(${quoteJava(name)}, ${quoteJava(value)})`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
lines.push(' .build();', '', 'try (Response response = client.newCall(request).execute()) {', ' System.out.println(response.body().string());', '}');
|
|
54
|
+
return lines.join('\n');
|
|
13
55
|
},
|
|
14
56
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unirest.d.ts","sourceRoot":"","sources":["../../../../src/plugins/java/unirest/unirest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD
|
|
1
|
+
{"version":3,"file":"unirest.d.ts","sourceRoot":"","sources":["../../../../src/plugins/java/unirest/unirest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD,0FAA0F;AAC1F,eAAO,MAAM,WAAW,EAAE,MAezB,CAAA"}
|
|
@@ -1,14 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
/**
|
|
4
|
-
* java/unirest
|
|
5
|
-
*/
|
|
1
|
+
import { javaBody, quoteJava } from '../../../libs/java.js';
|
|
2
|
+
import { prepareRequest } from '../../../libs/prepare-request.js';
|
|
3
|
+
/** Generates a Unirest request including arbitrary methods and byte-preserving bodies. */
|
|
6
4
|
export const javaUnirest = {
|
|
7
5
|
target: 'java',
|
|
8
6
|
client: 'unirest',
|
|
9
7
|
title: 'Unirest',
|
|
10
|
-
generate(request) {
|
|
11
|
-
|
|
12
|
-
return
|
|
8
|
+
generate(request, configuration) {
|
|
9
|
+
const { url, method, headers, body } = prepareRequest(request, configuration);
|
|
10
|
+
return [
|
|
11
|
+
...javaBody(body),
|
|
12
|
+
`HttpResponse<String> response = Unirest.request(${quoteJava(method)}, ${quoteJava(url)})`,
|
|
13
|
+
...headers.map(({ name, value }) => ` .header(${quoteJava(name)}, ${quoteJava(value)})`),
|
|
14
|
+
...(body ? [' .body(body.toByteArray())'] : []),
|
|
15
|
+
' .asString();',
|
|
16
|
+
'System.out.println(response.getBody());',
|
|
17
|
+
].join('\n');
|
|
13
18
|
},
|
|
14
19
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jquery.d.ts","sourceRoot":"","sources":["../../../../src/plugins/js/jquery/jquery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;
|
|
1
|
+
{"version":3,"file":"jquery.d.ts","sourceRoot":"","sources":["../../../../src/plugins/js/jquery/jquery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAIpD,wEAAwE;AACxE,eAAO,MAAM,QAAQ,EAAE,MA2BtB,CAAA"}
|
|
@@ -1,14 +1,28 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* js/jquery
|
|
5
|
-
*/
|
|
1
|
+
import { prepareBrowserRequest } from '../../../libs/browser-request.js';
|
|
2
|
+
/** Generates a jQuery Ajax request with explicit body serialization. */
|
|
6
3
|
export const jsJquery = {
|
|
7
4
|
target: 'js',
|
|
8
5
|
client: 'jquery',
|
|
9
6
|
title: 'jQuery',
|
|
10
|
-
generate(request) {
|
|
11
|
-
|
|
12
|
-
return
|
|
7
|
+
generate(request, configuration) {
|
|
8
|
+
const { url, method, headers, setup, body, withCredentials } = prepareBrowserRequest(request, configuration);
|
|
9
|
+
return [
|
|
10
|
+
...setup,
|
|
11
|
+
'$.ajax({',
|
|
12
|
+
` url: ${JSON.stringify(url)},`,
|
|
13
|
+
` method: ${JSON.stringify(method)},`,
|
|
14
|
+
...(withCredentials ? [' xhrFields: { withCredentials: true },'] : []),
|
|
15
|
+
' processData: false,',
|
|
16
|
+
' contentType: false,',
|
|
17
|
+
...(headers.length
|
|
18
|
+
? [
|
|
19
|
+
' beforeSend(xhr) {',
|
|
20
|
+
...headers.map(({ name, value }) => ` xhr.setRequestHeader(${JSON.stringify(name)}, ${JSON.stringify(value)});`),
|
|
21
|
+
' },',
|
|
22
|
+
]
|
|
23
|
+
: []),
|
|
24
|
+
` data: ${body}`,
|
|
25
|
+
'}).done((response) => console.log(response));',
|
|
26
|
+
].join('\n');
|
|
13
27
|
},
|
|
14
28
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"xhr.d.ts","sourceRoot":"","sources":["../../../../src/plugins/js/xhr/xhr.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;
|
|
1
|
+
{"version":3,"file":"xhr.d.ts","sourceRoot":"","sources":["../../../../src/plugins/js/xhr/xhr.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAIpD,gEAAgE;AAChE,eAAO,MAAM,KAAK,EAAE,MAgBnB,CAAA"}
|