2017-02-20 18:32:27 +00:00
|
|
|
import clone from 'clone';
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as models from '../models';
|
|
|
|
import {getBasicAuthHeader, hasAuthHeader, setDefaultProtocol} from './misc';
|
2016-11-10 01:15:27 +00:00
|
|
|
import * as db from './database';
|
2017-02-20 18:32:27 +00:00
|
|
|
import * as templating from '../templating';
|
2016-04-15 02:13:49 +00:00
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
export async function render (template, context = {}) {
|
|
|
|
return templating.render(template, {context});
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
export async function buildRenderContext (ancestors, rootEnvironment, subEnvironment) {
|
2017-01-13 19:21:03 +00:00
|
|
|
if (!Array.isArray(ancestors)) {
|
|
|
|
ancestors = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const environments = [];
|
2016-09-03 04:32:45 +00:00
|
|
|
|
|
|
|
if (rootEnvironment) {
|
2017-01-13 19:21:03 +00:00
|
|
|
environments.push(rootEnvironment.data);
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (subEnvironment) {
|
2017-01-13 19:21:03 +00:00
|
|
|
environments.push(subEnvironment.data);
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 19:21:03 +00:00
|
|
|
for (const doc of ancestors.reverse()) {
|
2016-09-03 04:32:45 +00:00
|
|
|
if (!doc.environment) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-13 19:21:03 +00:00
|
|
|
environments.push(doc.environment);
|
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2017-01-24 18:51:25 +00:00
|
|
|
// At this point, environments is a list of environments ordered
|
|
|
|
// from top-most parent to bottom-most child
|
|
|
|
|
2017-01-13 19:21:03 +00:00
|
|
|
const renderContext = {};
|
|
|
|
for (const environment of environments) {
|
2017-01-24 18:51:25 +00:00
|
|
|
// Do an Object.assign, but render each property as it overwrites. This
|
|
|
|
// way we can keep same-name variables from the parent context.
|
2017-02-20 18:32:27 +00:00
|
|
|
await _objectDeepAssignRender(renderContext, environment);
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 18:51:25 +00:00
|
|
|
// Render the context with itself to fill in the rest.
|
2017-02-07 16:09:12 +00:00
|
|
|
let finalRenderContext = renderContext;
|
|
|
|
|
|
|
|
// Render up to 5 levels of recursive references.
|
|
|
|
for (let i = 0; i < 3; i++) {
|
2017-02-20 18:32:27 +00:00
|
|
|
finalRenderContext = await recursiveRender(finalRenderContext, finalRenderContext);
|
2017-02-07 16:09:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return finalRenderContext;
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
/**
|
|
|
|
* Recursively render any JS object and return a new one
|
|
|
|
* @param {*} originalObj - object to render
|
|
|
|
* @param {object} context - context to render against
|
|
|
|
* @return {Promise.<*>}
|
|
|
|
*/
|
|
|
|
export async function recursiveRender (originalObj, context) {
|
|
|
|
const obj = clone(originalObj);
|
|
|
|
const toS = obj => Object.prototype.toString.call(obj);
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
// Make a copy so no one gets mad :)
|
|
|
|
async function next (x) {
|
|
|
|
// Leave these types alone
|
|
|
|
if (
|
|
|
|
toS(x) === '[object Date]' ||
|
|
|
|
toS(x) === '[object RegExp]' ||
|
|
|
|
toS(x) === '[object Error]' ||
|
|
|
|
toS(x) === '[object Boolean]' ||
|
|
|
|
toS(x) === '[object Number]' ||
|
|
|
|
toS(x) === '[object Null]' ||
|
|
|
|
toS(x) === '[object Undefined]'
|
|
|
|
) {
|
|
|
|
// Do nothing to these types
|
|
|
|
} else if (toS(x) === '[object String]') {
|
|
|
|
try {
|
|
|
|
x = render(x, context);
|
|
|
|
} catch (err) {
|
|
|
|
// Failed to render Request
|
|
|
|
// const path = this.path.join('.');
|
|
|
|
const path = 'TODO"';
|
|
|
|
throw new Error(`Failed to render Request.${path}: "${err.message}"`);
|
|
|
|
}
|
|
|
|
} else if (Array.isArray(x)) {
|
|
|
|
for (let i = 0; i < x.length; i++) {
|
|
|
|
x[i] = await next(x[i]);
|
|
|
|
}
|
|
|
|
} else if (typeof x === 'object') {
|
|
|
|
const keys = Object.keys(x);
|
|
|
|
for (const key of keys) {
|
|
|
|
x[key] = await next(x[key]);
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
2016-11-29 20:55:31 +00:00
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(obj);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-08-15 22:31:30 +00:00
|
|
|
|
2017-02-13 08:12:02 +00:00
|
|
|
export async function getRenderContext (request, environmentId, ancestors = null) {
|
|
|
|
if (!ancestors) {
|
|
|
|
ancestors = await db.withAncestors(request);
|
|
|
|
}
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2017-02-13 08:12:02 +00:00
|
|
|
const workspace = ancestors.find(doc => doc.type === models.workspace.type);
|
2016-11-10 01:15:27 +00:00
|
|
|
const rootEnvironment = await models.environment.getOrCreateForWorkspace(workspace);
|
|
|
|
const subEnvironment = await models.environment.getById(environmentId);
|
2016-10-02 20:57:00 +00:00
|
|
|
|
|
|
|
// Generate the context we need to render
|
2017-02-13 08:12:02 +00:00
|
|
|
return buildRenderContext(
|
2016-10-02 20:57:00 +00:00
|
|
|
ancestors,
|
|
|
|
rootEnvironment,
|
|
|
|
subEnvironment
|
|
|
|
);
|
2017-02-13 08:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getRenderedRequest (request, environmentId) {
|
|
|
|
const ancestors = await db.withAncestors(request);
|
|
|
|
const workspace = ancestors.find(doc => doc.type === models.workspace.type);
|
|
|
|
const cookieJar = await models.cookieJar.getOrCreateForWorkspace(workspace);
|
|
|
|
|
|
|
|
const renderContext = await getRenderContext(request, environmentId, ancestors);
|
2016-10-02 20:57:00 +00:00
|
|
|
|
|
|
|
// Render all request properties
|
2017-02-20 18:32:27 +00:00
|
|
|
const renderedRequest = await recursiveRender(request, renderContext);
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
// Remove disabled params
|
|
|
|
renderedRequest.parameters = renderedRequest.parameters.filter(p => !p.disabled);
|
|
|
|
|
|
|
|
// Remove disabled headers
|
|
|
|
renderedRequest.headers = renderedRequest.headers.filter(p => !p.disabled);
|
|
|
|
|
|
|
|
// Remove disabled body params
|
|
|
|
if (renderedRequest.body && Array.isArray(renderedRequest.body.params)) {
|
|
|
|
renderedRequest.body.params = renderedRequest.body.params.filter(p => !p.disabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove disabled authentication
|
|
|
|
if (renderedRequest.authentication && renderedRequest.authentication.disabled) {
|
|
|
|
renderedRequest.authentication = {}
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
// Default the proto if it doesn't exist
|
|
|
|
renderedRequest.url = setDefaultProtocol(renderedRequest.url);
|
|
|
|
|
|
|
|
// Add the yummy cookies
|
|
|
|
renderedRequest.cookieJar = cookieJar;
|
|
|
|
|
|
|
|
// Add authentication
|
|
|
|
const missingAuthHeader = !hasAuthHeader(renderedRequest.headers);
|
|
|
|
if (missingAuthHeader && renderedRequest.authentication.username) {
|
|
|
|
const {username, password} = renderedRequest.authentication;
|
|
|
|
const header = getBasicAuthHeader(username, password);
|
|
|
|
renderedRequest.headers.push(header);
|
|
|
|
}
|
|
|
|
|
|
|
|
return renderedRequest;
|
|
|
|
}
|
2017-01-24 18:51:25 +00:00
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
async function _objectDeepAssignRender (base, obj) {
|
2017-01-24 18:59:43 +00:00
|
|
|
for (const key of Object.keys(obj)) {
|
|
|
|
/*
|
|
|
|
* If we're overwriting a string, try to render it first with the base as
|
|
|
|
* a context. This allows for the following scenario:
|
|
|
|
*
|
|
|
|
* base: { base_url: 'google.com' }
|
|
|
|
* obj: { base_url: '{{ base_url }}/foo' }
|
|
|
|
* final: { base_url: 'google.com/foo' }
|
|
|
|
*
|
|
|
|
* A regular Object.assign would yield { base_url: '{{ base_url }}/foo' } and the
|
|
|
|
* original base_url of google.com would be lost.
|
2017-02-07 16:09:12 +00:00
|
|
|
*/
|
2017-01-24 18:59:43 +00:00
|
|
|
if (typeof base[key] === 'string') {
|
2017-02-20 18:32:27 +00:00
|
|
|
base[key] = await render(obj[key], base);
|
2017-01-24 18:51:25 +00:00
|
|
|
} else {
|
2017-01-24 18:59:43 +00:00
|
|
|
base[key] = obj[key];
|
2017-01-24 18:51:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|