insomnia/app/backend/export/legacy.js

67 lines
1.9 KiB
JavaScript
Raw Normal View History

2016-11-10 01:15:27 +00:00
import * as models from '../models';
2016-11-10 02:40:53 +00:00
import {getContentTypeFromHeaders} from '../../common/constants';
2016-08-15 22:31:30 +00:00
const FORMAT_MAP = {
json: 'application/json',
xml: 'application/xml',
form: 'application/x-www-form-urlencoded',
text: 'text/plain'
};
export async function importRequestGroupLegacy (importedRequestGroup, parentId, index = 1) {
2016-11-10 01:15:27 +00:00
const requestGroup = await models.requestGroup.create({
2016-08-15 22:31:30 +00:00
parentId,
name: importedRequestGroup.name,
environment: (importedRequestGroup.environments || {}).base || {},
metaCollapsed: true,
metaSortKey: index * 1000
});
// Sometimes (maybe all the time, I can't remember) requests will be nested
if (importedRequestGroup.hasOwnProperty('requests')) {
// Let's process them oldest to newest
importedRequestGroup.requests.map(
(r, i) => importRequestLegacy(r, requestGroup._id, index * 1000 + i)
);
}
}
export function importRequestLegacy (importedRequest, parentId, index = 1) {
2016-08-15 22:31:30 +00:00
let auth = {};
if (importedRequest.authentication.username) {
auth = {
username: importedRequest.authentication.username,
password: importedRequest.authentication.password
}
}
// Add the content type header
const headers = importedRequest.headers || [];
const contentType = getContentTypeFromHeaders(headers);
if (!contentType) {
const derivedContentType = FORMAT_MAP[importedRequest.__insomnia.format];
if (derivedContentType) {
headers.push({
name: 'Content-Type',
value: FORMAT_MAP[importedRequest.__insomnia.format]
});
}
}
2016-11-10 01:15:27 +00:00
models.request.create({
2016-08-15 22:31:30 +00:00
parentId,
_id: importedRequest._id,
name: importedRequest.name,
url: importedRequest.url,
method: importedRequest.method,
body: importedRequest.body,
headers: headers,
parameters: importedRequest.params || [],
metaSortKey: index * 1000,
contentType: FORMAT_MAP[importedRequest.__insomnia.format] || 'text/plain',
authentication: auth
});
}
2016-08-15 22:31:30 +00:00