insomnia/app/backend/export/legacy.js
Gregory Schier 318c35c2cb Move a bunch of stuff to async/await (#39)
* Some minor implementations

* Some more

* Lots more

* Removed 'backend' alias

* removed all promises

* Removed a bunch of module exports stuff

* Some morE'

* Fix

* custom DNS

* Tests for DNS

* bug fix

* Some small adjustments

* Small stuff
2016-10-02 13:57:00 -07:00

67 lines
1.9 KiB
JavaScript

import * as db from '../database/index';
import {getContentTypeFromHeaders} from '../contentTypes';
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) {
const requestGroup = await db.requestGroup.create({
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) {
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]
});
}
}
db.request.create({
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
});
}