Postman import form data

This commit is contained in:
Gregory Schier
2023-11-13 11:48:28 -08:00
parent 91074a35d8
commit c884cedfc2
2 changed files with 96 additions and 56 deletions

View File

@@ -1,12 +1,12 @@
const y = 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json', const f = 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json',
f = 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json', b = 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json',
b = [f, y]; w = [b, f];
function T(e) { function A(t) {
const t = h(e); const e = m(t);
if (t == null) return; if (e == null) return;
const i = s(t.info); const r = s(e.info);
if (!b.includes(i.schema) || !Array.isArray(t.item)) return; if (!w.includes(r.schema) || !Array.isArray(e.item)) return;
const r = { const a = {
workspaces: [], workspaces: [],
environments: [], environments: [],
requests: [], requests: [],
@@ -15,69 +15,69 @@ function T(e) {
c = { c = {
model: 'workspace', model: 'workspace',
id: 'wrk_0', id: 'wrk_0',
name: i.name || 'Postman Import', name: r.name || 'Postman Import',
description: i.description || '', description: r.description || '',
}; };
r.workspaces.push(c); a.workspaces.push(c);
const d = (o, l = null) => { const p = (o, l = null) => {
if (typeof o.name == 'string' && Array.isArray(o.item)) { if (typeof o.name == 'string' && Array.isArray(o.item)) {
const n = { const n = {
model: 'folder', model: 'folder',
workspaceId: c.id, workspaceId: c.id,
id: `fld_${r.folders.length}`, id: `fld_${a.folders.length}`,
name: o.name, name: o.name,
folderId: l, folderId: l,
}; };
r.folders.push(n); a.folders.push(n);
for (const a of o.item) d(a, n.id); for (const i of o.item) p(i, n.id);
} else if (typeof o.name == 'string' && 'request' in o) { } else if (typeof o.name == 'string' && 'request' in o) {
const n = s(o.request), const n = s(o.request),
a = q(n.body), i = T(n.body),
u = g(n.auth), u = g(n.auth),
m = { y = {
model: 'http_request', model: 'http_request',
id: `req_${r.requests.length}`, id: `req_${a.requests.length}`,
workspaceId: c.id, workspaceId: c.id,
folderId: l, folderId: l,
name: o.name, name: o.name,
method: n.method || 'GET', method: n.method || 'GET',
url: typeof n.url == 'string' ? n.url : s(n.url).raw, url: typeof n.url == 'string' ? n.url : s(n.url).raw,
body: a.body, body: i.body,
bodyType: a.bodyType, bodyType: i.bodyType,
authentication: u.authentication, authentication: u.authentication,
authenticationType: u.authenticationType, authenticationType: u.authenticationType,
headers: [ headers: [
...a.headers, ...i.headers,
...u.headers, ...u.headers,
...A(n.header).map((p) => ({ ...h(n.header).map((d) => ({
name: p.key, name: d.key,
value: p.value, value: d.value,
enabled: !p.disabled, enabled: !d.disabled,
})), })),
], ],
}; };
r.requests.push(m); a.requests.push(y);
} else console.log('Unknown item', o, l); } else console.log('Unknown item', o, l);
}; };
for (const o of t.item) d(o); for (const o of e.item) p(o);
return { resources: r }; return { resources: a };
} }
function g(e) { function g(t) {
const t = s(e); const e = s(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 || '',
}, },
} }
: { headers: [], authenticationType: null, authentication: {} }; : { headers: [], authenticationType: null, authentication: {} };
} }
function q(e) { function T(t) {
const t = s(e); const e = s(t);
return 'graphql' in t return 'graphql' in e
? { ? {
headers: [ headers: [
{ {
@@ -87,24 +87,44 @@ function q(e) {
}, },
], ],
bodyType: 'graphql', bodyType: 'graphql',
body: JSON.stringify( body: {
{ query: t.graphql.query, variables: h(t.graphql.variables) }, text: JSON.stringify(
null, { query: e.graphql.query, variables: m(e.graphql.variables) },
2, null,
), 2,
),
},
} }
: { headers: [], bodyType: null, body: null }; : 'formdata' in e
? {
headers: [
{
name: 'Content-Type',
value: 'application/x-www-form-urlencoded',
enabled: !0,
},
],
bodyType: 'application/x-www-form-urlencoded',
body: {
form: h(e.formdata).map((r) => ({
enabled: !r.disabled,
name: r.key ?? '',
value: r.value ?? '',
})),
},
}
: { headers: [], bodyType: null, body: {} };
} }
function h(e) { function m(t) {
try { try {
return s(JSON.parse(e)); return s(JSON.parse(t));
} catch {} } catch {}
return null; return null;
} }
function s(e) { function s(t) {
return Object.prototype.toString.call(e) === '[object Object]' ? e : {}; return Object.prototype.toString.call(t) === '[object Object]' ? t : {};
} }
function A(e) { function h(t) {
return Object.prototype.toString.call(e) === '[object Array]' ? e : []; return Object.prototype.toString.call(t) === '[object Array]' ? t : [];
} }
export { T as pluginHookImport }; export { A as pluginHookImport };

View File

@@ -123,15 +123,35 @@ function importBody(rawBody: any): Pick<HttpRequest, 'body' | 'bodyType' | 'head
}, },
], ],
bodyType: 'graphql', bodyType: 'graphql',
body: JSON.stringify( body: {
{ query: body.graphql.query, variables: parseJSONToRecord(body.graphql.variables) }, text: JSON.stringify(
null, { query: body.graphql.query, variables: parseJSONToRecord(body.graphql.variables) },
2, null,
), 2,
),
},
};
} else if ('formdata' in body) {
return {
headers: [
{
name: 'Content-Type',
value: 'application/x-www-form-urlencoded',
enabled: true,
},
],
bodyType: 'application/x-www-form-urlencoded',
body: {
form: toArray(body.formdata).map((f) => ({
enabled: !f.disabled,
name: f.key ?? '',
value: f.value ?? '',
})),
},
}; };
} else { } else {
// TODO: support other body types // TODO: support other body types
return { headers: [], bodyType: null, body: null }; return { headers: [], bodyType: null, body: {} };
} }
} }