@scalar/snippetz 0.7.8 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/dist/clients/index.d.ts.map +1 -1
  2. package/dist/clients/index.js +5 -4
  3. package/dist/httpsnippet-lite/targets/c/libcurl/client.d.ts.map +1 -1
  4. package/dist/httpsnippet-lite/targets/c/libcurl/client.js +4 -2
  5. package/dist/libs/http.d.ts +0 -4
  6. package/dist/libs/http.d.ts.map +1 -1
  7. package/dist/libs/http.js +1 -11
  8. package/dist/libs/javascript.d.ts.map +1 -1
  9. package/dist/libs/javascript.js +6 -3
  10. package/dist/plugins/csharp/httpclient/httpclient.d.ts.map +1 -1
  11. package/dist/plugins/csharp/httpclient/httpclient.js +2 -3
  12. package/dist/plugins/dart/http/http.d.ts.map +1 -1
  13. package/dist/plugins/dart/http/http.js +1 -4
  14. package/dist/plugins/go/native/native.d.ts.map +1 -1
  15. package/dist/plugins/go/native/native.js +191 -5
  16. package/dist/plugins/http/http11/http11.d.ts.map +1 -1
  17. package/dist/plugins/http/http11/http11.js +2 -6
  18. package/dist/plugins/js/axios/axios.d.ts +1 -2
  19. package/dist/plugins/js/axios/axios.d.ts.map +1 -1
  20. package/dist/plugins/js/axios/axios.js +2 -11
  21. package/dist/plugins/js/fetch/fetch.d.ts.map +1 -1
  22. package/dist/plugins/js/fetch/fetch.js +2 -3
  23. package/dist/plugins/node/axios/axios.d.ts +1 -2
  24. package/dist/plugins/node/axios/axios.d.ts.map +1 -1
  25. package/dist/plugins/node/axios/axios.js +2 -11
  26. package/dist/plugins/node/fetch/fetch.d.ts.map +1 -1
  27. package/dist/plugins/node/fetch/fetch.js +2 -3
  28. package/dist/plugins/node/undici/undici.d.ts.map +1 -1
  29. package/dist/plugins/node/undici/undici.js +2 -3
  30. package/dist/plugins/php/curl/curl.d.ts.map +1 -1
  31. package/dist/plugins/php/curl/curl.js +1 -3
  32. package/dist/plugins/php/laravel/index.d.ts +2 -0
  33. package/dist/plugins/php/laravel/index.d.ts.map +1 -0
  34. package/dist/plugins/php/laravel/index.js +1 -0
  35. package/dist/plugins/php/laravel/laravel.d.ts +6 -0
  36. package/dist/plugins/php/laravel/laravel.d.ts.map +1 -0
  37. package/dist/plugins/php/laravel/laravel.js +134 -0
  38. package/dist/plugins/r/httr2/httr2.d.ts +6 -0
  39. package/dist/plugins/r/httr2/httr2.d.ts.map +1 -0
  40. package/dist/plugins/r/httr2/httr2.js +159 -0
  41. package/dist/plugins/r/httr2/index.d.ts +2 -0
  42. package/dist/plugins/r/httr2/index.d.ts.map +1 -0
  43. package/dist/plugins/r/httr2/index.js +1 -0
  44. package/dist/plugins/ruby/native/native.d.ts.map +1 -1
  45. package/dist/plugins/ruby/native/native.js +138 -5
  46. package/dist/plugins/shared/axios.d.ts +3 -0
  47. package/dist/plugins/shared/axios.d.ts.map +1 -0
  48. package/dist/plugins/shared/axios.js +148 -0
  49. package/dist/plugins/shell/curl/curl.d.ts.map +1 -1
  50. package/dist/plugins/shell/curl/curl.js +1 -3
  51. package/dist/snippetz.d.ts +1 -1
  52. package/package.json +12 -7
  53. package/dist/httpsnippet-lite/targets/go/native/client.d.ts +0 -12
  54. package/dist/httpsnippet-lite/targets/go/native/client.d.ts.map +0 -1
  55. package/dist/httpsnippet-lite/targets/go/native/client.js +0 -157
  56. package/dist/httpsnippet-lite/targets/javascript/axios/client.d.ts +0 -12
  57. package/dist/httpsnippet-lite/targets/javascript/axios/client.d.ts.map +0 -1
  58. package/dist/httpsnippet-lite/targets/javascript/axios/client.js +0 -86
  59. package/dist/httpsnippet-lite/targets/node/axios/client.d.ts +0 -3
  60. package/dist/httpsnippet-lite/targets/node/axios/client.d.ts.map +0 -1
  61. package/dist/httpsnippet-lite/targets/node/axios/client.js +0 -78
  62. package/dist/httpsnippet-lite/targets/r/httr/client.d.ts +0 -12
  63. package/dist/httpsnippet-lite/targets/r/httr/client.d.ts.map +0 -1
  64. package/dist/httpsnippet-lite/targets/r/httr/client.js +0 -115
  65. package/dist/httpsnippet-lite/targets/ruby/native/client.d.ts +0 -3
  66. package/dist/httpsnippet-lite/targets/ruby/native/client.d.ts.map +0 -1
  67. package/dist/httpsnippet-lite/targets/ruby/native/client.js +0 -67
  68. package/dist/plugins/r/httr/httr.d.ts +0 -6
  69. package/dist/plugins/r/httr/httr.d.ts.map +0 -1
  70. package/dist/plugins/r/httr/httr.js +0 -14
  71. package/dist/plugins/r/httr/index.d.ts +0 -2
  72. package/dist/plugins/r/httr/index.d.ts.map +0 -1
  73. package/dist/plugins/r/httr/index.js +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/clients/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAyCpD;;GAEG;AACH,eAAO,MAAM,OAAO,EAAE,MAAM,EA+H3B,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/clients/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AA0CpD;;GAEG;AACH,eAAO,MAAM,OAAO,EAAE,MAAM,EA+H3B,CAAA"}
@@ -24,12 +24,13 @@ import { objcNsurlsession } from '../plugins/objc/nsurlsession/index.js';
24
24
  import { ocamlCohttp } from '../plugins/ocaml/cohttp/index.js';
25
25
  import { phpCurl } from '../plugins/php/curl/index.js';
26
26
  import { phpGuzzle } from '../plugins/php/guzzle/index.js';
27
+ import { phpLaravel } from '../plugins/php/laravel/index.js';
27
28
  import { powershellRestmethod } from '../plugins/powershell/restmethod/index.js';
28
29
  import { powershellWebrequest } from '../plugins/powershell/webrequest/index.js';
29
30
  import { pythonHttpxAsync, pythonHttpxSync } from '../plugins/python/httpx/index.js';
30
31
  import { pythonPython3 } from '../plugins/python/python3/index.js';
31
32
  import { pythonRequests } from '../plugins/python/requests/index.js';
32
- import { rHttr } from '../plugins/r/httr/index.js';
33
+ import { rHttr2 } from '../plugins/r/httr2/index.js';
33
34
  import { rubyNative } from '../plugins/ruby/native/index.js';
34
35
  import { rustReqwest } from '../plugins/rust/reqwest/index.js';
35
36
  import { shellCurl } from '../plugins/shell/curl/index.js';
@@ -122,7 +123,7 @@ export const clients = [
122
123
  key: 'php',
123
124
  title: 'PHP',
124
125
  default: 'curl',
125
- clients: [phpCurl, phpGuzzle],
126
+ clients: [phpCurl, phpGuzzle, phpLaravel],
126
127
  },
127
128
  {
128
129
  key: 'powershell',
@@ -139,8 +140,8 @@ export const clients = [
139
140
  {
140
141
  key: 'r',
141
142
  title: 'R',
142
- default: 'httr',
143
- clients: [rHttr],
143
+ default: 'httr2',
144
+ clients: [rHttr2],
144
145
  },
145
146
  {
146
147
  key: 'ruby',
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../../../src/httpsnippet-lite/targets/c/libcurl/client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAA;AAG/D,eAAO,MAAM,OAAO,EAAE,MAkErB,CAAA"}
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../../../src/httpsnippet-lite/targets/c/libcurl/client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAA;AAE/D,eAAO,MAAM,OAAO,EAAE,MAqErB,CAAA"}
@@ -1,6 +1,5 @@
1
1
  import { CodeBuilder } from '../../../../httpsnippet-lite/helpers/code-builder.js';
2
2
  import { escapeForDoubleQuotes } from '../../../../httpsnippet-lite/helpers/escape.js';
3
- import { createSearchParams } from '../../../../libs/http.js';
4
3
  export const libcurl = {
5
4
  info: {
6
5
  key: 'libcurl',
@@ -42,7 +41,10 @@ export const libcurl = {
42
41
  push(`curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, ${JSON.stringify(postData.text)});`);
43
42
  }
44
43
  else if (postData.mimeType === 'application/x-www-form-urlencoded' && postData.params) {
45
- push(`curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "${createSearchParams(postData.params).toString()}");`);
44
+ const formBody = postData.params
45
+ .map((param) => new URLSearchParams([[param.name, param.value ?? '']]).toString())
46
+ .join('&');
47
+ push(`curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "${formBody}");`);
46
48
  }
47
49
  else if (postData.mimeType === 'multipart/form-data' && postData.params) {
48
50
  push('curl_mime *mime = curl_mime_init(hnd);');
@@ -12,10 +12,6 @@ export declare function buildQueryString(queryParams?: Array<{
12
12
  name: string;
13
13
  value: string;
14
14
  }>): string;
15
- /**
16
- * Creates a new URL search params object and sets query params so we can handle arrays
17
- */
18
- export declare const createSearchParams: (query?: HarRequest["queryString"]) => URLSearchParams;
19
15
  /**
20
16
  * Adds a named value while preserving repeated keys as arrays.
21
17
  */
@@ -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;;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,kBAAkB,GAAI,QAAO,UAAU,CAAC,aAAa,CAAM,oBAQvE,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,CAQ1G;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"}
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;;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,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,CAQ1G;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"}
package/dist/libs/http.js CHANGED
@@ -14,19 +14,9 @@ export function buildQueryString(queryParams) {
14
14
  if (!queryParams?.length) {
15
15
  return '';
16
16
  }
17
- const queryPairs = queryParams.map((param) => `${encodeURIComponent(param.name)}=${encodeURIComponent(param.value)}`);
17
+ const queryPairs = queryParams.map((param) => `${param.name}=${param.value}`);
18
18
  return `?${queryPairs.join('&')}`;
19
19
  }
20
- /**
21
- * Creates a new URL search params object and sets query params so we can handle arrays
22
- */
23
- export const createSearchParams = (query = []) => {
24
- const searchParams = new URLSearchParams();
25
- query.forEach((q) => {
26
- searchParams.append(q.name, q.value);
27
- });
28
- return searchParams;
29
- };
30
20
  /**
31
21
  * Adds a named value while preserving repeated keys as arrays.
32
22
  */
@@ -1 +1 @@
1
- {"version":3,"file":"javascript.d.ts","sourceRoot":"","sources":["../../src/libs/javascript.ts"],"names":[],"mappings":"AASA;;;GAGG;AACH,qBAAa,GAAG;IACK,KAAK,EAAE,MAAM;gBAAb,KAAK,EAAE,MAAM;CACjC;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,SAAI,GAAG,MAAM,CA2D3E"}
1
+ {"version":3,"file":"javascript.d.ts","sourceRoot":"","sources":["../../src/libs/javascript.ts"],"names":[],"mappings":"AAaA;;;GAGG;AACH,qBAAa,GAAG;IACK,KAAK,EAAE,MAAM;gBAAb,KAAK,EAAE,MAAM;CACjC;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,SAAI,GAAG,MAAM,CA2D3E"}
@@ -1,10 +1,13 @@
1
1
  /**
2
2
  * Checks if a key needs to be wrapped in quotes when used as an object property
3
3
  *
4
- * Returns true if the key contains spaces or hyphens
4
+ * Returns true when the key is not a valid JavaScript identifier.
5
5
  */
6
6
  function needsQuotes(key) {
7
- return /\s|-/.test(key);
7
+ return !/^[$A-Z_][0-9A-Z_$]*$/i.test(key);
8
+ }
9
+ function escapeObjectKey(key) {
10
+ return key.replaceAll('\\', '\\\\').replaceAll('\n', '\\n').replaceAll('\r', '\\r').replaceAll("'", "\\'");
8
11
  }
9
12
  /**
10
13
  * Represents a raw code that should not be quoted, e.g. `JSON.stringify(...)`.
@@ -29,7 +32,7 @@ export function objectToString(obj, indent = 0) {
29
32
  return '{}';
30
33
  }
31
34
  for (const [key, value] of Object.entries(obj)) {
32
- const formattedKey = needsQuotes(key) ? `'${key}'` : key;
35
+ const formattedKey = needsQuotes(key) ? `'${escapeObjectKey(key)}'` : key;
33
36
  if (value instanceof Raw) {
34
37
  const lines = value.value.split('\n');
35
38
  let formattedValue = `${value.value}`;
@@ -1 +1 @@
1
- {"version":3,"file":"httpclient.d.ts","sourceRoot":"","sources":["../../../../src/plugins/csharp/httpclient/httpclient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAuB,MAAM,wBAAwB,CAAA;AAKzE;;GAEG;AACH,eAAO,MAAM,gBAAgB,EAAE,MA4C9B,CAAA"}
1
+ {"version":3,"file":"httpclient.d.ts","sourceRoot":"","sources":["../../../../src/plugins/csharp/httpclient/httpclient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAuB,MAAM,wBAAwB,CAAA;AAKzE;;GAEG;AACH,eAAO,MAAM,gBAAgB,EAAE,MA2C9B,CAAA"}
@@ -1,5 +1,5 @@
1
1
  import { encode } from 'js-base64';
2
- import { createSearchParams } from '../../../libs/http.js';
2
+ import { buildQueryString } from '../../../libs/http.js';
3
3
  /**
4
4
  * csharp/httpclient
5
5
  */
@@ -17,8 +17,7 @@ export const csharpHttpclient = {
17
17
  // Normalization
18
18
  normalizedRequest.method = normalizedRequest.method.toUpperCase();
19
19
  // Build URL with query string
20
- const searchParams = createSearchParams(normalizedRequest.queryString);
21
- const queryString = searchParams.size ? `?${searchParams.toString()}` : '';
20
+ const queryString = buildQueryString(normalizedRequest.queryString);
22
21
  const url = `${normalizedRequest.url}${queryString}`;
23
22
  // Start building the snippet
24
23
  const lines = [];
@@ -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,MA4GtB,CAAA"}
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,MAyGtB,CAAA"}
@@ -56,10 +56,7 @@ export const dartHttp = {
56
56
  }
57
57
  // Handle query string
58
58
  const queryString = normalizedRequest.queryString?.length
59
- ? '?' +
60
- normalizedRequest.queryString
61
- .map((param) => `${encodeURIComponent(param.name)}=${encodeURIComponent(param.value)}`)
62
- .join('&')
59
+ ? '?' + normalizedRequest.queryString.map((param) => `${param.name}=${param.value}`).join('&')
63
60
  : '';
64
61
  const url = `${normalizedRequest.url}${queryString}`;
65
62
  // Handle body
@@ -1 +1 @@
1
- {"version":3,"file":"native.d.ts","sourceRoot":"","sources":["../../../../src/plugins/go/native/native.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD;;GAEG;AACH,eAAO,MAAM,QAAQ,EAAE,MAQtB,CAAA"}
1
+ {"version":3,"file":"native.d.ts","sourceRoot":"","sources":["../../../../src/plugins/go/native/native.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAqMpD;;GAEG;AACH,eAAO,MAAM,QAAQ,EAAE,MAqEtB,CAAA"}
@@ -1,5 +1,144 @@
1
- import { native } from '../../../httpsnippet-lite/targets/go/native/client.js';
2
- import { convertWithHttpSnippetLite } from '../../../utils/convertWithHttpSnippetLite.js';
1
+ import { buildQueryString } from '../../../libs/http.js';
2
+ const formatImport = (entry) => {
3
+ if (entry.includes(' ')) {
4
+ return entry;
5
+ }
6
+ return goString(entry);
7
+ };
8
+ const goString = (value) => JSON.stringify(value);
9
+ const normalizeMethod = (method) => (method || 'GET').toUpperCase();
10
+ const normalizeUrl = (url) => {
11
+ if (!url) {
12
+ return '';
13
+ }
14
+ try {
15
+ const parsedUrl = new URL(url);
16
+ if (parsedUrl.pathname === '/') {
17
+ return `${parsedUrl.origin}${parsedUrl.search}${parsedUrl.hash}`;
18
+ }
19
+ return parsedUrl.toString();
20
+ }
21
+ catch {
22
+ return url;
23
+ }
24
+ };
25
+ const joinUrlAndQuery = (url, queryString) => {
26
+ const query = buildQueryString(queryString);
27
+ if (!query) {
28
+ return url;
29
+ }
30
+ if (!url) {
31
+ return query;
32
+ }
33
+ return `${url}${url.includes('?') ? '&' : '?'}${query.slice(1)}`;
34
+ };
35
+ const collectHeaders = (headers, cookies) => {
36
+ const dedupedHeaders = new Map();
37
+ headers?.forEach((header) => {
38
+ if (header.name) {
39
+ dedupedHeaders.set(header.name, header.value ?? '');
40
+ }
41
+ });
42
+ if (cookies?.length) {
43
+ const cookieHeader = cookies
44
+ .map((cookie) => `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`)
45
+ .join('; ');
46
+ dedupedHeaders.set('Cookie', cookieHeader);
47
+ }
48
+ return Array.from(dedupedHeaders.entries()).map(([name, value]) => ({ name, value }));
49
+ };
50
+ const toPrettyJson = (text) => {
51
+ if (text === undefined) {
52
+ return '';
53
+ }
54
+ try {
55
+ return JSON.stringify(JSON.parse(text), null, 2);
56
+ }
57
+ catch {
58
+ return text;
59
+ }
60
+ };
61
+ const canUseRawStringLiteral = (value) => {
62
+ return !value.includes('`');
63
+ };
64
+ const toGoStringLiteral = (value, preferRawLiteral = false) => {
65
+ if (preferRawLiteral && canUseRawStringLiteral(value)) {
66
+ return `\`${value}\``;
67
+ }
68
+ return goString(value);
69
+ };
70
+ const buildBodySection = (postData) => {
71
+ const imports = new Set();
72
+ if (!postData) {
73
+ return {
74
+ imports,
75
+ setupLines: [],
76
+ requestBody: 'nil',
77
+ needsMultipartContentTypeHeader: false,
78
+ };
79
+ }
80
+ if (postData.mimeType === 'application/x-www-form-urlencoded' && postData.params?.length) {
81
+ imports.add('neturl "net/url"');
82
+ imports.add('strings');
83
+ return {
84
+ imports,
85
+ setupLines: [
86
+ 'postData := neturl.Values{}',
87
+ ...postData.params.map((param) => `postData.Set(${goString(param.name)}, ${goString(param.value ?? '')})`),
88
+ ],
89
+ requestBody: 'strings.NewReader(postData.Encode())',
90
+ needsMultipartContentTypeHeader: false,
91
+ };
92
+ }
93
+ if (postData.mimeType === 'multipart/form-data' && postData.params?.length) {
94
+ imports.add('bytes');
95
+ imports.add('mime/multipart');
96
+ const setupLines = ['payload := &bytes.Buffer{}', 'writer := multipart.NewWriter(payload)'];
97
+ const hasFile = postData.params.some((param) => param.fileName !== undefined);
98
+ let hasDeclaredPart = false;
99
+ let hasDeclaredFile = false;
100
+ if (hasFile) {
101
+ imports.add('os');
102
+ imports.add('io');
103
+ }
104
+ postData.params.forEach((param) => {
105
+ setupLines.push('');
106
+ if (param.fileName !== undefined) {
107
+ setupLines.push(`part, _ ${hasDeclaredPart ? '=' : ':='} writer.CreateFormFile(${goString(param.name)}, ${goString(param.fileName)})`);
108
+ setupLines.push('');
109
+ setupLines.push(`f, _ ${hasDeclaredFile ? '=' : ':='} os.Open(${goString(param.fileName)})`);
110
+ setupLines.push('defer f.Close()');
111
+ setupLines.push('');
112
+ setupLines.push('_, _ = io.Copy(part, f)');
113
+ hasDeclaredPart = true;
114
+ hasDeclaredFile = true;
115
+ }
116
+ else {
117
+ setupLines.push(`_ = writer.WriteField(${goString(param.name)}, ${goString(param.value ?? '')})`);
118
+ }
119
+ });
120
+ setupLines.push('writer.Close()');
121
+ return {
122
+ imports,
123
+ setupLines,
124
+ requestBody: 'payload',
125
+ needsMultipartContentTypeHeader: true,
126
+ };
127
+ }
128
+ imports.add('strings');
129
+ const payload = postData.mimeType === 'application/json' ? toPrettyJson(postData.text) : (postData.text ?? '');
130
+ return {
131
+ imports,
132
+ setupLines: [
133
+ `payload := strings.NewReader(${toGoStringLiteral(payload, postData.mimeType === 'application/json')})`,
134
+ ],
135
+ requestBody: 'payload',
136
+ needsMultipartContentTypeHeader: false,
137
+ };
138
+ };
139
+ const buildImports = (bodyImports) => {
140
+ return Array.from(new Set(['fmt', 'io', 'net/http', ...bodyImports])).sort();
141
+ };
3
142
  /**
4
143
  * go/native
5
144
  */
@@ -7,8 +146,55 @@ export const goNative = {
7
146
  target: 'go',
8
147
  client: 'native',
9
148
  title: 'NewRequest',
10
- generate(request) {
11
- // TODO: Write an own converter
12
- return convertWithHttpSnippetLite(native, request);
149
+ generate(request, configuration) {
150
+ if (!request) {
151
+ return '';
152
+ }
153
+ const method = normalizeMethod(request.method);
154
+ const fullUrl = normalizeUrl(joinUrlAndQuery(request.url ?? '', request.queryString));
155
+ const headers = collectHeaders(request.headers, request.cookies);
156
+ const bodySection = buildBodySection(request.postData);
157
+ const imports = buildImports(bodySection.imports);
158
+ const lines = [
159
+ 'package main',
160
+ '',
161
+ 'import (',
162
+ ...imports.map((entry) => `\t${formatImport(entry)}`),
163
+ ')',
164
+ '',
165
+ 'func main() {',
166
+ `\trequestUrl := ${goString(fullUrl)}`,
167
+ '',
168
+ ...bodySection.setupLines.map((line) => (line ? `\t${line}` : '')),
169
+ ];
170
+ if (bodySection.setupLines.length) {
171
+ lines.push('');
172
+ }
173
+ lines.push(`\treq, _ := http.NewRequest(${goString(method)}, requestUrl, ${bodySection.requestBody})`);
174
+ lines.push('');
175
+ if (bodySection.needsMultipartContentTypeHeader) {
176
+ lines.push('\treq.Header.Set("Content-Type", writer.FormDataContentType())');
177
+ }
178
+ if (configuration?.auth?.username && configuration?.auth?.password) {
179
+ lines.push(`\treq.SetBasicAuth(${goString(configuration.auth.username)}, ${goString(configuration.auth.password)})`);
180
+ }
181
+ headers.forEach((header) => {
182
+ lines.push(`\treq.Header.Add(${goString(header.name)}, ${goString(header.value)})`);
183
+ });
184
+ if (bodySection.needsMultipartContentTypeHeader ||
185
+ headers.length ||
186
+ (configuration?.auth?.username && configuration.auth.password)) {
187
+ lines.push('');
188
+ }
189
+ lines.push('\tres, _ := http.DefaultClient.Do(req)');
190
+ lines.push('');
191
+ lines.push('\tdefer res.Body.Close()');
192
+ lines.push('\tbody, _ := io.ReadAll(res.Body)');
193
+ lines.push('');
194
+ lines.push('\tfmt.Println(res)');
195
+ lines.push('\tfmt.Println(string(body))');
196
+ lines.push('');
197
+ lines.push('}');
198
+ return lines.join('\n');
13
199
  },
14
200
  };
@@ -1 +1 @@
1
- {"version":3,"file":"http11.d.ts","sourceRoot":"","sources":["../../../../src/plugins/http/http11/http11.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAEpD;;GAEG;AACH,eAAO,MAAM,UAAU,EAAE,MAsHxB,CAAA"}
1
+ {"version":3,"file":"http11.d.ts","sourceRoot":"","sources":["../../../../src/plugins/http/http11/http11.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAEpD;;GAEG;AACH,eAAO,MAAM,UAAU,EAAE,MAkHxB,CAAA"}
@@ -31,9 +31,7 @@ export const httpHttp11 = {
31
31
  let requestString = `${normalizedRequest.method} ${path} HTTP/1.1\r\n`;
32
32
  // Handle query string parameters
33
33
  if (normalizedRequest.queryString.length) {
34
- const queryString = normalizedRequest.queryString
35
- .map((param) => `${encodeURIComponent(param.name)}=${encodeURIComponent(param.value)}`)
36
- .join('&');
34
+ const queryString = normalizedRequest.queryString.map((param) => `${param.name}=${param.value}`).join('&');
37
35
  // Append query string to the path
38
36
  requestString = `${normalizedRequest.method} ${path}?${queryString} HTTP/1.1\r\n`;
39
37
  }
@@ -52,9 +50,7 @@ export const httpHttp11 = {
52
50
  });
53
51
  // Query string parameters
54
52
  if (normalizedRequest.queryString.length) {
55
- const queryString = normalizedRequest.queryString
56
- .map((param) => `${encodeURIComponent(param.name)}=${encodeURIComponent(param.value)}`)
57
- .join('&');
53
+ const queryString = normalizedRequest.queryString.map((param) => `${param.name}=${param.value}`).join('&');
58
54
  // Append query string to the path
59
55
  requestString = `${normalizedRequest.method} ${path}?${queryString} HTTP/1.1\r\n`;
60
56
  }
@@ -1,6 +1,5 @@
1
- import type { Plugin } from '@scalar/types/snippetz';
2
1
  /**
3
2
  * js/axios
4
3
  */
5
- export declare const jsAxios: Plugin;
4
+ export declare const jsAxios: import("@scalar/types/snippetz").Plugin;
6
5
  //# sourceMappingURL=axios.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"axios.d.ts","sourceRoot":"","sources":["../../../../src/plugins/js/axios/axios.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD;;GAEG;AACH,eAAO,MAAM,OAAO,EAAE,MAQrB,CAAA"}
1
+ {"version":3,"file":"axios.d.ts","sourceRoot":"","sources":["../../../../src/plugins/js/axios/axios.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,eAAO,MAAM,OAAO,yCAA0B,CAAA"}
@@ -1,14 +1,5 @@
1
- import { axios } from '../../../httpsnippet-lite/targets/javascript/axios/client.js';
2
- import { convertWithHttpSnippetLite } from '../../../utils/convertWithHttpSnippetLite.js';
1
+ import { createAxiosPlugin } from '../../../plugins/shared/axios.js';
3
2
  /**
4
3
  * js/axios
5
4
  */
6
- export const jsAxios = {
7
- target: 'js',
8
- client: 'axios',
9
- title: 'Axios',
10
- generate(request) {
11
- // TODO: Write an own converter
12
- return convertWithHttpSnippetLite(axios, request);
13
- },
14
- };
5
+ export const jsAxios = createAxiosPlugin('js');
@@ -1 +1 @@
1
- {"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../../../src/plugins/js/fetch/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD;;GAEG;AACH,eAAO,MAAM,OAAO,EAAE,MAuFrB,CAAA"}
1
+ {"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../../../src/plugins/js/fetch/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD;;GAEG;AACH,eAAO,MAAM,OAAO,EAAE,MAsFrB,CAAA"}
@@ -1,4 +1,4 @@
1
- import { createSearchParams } from '../../../libs/http.js';
1
+ import { buildQueryString } from '../../../libs/http.js';
2
2
  import { Raw, objectToString } from '../../../libs/javascript.js';
3
3
  /**
4
4
  * js/fetch
@@ -21,8 +21,7 @@ export const jsFetch = {
21
21
  method: normalizedRequest.method === 'GET' ? undefined : normalizedRequest.method,
22
22
  };
23
23
  // Query
24
- const searchParams = createSearchParams(normalizedRequest.queryString);
25
- const queryString = searchParams.size ? `?${searchParams.toString()}` : '';
24
+ const queryString = buildQueryString(normalizedRequest.queryString);
26
25
  // Headers
27
26
  if (normalizedRequest.headers?.length) {
28
27
  options.headers = {};
@@ -1,6 +1,5 @@
1
- import type { Plugin } from '@scalar/types/snippetz';
2
1
  /**
3
2
  * node/axios
4
3
  */
5
- export declare const nodeAxios: Plugin;
4
+ export declare const nodeAxios: import("@scalar/types/snippetz").Plugin;
6
5
  //# sourceMappingURL=axios.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"axios.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/axios/axios.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD;;GAEG;AACH,eAAO,MAAM,SAAS,EAAE,MAQvB,CAAA"}
1
+ {"version":3,"file":"axios.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/axios/axios.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,eAAO,MAAM,SAAS,yCAA4B,CAAA"}
@@ -1,14 +1,5 @@
1
- import { axios } from '../../../httpsnippet-lite/targets/node/axios/client.js';
2
- import { convertWithHttpSnippetLite } from '../../../utils/convertWithHttpSnippetLite.js';
1
+ import { createAxiosPlugin } from '../../../plugins/shared/axios.js';
3
2
  /**
4
3
  * node/axios
5
4
  */
6
- export const nodeAxios = {
7
- target: 'node',
8
- client: 'axios',
9
- title: 'Axios',
10
- generate(request) {
11
- // TODO: Write an own converter
12
- return convertWithHttpSnippetLite(axios, request);
13
- },
14
- };
5
+ export const nodeAxios = createAxiosPlugin('node');
@@ -1 +1 @@
1
- {"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/fetch/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD;;GAEG;AACH,eAAO,MAAM,SAAS,EAAE,MA4FvB,CAAA"}
1
+ {"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/fetch/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD;;GAEG;AACH,eAAO,MAAM,SAAS,EAAE,MA2FvB,CAAA"}
@@ -1,4 +1,4 @@
1
- import { createSearchParams } from '../../../libs/http.js';
1
+ import { buildQueryString } from '../../../libs/http.js';
2
2
  import { Raw, objectToString } from '../../../libs/javascript.js';
3
3
  /**
4
4
  * node/fetch
@@ -21,8 +21,7 @@ export const nodeFetch = {
21
21
  method: normalizedRequest.method === 'GET' ? undefined : normalizedRequest.method,
22
22
  };
23
23
  // Query
24
- const searchParams = createSearchParams(normalizedRequest.queryString);
25
- const queryString = searchParams.size ? `?${searchParams.toString()}` : '';
24
+ const queryString = buildQueryString(normalizedRequest.queryString);
26
25
  // Headers
27
26
  if (normalizedRequest.headers?.length) {
28
27
  options.headers = {};
@@ -1 +1 @@
1
- {"version":3,"file":"undici.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/undici/undici.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD;;GAEG;AACH,eAAO,MAAM,UAAU,EAAE,MAqExB,CAAA"}
1
+ {"version":3,"file":"undici.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/undici/undici.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAKpD;;GAEG;AACH,eAAO,MAAM,UAAU,EAAE,MAoExB,CAAA"}
@@ -1,4 +1,4 @@
1
- import { createSearchParams } from '../../../libs/http.js';
1
+ import { buildQueryString } from '../../../libs/http.js';
2
2
  import { Raw, objectToString } from '../../../libs/javascript.js';
3
3
  /**
4
4
  * node/undici
@@ -20,8 +20,7 @@ export const nodeUndici = {
20
20
  method: normalizedRequest.method === 'GET' ? undefined : normalizedRequest.method,
21
21
  };
22
22
  // Query
23
- const searchParams = createSearchParams(normalizedRequest.queryString);
24
- const queryString = searchParams.size ? `?${searchParams.toString()}` : '';
23
+ const queryString = buildQueryString(normalizedRequest.queryString);
25
24
  // Headers
26
25
  if (normalizedRequest.headers?.length) {
27
26
  options.headers = {};
@@ -1 +1 @@
1
- {"version":3,"file":"curl.d.ts","sourceRoot":"","sources":["../../../../src/plugins/php/curl/curl.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAIpD;;GAEG;AACH,eAAO,MAAM,OAAO,EAAE,MAiKrB,CAAA"}
1
+ {"version":3,"file":"curl.d.ts","sourceRoot":"","sources":["../../../../src/plugins/php/curl/curl.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAIpD;;GAEG;AACH,eAAO,MAAM,OAAO,EAAE,MA+JrB,CAAA"}
@@ -23,9 +23,7 @@ export const phpCurl = {
23
23
  ? '?' +
24
24
  normalizedRequest.queryString
25
25
  .map((param) => {
26
- const encodedName = encodeURIComponent(param.name);
27
- const encodedValue = encodeURIComponent(param.value);
28
- return `${encodedName}=${encodedValue}`;
26
+ return `${param.name}=${param.value}`;
29
27
  })
30
28
  .join('&')
31
29
  : '';
@@ -0,0 +1,2 @@
1
+ export { phpLaravel } from './laravel.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/plugins/php/laravel/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA"}
@@ -0,0 +1 @@
1
+ export { phpLaravel } from './laravel.js';
@@ -0,0 +1,6 @@
1
+ import type { Plugin } from '@scalar/types/snippetz';
2
+ /**
3
+ * php/laravel
4
+ */
5
+ export declare const phpLaravel: Plugin;
6
+ //# sourceMappingURL=laravel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"laravel.d.ts","sourceRoot":"","sources":["../../../../src/plugins/php/laravel/laravel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAyCpD;;GAEG;AACH,eAAO,MAAM,UAAU,EAAE,MAiIxB,CAAA"}