mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-11 03:26:58 +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 };
|
||||
|
||||
@@ -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