mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-23 18:01:08 +01:00
Some postman import improvements
This commit is contained in:
@@ -8,10 +8,10 @@ function S(e, t) {
|
||||
workspaceId: t,
|
||||
model: 'environment',
|
||||
name: e.name,
|
||||
variables: Object.entries(e.data).map(([n, s]) => ({
|
||||
variables: Object.entries(e.data).map(([n, a]) => ({
|
||||
enabled: !0,
|
||||
name: n,
|
||||
value: `${s}`,
|
||||
value: `${a}`,
|
||||
})),
|
||||
}
|
||||
);
|
||||
@@ -19,10 +19,10 @@ function S(e, t) {
|
||||
function I(e) {
|
||||
return m(e) && e._type === 'workspace';
|
||||
}
|
||||
function g(e) {
|
||||
function y(e) {
|
||||
return m(e) && e._type === 'request_group';
|
||||
}
|
||||
function y(e) {
|
||||
function g(e) {
|
||||
return m(e) && e._type === 'request';
|
||||
}
|
||||
function f(e) {
|
||||
@@ -31,41 +31,41 @@ function f(e) {
|
||||
function m(e) {
|
||||
return Object.prototype.toString.call(e) === '[object Object]';
|
||||
}
|
||||
function O(e) {
|
||||
function w(e) {
|
||||
return Object.prototype.toString.call(e) === '[object String]';
|
||||
}
|
||||
function _(e) {
|
||||
function O(e) {
|
||||
return Object.entries(e).map(([t, n]) => ({
|
||||
enabled: !0,
|
||||
name: t,
|
||||
value: `${n}`,
|
||||
}));
|
||||
}
|
||||
function d(e) {
|
||||
return O(e) ? e.replaceAll(/{{\s*(_\.)?([^}]+)\s*}}/g, '${[$2]}') : e;
|
||||
function l(e) {
|
||||
return w(e) ? e.replaceAll(/{{\s*(_\.)?([^}]+)\s*}}/g, '${[$2]}') : e;
|
||||
}
|
||||
function h(e, t, n = 0) {
|
||||
var u, r;
|
||||
var c, o;
|
||||
console.log('IMPORTING REQUEST', e._id, e.name, JSON.stringify(e, null, 2));
|
||||
let s = null,
|
||||
o = null;
|
||||
((u = e.body) == null ? void 0 : u.mimeType) === 'application/graphql'
|
||||
? ((s = 'graphql'), (o = d(e.body.text)))
|
||||
: ((r = e.body) == null ? void 0 : r.mimeType) === 'application/json' &&
|
||||
((s = 'application/json'), (o = d(e.body.text)));
|
||||
let a = null,
|
||||
l = {};
|
||||
r = null;
|
||||
((c = e.body) == null ? void 0 : c.mimeType) === 'application/graphql'
|
||||
? ((a = 'graphql'), (r = l(e.body.text)))
|
||||
: ((o = e.body) == null ? void 0 : o.mimeType) === 'application/json' &&
|
||||
((a = 'application/json'), (r = l(e.body.text)));
|
||||
let i = null,
|
||||
u = {};
|
||||
return (
|
||||
e.authentication.type === 'bearer'
|
||||
? ((a = 'bearer'),
|
||||
(l = {
|
||||
token: d(e.authentication.token),
|
||||
? ((i = 'bearer'),
|
||||
(u = {
|
||||
token: l(e.authentication.token),
|
||||
}))
|
||||
: e.authentication.type === 'basic' &&
|
||||
((a = 'basic'),
|
||||
(l = {
|
||||
username: d(e.authentication.username),
|
||||
password: d(e.authentication.password),
|
||||
((i = 'basic'),
|
||||
(u = {
|
||||
username: l(e.authentication.username),
|
||||
password: l(e.authentication.password),
|
||||
})),
|
||||
{
|
||||
id: e._id,
|
||||
@@ -76,36 +76,23 @@ function h(e, t, n = 0) {
|
||||
model: 'http_request',
|
||||
sortPriority: n,
|
||||
name: e.name,
|
||||
url: d(e.url),
|
||||
body: o,
|
||||
bodyType: s,
|
||||
authentication: l,
|
||||
authenticationType: a,
|
||||
url: l(e.url),
|
||||
body: r,
|
||||
bodyType: a,
|
||||
authentication: u,
|
||||
authenticationType: i,
|
||||
method: e.method,
|
||||
headers: (e.headers ?? [])
|
||||
.map(({ name: c, value: p, disabled: i }) => ({
|
||||
enabled: !i,
|
||||
name: c,
|
||||
.map(({ name: d, value: p, disabled: s }) => ({
|
||||
enabled: !s,
|
||||
name: d,
|
||||
value: p,
|
||||
}))
|
||||
.filter(({ name: c, value: p }) => c !== '' || p !== ''),
|
||||
.filter(({ name: d, value: p }) => d !== '' || p !== ''),
|
||||
}
|
||||
);
|
||||
}
|
||||
function w(e, t) {
|
||||
return (
|
||||
console.log('IMPORTING Workspace', 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', ''),
|
||||
model: 'workspace',
|
||||
name: e.name,
|
||||
variables: t,
|
||||
}
|
||||
);
|
||||
}
|
||||
function D(e, t) {
|
||||
function _(e, t) {
|
||||
return (
|
||||
console.log('IMPORTING FOLDER', e._id, e.name, JSON.stringify(e, null, 2)),
|
||||
{
|
||||
@@ -120,32 +107,40 @@ function D(e, t) {
|
||||
);
|
||||
}
|
||||
function b(e) {
|
||||
console.log('RUNNING INSOMNIA');
|
||||
let t;
|
||||
try {
|
||||
t = JSON.parse(e);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
if (!m(t)) return;
|
||||
if (!m(t) || !Array.isArray(t.resources)) return;
|
||||
const n = {
|
||||
workspaces: [],
|
||||
requests: [],
|
||||
environments: [],
|
||||
folders: [],
|
||||
},
|
||||
s = t.resources.filter(I);
|
||||
for (const o of s) {
|
||||
const a = t.resources.find((r) => f(r) && r.parentId === o._id);
|
||||
n.workspaces.push(w(o, a ? _(a.data) : []));
|
||||
const l = t.resources.filter((r) => f(r) && r.parentId === (a == null ? void 0 : a._id));
|
||||
n.environments.push(...l.map((r) => S(r, o._id)));
|
||||
const u = (r) => {
|
||||
const c = t.resources.filter((i) => i.parentId === r);
|
||||
a = t.resources.filter(I);
|
||||
for (const r of a) {
|
||||
const i = t.resources.find((o) => f(o) && o.parentId === r._id);
|
||||
n.workspaces.push({
|
||||
id: r._id,
|
||||
createdAt: new Date(a.created ?? Date.now()).toISOString().replace('Z', ''),
|
||||
updatedAt: new Date(a.updated ?? Date.now()).toISOString().replace('Z', ''),
|
||||
model: 'workspace',
|
||||
name: r.name,
|
||||
variables: i ? O(i.data) : [],
|
||||
});
|
||||
const u = t.resources.filter((o) => f(o) && o.parentId === (i == null ? void 0 : i._id));
|
||||
n.environments.push(...u.map((o) => S(o, r._id)));
|
||||
const c = (o) => {
|
||||
const d = t.resources.filter((s) => s.parentId === o);
|
||||
let p = 0;
|
||||
for (const i of c)
|
||||
g(i) ? (n.folders.push(D(i, o._id)), u(i._id)) : y(i) && n.requests.push(h(i, o._id, p++));
|
||||
for (const s of d)
|
||||
y(s) ? (n.folders.push(_(s, r._id)), c(s._id)) : g(s) && n.requests.push(h(s, r._id, p++));
|
||||
};
|
||||
u(o._id);
|
||||
c(r._id);
|
||||
}
|
||||
return (
|
||||
(n.requests = n.requests.filter(Boolean)),
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
/**
|
||||
* Import an Insomnia workspace object.
|
||||
* @param {Object} w - The workspace object to import.
|
||||
*/
|
||||
export function importWorkspace(w, variables) {
|
||||
console.log('IMPORTING Workspace', w._id, w.name, JSON.stringify(w, null, 2));
|
||||
return {
|
||||
id: w._id,
|
||||
createdAt: new Date(w.created ?? Date.now()).toISOString().replace('Z', ''),
|
||||
updatedAt: new Date(w.updated ?? Date.now()).toISOString().replace('Z', ''),
|
||||
model: 'workspace',
|
||||
name: w.name,
|
||||
variables,
|
||||
};
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
import { importEnvironment } from './importers/environment.js';
|
||||
import { importRequest } from './importers/request.js';
|
||||
import { importWorkspace } from './importers/workspace.js';
|
||||
import {
|
||||
isEnvironment,
|
||||
isJSObject,
|
||||
@@ -12,16 +11,16 @@ import { parseVariables } from './helpers/variables.js';
|
||||
import { importFolder } from './importers/folder.js';
|
||||
|
||||
export function pluginHookImport(contents) {
|
||||
console.log('RUNNING INSOMNIA');
|
||||
let parsed;
|
||||
try {
|
||||
parsed = JSON.parse(contents);
|
||||
} catch (e) {
|
||||
return undefined;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isJSObject(parsed)) {
|
||||
return undefined;
|
||||
}
|
||||
if (!isJSObject(parsed)) return;
|
||||
if (!Array.isArray(parsed.resources)) return;
|
||||
|
||||
const resources = {
|
||||
workspaces: [],
|
||||
@@ -36,12 +35,14 @@ export function pluginHookImport(contents) {
|
||||
const baseEnvironment = parsed.resources.find(
|
||||
(r) => isEnvironment(r) && r.parentId === workspaceToImport._id,
|
||||
);
|
||||
resources.workspaces.push(
|
||||
importWorkspace(
|
||||
workspaceToImport,
|
||||
baseEnvironment ? parseVariables(baseEnvironment.data) : [],
|
||||
),
|
||||
);
|
||||
resources.workspaces.push({
|
||||
id: workspaceToImport._id,
|
||||
createdAt: new Date(workspacesToImport.created ?? Date.now()).toISOString().replace('Z', ''),
|
||||
updatedAt: new Date(workspacesToImport.updated ?? Date.now()).toISOString().replace('Z', ''),
|
||||
model: 'workspace',
|
||||
name: workspaceToImport.name,
|
||||
variables: baseEnvironment ? parseVariables(baseEnvironment.data) : [],
|
||||
});
|
||||
const environmentsToImport = parsed.resources.filter(
|
||||
(r) => isEnvironment(r) && r.parentId === baseEnvironment?._id,
|
||||
);
|
||||
|
||||
@@ -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 };
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { Environment, Folder, HttpRequest, Workspace } from '../../../../src-web/lib/models';
|
||||
|
||||
const POSTMAN_2_1_0_SCHEMA = 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json';
|
||||
const POSTMAN_2_0_0_SCHEMA = 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json';
|
||||
const VALID_SCHEMAS = [POSTMAN_2_0_0_SCHEMA, POSTMAN_2_1_0_SCHEMA];
|
||||
|
||||
type AtLeast<T, K extends keyof T> = Partial<T> & Pick<T, K>;
|
||||
|
||||
@@ -16,7 +18,8 @@ export function pluginHookImport(contents: string): { resources: ExportResources
|
||||
if (root == null) return;
|
||||
|
||||
const info = toRecord(root.info);
|
||||
if (info.schema !== POSTMAN_2_1_0_SCHEMA || !Array.isArray(root.item)) {
|
||||
const isValidSchema = VALID_SCHEMAS.includes(info.schema);
|
||||
if (!isValidSchema || !Array.isArray(root.item)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -51,6 +54,7 @@ export function pluginHookImport(contents: string): { resources: ExportResources
|
||||
} else if (typeof v.name === 'string' && 'request' in v) {
|
||||
const r = toRecord(v.request);
|
||||
const bodyPatch = importBody(r.body);
|
||||
const authPatch = importAuth(r.auth);
|
||||
const request: ExportResources['requests'][0] = {
|
||||
model: 'http_request',
|
||||
id: `req_${exportResources.requests.length}`,
|
||||
@@ -58,13 +62,22 @@ export function pluginHookImport(contents: string): { resources: ExportResources
|
||||
folderId,
|
||||
name: v.name,
|
||||
method: r.method || 'GET',
|
||||
url: toRecord(r.url).raw,
|
||||
headers: [...bodyPatch.headers, ...toArray(r.header).map(importHeader)],
|
||||
url: typeof r.url === 'string' ? r.url : toRecord(r.url).raw,
|
||||
body: bodyPatch.body,
|
||||
bodyType: bodyPatch.bodyType,
|
||||
|
||||
// TODO: support auth
|
||||
// ...importAuth(r.auth),
|
||||
authentication: authPatch.authentication,
|
||||
authenticationType: authPatch.authenticationType,
|
||||
headers: [
|
||||
...bodyPatch.headers,
|
||||
...authPatch.headers,
|
||||
...toArray(r.header).map((h) => {
|
||||
return {
|
||||
name: h.key,
|
||||
value: h.value,
|
||||
enabled: !h.disabled,
|
||||
};
|
||||
}),
|
||||
],
|
||||
};
|
||||
exportResources.requests.push(request);
|
||||
} else {
|
||||
@@ -79,10 +92,23 @@ export function pluginHookImport(contents: string): { resources: ExportResources
|
||||
return { resources: exportResources };
|
||||
}
|
||||
|
||||
function importHeader(h: any): HttpRequest['headers'][0] {
|
||||
const header = toRecord(h);
|
||||
// TODO: support header
|
||||
return { name: header.key, value: header.value, enabled: true };
|
||||
function importAuth(
|
||||
rawAuth: any,
|
||||
): Pick<HttpRequest, 'authentication' | 'authenticationType' | 'headers'> {
|
||||
const auth = toRecord(rawAuth);
|
||||
if ('basic' in auth) {
|
||||
return {
|
||||
headers: [],
|
||||
authenticationType: 'basic',
|
||||
authentication: {
|
||||
username: auth.basic.username || '',
|
||||
password: auth.basic.password || '',
|
||||
},
|
||||
};
|
||||
} else {
|
||||
// TODO: support other auth types
|
||||
return { headers: [], authenticationType: null, authentication: {} };
|
||||
}
|
||||
}
|
||||
|
||||
function importBody(rawBody: any): Pick<HttpRequest, 'body' | 'bodyType' | 'headers'> {
|
||||
|
||||
Reference in New Issue
Block a user