UNPKG

insomnia-importers

Version:

Various data importers for Insomnia

95 lines 3.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convert = exports.description = exports.name = exports.id = void 0; exports.id = 'har'; exports.name = 'HAR 1.2'; exports.description = 'Importer for HTTP Archive 1.2'; let requestCount = 1; const extractRequests = (harRoot) => { const { log, httpVersion, method, url } = harRoot; if (!log && httpVersion && method && url) { // If there is not "log" property, try to use the root object if it looks like a request return [harRoot]; } return log.entries.map(({ comment, request }) => { if (comment && request && !request.comment) { // Preserve the entry comment for request name generation request.comment = comment; } return request; }); }; const removeComment = (obj) => { const { comment, ...newObject } = obj; return newObject; }; const importPostData = (postData) => { if (!postData) { return {}; } const { params, mimeType = '', text = '' } = postData; if (params && params.length) { return { mimeType: mimeType || 'application/x-www-form-urlencoded', params: params.map(({ name, fileName, value = '' }) => ({ name, ...(fileName ? { fileName } : { value }), })), }; } else { return { mimeType, text, }; } }; const importRequest = (request) => { var _a, _b; const cookieHeaderValue = ((_a = request.cookies) !== null && _a !== void 0 ? _a : []) .map(({ name, value }) => `${name}=${value}`) .join('; '); const headers = request.headers ? request.headers.map(removeComment) : []; // Convert cookie value to header const existingCookieHeader = headers.find(header => header.name.toLowerCase() === 'cookie'); if (cookieHeaderValue && existingCookieHeader) { // Has existing cookie header, so let's update it existingCookieHeader.value += `; ${cookieHeaderValue}`; } else if (cookieHeaderValue) { // No existing cookie header, so let's make a new one headers.push({ name: 'Cookie', value: cookieHeaderValue, }); } const count = requestCount++; return { _type: 'request', _id: `__REQ_${count}__`, name: request.comment || request.url || `HAR Import ${count}`, parentId: '__WORKSPACE_ID__', url: request.url, method: (_b = request.method) === null || _b === void 0 ? void 0 : _b.toUpperCase(), body: importPostData(request.postData), parameters: request.queryString ? request.queryString.map(removeComment) : [], headers: headers, // Authentication isn't part of HAR, but we should be able to sniff for things like Basic Authentication headers and pull out the auth info authentication: {}, }; }; const convert = rawData => { requestCount = 1; try { const data = JSON.parse(rawData); const requests = extractRequests(data); return requests.map(importRequest); } catch (error) { return null; } }; exports.convert = convert; //# sourceMappingURL=har.js.map