Fix plugins

This commit is contained in:
Gregory Schier committed 2024-06-13 07:03:50 -07:00
1 parent 4469b84ad6
commit 6c331ed734
26 files changed
+793 -72

No files matched your search

@@ -0,0 +1,184 @@
const _ = "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", C = "https://schema.getpostman.com/json/collection/v2.0.0/collection.json", O = [C, _];
function v(e, t) {
var w;
console.log("CTX", e);
const a = q(t);
if (a == null)
return;
const s = i(a.info);
if (!O.includes(s.schema) || !Array.isArray(a.item))
return;
const u = k(a.auth), n = {
workspaces: [],
environments: [],
httpRequests: [],
folders: []
}, p = {
model: "workspace",
id: b("workspace"),
name: s.name || "Postman Import",
description: s.description || "",
variables: ((w = a.variable) == null ? void 0 : w.map((r) => ({
name: r.key,
value: r.value
}))) ?? []
};
n.workspaces.push(p);
const g = (r, d = null) => {
if (typeof r.name == "string" && Array.isArray(r.item)) {
const o = {
model: "folder",
workspaceId: p.id,
id: b("folder"),
name: r.name,
folderId: d
};
n.folders.push(o);
for (const l of r.item)
g(l, o.id);
} else if (typeof r.name == "string" && "request" in r) {
const o = i(r.request), l = j(o.body), A = k(o.auth), m = A.authenticationType == null ? u : A, S = {
model: "http_request",
id: b("http_request"),
workspaceId: p.id,
folderId: d,
name: r.name,
method: o.method || "GET",
url: typeof o.url == "string" ? o.url : i(o.url).raw,
body: l.body,
bodyType: l.bodyType,
authentication: m.authentication,
authenticationType: m.authenticationType,
headers: [
...l.headers,
...m.headers,
...f(o.header).map((y) => ({
name: y.key,
value: y.value,
enabled: !y.disabled
}))
]
};
n.httpRequests.push(S);
} else
console.log("Unknown item", r, d);
};
for (const r of a.item)
g(r);
return { resources: T(n) };
}
function k(e) {
const t = i(e);
return "basic" in t ? {
headers: [],
authenticationType: "basic",
authentication: {
username: t.basic.username || "",
password: t.basic.password || ""
}
} : "bearer" in t ? {
headers: [],
authenticationType: "bearer",
authentication: {
token: t.bearer.token || ""
}
} : { headers: [], authenticationType: null, authentication: {} };
}
function j(e) {
var a, s, c, u;
const t = i(e);
return "graphql" in t ? {
headers: [
{
name: "Content-Type",
value: "application/json",
enabled: !0
}
],
bodyType: "graphql",
body: {
text: JSON.stringify(
{ query: t.graphql.query, variables: q(t.graphql.variables) },
null,
2
)
}
} : "urlencoded" in t ? {
headers: [
{
name: "Content-Type",
value: "application/x-www-form-urlencoded",
enabled: !0
}
],
bodyType: "application/x-www-form-urlencoded",
body: {
form: f(t.urlencoded).map((n) => ({
enabled: !n.disabled,
name: n.key ?? "",
value: n.value ?? ""
}))
}
} : "formdata" in t ? {
headers: [
{
name: "Content-Type",
value: "multipart/form-data",
enabled: !0
}
],
bodyType: "multipart/form-data",
body: {
form: f(t.formdata).map(
(n) => n.src != null ? {
enabled: !n.disabled,
contentType: n.contentType ?? null,
name: n.key ?? "",
file: n.src ?? ""
} : {
enabled: !n.disabled,
name: n.key ?? "",
value: n.value ?? ""
}
)
}
} : "raw" in t ? {
headers: [
{
name: "Content-Type",
value: ((s = (a = t.options) == null ? void 0 : a.raw) == null ? void 0 : s.language) === "json" ? "application/json" : "",
enabled: !0
}
],
bodyType: ((u = (c = t.options) == null ? void 0 : c.raw) == null ? void 0 : u.language) === "json" ? "application/json" : "other",
body: {
text: t.raw ?? ""
}
} : { headers: [], bodyType: null, body: {} };
}
function q(e) {
try {
return i(JSON.parse(e));
} catch {
}
return null;
}
function i(e) {
return Object.prototype.toString.call(e) === "[object Object]" ? e : {};
}
function f(e) {
return Object.prototype.toString.call(e) === "[object Array]" ? e : [];
}
function T(e) {
return typeof e == "string" ? e.replace(/{{\s*(_\.)?([^}]+)\s*}}/g, "${[$2]}") : Array.isArray(e) && e != null ? e.map(T) : typeof e == "object" && e != null ? Object.fromEntries(
Object.entries(e).map(([t, a]) => [t, T(a)])
) : e;
}
const h = {};
function b(e) {
return h[e] = (h[e] ?? -1) + 1, `GENERATE_ID::${e.toUpperCase()}_${h[e]}`;
}
export {
v as pluginHookImport
};
//# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
{"version":3,"file":"index.mjs","sources":["../../../plugins/importer-postman/src/index.ts"],"sourcesContent":["import { Environment, Folder, HttpRequest, Model, Workspace } from '../../../src-web/lib/models';\n\nconst POSTMAN_2_1_0_SCHEMA = 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json';\nconst POSTMAN_2_0_0_SCHEMA = 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json';\nconst VALID_SCHEMAS = [POSTMAN_2_0_0_SCHEMA, POSTMAN_2_1_0_SCHEMA];\n\ntype AtLeast<T, K extends keyof T> = Partial<T> & Pick<T, K>;\n\ninterface ExportResources {\n workspaces: AtLeast<Workspace, 'name' | 'id' | 'model'>[];\n environments: AtLeast<Environment, 'name' | 'id' | 'model' | 'workspaceId'>[];\n httpRequests: AtLeast<HttpRequest, 'name' | 'id' | 'model' | 'workspaceId'>[];\n folders: AtLeast<Folder, 'name' | 'id' | 'model' | 'workspaceId'>[];\n}\n\nexport function pluginHookImport(\n ctx: any,\n contents: string,\n): { resources: ExportResources } | undefined {\n console.log('CTX', ctx);\n const root = parseJSONToRecord(contents);\n if (root == null) return;\n\n const info = toRecord(root.info);\n const isValidSchema = VALID_SCHEMAS.includes(info.schema);\n if (!isValidSchema || !Array.isArray(root.item)) {\n return;\n }\n\n const globalAuth = importAuth(root.auth);\n\n const exportResources: ExportResources = {\n workspaces: [],\n environments: [],\n httpRequests: [],\n folders: [],\n };\n\n const workspace: ExportResources['workspaces'][0] = {\n model: 'workspace',\n id: generateId('workspace'),\n name: info.name || 'Postman Import',\n description: info.description || '',\n variables:\n root.variable?.map((v: any) => ({\n name: v.key,\n value: v.value,\n })) ?? [],\n };\n exportResources.workspaces.push(workspace);\n\n const importItem = (v: Record<string, any>, folderId: string | null = null) => {\n if (typeof v.name === 'string' && Array.isArray(v.item)) {\n const folder: ExportResources['folders'][0] = {\n model: 'folder',\n workspaceId: workspace.id,\n id: generateId('folder'),\n name: v.name,\n folderId,\n };\n exportResources.folders.push(folder);\n for (const child of v.item) {\n importItem(child, folder.id);\n }\n } else if (typeof v.name === 'string' && 'request' in v) {\n const r = toRecord(v.request);\n const bodyPatch = importBody(r.body);\n const requestAuthPath = importAuth(r.auth);\n const authPatch = requestAuthPath.authenticationType == null ? globalAuth : requestAuthPath;\n const request: ExportResources['httpRequests'][0] = {\n model: 'http_request',\n id: generateId('http_request'),\n workspaceId: workspace.id,\n folderId,\n name: v.name,\n method: r.method || 'GET',\n url: typeof r.url === 'string' ? r.url : toRecord(r.url).raw,\n body: bodyPatch.body,\n bodyType: bodyPatch.bodyType,\n authentication: authPatch.authentication,\n authenticationType: authPatch.authenticationType,\n headers: [\n ...bodyPatch.headers,\n ...authPatch.headers,\n ...toArray(r.header).map((h) => {\n return {\n name: h.key,\n value: h.value,\n enabled: !h.disabled,\n };\n }),\n ],\n };\n exportResources.httpRequests.push(request);\n } else {\n console.log('Unknown item', v, folderId);\n }\n };\n\n for (const item of root.item) {\n importItem(item);\n }\n\n return { resources: convertTemplateSyntax(exportResources) };\n}\n\nfunction importAuth(\n rawAuth: any,\n): Pick<HttpRequest, 'authentication' | 'authenticationType' | 'headers'> {\n const auth = toRecord(rawAuth);\n if ('basic' in auth) {\n return {\n headers: [],\n authenticationType: 'basic',\n authentication: {\n username: auth.basic.username || '',\n password: auth.basic.password || '',\n },\n };\n } else if ('bearer' in auth) {\n return {\n headers: [],\n authenticationType: 'bearer',\n authentication: {\n token: auth.bearer.token || '',\n },\n };\n } else {\n // TODO: support other auth types\n return { headers: [], authenticationType: null, authentication: {} };\n }\n}\n\nfunction importBody(rawBody: any): Pick<HttpRequest, 'body' | 'bodyType' | 'headers'> {\n const body = toRecord(rawBody);\n if ('graphql' in body) {\n return {\n headers: [\n {\n name: 'Content-Type',\n value: 'application/json',\n enabled: true,\n },\n ],\n bodyType: 'graphql',\n body: {\n text: JSON.stringify(\n { query: body.graphql.query, variables: parseJSONToRecord(body.graphql.variables) },\n null,\n 2,\n ),\n },\n };\n } else if ('urlencoded' in body) {\n return {\n hLine truncated