Improve Curl imports

This commit is contained in:
Gregory Schier
2024-05-14 08:28:01 -07:00
parent 8b9a5cb5fb
commit 35e8155ecf
3 changed files with 72 additions and 38 deletions

View File

@@ -184,7 +184,7 @@ export function importCommand(parseEntries: ParseEntry[], workspaceId: string) {
urlParameters = urlParameters =
search?.split('&').map((p) => { search?.split('&').map((p) => {
const v = splitOnce(p, '='); const v = splitOnce(p, '=');
return { name: v[0] ?? '', value: v[1] ?? '' }; return { name: v[0] ?? '', value: v[1] ?? '', enabled: true };
}) ?? []; }) ?? [];
url = baseUrl ?? urlArg; url = baseUrl ?? urlArg;
@@ -212,11 +212,13 @@ export function importCommand(parseEntries: ParseEntry[], workspaceId: string) {
return { return {
name: (name ?? '').trim().replace(/;$/, ''), name: (name ?? '').trim().replace(/;$/, ''),
value: '', value: '',
enabled: true,
}; };
} }
return { return {
name: (name ?? '').trim(), name: (name ?? '').trim(),
value: value.trim(), value: value.trim(),
enabled: true,
}; };
}); });
@@ -243,6 +245,7 @@ export function importCommand(parseEntries: ParseEntry[], workspaceId: string) {
headers.push({ headers.push({
name: 'Cookie', name: 'Cookie',
value: cookieHeaderValue, value: cookieHeaderValue,
enabled: true,
}); });
} }
@@ -286,12 +289,17 @@ export function importCommand(parseEntries: ParseEntry[], workspaceId: string) {
) { ) {
bodyType = mimeType ?? 'application/x-www-form-urlencoded'; bodyType = mimeType ?? 'application/x-www-form-urlencoded';
body = { body = {
params: dataParameters.map((parameter) => ({ form: dataParameters.map((parameter) => ({
...parameter, ...parameter,
name: decodeURIComponent(parameter.name || ''), name: decodeURIComponent(parameter.name || ''),
value: decodeURIComponent(parameter.value || ''), value: decodeURIComponent(parameter.value || ''),
})), })),
}; };
headers.push({
name: 'Content-Type',
value: 'application/x-www-form-urlencoded',
enabled: true,
});
} else if (dataParameters.length > 0) { } else if (dataParameters.length > 0) {
bodyType = bodyType =
mimeType === 'application/json' || mimeType === 'text/xml' || mimeType === 'text/plain' mimeType === 'application/json' || mimeType === 'text/xml' || mimeType === 'text/plain'
@@ -307,13 +315,20 @@ export function importCommand(parseEntries: ParseEntry[], workspaceId: string) {
body = { body = {
form: formDataParams, form: formDataParams,
}; };
if (mimeType == null) {
headers.push({
name: 'Content-Type',
value: 'multipart/form-data',
enabled: true,
});
}
} }
// Method // Method
let method = getPairValue(pairsByName, '', ['X', 'request']).toUpperCase(); let method = getPairValue(pairsByName, '', ['X', 'request']).toUpperCase();
if (method === '' && body) { if (method === '' && body) {
method = 'text' in body || 'params' in body ? 'POST' : 'GET'; method = 'text' in body || 'form' in body ? 'POST' : 'GET';
} }
const request: ExportResources['httpRequests'][0] = { const request: ExportResources['httpRequests'][0] = {
@@ -342,6 +357,7 @@ const pairsToDataParameters = (keyedPairs: PairsByName) => {
value: string; value: string;
contentType?: string; contentType?: string;
filePath?: string; filePath?: string;
enabled?: boolean;
}[] = []; }[] = [];
for (const flagName of DATA_FLAGS) { for (const flagName of DATA_FLAGS) {
@@ -361,11 +377,13 @@ const pairsToDataParameters = (keyedPairs: PairsByName) => {
name: name ?? '', name: name ?? '',
value: '', value: '',
filePath: p.slice(1), filePath: p.slice(1),
enabled: true,
}); });
} else { } else {
dataParameters.push({ dataParameters.push({
name: name ?? '', name: name ?? '',
value: flagName === 'data-urlencode' ? encodeURIComponent(value ?? '') : value ?? '', value: flagName === 'data-urlencode' ? encodeURIComponent(value ?? '') : value ?? '',
enabled: true,
}); });
} }
} }

View File

@@ -122,6 +122,13 @@ describe('importer-curl', () => {
baseRequest({ baseRequest({
method: 'POST', method: 'POST',
url: 'https://yaak.app', url: 'https://yaak.app',
headers: [
{
name: 'Content-Type',
value: 'multipart/form-data',
enabled: true,
},
],
bodyType: 'multipart/form-data', bodyType: 'multipart/form-data',
body: { body: {
form: [ form: [
@@ -145,11 +152,18 @@ describe('importer-curl', () => {
method: 'POST', method: 'POST',
url: 'https://yaak.app', url: 'https://yaak.app',
bodyType: 'application/x-www-form-urlencoded', bodyType: 'application/x-www-form-urlencoded',
headers: [
{
name: 'Content-Type',
value: 'application/x-www-form-urlencoded',
enabled: true,
},
],
body: { body: {
params: [ form: [
{ name: 'a', value: '' }, { name: 'a', value: '', enabled: true },
{ name: 'b', value: '' }, { name: 'b', value: '', enabled: true },
{ name: 'c', value: 'ccc' }, { name: 'c', value: 'ccc', enabled: true },
], ],
}, },
}), }),
@@ -168,7 +182,7 @@ describe('importer-curl', () => {
baseRequest({ baseRequest({
method: 'POST', method: 'POST',
url: 'https://yaak.app', url: 'https://yaak.app',
headers: [{ name: 'Content-Type', value: 'text/plain' }], headers: [{ name: 'Content-Type', value: 'text/plain', enabled: true }],
bodyType: 'text/plain', bodyType: 'text/plain',
body: { text: 'a&b&c=ccc' }, body: { text: 'a&b&c=ccc' },
}), }),
@@ -189,7 +203,7 @@ describe('importer-curl', () => {
baseRequest({ baseRequest({
method: 'POST', method: 'POST',
url: 'https://yaak.app', url: 'https://yaak.app',
headers: [{ name: 'Content-Type', value: 'application/json' }], headers: [{ name: 'Content-Type', value: 'application/json', enabled: true }],
bodyType: 'application/json', bodyType: 'application/json',
body: { text: '{\n "foo":"bar"\n}' }, body: { text: '{\n "foo":"bar"\n}' },
}), }),
@@ -208,10 +222,10 @@ describe('importer-curl', () => {
baseRequest({ baseRequest({
url: 'https://yaak.app', url: 'https://yaak.app',
headers: [ headers: [
{ name: 'Name', value: '' }, { name: 'Name', value: '', enabled: true },
{ name: 'Foo', value: 'bar' }, { name: 'Foo', value: 'bar', enabled: true },
{ name: 'AAA', value: 'bbb' }, { name: 'AAA', value: 'bbb', enabled: true },
{ name: '', value: 'ccc' }, { name: '', value: 'ccc', enabled: true },
], ],
}), }),
], ],
@@ -262,7 +276,7 @@ describe('importer-curl', () => {
httpRequests: [ httpRequests: [
baseRequest({ baseRequest({
url: 'https://yaak.app', url: 'https://yaak.app',
headers: [{ name: 'Cookie', value: 'foo=bar' }], headers: [{ name: 'Cookie', value: 'foo=bar', enabled: true }],
}), }),
], ],
}, },
@@ -277,8 +291,8 @@ describe('importer-curl', () => {
baseRequest({ baseRequest({
url: 'https://yaak.app', url: 'https://yaak.app',
urlParameters: [ urlParameters: [
{ name: 'foo', value: 'bar' }, { name: 'foo', value: 'bar', enabled: true },
{ name: 'baz', value: 'a%20a' }, { name: 'baz', value: 'a%20a', enabled: true },
], ],
}), }),
], ],

View File

@@ -40,7 +40,7 @@ function K(n, s, e) {
if (D.test(a)) if (D.test(a))
return { op: a }; return { op: a };
var x = !1, O = !1, p = "", A = !1, i; var x = !1, O = !1, p = "", A = !1, i;
function b() { function $() {
i += 1; i += 1;
var v, d, T = a.charAt(i); var v, d, T = a.charAt(i);
if (T === "{") { if (T === "{") {
@@ -62,7 +62,7 @@ function K(n, s, e) {
if (A = A || !x && (u === "*" || u === "?"), O) if (A = A || !x && (u === "*" || u === "?"), O)
p += u, O = !1; p += u, O = !1;
else if (x) else if (x)
u === x ? x = !1 : x == _ ? p += u : u === t ? (i += 1, u = a.charAt(i), u === G || u === t || u === U ? p += u : p += t + u) : u === U ? p += b() : p += u; u === x ? x = !1 : x == _ ? p += u : u === t ? (i += 1, u = a.charAt(i), u === G || u === t || u === U ? p += u : p += t + u) : u === U ? p += $() : p += u;
else if (u === G || u === _) else if (u === G || u === _)
x = u; x = u;
else { else {
@@ -73,7 +73,7 @@ function K(n, s, e) {
var E = { comment: n.slice(r.index + i + 1) }; var E = { comment: n.slice(r.index + i + 1) };
return p.length ? [p, E] : [E]; return p.length ? [p, E] : [E];
} else } else
u === t ? O = !0 : u === U ? p += b() : p += u; u === t ? O = !0 : u === U ? p += $() : p += u;
} }
} }
return A ? { op: "glob", pattern: p } : p; return A ? { op: "glob", pattern: p } : p;
@@ -158,13 +158,13 @@ function te(n, s) {
for (let o = 1; o < n.length; o++) { for (let o = 1; o < n.length; o++) {
let l = n[o]; let l = n[o];
if (typeof l == "string" && (l = l.trim()), typeof l == "string" && l.match(/^-{1,2}[\w-]+/)) { if (typeof l == "string" && (l = l.trim()), typeof l == "string" && l.match(/^-{1,2}[\w-]+/)) {
const y = l[0] === "-" && l[1] !== "-"; const b = l[0] === "-" && l[1] !== "-";
let h = l.replace(/^-{1,2}/, ""); let h = l.replace(/^-{1,2}/, "");
if (!ee.includes(h)) if (!ee.includes(h))
continue; continue;
let $; let y;
const S = n[o + 1]; const S = n[o + 1];
y && h.length > 1 ? ($ = h.slice(1), h = h.slice(0, 1)) : typeof S == "string" && !S.startsWith("-") ? ($ = S, o++) : $ = !0, e[h] = e[h] || [], e[h].push($); b && h.length > 1 ? (y = h.slice(1), h = h.slice(0, 1)) : typeof S == "string" && !S.startsWith("-") ? (y = S, o++) : y = !0, e[h] = e[h] || [], e[h].push(y);
} else } else
l && t.push(l); l && t.push(l);
} }
@@ -172,7 +172,7 @@ function te(n, s) {
const f = C(e, t[0] || "", ["url"]), [w, r] = W(f, "?"); const f = C(e, t[0] || "", ["url"]), [w, r] = W(f, "?");
c = (r == null ? void 0 : r.split("&").map((o) => { c = (r == null ? void 0 : r.split("&").map((o) => {
const l = W(o, "="); const l = W(o, "=");
return { name: l[0] ?? "", value: l[1] ?? "" }; return { name: l[0] ?? "", value: l[1] ?? "", enabled: !0 };
})) ?? [], m = w ?? f; })) ?? [], m = w ?? f;
const [a, x] = C(e, "", ["u", "user"]).split(/:(.*)$/), O = C(e, !1, ["digest"]), p = a ? O ? "digest" : "basic" : null, A = a ? { const [a, x] = C(e, "", ["u", "user"]).split(/:(.*)$/), O = C(e, !1, ["digest"]), p = a ? O ? "digest" : "basic" : null, A = a ? {
username: a.trim(), username: a.trim(),
@@ -181,39 +181,39 @@ function te(n, s) {
...e.header || [], ...e.header || [],
...e.H || [] ...e.H || []
].map((o) => { ].map((o) => {
const [l, y] = o.split(/:(.*)$/); const [l, b] = o.split(/:(.*)$/);
return y ? { return b ? {
name: (l ?? "").trim(), name: (l ?? "").trim(),
value: y.trim() value: b.trim()
} : { } : {
name: (l ?? "").trim().replace(/;$/, ""), name: (l ?? "").trim().replace(/;$/, ""),
value: "" value: ""
}; };
}), b = [ }), $ = [
...e.cookie || [], ...e.cookie || [],
...e.b || [] ...e.b || []
].map((o) => { ].map((o) => {
const l = o.split("=", 1)[0], y = o.replace(`${l}=`, ""); const l = o.split("=", 1)[0], b = o.replace(`${l}=`, "");
return `${l}=${y}`; return `${l}=${b}`;
}).join("; "), u = i.find((o) => o.name.toLowerCase() === "cookie"); }).join("; "), u = i.find((o) => o.name.toLowerCase() === "cookie");
b && u ? u.value += `; ${b}` : b && i.push({ $ && u ? u.value += `; ${$}` : $ && i.push({
name: "Cookie", name: "Cookie",
value: b value: $
}); });
const E = ne(e), v = i.find((o) => o.name.toLowerCase() === "content-type"), d = v ? v.value.split(";")[0] : null, T = [ const E = ne(e), v = i.find((o) => o.name.toLowerCase() === "content-type"), d = v ? v.value.split(";")[0] : null, T = [
...e.form || [], ...e.form || [],
...e.F || [] ...e.F || []
].map((o) => { ].map((o) => {
const l = o.split("="), y = l[0] ?? "", h = l[1] ?? "", $ = { const l = o.split("="), b = l[0] ?? "", h = l[1] ?? "", y = {
name: y, name: b,
enabled: !0 enabled: !0
}; };
return h.indexOf("@") === 0 ? $.file = h.slice(1) : $.value = h, $; return h.indexOf("@") === 0 ? y.file = h.slice(1) : y.value = h, y;
}); });
let g = {}, I = null; let g = {}, I = null;
const B = C(e, !1, ["G", "get"]); const B = C(e, !1, ["G", "get"]);
E.length > 0 && B ? c.push(...E) : E.length > 0 && (d == null || d === "application/x-www-form-urlencoded") ? (I = d ?? "application/x-www-form-urlencoded", g = { E.length > 0 && B ? c.push(...E) : E.length > 0 && (d == null || d === "application/x-www-form-urlencoded") ? (I = d ?? "application/x-www-form-urlencoded", g = {
params: E.map((o) => ({ form: E.map((o) => ({
...o, ...o,
name: decodeURIComponent(o.name || ""), name: decodeURIComponent(o.name || ""),
value: decodeURIComponent(o.value || "") value: decodeURIComponent(o.value || "")
@@ -224,7 +224,7 @@ function te(n, s) {
form: T form: T
}); });
let P = C(e, "", ["X", "request"]).toUpperCase(); let P = C(e, "", ["X", "request"]).toUpperCase();
return P === "" && g && (P = "text" in g || "params" in g ? "POST" : "GET"), { return P === "" && g && (P = "text" in g || "form" in g ? "POST" : "GET"), {
id: N("http_request"), id: N("http_request"),
model: "http_request", model: "http_request",
workspaceId: s, workspaceId: s,
@@ -253,10 +253,12 @@ const ne = (n) => {
c.startsWith("@") ? s.push({ c.startsWith("@") ? s.push({
name: m ?? "", name: m ?? "",
value: "", value: "",
filePath: c.slice(1) filePath: c.slice(1),
enabled: !0
}) : s.push({ }) : s.push({
name: m ?? "", name: m ?? "",
value: e === "data-urlencode" ? encodeURIComponent(f ?? "") : f ?? "" value: e === "data-urlencode" ? encodeURIComponent(f ?? "") : f ?? "",
enabled: !0
}); });
} }
} }