mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-23 18:01:21 +01:00
Curl import (#24)
This commit is contained in:
283
src-tauri/plugins/importer-curl/index.mjs
Normal file
283
src-tauri/plugins/importer-curl/index.mjs
Normal file
@@ -0,0 +1,283 @@
|
||||
var j = "(?:" + [
|
||||
"\\|\\|",
|
||||
"\\&\\&",
|
||||
";;",
|
||||
"\\|\\&",
|
||||
"\\<\\(",
|
||||
"\\<\\<\\<",
|
||||
">>",
|
||||
">\\&",
|
||||
"<\\&",
|
||||
"[&;()|<>]"
|
||||
].join("|") + ")", D = new RegExp("^" + j + "$"), q = "|&;()<> \\t", M = '"((\\\\"|[^"])*?)"', Q = "'((\\\\'|[^'])*?)'", V = /^#$/, _ = "'", G = '"', U = "$", R = "", z = 4294967296;
|
||||
for (var L = 0; L < 4; L++)
|
||||
R += (z * Math.random()).toString(16);
|
||||
var J = new RegExp("^" + R);
|
||||
function X(n, s) {
|
||||
for (var e = s.lastIndex, t = [], c; c = s.exec(n); )
|
||||
t.push(c), s.lastIndex === c.index && (s.lastIndex += 1);
|
||||
return s.lastIndex = e, t;
|
||||
}
|
||||
function F(n, s, e) {
|
||||
var t = typeof n == "function" ? n(e) : n[e];
|
||||
return typeof t > "u" && e != "" ? t = "" : typeof t > "u" && (t = "$"), typeof t == "object" ? s + R + JSON.stringify(t) + R : s + t;
|
||||
}
|
||||
function K(n, s, e) {
|
||||
e || (e = {});
|
||||
var t = e.escape || "\\", c = "(\\" + t + `['"` + q + `]|[^\\s'"` + q + "])+", m = new RegExp([
|
||||
"(" + j + ")",
|
||||
// control chars
|
||||
"(" + c + "|" + M + "|" + Q + ")+"
|
||||
].join("|"), "g"), f = X(n, m);
|
||||
if (f.length === 0)
|
||||
return [];
|
||||
s || (s = {});
|
||||
var w = !1;
|
||||
return f.map(function(r) {
|
||||
var a = r[0];
|
||||
if (!a || w)
|
||||
return;
|
||||
if (D.test(a))
|
||||
return { op: a };
|
||||
var x = !1, O = !1, p = "", A = !1, i;
|
||||
function b() {
|
||||
i += 1;
|
||||
var v, d, T = a.charAt(i);
|
||||
if (T === "{") {
|
||||
if (i += 1, a.charAt(i) === "}")
|
||||
throw new Error("Bad substitution: " + a.slice(i - 2, i + 1));
|
||||
if (v = a.indexOf("}", i), v < 0)
|
||||
throw new Error("Bad substitution: " + a.slice(i));
|
||||
d = a.slice(i, v), i = v;
|
||||
} else if (/[*@#?$!_-]/.test(T))
|
||||
d = T, i += 1;
|
||||
else {
|
||||
var g = a.slice(i);
|
||||
v = g.match(/[^\w\d_]/), v ? (d = g.slice(0, v.index), i += v.index - 1) : (d = g, i = a.length);
|
||||
}
|
||||
return F(s, "", d);
|
||||
}
|
||||
for (i = 0; i < a.length; i++) {
|
||||
var u = a.charAt(i);
|
||||
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;
|
||||
else if (u === G || u === _)
|
||||
x = u;
|
||||
else {
|
||||
if (D.test(u))
|
||||
return { op: a };
|
||||
if (V.test(u)) {
|
||||
w = !0;
|
||||
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;
|
||||
}
|
||||
}
|
||||
return A ? { op: "glob", pattern: p } : p;
|
||||
}).reduce(function(r, a) {
|
||||
return typeof a > "u" ? r : r.concat(a);
|
||||
}, []);
|
||||
}
|
||||
var Y = function(s, e, t) {
|
||||
var c = K(s, e, t);
|
||||
return typeof e != "function" ? c : c.reduce(function(m, f) {
|
||||
if (typeof f == "object")
|
||||
return m.concat(f);
|
||||
var w = f.split(RegExp("(" + R + ".*?" + R + ")", "g"));
|
||||
return w.length === 1 ? m.concat(w[0]) : m.concat(w.filter(Boolean).map(function(r) {
|
||||
return J.test(r) ? JSON.parse(r.split(R)[1]) : r;
|
||||
}));
|
||||
}, []);
|
||||
}, Z = Y;
|
||||
const ae = "curl", se = "cURL", ie = "cURL command line tool", H = ["d", "data", "data-raw", "data-urlencode", "data-binary", "data-ascii"], ee = [
|
||||
["url"],
|
||||
// Specify the URL explicitly
|
||||
["user", "u"],
|
||||
// Authentication
|
||||
["digest"],
|
||||
// Apply auth as digest
|
||||
["header", "H"],
|
||||
["cookie", "b"],
|
||||
["get", "G"],
|
||||
// Put the post data in the URL
|
||||
["d", "data"],
|
||||
// Add url encoded data
|
||||
["data-raw"],
|
||||
["data-urlencode"],
|
||||
["data-binary"],
|
||||
["data-ascii"],
|
||||
["form", "F"],
|
||||
// Add multipart data
|
||||
["request", "X"],
|
||||
// Request method
|
||||
H
|
||||
].flatMap((n) => n), oe = (n) => {
|
||||
if (!n.match(/^\s*curl /))
|
||||
return null;
|
||||
const s = [], e = n.replace(/([^\\])\n/g, "$1; ");
|
||||
let t = [];
|
||||
const m = Z(e).flatMap((r) => typeof r == "string" && r.startsWith("-") && !r.startsWith("--") && r.length > 2 ? [r.slice(0, 2), r.slice(2)] : r);
|
||||
for (const r of m) {
|
||||
if (typeof r == "string") {
|
||||
r.startsWith("$") ? t.push(r.slice(1)) : t.push(r);
|
||||
continue;
|
||||
}
|
||||
if ("comment" in r)
|
||||
continue;
|
||||
const { op: a } = r;
|
||||
if (a === ";") {
|
||||
s.push(t), t = [];
|
||||
continue;
|
||||
}
|
||||
if (a != null && a.startsWith("$")) {
|
||||
const x = a.slice(2, a.length - 1).replace(/\\'/g, "'");
|
||||
t.push(x);
|
||||
continue;
|
||||
}
|
||||
a === "glob" && t.push(r.pattern);
|
||||
}
|
||||
s.push(t);
|
||||
const f = {
|
||||
model: "workspace",
|
||||
id: N("workspace"),
|
||||
name: "Curl Import"
|
||||
};
|
||||
return {
|
||||
resources: {
|
||||
httpRequests: s.filter((r) => r[0] === "curl").map((r) => te(r, f.id)),
|
||||
workspaces: [f]
|
||||
}
|
||||
};
|
||||
};
|
||||
function te(n, s) {
|
||||
const e = {}, t = [];
|
||||
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 $ = l[0] === "-" && l[1] !== "-";
|
||||
let h = l.replace(/^-{1,2}/, "");
|
||||
if (!ee.includes(h))
|
||||
continue;
|
||||
let y;
|
||||
const S = n[o + 1];
|
||||
$ && 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);
|
||||
}
|
||||
let c, m;
|
||||
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] ?? "" };
|
||||
})) ?? [], 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(),
|
||||
password: (x ?? "").trim()
|
||||
} : {}, i = [
|
||||
...e.header || [],
|
||||
...e.H || []
|
||||
].map((o) => {
|
||||
const [l, $] = o.split(/:(.*)$/);
|
||||
return $ ? {
|
||||
name: (l ?? "").trim(),
|
||||
value: $.trim()
|
||||
} : {
|
||||
name: (l ?? "").trim().replace(/;$/, ""),
|
||||
value: ""
|
||||
};
|
||||
}), b = [
|
||||
...e.cookie || [],
|
||||
...e.b || []
|
||||
].map((o) => {
|
||||
const l = o.split("=", 1)[0], $ = o.replace(`${l}=`, "");
|
||||
return `${l}=${$}`;
|
||||
}).join("; "), u = i.find((o) => o.name.toLowerCase() === "cookie");
|
||||
b && u ? u.value += `; ${b}` : b && i.push({
|
||||
name: "Cookie",
|
||||
value: b
|
||||
});
|
||||
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("="), $ = l[0] ?? "", h = l[1] ?? "", y = {
|
||||
name: $,
|
||||
enabled: !0
|
||||
};
|
||||
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) => ({
|
||||
...o,
|
||||
name: decodeURIComponent(o.name || ""),
|
||||
value: decodeURIComponent(o.value || "")
|
||||
}))
|
||||
}) : E.length > 0 ? (I = d === "application/json" || d === "text/xml" || d === "text/plain" ? d : "other", g = {
|
||||
text: E.map(({ name: o, value: l }) => o && l ? `${o}=${l}` : o || l).join("&")
|
||||
}) : T.length && (I = d ?? "multipart/form-data", g = {
|
||||
form: T
|
||||
});
|
||||
let P = C(e, "", ["X", "request"]).toUpperCase();
|
||||
return P === "" && g && (P = "text" in g || "params" in g ? "POST" : "GET"), {
|
||||
id: N("http_request"),
|
||||
model: "http_request",
|
||||
workspaceId: s,
|
||||
name: "",
|
||||
urlParameters: c,
|
||||
url: m,
|
||||
method: P,
|
||||
headers: i,
|
||||
authentication: A,
|
||||
authenticationType: p,
|
||||
body: g,
|
||||
bodyType: I,
|
||||
folderId: null,
|
||||
sortPriority: 0
|
||||
};
|
||||
}
|
||||
const ne = (n) => {
|
||||
let s = [];
|
||||
for (const e of H) {
|
||||
const t = n[e];
|
||||
if (!(!t || t.length === 0))
|
||||
for (const c of t) {
|
||||
if (typeof c != "string")
|
||||
continue;
|
||||
const [m, f] = c.split("=");
|
||||
c.startsWith("@") ? s.push({
|
||||
name: m ?? "",
|
||||
value: "",
|
||||
filePath: c.slice(1)
|
||||
}) : s.push({
|
||||
name: m ?? "",
|
||||
value: e === "data-urlencode" ? encodeURIComponent(f ?? "") : f ?? ""
|
||||
});
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}, C = (n, s, e) => {
|
||||
for (const t of e)
|
||||
if (n[t] && n[t].length)
|
||||
return n[t][0];
|
||||
return s;
|
||||
};
|
||||
function W(n, s) {
|
||||
const e = n.indexOf(s);
|
||||
return e > -1 ? [n.slice(0, e), n.slice(e + 1)] : [n];
|
||||
}
|
||||
const k = {};
|
||||
function N(n) {
|
||||
return k[n] = (k[n] ?? -1) + 1, `GENERATE_ID::${n.toUpperCase()}_${k[n]}`;
|
||||
}
|
||||
export {
|
||||
ie as description,
|
||||
ae as id,
|
||||
te as importCommand,
|
||||
se as name,
|
||||
oe as pluginHookImport
|
||||
};
|
||||
@@ -1,165 +1,179 @@
|
||||
function g(e, n) {
|
||||
return console.log("IMPORTING Environment", e._id, e.name, JSON.stringify(e, null, 2)), {
|
||||
id: e._id,
|
||||
createdAt: new Date(e.created ?? Date.now()).toISOString().replace("Z", ""),
|
||||
updatedAt: new Date(e.updated ?? Date.now()).toISOString().replace("Z", ""),
|
||||
workspaceId: n,
|
||||
model: "environment",
|
||||
name: e.name,
|
||||
variables: Object.entries(e.data).map(([t, a]) => ({
|
||||
enabled: !0,
|
||||
name: t,
|
||||
value: `${a}`
|
||||
}))
|
||||
};
|
||||
}
|
||||
function S(e) {
|
||||
return m(e) && e._type === "workspace";
|
||||
}
|
||||
function I(e) {
|
||||
return m(e) && e._type === "request_group";
|
||||
}
|
||||
function y(e) {
|
||||
return m(e) && e._type === "request";
|
||||
}
|
||||
function h(e) {
|
||||
return m(e) && e._type === "grpc_request";
|
||||
}
|
||||
function f(e) {
|
||||
return m(e) && e._type === "environment";
|
||||
}
|
||||
function m(e) {
|
||||
return Object.prototype.toString.call(e) === "[object Object]";
|
||||
}
|
||||
function w(e) {
|
||||
return Object.prototype.toString.call(e) === "[object String]";
|
||||
}
|
||||
function O(e) {
|
||||
return Object.entries(e).map(([n, t]) => ({
|
||||
enabled: !0,
|
||||
name: n,
|
||||
value: `${t}`
|
||||
}));
|
||||
}
|
||||
function d(e) {
|
||||
return w(e) ? e.replaceAll(/{{\s*(_\.)?([^}]+)\s*}}/g, "${[$2]}") : e;
|
||||
}
|
||||
function _(e, n, t = 0) {
|
||||
var l, r;
|
||||
console.log("IMPORTING REQUEST", e._id, e.name, JSON.stringify(e, null, 2));
|
||||
let a = null, o = null;
|
||||
((l = e.body) == null ? void 0 : l.mimeType) === "application/graphql" ? (a = "graphql", o = d(e.body.text)) : ((r = e.body) == null ? void 0 : r.mimeType) === "application/json" && (a = "application/json", o = d(e.body.text));
|
||||
let s = null, p = {};
|
||||
return e.authentication.type === "bearer" ? (s = "bearer", p = {
|
||||
token: d(e.authentication.token)
|
||||
}) : e.authentication.type === "basic" && (s = "basic", p = {
|
||||
username: d(e.authentication.username),
|
||||
password: d(e.authentication.password)
|
||||
}), {
|
||||
id: e._id,
|
||||
createdAt: new Date(e.created ?? Date.now()).toISOString().replace("Z", ""),
|
||||
updatedAt: new Date(e.updated ?? Date.now()).toISOString().replace("Z", ""),
|
||||
workspaceId: n,
|
||||
folderId: e.parentId === n ? null : e.parentId,
|
||||
model: "http_request",
|
||||
sortPriority: t,
|
||||
name: e.name,
|
||||
url: d(e.url),
|
||||
body: o,
|
||||
bodyType: a,
|
||||
authentication: p,
|
||||
authenticationType: s,
|
||||
method: e.method,
|
||||
headers: (e.headers ?? []).map(({ name: u, value: c, disabled: i }) => ({
|
||||
enabled: !i,
|
||||
name: u,
|
||||
value: c
|
||||
})).filter(({ name: u, value: c }) => u !== "" || c !== "")
|
||||
};
|
||||
}
|
||||
function R(e, n) {
|
||||
return console.log("IMPORTING FOLDER", e._id, e.name, JSON.stringify(e, null, 2)), {
|
||||
id: e._id,
|
||||
createdAt: new Date(e.created ?? Date.now()).toISOString().replace("Z", ""),
|
||||
updatedAt: new Date(e.updated ?? Date.now()).toISOString().replace("Z", ""),
|
||||
folderId: e.parentId === n ? null : e.parentId,
|
||||
workspaceId: n,
|
||||
model: "folder",
|
||||
name: e.name
|
||||
};
|
||||
}
|
||||
function D(e, n, t = 0) {
|
||||
var p;
|
||||
console.log("IMPORTING GRPC REQUEST", e._id, e.name, JSON.stringify(e, null, 2));
|
||||
const a = e.protoMethodName.split("/").filter((l) => l !== ""), o = a[0] ?? null, s = a[1] ?? null;
|
||||
return {
|
||||
id: e._id,
|
||||
createdAt: new Date(e.created ?? Date.now()).toISOString().replace("Z", ""),
|
||||
updatedAt: new Date(e.updated ?? Date.now()).toISOString().replace("Z", ""),
|
||||
workspaceId: n,
|
||||
folderId: e.parentId === n ? null : e.parentId,
|
||||
model: "grpc_request",
|
||||
sortPriority: t,
|
||||
name: e.name,
|
||||
url: d(e.url),
|
||||
service: o,
|
||||
method: s,
|
||||
message: ((p = e.body) == null ? void 0 : p.text) ?? "",
|
||||
metadata: (e.metadata ?? []).map(({ name: l, value: r, disabled: u }) => ({
|
||||
enabled: !u,
|
||||
name: l,
|
||||
value: r
|
||||
})).filter(({ name: l, value: r }) => l !== "" || r !== "")
|
||||
};
|
||||
}
|
||||
function q(e) {
|
||||
let n;
|
||||
function A(e) {
|
||||
let a;
|
||||
try {
|
||||
n = JSON.parse(e);
|
||||
a = JSON.parse(e);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
if (!m(n) || !Array.isArray(n.resources))
|
||||
if (!c(a) || !Array.isArray(a.resources))
|
||||
return;
|
||||
const t = {
|
||||
const n = {
|
||||
workspaces: [],
|
||||
httpRequests: [],
|
||||
grpcRequests: [],
|
||||
environments: [],
|
||||
folders: []
|
||||
}, a = n.resources.filter(S);
|
||||
for (const o of a) {
|
||||
const s = n.resources.find(
|
||||
(r) => f(r) && r.parentId === o._id
|
||||
}, o = a.resources.filter(_);
|
||||
for (const r of o) {
|
||||
const l = a.resources.find(
|
||||
(i) => w(i) && i.parentId === r._id
|
||||
);
|
||||
t.workspaces.push({
|
||||
id: o._id,
|
||||
createdAt: new Date(a.created ?? Date.now()).toISOString().replace("Z", ""),
|
||||
updatedAt: new Date(a.updated ?? Date.now()).toISOString().replace("Z", ""),
|
||||
n.workspaces.push({
|
||||
id: d(r._id),
|
||||
createdAt: new Date(o.created ?? Date.now()).toISOString().replace("Z", ""),
|
||||
updatedAt: new Date(o.updated ?? Date.now()).toISOString().replace("Z", ""),
|
||||
model: "workspace",
|
||||
name: o.name,
|
||||
variables: s ? O(s.data) : []
|
||||
name: r.name,
|
||||
variables: l ? h(l.data) : []
|
||||
});
|
||||
const p = n.resources.filter(
|
||||
(r) => f(r) && r.parentId === (s == null ? void 0 : s._id)
|
||||
const p = a.resources.filter(
|
||||
(i) => w(i) && i.parentId === (l == null ? void 0 : l._id)
|
||||
);
|
||||
t.environments.push(
|
||||
...p.map((r) => g(r, o._id))
|
||||
n.environments.push(
|
||||
...p.map((i) => b(i, r._id))
|
||||
);
|
||||
const l = (r) => {
|
||||
const u = n.resources.filter((i) => i.parentId === r);
|
||||
let c = 0;
|
||||
for (const i of u)
|
||||
I(i) ? (t.folders.push(R(i, o._id)), l(i._id)) : y(i) ? t.httpRequests.push(
|
||||
_(i, o._id, c++)
|
||||
) : h(i) && (console.log("GRPC", JSON.stringify(i, null, 1)), t.grpcRequests.push(
|
||||
D(i, o._id, c++)
|
||||
));
|
||||
const s = (i) => {
|
||||
const f = a.resources.filter((t) => t.parentId === i);
|
||||
let m = 0;
|
||||
for (const t of f)
|
||||
D(t) ? (n.folders.push(I(t, r._id)), s(t._id)) : v(t) ? n.httpRequests.push(
|
||||
g(t, r._id, m++)
|
||||
) : q(t) && n.grpcRequests.push(
|
||||
S(t, r._id, m++)
|
||||
);
|
||||
};
|
||||
l(o._id);
|
||||
s(r._id);
|
||||
}
|
||||
return t.httpRequests = t.httpRequests.filter(Boolean), t.grpcRequests = t.grpcRequests.filter(Boolean), t.environments = t.environments.filter(Boolean), t.workspaces = t.workspaces.filter(Boolean), { resources: t };
|
||||
return n.httpRequests = n.httpRequests.filter(Boolean), n.grpcRequests = n.grpcRequests.filter(Boolean), n.environments = n.environments.filter(Boolean), n.workspaces = n.workspaces.filter(Boolean), { resources: n };
|
||||
}
|
||||
function b(e, a) {
|
||||
return {
|
||||
id: d(e._id),
|
||||
createdAt: new Date(e.created ?? Date.now()).toISOString().replace("Z", ""),
|
||||
updatedAt: new Date(e.updated ?? Date.now()).toISOString().replace("Z", ""),
|
||||
workspaceId: d(a),
|
||||
model: "environment",
|
||||
name: e.name,
|
||||
variables: Object.entries(e.data).map(([n, o]) => ({
|
||||
enabled: !0,
|
||||
name: n,
|
||||
value: `${o}`
|
||||
}))
|
||||
};
|
||||
}
|
||||
function I(e, a) {
|
||||
return {
|
||||
id: d(e._id),
|
||||
createdAt: new Date(e.created ?? Date.now()).toISOString().replace("Z", ""),
|
||||
updatedAt: new Date(e.updated ?? Date.now()).toISOString().replace("Z", ""),
|
||||
folderId: e.parentId === a ? null : d(e.parentId),
|
||||
workspaceId: d(a),
|
||||
model: "folder",
|
||||
name: e.name
|
||||
};
|
||||
}
|
||||
function S(e, a, n = 0) {
|
||||
var p;
|
||||
const o = e.protoMethodName.split("/").filter((s) => s !== ""), r = o[0] ?? null, l = o[1] ?? null;
|
||||
return {
|
||||
id: d(e._id),
|
||||
createdAt: new Date(e.created ?? Date.now()).toISOString().replace("Z", ""),
|
||||
updatedAt: new Date(e.updated ?? Date.now()).toISOString().replace("Z", ""),
|
||||
workspaceId: d(a),
|
||||
folderId: e.parentId === a ? null : d(e.parentId),
|
||||
model: "grpc_request",
|
||||
sortPriority: n,
|
||||
name: e.name,
|
||||
url: u(e.url),
|
||||
service: r,
|
||||
method: l,
|
||||
message: ((p = e.body) == null ? void 0 : p.text) ?? "",
|
||||
metadata: (e.metadata ?? []).map((s) => ({
|
||||
enabled: !s.disabled,
|
||||
name: s.name ?? "",
|
||||
value: s.value ?? ""
|
||||
})).filter(({ name: s, value: i }) => s !== "" || i !== "")
|
||||
};
|
||||
}
|
||||
function g(e, a, n = 0) {
|
||||
var s, i, f, m;
|
||||
let o = null, r = {};
|
||||
e.body.mimeType === "application/octet-stream" ? (o = "binary", r = { filePath: e.body.fileName ?? "" }) : ((s = e.body) == null ? void 0 : s.mimeType) === "application/x-www-form-urlencoded" ? (o = "application/x-www-form-urlencoded", r = {
|
||||
form: (e.body.params ?? []).map((t) => ({
|
||||
enabled: !t.disabled,
|
||||
name: t.name ?? "",
|
||||
value: t.value ?? ""
|
||||
}))
|
||||
}) : ((i = e.body) == null ? void 0 : i.mimeType) === "multipart/form-data" ? (o = "multipart/form-data", r = {
|
||||
form: (e.body.params ?? []).map((t) => ({
|
||||
enabled: !t.disabled,
|
||||
name: t.name ?? "",
|
||||
value: t.value ?? "",
|
||||
file: t.fileName ?? null
|
||||
}))
|
||||
}) : ((f = e.body) == null ? void 0 : f.mimeType) === "application/graphql" ? (o = "graphql", r = { text: u(e.body.text ?? "") }) : ((m = e.body) == null ? void 0 : m.mimeType) === "application/json" && (o = "application/json", r = { text: u(e.body.text ?? "") });
|
||||
let l = null, p = {};
|
||||
return e.authentication.type === "bearer" ? (l = "bearer", p = {
|
||||
token: u(e.authentication.token)
|
||||
}) : e.authentication.type === "basic" && (l = "basic", p = {
|
||||
username: u(e.authentication.username),
|
||||
password: u(e.authentication.password)
|
||||
}), {
|
||||
id: d(e._id),
|
||||
createdAt: new Date(e.created ?? Date.now()).toISOString().replace("Z", ""),
|
||||
updatedAt: new Date(e.updated ?? Date.now()).toISOString().replace("Z", ""),
|
||||
workspaceId: d(a),
|
||||
folderId: e.parentId === a ? null : d(e.parentId),
|
||||
model: "http_request",
|
||||
sortPriority: n,
|
||||
name: e.name,
|
||||
url: u(e.url),
|
||||
body: r,
|
||||
bodyType: o,
|
||||
authentication: p,
|
||||
authenticationType: l,
|
||||
method: e.method,
|
||||
headers: (e.headers ?? []).map((t) => ({
|
||||
enabled: !t.disabled,
|
||||
name: t.name ?? "",
|
||||
value: t.value ?? ""
|
||||
})).filter(({ name: t, value: y }) => t !== "" || y !== "")
|
||||
};
|
||||
}
|
||||
function h(e) {
|
||||
return Object.entries(e).map(([a, n]) => ({
|
||||
enabled: !0,
|
||||
name: a,
|
||||
value: `${n}`
|
||||
}));
|
||||
}
|
||||
function u(e) {
|
||||
return O(e) ? e.replaceAll(/{{\s*(_\.)?([^}]+)\s*}}/g, "${[$2]}") : e;
|
||||
}
|
||||
function _(e) {
|
||||
return c(e) && e._type === "workspace";
|
||||
}
|
||||
function D(e) {
|
||||
return c(e) && e._type === "request_group";
|
||||
}
|
||||
function v(e) {
|
||||
return c(e) && e._type === "request";
|
||||
}
|
||||
function q(e) {
|
||||
return c(e) && e._type === "grpc_request";
|
||||
}
|
||||
function w(e) {
|
||||
return c(e) && e._type === "environment";
|
||||
}
|
||||
function c(e) {
|
||||
return Object.prototype.toString.call(e) === "[object Object]";
|
||||
}
|
||||
function O(e) {
|
||||
return Object.prototype.toString.call(e) === "[object String]";
|
||||
}
|
||||
function d(e) {
|
||||
return e.startsWith("GENERATE_ID::") ? e : `GENERATE_ID::${e}`;
|
||||
}
|
||||
export {
|
||||
q as pluginHookImport
|
||||
A as pluginHookImport
|
||||
};
|
||||
|
||||
@@ -1,92 +1,92 @@
|
||||
const q = "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", S = "https://schema.getpostman.com/json/collection/v2.0.0/collection.json", _ = [S, q];
|
||||
function j(t) {
|
||||
const S = "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", _ = "https://schema.getpostman.com/json/collection/v2.0.0/collection.json", O = [_, S];
|
||||
function v(e) {
|
||||
var g;
|
||||
const e = A(t);
|
||||
if (e == null)
|
||||
const t = k(e);
|
||||
if (t == null)
|
||||
return;
|
||||
const a = l(e.info);
|
||||
if (!_.includes(a.schema) || !Array.isArray(e.item))
|
||||
const o = i(t.info);
|
||||
if (!O.includes(o.schema) || !Array.isArray(t.item))
|
||||
return;
|
||||
const c = w(e.auth), s = {
|
||||
const u = A(t.auth), s = {
|
||||
workspaces: [],
|
||||
environments: [],
|
||||
httpRequests: [],
|
||||
folders: []
|
||||
}, n = {
|
||||
model: "workspace",
|
||||
id: y("wk"),
|
||||
name: a.name || "Postman Import",
|
||||
description: a.description || "",
|
||||
variables: ((g = e.variable) == null ? void 0 : g.map((r) => ({
|
||||
id: h("workspace"),
|
||||
name: o.name || "Postman Import",
|
||||
description: o.description || "",
|
||||
variables: ((g = t.variable) == null ? void 0 : g.map((r) => ({
|
||||
name: r.key,
|
||||
value: r.value
|
||||
}))) ?? []
|
||||
};
|
||||
s.workspaces.push(n);
|
||||
const f = (r, d = null) => {
|
||||
const T = (r, p = null) => {
|
||||
if (typeof r.name == "string" && Array.isArray(r.item)) {
|
||||
const o = {
|
||||
const a = {
|
||||
model: "folder",
|
||||
workspaceId: n.id,
|
||||
id: y("fl"),
|
||||
id: h("folder"),
|
||||
name: r.name,
|
||||
folderId: d
|
||||
folderId: p
|
||||
};
|
||||
s.folders.push(o);
|
||||
for (const u of r.item)
|
||||
f(u, o.id);
|
||||
s.folders.push(a);
|
||||
for (const l of r.item)
|
||||
T(l, a.id);
|
||||
} else if (typeof r.name == "string" && "request" in r) {
|
||||
const o = l(r.request), u = O(o.body), T = w(o.auth), p = T.authenticationType == null ? c : T, k = {
|
||||
const a = i(r.request), l = j(a.body), w = A(a.auth), d = w.authenticationType == null ? u : w, q = {
|
||||
model: "http_request",
|
||||
id: y("rq"),
|
||||
id: h("http_request"),
|
||||
workspaceId: n.id,
|
||||
folderId: d,
|
||||
folderId: p,
|
||||
name: r.name,
|
||||
method: o.method || "GET",
|
||||
url: typeof o.url == "string" ? o.url : l(o.url).raw,
|
||||
body: u.body,
|
||||
bodyType: u.bodyType,
|
||||
authentication: p.authentication,
|
||||
authenticationType: p.authenticationType,
|
||||
method: a.method || "GET",
|
||||
url: typeof a.url == "string" ? a.url : i(a.url).raw,
|
||||
body: l.body,
|
||||
bodyType: l.bodyType,
|
||||
authentication: d.authentication,
|
||||
authenticationType: d.authenticationType,
|
||||
headers: [
|
||||
...u.headers,
|
||||
...p.headers,
|
||||
...h(o.header).map((m) => ({
|
||||
...l.headers,
|
||||
...d.headers,
|
||||
...b(a.header).map((m) => ({
|
||||
name: m.key,
|
||||
value: m.value,
|
||||
enabled: !m.disabled
|
||||
}))
|
||||
]
|
||||
};
|
||||
s.httpRequests.push(k);
|
||||
s.httpRequests.push(q);
|
||||
} else
|
||||
console.log("Unknown item", r, d);
|
||||
console.log("Unknown item", r, p);
|
||||
};
|
||||
for (const r of e.item)
|
||||
f(r);
|
||||
return { resources: b(s) };
|
||||
for (const r of t.item)
|
||||
T(r);
|
||||
return { resources: f(s) };
|
||||
}
|
||||
function w(t) {
|
||||
const e = l(t);
|
||||
return "basic" in e ? {
|
||||
function A(e) {
|
||||
const t = i(e);
|
||||
return "basic" in t ? {
|
||||
headers: [],
|
||||
authenticationType: "basic",
|
||||
authentication: {
|
||||
username: e.basic.username || "",
|
||||
password: e.basic.password || ""
|
||||
username: t.basic.username || "",
|
||||
password: t.basic.password || ""
|
||||
}
|
||||
} : "bearer" in e ? {
|
||||
} : "bearer" in t ? {
|
||||
headers: [],
|
||||
authenticationType: "bearer",
|
||||
authentication: {
|
||||
token: e.bearer.token || ""
|
||||
token: t.bearer.token || ""
|
||||
}
|
||||
} : { headers: [], authenticationType: null, authentication: {} };
|
||||
}
|
||||
function O(t) {
|
||||
var a, i, c, s;
|
||||
const e = l(t);
|
||||
return "graphql" in e ? {
|
||||
function j(e) {
|
||||
var o, c, u, s;
|
||||
const t = i(e);
|
||||
return "graphql" in t ? {
|
||||
headers: [
|
||||
{
|
||||
name: "Content-Type",
|
||||
@@ -97,12 +97,12 @@ function O(t) {
|
||||
bodyType: "graphql",
|
||||
body: {
|
||||
text: JSON.stringify(
|
||||
{ query: e.graphql.query, variables: A(e.graphql.variables) },
|
||||
{ query: t.graphql.query, variables: k(t.graphql.variables) },
|
||||
null,
|
||||
2
|
||||
)
|
||||
}
|
||||
} : "urlencoded" in e ? {
|
||||
} : "urlencoded" in t ? {
|
||||
headers: [
|
||||
{
|
||||
name: "Content-Type",
|
||||
@@ -112,13 +112,13 @@ function O(t) {
|
||||
],
|
||||
bodyType: "application/x-www-form-urlencoded",
|
||||
body: {
|
||||
form: h(e.urlencoded).map((n) => ({
|
||||
form: b(t.urlencoded).map((n) => ({
|
||||
enabled: !n.disabled,
|
||||
name: n.key ?? "",
|
||||
value: n.value ?? ""
|
||||
}))
|
||||
}
|
||||
} : "formdata" in e ? {
|
||||
} : "formdata" in t ? {
|
||||
headers: [
|
||||
{
|
||||
name: "Content-Type",
|
||||
@@ -128,7 +128,7 @@ function O(t) {
|
||||
],
|
||||
bodyType: "multipart/form-data",
|
||||
body: {
|
||||
form: h(e.formdata).map(
|
||||
form: b(t.formdata).map(
|
||||
(n) => n.src != null ? {
|
||||
enabled: !n.disabled,
|
||||
name: n.key ?? "",
|
||||
@@ -140,46 +140,42 @@ function O(t) {
|
||||
}
|
||||
)
|
||||
}
|
||||
} : "raw" in e ? {
|
||||
} : "raw" in t ? {
|
||||
headers: [
|
||||
{
|
||||
name: "Content-Type",
|
||||
value: ((i = (a = e.options) == null ? void 0 : a.raw) == null ? void 0 : i.language) === "json" ? "application/json" : "",
|
||||
value: ((c = (o = t.options) == null ? void 0 : o.raw) == null ? void 0 : c.language) === "json" ? "application/json" : "",
|
||||
enabled: !0
|
||||
}
|
||||
],
|
||||
bodyType: ((s = (c = e.options) == null ? void 0 : c.raw) == null ? void 0 : s.language) === "json" ? "application/json" : "other",
|
||||
bodyType: ((s = (u = t.options) == null ? void 0 : u.raw) == null ? void 0 : s.language) === "json" ? "application/json" : "other",
|
||||
body: {
|
||||
text: e.raw ?? ""
|
||||
text: t.raw ?? ""
|
||||
}
|
||||
} : { headers: [], bodyType: null, body: {} };
|
||||
}
|
||||
function A(t) {
|
||||
function k(e) {
|
||||
try {
|
||||
return l(JSON.parse(t));
|
||||
return i(JSON.parse(e));
|
||||
} catch {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
function l(t) {
|
||||
return Object.prototype.toString.call(t) === "[object Object]" ? t : {};
|
||||
function i(e) {
|
||||
return Object.prototype.toString.call(e) === "[object Object]" ? e : {};
|
||||
}
|
||||
function h(t) {
|
||||
return Object.prototype.toString.call(t) === "[object Array]" ? t : [];
|
||||
function b(e) {
|
||||
return Object.prototype.toString.call(e) === "[object Array]" ? e : [];
|
||||
}
|
||||
function b(t) {
|
||||
return typeof t == "string" ? t.replace(/{{\s*(_\.)?([^}]+)\s*}}/g, "${[$2]}") : Array.isArray(t) && t != null ? t.map(b) : typeof t == "object" && t != null ? Object.fromEntries(
|
||||
Object.entries(t).map(([e, a]) => [e, b(a)])
|
||||
) : t;
|
||||
function f(e) {
|
||||
return typeof e == "string" ? e.replace(/{{\s*(_\.)?([^}]+)\s*}}/g, "${[$2]}") : Array.isArray(e) && e != null ? e.map(f) : typeof e == "object" && e != null ? Object.fromEntries(
|
||||
Object.entries(e).map(([t, o]) => [t, f(o)])
|
||||
) : e;
|
||||
}
|
||||
function y(t) {
|
||||
const e = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
let a = `${t}_`;
|
||||
for (let i = 0; i < 10; i++)
|
||||
a += e[Math.floor(Math.random() * e.length)];
|
||||
return a;
|
||||
const y = {};
|
||||
function h(e) {
|
||||
return y[e] = (y[e] ?? -1) + 1, `GENERATE_ID::${e.toUpperCase()}_${y[e]}`;
|
||||
}
|
||||
export {
|
||||
y as generateId,
|
||||
j as pluginHookImport
|
||||
v as pluginHookImport
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user