2016-04-17 01:52:10 +00:00
|
|
|
import * as db from '../database'
|
|
|
|
|
|
|
|
const TYPE_REQUEST = 'request';
|
|
|
|
const TYPE_REQUEST_GROUP = 'request_group';
|
|
|
|
const FORMAT_MAP = {
|
|
|
|
'json': 'application/json'
|
2016-04-26 07:29:24 +00:00
|
|
|
// TODO: Fill these out
|
2016-04-17 01:52:10 +00:00
|
|
|
};
|
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
function importRequestGroup (iRequestGroup, parentId, exportFormat) {
|
2016-04-17 01:52:10 +00:00
|
|
|
if (exportFormat === 1) {
|
2016-04-26 07:29:24 +00:00
|
|
|
db.requestGroupCreate({
|
|
|
|
parentId,
|
|
|
|
collapsed: true,
|
2016-04-17 01:52:10 +00:00
|
|
|
name: iRequestGroup.name,
|
|
|
|
environment: (iRequestGroup.environments || {}).base || {}
|
2016-04-26 07:29:24 +00:00
|
|
|
}).then(requestGroup => {
|
|
|
|
// Sometimes (maybe all the time, I can't remember) requests will be nested
|
|
|
|
if (iRequestGroup.hasOwnProperty('requests')) {
|
|
|
|
// Let's process them oldest to newest
|
|
|
|
iRequestGroup.requests.reverse();
|
|
|
|
iRequestGroup.requests.map(
|
|
|
|
r => importRequest(r, requestGroup._id, exportFormat)
|
|
|
|
);
|
|
|
|
}
|
2016-04-17 01:52:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
function importRequest (iRequest, parentId, exportFormat) {
|
2016-04-17 01:52:10 +00:00
|
|
|
if (exportFormat === 1) {
|
|
|
|
let auth = {};
|
|
|
|
if (iRequest.authentication.username) {
|
|
|
|
auth = {
|
|
|
|
username: iRequest.authentication.username,
|
|
|
|
password: iRequest.authentication.password
|
|
|
|
}
|
|
|
|
}
|
2016-04-26 07:29:24 +00:00
|
|
|
|
2016-04-17 01:52:10 +00:00
|
|
|
db.requestCreate({
|
2016-04-26 07:29:24 +00:00
|
|
|
parentId,
|
2016-04-17 01:52:10 +00:00
|
|
|
name: iRequest.name,
|
|
|
|
url: iRequest.url,
|
|
|
|
method: iRequest.method,
|
|
|
|
body: iRequest.body,
|
|
|
|
headers: iRequest.headers || [],
|
|
|
|
params: iRequest.params || [],
|
|
|
|
contentType: FORMAT_MAP[iRequest.__insomnia.format] || 'text/plain',
|
2016-04-26 07:29:24 +00:00
|
|
|
authentication: auth
|
2016-04-17 01:52:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
export default function (workspace, txt) {
|
2016-04-17 01:52:10 +00:00
|
|
|
let data;
|
2016-04-26 07:29:24 +00:00
|
|
|
|
2016-04-17 01:52:10 +00:00
|
|
|
try {
|
|
|
|
data = JSON.parse(txt);
|
|
|
|
} catch (e) {
|
2016-04-26 07:29:24 +00:00
|
|
|
// TODO: Handle these errors
|
|
|
|
return;
|
2016-04-17 01:52:10 +00:00
|
|
|
}
|
2016-04-26 07:29:24 +00:00
|
|
|
|
2016-04-17 01:52:10 +00:00
|
|
|
|
|
|
|
if (!data.hasOwnProperty('_type') || !data.hasOwnProperty('items')) {
|
2016-04-26 07:29:24 +00:00
|
|
|
// TODO: Handle these errors
|
|
|
|
return;
|
2016-04-17 01:52:10 +00:00
|
|
|
}
|
2016-04-26 07:29:24 +00:00
|
|
|
|
|
|
|
data.items.reverse().filter(i => i._type === TYPE_REQUEST_GROUP).map(
|
|
|
|
rg => importRequestGroup(rg, workspace._id, data.__export_format)
|
2016-04-17 01:52:10 +00:00
|
|
|
);
|
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
data.items.reverse().filter(i => i._type === TYPE_REQUEST).map(
|
|
|
|
r => importRequest(r, workspace._id, data.__export_format)
|
2016-04-17 01:52:10 +00:00
|
|
|
);
|
|
|
|
}
|