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'; type AtLeast = Partial & Pick; interface ExportResources { workspaces: AtLeast[]; environments: AtLeast[]; requests: AtLeast[]; folders: AtLeast[]; } export function pluginHookImport(contents: string): { resources: ExportResources } | undefined { const root = parseJSONToRecord(contents); if (root == null) return; const info = toRecord(root.info); if (info.schema !== POSTMAN_2_1_0_SCHEMA || !Array.isArray(root.item)) { return; } const exportResources: ExportResources = { workspaces: [], environments: [], requests: [], folders: [], }; const workspace: ExportResources['workspaces'][0] = { model: 'workspace', id: 'wrk_0', name: info.name || 'Postman Import', description: info.description || '', }; exportResources.workspaces.push(workspace); const importItem = (v: Record, folderId: string | null = null) => { if (typeof v.name === 'string' && Array.isArray(v.item)) { const folder: ExportResources['folders'][0] = { model: 'folder', workspaceId: workspace.id, id: `fld_${exportResources.folders.length}`, name: v.name, folderId, }; exportResources.folders.push(folder); for (const child of v.item) { importItem(child, folder.id); } } else if (typeof v.name === 'string' && 'request' in v) { const r = toRecord(v.request); const bodyPatch = importBody(r.body); const request: ExportResources['requests'][0] = { model: 'http_request', id: `req_${exportResources.requests.length}`, workspaceId: workspace.id, folderId, name: v.name, method: r.method || 'GET', url: toRecord(r.url).raw, headers: [...bodyPatch.headers, ...toArray(r.header).map(importHeader)], body: bodyPatch.body, bodyType: bodyPatch.bodyType, // TODO: support auth // ...importAuth(r.auth), }; exportResources.requests.push(request); } else { console.log('Unknown item', v, folderId); } }; for (const item of root.item) { importItem(item); } 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 importBody(rawBody: any): Pick { const body = toRecord(rawBody); if ('graphql' in body) { return { headers: [ { name: 'Content-Type', value: 'application/json', enabled: true, }, ], bodyType: 'graphql', body: JSON.stringify( { query: body.graphql.query, variables: parseJSONToRecord(body.graphql.variables) }, null, 2, ), }; } else { // TODO: support other body types return { headers: [], bodyType: null, body: null }; } } function parseJSONToRecord(jsonStr: string): Record | null { try { return toRecord(JSON.parse(jsonStr)); } catch (err) {} return null; } function toRecord(value: any): Record { if (Object.prototype.toString.call(value) === '[object Object]') return value; else return {}; } function toArray(value: any): any[] { if (Object.prototype.toString.call(value) === '[object Array]') return value; else return []; }