Improve Curl imports

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

View File

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

View File

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

View File

@@ -40,7 +40,7 @@ function K(n, s, e) {
if (D.test(a))
return { op: a };
var x = !1, O = !1, p = "", A = !1, i;
function b() {
function $() {
i += 1;
var v, d, T = a.charAt(i);
if (T === "{") {
@@ -62,7 +62,7 @@ function K(n, s, e) {
if (A = A || !x && (u === "*" || u === "?"), O)
p += u, O = !1;
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 === _)
x = u;
else {
@@ -73,7 +73,7 @@ function K(n, s, e) {
var E = { comment: n.slice(r.index + i + 1) };
return p.length ? [p, E] : [E];
} 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;
@@ -158,13 +158,13 @@ function te(n, s) {
for (let o = 1; o < n.length; o++) {
let l = n[o];
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}/, "");
if (!ee.includes(h))
continue;
let $;
let y;
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
l && t.push(l);
}
@@ -172,7 +172,7 @@ function te(n, s) {
const f = C(e, t[0] || "", ["url"]), [w, r] = W(f, "?");
c = (r == null ? void 0 : r.split("&").map((o) => {
const l = W(o, "=");
return { name: l[0] ?? "", value: l[1] ?? "" };
return { name: l[0] ?? "", value: l[1] ?? "", enabled: !0 };
})) ?? [], m = w ?? f;
const [a, x] = C(e, "", ["u", "user"]).split(/:(.*)$/), O = C(e, !1, ["digest"]), p = a ? O ? "digest" : "basic" : null, A = a ? {
username: a.trim(),
@@ -181,39 +181,39 @@ function te(n, s) {
...e.header || [],
...e.H || []
].map((o) => {
const [l, y] = o.split(/:(.*)$/);
return y ? {
const [l, b] = o.split(/:(.*)$/);
return b ? {
name: (l ?? "").trim(),
value: y.trim()
value: b.trim()
} : {
name: (l ?? "").trim().replace(/;$/, ""),
value: ""
};
}), b = [
}), $ = [
...e.cookie || [],
...e.b || []
].map((o) => {
const l = o.split("=", 1)[0], y = o.replace(`${l}=`, "");
return `${l}=${y}`;
const l = o.split("=", 1)[0], b = o.replace(`${l}=`, "");
return `${l}=${b}`;
}).join("; "), u = i.find((o) => o.name.toLowerCase() === "cookie");
b && u ? u.value += `; ${b}` : b && i.push({
$ && u ? u.value += `; ${$}` : $ && i.push({
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 = [
...e.form || [],
...e.F || []
].map((o) => {
const l = o.split("="), y = l[0] ?? "", h = l[1] ?? "", $ = {
name: y,
const l = o.split("="), b = l[0] ?? "", h = l[1] ?? "", y = {
name: b,
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;
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 = {
params: E.map((o) => ({
form: E.map((o) => ({
...o,
name: decodeURIComponent(o.name || ""),
value: decodeURIComponent(o.value || "")
@@ -224,7 +224,7 @@ function te(n, s) {
form: T
});
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"),
model: "http_request",
workspaceId: s,
@@ -253,10 +253,12 @@ const ne = (n) => {
c.startsWith("@") ? s.push({
name: m ?? "",
value: "",
filePath: c.slice(1)
filePath: c.slice(1),
enabled: !0
}) : s.push({
name: m ?? "",
value: e === "data-urlencode" ? encodeURIComponent(f ?? "") : f ?? ""
value: e === "data-urlencode" ? encodeURIComponent(f ?? "") : f ?? "",
enabled: !0
});
}
}