mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-20 15:51:23 +02:00
Some postman import improvements
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
const m = 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json';
|
||||
function b(e) {
|
||||
const r = p(e);
|
||||
if (r == null) return;
|
||||
const i = s(r.info);
|
||||
if (i.schema !== m || !Array.isArray(r.item)) return;
|
||||
const n = {
|
||||
const y = 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json',
|
||||
f = 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json',
|
||||
b = [f, y];
|
||||
function T(e) {
|
||||
const t = h(e);
|
||||
if (t == null) return;
|
||||
const i = s(t.info);
|
||||
if (!b.includes(i.schema) || !Array.isArray(t.item)) return;
|
||||
const r = {
|
||||
workspaces: [],
|
||||
environments: [],
|
||||
requests: [],
|
||||
@@ -16,48 +18,66 @@ function b(e) {
|
||||
name: i.name || 'Postman Import',
|
||||
description: i.description || '',
|
||||
};
|
||||
n.workspaces.push(c);
|
||||
const u = (o, l = null) => {
|
||||
r.workspaces.push(c);
|
||||
const d = (o, l = null) => {
|
||||
if (typeof o.name == 'string' && Array.isArray(o.item)) {
|
||||
const t = {
|
||||
const n = {
|
||||
model: 'folder',
|
||||
workspaceId: c.id,
|
||||
id: `fld_${n.folders.length}`,
|
||||
id: `fld_${r.folders.length}`,
|
||||
name: o.name,
|
||||
folderId: l,
|
||||
};
|
||||
n.folders.push(t);
|
||||
for (const a of o.item) u(a, t.id);
|
||||
r.folders.push(n);
|
||||
for (const a of o.item) d(a, n.id);
|
||||
} else if (typeof o.name == 'string' && 'request' in o) {
|
||||
const t = s(o.request),
|
||||
a = f(t.body),
|
||||
d = {
|
||||
const n = s(o.request),
|
||||
a = q(n.body),
|
||||
u = g(n.auth),
|
||||
m = {
|
||||
model: 'http_request',
|
||||
id: `req_${n.requests.length}`,
|
||||
id: `req_${r.requests.length}`,
|
||||
workspaceId: c.id,
|
||||
folderId: l,
|
||||
name: o.name,
|
||||
method: t.method || 'GET',
|
||||
url: s(t.url).raw,
|
||||
headers: [...a.headers, ...h(t.header).map(y)],
|
||||
method: n.method || 'GET',
|
||||
url: typeof n.url == 'string' ? n.url : s(n.url).raw,
|
||||
body: a.body,
|
||||
bodyType: a.bodyType,
|
||||
// TODO: support auth
|
||||
// ...importAuth(r.auth),
|
||||
authentication: u.authentication,
|
||||
authenticationType: u.authenticationType,
|
||||
headers: [
|
||||
...a.headers,
|
||||
...u.headers,
|
||||
...A(n.header).map((p) => ({
|
||||
name: p.key,
|
||||
value: p.value,
|
||||
enabled: !p.disabled,
|
||||
})),
|
||||
],
|
||||
};
|
||||
n.requests.push(d);
|
||||
r.requests.push(m);
|
||||
} else console.log('Unknown item', o, l);
|
||||
};
|
||||
for (const o of r.item) u(o);
|
||||
return { resources: n };
|
||||
for (const o of t.item) d(o);
|
||||
return { resources: r };
|
||||
}
|
||||
function y(e) {
|
||||
const r = s(e);
|
||||
return { name: r.key, value: r.value, enabled: !0 };
|
||||
function g(e) {
|
||||
const t = s(e);
|
||||
return 'basic' in t
|
||||
? {
|
||||
headers: [],
|
||||
authenticationType: 'basic',
|
||||
authentication: {
|
||||
username: t.basic.username || '',
|
||||
password: t.basic.password || '',
|
||||
},
|
||||
}
|
||||
: { headers: [], authenticationType: null, authentication: {} };
|
||||
}
|
||||
function f(e) {
|
||||
const r = s(e);
|
||||
return 'graphql' in r
|
||||
function q(e) {
|
||||
const t = s(e);
|
||||
return 'graphql' in t
|
||||
? {
|
||||
headers: [
|
||||
{
|
||||
@@ -68,14 +88,14 @@ function f(e) {
|
||||
],
|
||||
bodyType: 'graphql',
|
||||
body: JSON.stringify(
|
||||
{ query: r.graphql.query, variables: p(r.graphql.variables) },
|
||||
{ query: t.graphql.query, variables: h(t.graphql.variables) },
|
||||
null,
|
||||
2,
|
||||
),
|
||||
}
|
||||
: { bodyType: null, body: null };
|
||||
: { headers: [], bodyType: null, body: null };
|
||||
}
|
||||
function p(e) {
|
||||
function h(e) {
|
||||
try {
|
||||
return s(JSON.parse(e));
|
||||
} catch {}
|
||||
@@ -84,7 +104,7 @@ function p(e) {
|
||||
function s(e) {
|
||||
return Object.prototype.toString.call(e) === '[object Object]' ? e : {};
|
||||
}
|
||||
function h(e) {
|
||||
function A(e) {
|
||||
return Object.prototype.toString.call(e) === '[object Array]' ? e : [];
|
||||
}
|
||||
export { b as pluginHookImport };
|
||||
export { T as pluginHookImport };
|
||||
|
||||
Reference in New Issue
Block a user