Postman ID generation

This commit is contained in:
Gregory Schier
2023-11-19 20:54:02 -08:00
parent 0290aba982
commit 26b6c48657
2 changed files with 86 additions and 70 deletions

View File

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

View File

@@ -32,7 +32,7 @@ export function pluginHookImport(contents: string): { resources: ExportResources
const workspace: ExportResources['workspaces'][0] = { const workspace: ExportResources['workspaces'][0] = {
model: 'workspace', model: 'workspace',
id: 'wrk_0', id: generateId('wk'),
name: info.name || 'Postman Import', name: info.name || 'Postman Import',
description: info.description || '', description: info.description || '',
}; };
@@ -43,7 +43,7 @@ export function pluginHookImport(contents: string): { resources: ExportResources
const folder: ExportResources['folders'][0] = { const folder: ExportResources['folders'][0] = {
model: 'folder', model: 'folder',
workspaceId: workspace.id, workspaceId: workspace.id,
id: `fld_${exportResources.folders.length}`, id: generateId('fl'),
name: v.name, name: v.name,
folderId, folderId,
}; };
@@ -57,7 +57,7 @@ export function pluginHookImport(contents: string): { resources: ExportResources
const authPatch = importAuth(r.auth); const authPatch = importAuth(r.auth);
const request: ExportResources['requests'][0] = { const request: ExportResources['requests'][0] = {
model: 'http_request', model: 'http_request',
id: `req_${exportResources.requests.length}`, id: generateId('rq'),
workspaceId: workspace.id, workspaceId: workspace.id,
folderId, folderId,
name: v.name, name: v.name,
@@ -212,3 +212,12 @@ function convertTemplateSyntax<T>(obj: T): T {
return obj; return obj;
} }
} }
function generateId(prefix: 'wk' | 'rq' | 'fl'): string {
const alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
let id = `${prefix}_`;
for (let i = 0; i < 10; i++) {
id += alphabet[Math.floor(Math.random() * alphabet.length)];
}
return id;
}