insomnia/app/backend/export/legacy.js

69 lines
2.0 KiB
JavaScript
Raw Normal View History

2016-09-21 00:03:26 +00:00
'use strict';
const db = require('../database/index');
const {getContentTypeFromHeaders} = require('../contentTypes');
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'
};
2016-09-21 00:03:26 +00:00
module.exports.importRequestGroupLegacy = (importedRequestGroup, parentId, index = 1) => {
2016-09-21 20:49:54 +00:00
return db.requestGroup.create({
2016-08-15 22:31:30 +00:00
parentId,
name: importedRequestGroup.name,
environment: (importedRequestGroup.environments || {}).base || {},
metaCollapsed: true,
metaSortKey: index * 1000
}).then(requestGroup => {
// 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(
2016-09-21 00:03:26 +00:00
(r, i) => module.exports.importRequestLegacy(r, requestGroup._id, index * 1000 + i)
2016-08-15 22:31:30 +00:00
);
}
});
2016-09-21 00:03:26 +00:00
};
2016-08-15 22:31:30 +00:00
2016-09-21 00:03:26 +00:00
module.exports.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-09-21 20:49:54 +00:00
db.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-09-21 00:03:26 +00:00
};
2016-08-15 22:31:30 +00:00