@scalar/snippetz 0.8.0 → 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.
- package/dist/clients/index.d.ts.map +1 -1
- package/dist/clients/index.js +5 -4
- package/dist/libs/javascript.d.ts.map +1 -1
- package/dist/libs/javascript.js +6 -3
- package/dist/plugins/go/native/native.d.ts.map +1 -1
- package/dist/plugins/go/native/native.js +191 -5
- package/dist/plugins/js/axios/axios.d.ts +1 -2
- package/dist/plugins/js/axios/axios.d.ts.map +1 -1
- package/dist/plugins/js/axios/axios.js +2 -11
- package/dist/plugins/node/axios/axios.d.ts +1 -2
- package/dist/plugins/node/axios/axios.d.ts.map +1 -1
- package/dist/plugins/node/axios/axios.js +2 -11
- package/dist/plugins/php/laravel/index.d.ts +2 -0
- package/dist/plugins/php/laravel/index.d.ts.map +1 -0
- package/dist/plugins/php/laravel/index.js +1 -0
- package/dist/plugins/php/laravel/laravel.d.ts +6 -0
- package/dist/plugins/php/laravel/laravel.d.ts.map +1 -0
- package/dist/plugins/php/laravel/laravel.js +134 -0
- package/dist/plugins/r/httr2/httr2.d.ts +6 -0
- package/dist/plugins/r/httr2/httr2.d.ts.map +1 -0
- package/dist/plugins/r/httr2/httr2.js +159 -0
- package/dist/plugins/r/httr2/index.d.ts +2 -0
- package/dist/plugins/r/httr2/index.d.ts.map +1 -0
- package/dist/plugins/r/httr2/index.js +1 -0
- package/dist/plugins/ruby/native/native.d.ts.map +1 -1
- package/dist/plugins/ruby/native/native.js +138 -5
- package/dist/plugins/shared/axios.d.ts +3 -0
- package/dist/plugins/shared/axios.d.ts.map +1 -0
- package/dist/plugins/shared/axios.js +148 -0
- package/dist/snippetz.d.ts +1 -1
- package/package.json +12 -7
- package/dist/httpsnippet-lite/targets/go/native/client.d.ts +0 -12
- package/dist/httpsnippet-lite/targets/go/native/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/go/native/client.js +0 -157
- package/dist/httpsnippet-lite/targets/javascript/axios/client.d.ts +0 -12
- package/dist/httpsnippet-lite/targets/javascript/axios/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/javascript/axios/client.js +0 -86
- package/dist/httpsnippet-lite/targets/node/axios/client.d.ts +0 -3
- package/dist/httpsnippet-lite/targets/node/axios/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/node/axios/client.js +0 -78
- package/dist/httpsnippet-lite/targets/r/httr/client.d.ts +0 -12
- package/dist/httpsnippet-lite/targets/r/httr/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/r/httr/client.js +0 -115
- package/dist/httpsnippet-lite/targets/ruby/native/client.d.ts +0 -3
- package/dist/httpsnippet-lite/targets/ruby/native/client.d.ts.map +0 -1
- package/dist/httpsnippet-lite/targets/ruby/native/client.js +0 -67
- package/dist/plugins/r/httr/httr.d.ts +0 -6
- package/dist/plugins/r/httr/httr.d.ts.map +0 -1
- package/dist/plugins/r/httr/httr.js +0 -14
- package/dist/plugins/r/httr/index.d.ts +0 -2
- package/dist/plugins/r/httr/index.d.ts.map +0 -1
- 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;
|
|
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"}
|
package/dist/clients/index.js
CHANGED
|
@@ -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 {
|
|
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: '
|
|
143
|
-
clients: [
|
|
143
|
+
default: 'httr2',
|
|
144
|
+
clients: [rHttr2],
|
|
144
145
|
},
|
|
145
146
|
{
|
|
146
147
|
key: 'ruby',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"javascript.d.ts","sourceRoot":"","sources":["../../src/libs/javascript.ts"],"names":[],"mappings":"
|
|
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"}
|
package/dist/libs/javascript.js
CHANGED
|
@@ -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
|
|
4
|
+
* Returns true when the key is not a valid JavaScript identifier.
|
|
5
5
|
*/
|
|
6
6
|
function needsQuotes(key) {
|
|
7
|
-
return
|
|
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":"native.d.ts","sourceRoot":"","sources":["../../../../src/plugins/go/native/native.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,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 {
|
|
2
|
-
|
|
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
|
-
|
|
12
|
-
|
|
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":"axios.d.ts","sourceRoot":"","sources":["../../../../src/plugins/js/axios/axios.ts"],"names":[],"mappings":"
|
|
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 {
|
|
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":"axios.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/axios/axios.ts"],"names":[],"mappings":"
|
|
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 {
|
|
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');
|
|
@@ -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 @@
|
|
|
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"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { accumulateRepeatedValue, buildQueryString } from '../../../libs/http.js';
|
|
2
|
+
import { objectToString } from '../../../libs/php.js';
|
|
3
|
+
const escapePhpString = (value) => value.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
|
|
4
|
+
const quotePhpString = (value) => `'${escapePhpString(value)}'`;
|
|
5
|
+
const escapePhpObjectKey = escapePhpString;
|
|
6
|
+
const escapeObjectKeys = (value) => {
|
|
7
|
+
if (Array.isArray(value)) {
|
|
8
|
+
return value.map((item) => escapeObjectKeys(item));
|
|
9
|
+
}
|
|
10
|
+
if (value && typeof value === 'object') {
|
|
11
|
+
return Object.entries(value).reduce((acc, [key, nestedValue]) => {
|
|
12
|
+
acc[escapePhpObjectKey(key)] = escapeObjectKeys(nestedValue);
|
|
13
|
+
return acc;
|
|
14
|
+
}, {});
|
|
15
|
+
}
|
|
16
|
+
return value;
|
|
17
|
+
};
|
|
18
|
+
const getCookieDomain = (url) => {
|
|
19
|
+
if (!url) {
|
|
20
|
+
return 'localhost';
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
return new URL(url).hostname || 'localhost';
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return 'localhost';
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* php/laravel
|
|
31
|
+
*/
|
|
32
|
+
export const phpLaravel = {
|
|
33
|
+
target: 'php',
|
|
34
|
+
client: 'laravel',
|
|
35
|
+
title: 'Laravel HTTP Client',
|
|
36
|
+
generate(request, configuration) {
|
|
37
|
+
const normalizedRequest = {
|
|
38
|
+
method: 'GET',
|
|
39
|
+
...request,
|
|
40
|
+
};
|
|
41
|
+
normalizedRequest.method = normalizedRequest.method.toUpperCase();
|
|
42
|
+
const queryString = buildQueryString(normalizedRequest.queryString);
|
|
43
|
+
const url = `${normalizedRequest.url ?? ''}${queryString}`;
|
|
44
|
+
const chain = [];
|
|
45
|
+
if (configuration?.auth?.username && configuration.auth.password) {
|
|
46
|
+
chain.push(`withBasicAuth(${quotePhpString(configuration.auth.username)}, ${quotePhpString(configuration.auth.password)})`);
|
|
47
|
+
}
|
|
48
|
+
if (normalizedRequest.headers?.length) {
|
|
49
|
+
const headers = {};
|
|
50
|
+
normalizedRequest.headers.forEach((header) => {
|
|
51
|
+
accumulateRepeatedValue(headers, header.name, header.value);
|
|
52
|
+
});
|
|
53
|
+
chain.push(`withHeaders(${objectToString(escapeObjectKeys(headers))})`);
|
|
54
|
+
}
|
|
55
|
+
if (normalizedRequest.cookies?.length) {
|
|
56
|
+
const cookies = {};
|
|
57
|
+
normalizedRequest.cookies.forEach((cookie) => {
|
|
58
|
+
accumulateRepeatedValue(cookies, cookie.name, cookie.value);
|
|
59
|
+
});
|
|
60
|
+
chain.push(`withCookies(${objectToString(escapeObjectKeys(cookies))}, ${quotePhpString(getCookieDomain(normalizedRequest.url ?? ''))})`);
|
|
61
|
+
}
|
|
62
|
+
let payload;
|
|
63
|
+
if (normalizedRequest.postData) {
|
|
64
|
+
if (normalizedRequest.postData.mimeType === 'application/json') {
|
|
65
|
+
if (normalizedRequest.postData.text) {
|
|
66
|
+
try {
|
|
67
|
+
payload = JSON.parse(normalizedRequest.postData.text);
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
chain.push(`withBody(${quotePhpString(normalizedRequest.postData.text)}, 'application/json')`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
else if (normalizedRequest.postData.mimeType === 'application/x-www-form-urlencoded' &&
|
|
75
|
+
normalizedRequest.postData.params) {
|
|
76
|
+
const formData = {};
|
|
77
|
+
normalizedRequest.postData.params.forEach((param) => {
|
|
78
|
+
accumulateRepeatedValue(formData, param.name, param.value ?? '');
|
|
79
|
+
});
|
|
80
|
+
chain.push('asForm()');
|
|
81
|
+
payload = formData;
|
|
82
|
+
}
|
|
83
|
+
else if (normalizedRequest.postData.mimeType === 'multipart/form-data' && normalizedRequest.postData.params) {
|
|
84
|
+
const multipartData = {};
|
|
85
|
+
normalizedRequest.postData.params.forEach((param) => {
|
|
86
|
+
if (param.fileName !== undefined) {
|
|
87
|
+
const args = [
|
|
88
|
+
quotePhpString(param.name),
|
|
89
|
+
`file_get_contents(${quotePhpString(param.fileName)})`,
|
|
90
|
+
quotePhpString(param.fileName),
|
|
91
|
+
];
|
|
92
|
+
if (param.contentType) {
|
|
93
|
+
args.push(objectToString(escapeObjectKeys({ 'Content-Type': param.contentType })));
|
|
94
|
+
}
|
|
95
|
+
chain.push(`attach(${args.join(', ')})`);
|
|
96
|
+
}
|
|
97
|
+
else if (param.contentType) {
|
|
98
|
+
chain.push(`attach(${quotePhpString(param.name)}, ${quotePhpString(param.value ?? '')}, null, ${objectToString(escapeObjectKeys({ 'Content-Type': param.contentType }))})`);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
accumulateRepeatedValue(multipartData, param.name, param.value ?? '');
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
if (Object.keys(multipartData).length > 0) {
|
|
105
|
+
payload = multipartData;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else if (normalizedRequest.postData.mimeType === 'application/octet-stream') {
|
|
109
|
+
chain.push(`withBody(${quotePhpString(normalizedRequest.postData.text ?? '')}, 'application/octet-stream')`);
|
|
110
|
+
}
|
|
111
|
+
else if (normalizedRequest.postData.text) {
|
|
112
|
+
try {
|
|
113
|
+
payload = JSON.parse(normalizedRequest.postData.text);
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
chain.push(`withBody(${quotePhpString(normalizedRequest.postData.text)}, ${quotePhpString(normalizedRequest.postData.mimeType || 'text/plain')})`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const requestMethod = normalizedRequest.method.toLowerCase();
|
|
121
|
+
const supportsDirectMethod = ['delete', 'get', 'head', 'patch', 'post', 'put'].includes(requestMethod);
|
|
122
|
+
const requestCall = supportsDirectMethod
|
|
123
|
+
? payload !== undefined && requestMethod !== 'head'
|
|
124
|
+
? `${requestMethod}(${quotePhpString(url)}, ${objectToString(escapeObjectKeys(payload), 1)})`
|
|
125
|
+
: `${requestMethod}(${quotePhpString(url)})`
|
|
126
|
+
: payload !== undefined
|
|
127
|
+
? `send(${quotePhpString(normalizedRequest.method)}, ${quotePhpString(url)}, ${objectToString(escapeObjectKeys(payload), 1)})`
|
|
128
|
+
: `send(${quotePhpString(normalizedRequest.method)}, ${quotePhpString(url)})`;
|
|
129
|
+
const expression = chain.length > 0
|
|
130
|
+
? [`Http::${chain[0]}`, ...chain.slice(1).map((method) => ` ->${method}`), ` ->${requestCall}`].join('\n')
|
|
131
|
+
: `Http::${requestCall}`;
|
|
132
|
+
return `use Illuminate\\Support\\Facades\\Http;\n\n$response = ${expression};`;
|
|
133
|
+
},
|
|
134
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"httr2.d.ts","sourceRoot":"","sources":["../../../../src/plugins/r/httr2/httr2.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,MAAM,EAAuB,MAAM,wBAAwB,CAAA;AAkDrF;;GAEG;AACH,eAAO,MAAM,MAAM,EAAE,MAuHpB,CAAA"}
|