insomnia/app/lib/render.js

90 lines
2.6 KiB
JavaScript
Raw Normal View History

import nunjucks from 'nunjucks';
import traverse from 'traverse';
2016-07-20 23:16:28 +00:00
import * as db from '../database'
import {TYPE_WORKSPACE} from '../database/index';
2016-04-15 02:13:49 +00:00
2016-07-20 21:15:11 +00:00
nunjucks.configure({
autoescape: false
});
2016-04-10 02:58:48 +00:00
2016-07-20 23:16:28 +00:00
export function getRenderedRequest (request) {
return db.requestGetAncestors(request).then(ancestors => {
const workspace = ancestors.find(doc => doc.type === TYPE_WORKSPACE);
return Promise.all([
db.environmentGetOrCreateForWorkspace(workspace),
2016-08-15 22:31:30 +00:00
db.environmentGetById(workspace.metaActiveEnvironmentId),
db.cookieJarGetOrCreateForWorkspace(workspace)
]).then(([rootEnvironment, subEnvironment, cookieJar]) => {
const renderContext = Object.assign(
{},
rootEnvironment.data,
subEnvironment ? subEnvironment.data : {}
);
for (let doc of ancestors) {
if (doc.type === TYPE_WORKSPACE) {
continue;
}
const environment = doc.environment || {};
Object.assign(renderContext, environment);
}
// Make a copy so no one gets mad :)
const renderedRequest = Object.assign({}, request);
try {
traverse(renderedRequest).forEach(function (x) {
if (typeof x === 'string') {
this.update(render(x, renderContext));
}
});
} catch (e) {
// Failed to render Request
throw new Error(`Render Failed: "${e.message}"`);
}
// Default the proto if it doesn't exist
if (renderedRequest.url.indexOf('://') === -1) {
renderedRequest.url = `http://${renderedRequest.url}`;
}
// Add the yummy cookies
renderedRequest.cookieJar = cookieJar;
2016-08-15 22:31:30 +00:00
// Do authentication
if (renderedRequest.authentication.username) {
const authHeader = renderedRequest.headers.find(
h => h.name.toLowerCase() === 'authorization'
);
if (!authHeader) {
const {username, password} = renderedRequest.authentication;
const header = getBasicAuthHeader(username, password);
renderedRequest.headers.push(header);
}
}
return new Promise(resolve => resolve(renderedRequest));
});
2016-07-20 23:16:28 +00:00
});
}
2016-08-15 22:31:30 +00:00
function render (template, context = {}) {
try {
return nunjucks.renderString(template, context);
} catch (e) {
throw new Error(
e.message.replace('(unknown path)\n ', '')
);
}
}
function getBasicAuthHeader (username, password) {
const name = 'Authorization';
const header = `${username || ''}:${password || ''}`;
const authString = new Buffer(header, 'utf8').toString('base64');
const value = `Basic ${authString}`;
return {name, value};
}