Postman bearer, global auth, global vars

This commit is contained in:
Gregory Schier
2024-03-20 07:26:46 -07:00
parent ab6cef064c
commit 41060d5d43
2 changed files with 72 additions and 46 deletions

View File

@@ -23,6 +23,8 @@ export function pluginHookImport(contents: string): { resources: ExportResources
return; return;
} }
const globalAuth = importAuth(root.auth);
const exportResources: ExportResources = { const exportResources: ExportResources = {
workspaces: [], workspaces: [],
environments: [], environments: [],
@@ -35,6 +37,10 @@ export function pluginHookImport(contents: string): { resources: ExportResources
id: generateId('wk'), id: generateId('wk'),
name: info.name || 'Postman Import', name: info.name || 'Postman Import',
description: info.description || '', description: info.description || '',
variables: root.variable?.map((v: any) => ({
name: v.key,
value: v.value,
})),
}; };
exportResources.workspaces.push(workspace); exportResources.workspaces.push(workspace);
@@ -54,7 +60,8 @@ export function pluginHookImport(contents: string): { resources: ExportResources
} else if (typeof v.name === 'string' && 'request' in v) { } else if (typeof v.name === 'string' && 'request' in v) {
const r = toRecord(v.request); const r = toRecord(v.request);
const bodyPatch = importBody(r.body); const bodyPatch = importBody(r.body);
const authPatch = importAuth(r.auth); const requestAuthPath = importAuth(r.auth);
const authPatch = requestAuthPath.authenticationType == null ? globalAuth : requestAuthPath;
const request: ExportResources['httpRequests'][0] = { const request: ExportResources['httpRequests'][0] = {
model: 'http_request', model: 'http_request',
id: generateId('rq'), id: generateId('rq'),
@@ -105,6 +112,14 @@ function importAuth(
password: auth.basic.password || '', password: auth.basic.password || '',
}, },
}; };
} else if ('bearer' in auth) {
return {
headers: [],
authenticationType: 'bearer',
authentication: {
token: auth.bearer.token || '',
},
};
} else { } else {
// TODO: support other auth types // TODO: support other auth types
return { headers: [], authenticationType: null, authentication: {} }; return { headers: [], authenticationType: null, authentication: {} };

View File

@@ -1,12 +1,13 @@
const T = "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", w = "https://schema.getpostman.com/json/collection/v2.0.0/collection.json", A = [w, T]; 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 q(e) { function v(t) {
const t = b(e); var b;
if (t == null) const e = w(t);
if (e == null)
return; return;
const n = a(t.info); const n = o(e.info);
if (!A.includes(n.schema) || !Array.isArray(t.item)) if (!_.includes(n.schema) || !Array.isArray(e.item))
return; return;
const i = { const A = g(e.auth), i = {
workspaces: [], workspaces: [],
environments: [], environments: [],
httpRequests: [], httpRequests: [],
@@ -15,30 +16,34 @@ function q(e) {
model: "workspace", model: "workspace",
id: m("wk"), id: m("wk"),
name: n.name || "Postman Import", name: n.name || "Postman Import",
description: n.description || "" description: n.description || "",
variables: (b = e.variable) == null ? void 0 : b.map((r) => ({
name: r.key,
value: r.value
}))
}; };
i.workspaces.push(c); i.workspaces.push(c);
const f = (r, u = null) => { const f = (r, u = null) => {
if (typeof r.name == "string" && Array.isArray(r.item)) { if (typeof r.name == "string" && Array.isArray(r.item)) {
const o = { const a = {
model: "folder", model: "folder",
workspaceId: c.id, workspaceId: c.id,
id: m("fl"), id: m("fl"),
name: r.name, name: r.name,
folderId: u folderId: u
}; };
i.folders.push(o); i.folders.push(a);
for (const s of r.item) for (const s of r.item)
f(s, o.id); f(s, a.id);
} else if (typeof r.name == "string" && "request" in r) { } else if (typeof r.name == "string" && "request" in r) {
const o = a(r.request), s = k(o.body), d = S(o.auth), g = { const a = o(r.request), s = O(a.body), T = g(a.auth), d = T.authenticationType == null ? A : T, k = {
model: "http_request", model: "http_request",
id: m("rq"), id: m("rq"),
workspaceId: c.id, workspaceId: c.id,
folderId: u, folderId: u,
name: r.name, name: r.name,
method: o.method || "GET", method: a.method || "GET",
url: typeof o.url == "string" ? o.url : a(o.url).raw, url: typeof a.url == "string" ? a.url : o(a.url).raw,
body: s.body, body: s.body,
bodyType: s.bodyType, bodyType: s.bodyType,
authentication: d.authentication, authentication: d.authentication,
@@ -46,35 +51,41 @@ function q(e) {
headers: [ headers: [
...s.headers, ...s.headers,
...d.headers, ...d.headers,
...y(o.header).map((p) => ({ ...y(a.header).map((p) => ({
name: p.key, name: p.key,
value: p.value, value: p.value,
enabled: !p.disabled enabled: !p.disabled
})) }))
] ]
}; };
i.httpRequests.push(g); i.httpRequests.push(k);
} else } else
console.log("Unknown item", r, u); console.log("Unknown item", r, u);
}; };
for (const r of t.item) for (const r of e.item)
f(r); f(r);
return { resources: h(i) }; return { resources: h(i) };
} }
function S(e) { function g(t) {
const t = a(e); const e = o(t);
return "basic" in t ? { return "basic" in e ? {
headers: [], headers: [],
authenticationType: "basic", authenticationType: "basic",
authentication: { authentication: {
username: t.basic.username || "", username: e.basic.username || "",
password: t.basic.password || "" password: e.basic.password || ""
}
} : "bearer" in e ? {
headers: [],
authenticationType: "bearer",
authentication: {
token: e.bearer.token || ""
} }
} : { headers: [], authenticationType: null, authentication: {} }; } : { headers: [], authenticationType: null, authentication: {} };
} }
function k(e) { function O(t) {
const t = a(e); const e = o(t);
return "graphql" in t ? { return "graphql" in e ? {
headers: [ headers: [
{ {
name: "Content-Type", name: "Content-Type",
@@ -85,12 +96,12 @@ function k(e) {
bodyType: "graphql", bodyType: "graphql",
body: { body: {
text: JSON.stringify( text: JSON.stringify(
{ query: t.graphql.query, variables: b(t.graphql.variables) }, { query: e.graphql.query, variables: w(e.graphql.variables) },
null, null,
2 2
) )
} }
} : "urlencoded" in t ? { } : "urlencoded" in e ? {
headers: [ headers: [
{ {
name: "Content-Type", name: "Content-Type",
@@ -100,13 +111,13 @@ function k(e) {
], ],
bodyType: "application/x-www-form-urlencoded", bodyType: "application/x-www-form-urlencoded",
body: { body: {
form: y(t.urlencoded).map((n) => ({ form: y(e.urlencoded).map((n) => ({
enabled: !n.disabled, enabled: !n.disabled,
name: n.key ?? "", name: n.key ?? "",
value: n.value ?? "" value: n.value ?? ""
})) }))
} }
} : "formdata" in t ? { } : "formdata" in e ? {
headers: [ headers: [
{ {
name: "Content-Type", name: "Content-Type",
@@ -116,7 +127,7 @@ function k(e) {
], ],
bodyType: "multipart/form-data", bodyType: "multipart/form-data",
body: { body: {
form: y(t.formdata).map( form: y(e.formdata).map(
(n) => n.src != null ? { (n) => n.src != null ? {
enabled: !n.disabled, enabled: !n.disabled,
name: n.key ?? "", name: n.key ?? "",
@@ -130,32 +141,32 @@ function k(e) {
} }
} : { headers: [], bodyType: null, body: {} }; } : { headers: [], bodyType: null, body: {} };
} }
function b(e) { function w(t) {
try { try {
return a(JSON.parse(e)); return o(JSON.parse(t));
} catch { } catch {
} }
return null; return null;
} }
function a(e) { function o(t) {
return Object.prototype.toString.call(e) === "[object Object]" ? e : {}; return Object.prototype.toString.call(t) === "[object Object]" ? t : {};
} }
function y(e) { function y(t) {
return Object.prototype.toString.call(e) === "[object Array]" ? e : []; return Object.prototype.toString.call(t) === "[object Array]" ? t : [];
} }
function h(e) { function h(t) {
return typeof e == "string" ? e.replace(/{{\s*(_\.)?([^}]+)\s*}}/g, "${[$2]}") : Array.isArray(e) && e != null ? e.map(h) : typeof e == "object" && e != null ? Object.fromEntries( return typeof t == "string" ? t.replace(/{{\s*(_\.)?([^}]+)\s*}}/g, "${[$2]}") : Array.isArray(t) && t != null ? t.map(h) : typeof t == "object" && t != null ? Object.fromEntries(
Object.entries(e).map(([t, n]) => [t, h(n)]) Object.entries(t).map(([e, n]) => [e, h(n)])
) : e; ) : t;
} }
function m(e) { function m(t) {
const t = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; const e = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
let n = `${e}_`; let n = `${t}_`;
for (let l = 0; l < 10; l++) for (let l = 0; l < 10; l++)
n += t[Math.floor(Math.random() * t.length)]; n += e[Math.floor(Math.random() * e.length)];
return n; return n;
} }
export { export {
m as generateId, m as generateId,
q as pluginHookImport v as pluginHookImport
}; };