2017-07-20 01:55:40 +00:00
|
|
|
// @flow
|
2018-06-25 17:42:50 +00:00
|
|
|
import type { Request } from '../models/request';
|
|
|
|
import type { BaseModel } from '../models/index';
|
2017-07-20 01:55:40 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
import { setDefaultProtocol } from 'insomnia-url';
|
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';
|
2018-06-28 03:43:38 +00:00
|
|
|
import { CONTENT_TYPE_GRAPHQL } from '../common/constants';
|
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';
|
2018-06-25 17:42:50 +00:00
|
|
|
import type { CookieJar } from '../models/cookie-jar';
|
|
|
|
import type { Environment } from '../models/environment';
|
2016-04-15 02:13:49 +00:00
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
export const KEEP_ON_ERROR = 'keep';
|
|
|
|
export const THROW_ON_ERROR = 'throw';
|
2018-06-09 03:22:39 +00:00
|
|
|
|
2019-05-03 14:53:18 +00:00
|
|
|
export type RenderPurpose = 'send' | 'general' | 'no-render';
|
2018-06-09 03:22:39 +00:00
|
|
|
|
|
|
|
export const RENDER_PURPOSE_SEND: RenderPurpose = 'send';
|
|
|
|
export const RENDER_PURPOSE_GENERAL: RenderPurpose = 'general';
|
2019-05-03 14:53:18 +00:00
|
|
|
export const RENDER_PURPOSE_NO_RENDER: RenderPurpose = 'no-render';
|
2017-07-11 01:05:54 +00:00
|
|
|
|
2019-05-15 20:01:36 +00:00
|
|
|
/** Key/value pairs to be provided to the render context */
|
|
|
|
export type ExtraRenderInfo = Array<{ name: string, value: any }>;
|
|
|
|
|
2017-07-20 01:55:40 +00:00
|
|
|
export type RenderedRequest = Request & {
|
2018-06-25 17:42:50 +00:00
|
|
|
cookies: Array<{ name: string, value: string, disabled?: boolean }>,
|
2018-12-12 17:36:11 +00:00
|
|
|
cookieJar: CookieJar,
|
2017-07-20 01:55:40 +00:00
|
|
|
};
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export async function buildRenderContext(
|
2017-07-20 01:55:40 +00:00
|
|
|
ancestors: Array<BaseModel> | null,
|
2017-09-25 21:32:58 +00:00
|
|
|
rootEnvironment: Environment | null,
|
|
|
|
subEnvironment: Environment | null,
|
2018-12-12 17:36:11 +00:00
|
|
|
baseContext: Object = {},
|
2017-07-20 01:55:40 +00:00
|
|
|
): Object {
|
2017-09-25 21:32:58 +00:00
|
|
|
const envObjects = [];
|
2016-09-03 04:32:45 +00:00
|
|
|
|
|
|
|
if (rootEnvironment) {
|
2017-09-25 21:32:58 +00:00
|
|
|
envObjects.push(rootEnvironment.data);
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (subEnvironment) {
|
2017-09-25 21:32:58 +00:00
|
|
|
envObjects.push(subEnvironment.data);
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 01:55:40 +00:00
|
|
|
for (const doc of (ancestors || []).reverse()) {
|
2018-03-29 17:59:32 +00:00
|
|
|
const environment = (doc: any).environment;
|
|
|
|
if (typeof environment === 'object' && environment !== null) {
|
|
|
|
envObjects.push(environment);
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
2017-01-13 19:21:03 +00:00
|
|
|
}
|
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-05-15 20:55:05 +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.
|
2019-08-11 09:12:48 +00:00
|
|
|
let renderContext = baseContext;
|
|
|
|
|
|
|
|
// Made the rendering into a recursive function to handle nested Objects
|
|
|
|
async function renderSubContext(subObject: Object, subContext: Object): Promise<any> {
|
|
|
|
const keys = _getOrderedEnvironmentKeys(subObject);
|
2017-05-15 20:55:05 +00:00
|
|
|
for (const key of keys) {
|
|
|
|
/*
|
2018-06-29 16:29:46 +00:00
|
|
|
* If we're overwriting a string, try to render it first using the same key from the base
|
|
|
|
* environment to support same-variable recursion. This allows for the following scenario:
|
2017-05-15 20:55:05 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-11-22 18:31:23 +00:00
|
|
|
if (Object.prototype.toString.call(subObject[key]) === '[object String]') {
|
2019-08-11 09:12:48 +00:00
|
|
|
const isSelfRecursive = subObject[key].match(`{{ ?${key}[ |][^}]*}}`);
|
2018-07-25 20:58:01 +00:00
|
|
|
|
|
|
|
if (isSelfRecursive) {
|
|
|
|
// If we're overwriting a variable that contains itself, make sure we
|
|
|
|
// render it first
|
2019-08-11 09:12:48 +00:00
|
|
|
subContext[key] = await render(
|
|
|
|
subObject[key],
|
|
|
|
subContext, // Only render with key being overwritten
|
2018-07-25 20:58:01 +00:00
|
|
|
null,
|
|
|
|
KEEP_ON_ERROR,
|
2018-12-12 17:36:11 +00:00
|
|
|
'Environment',
|
2018-07-25 20:58:01 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Otherwise it's just a regular replacement
|
2019-08-11 09:12:48 +00:00
|
|
|
subContext[key] = subObject[key];
|
2018-07-25 20:58:01 +00:00
|
|
|
}
|
2019-08-13 15:31:02 +00:00
|
|
|
} else if (Object.prototype.toString.call(subContext[key]) === '[object Object]') {
|
|
|
|
// Context is of Type object, Call this function recursively to handle nested objects.
|
2019-08-11 09:12:48 +00:00
|
|
|
subContext[key] = renderSubContext(subObject[key], subContext[key]);
|
2017-05-15 20:55:05 +00:00
|
|
|
} else {
|
2019-08-13 15:31:02 +00:00
|
|
|
// For all other Types, add the Object to the Context.
|
2019-08-11 09:12:48 +00:00
|
|
|
subContext[key] = subObject[key];
|
2017-05-15 20:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-11 09:12:48 +00:00
|
|
|
return subContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const envObject: Object of envObjects) {
|
|
|
|
// For every environment render the Objects
|
|
|
|
renderContext = await renderSubContext(envObject, renderContext);
|
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;
|
|
|
|
|
2017-11-27 15:52:35 +00:00
|
|
|
const keys = _getOrderedEnvironmentKeys(finalRenderContext);
|
2019-05-15 20:01:36 +00:00
|
|
|
|
|
|
|
// Render recursive references and tags.
|
|
|
|
const skipNextTime = {};
|
2017-02-07 16:09:12 +00:00
|
|
|
for (let i = 0; i < 3; i++) {
|
2017-11-27 15:52:35 +00:00
|
|
|
for (const key of keys) {
|
2019-05-15 20:01:36 +00:00
|
|
|
// Skip rendering keys that stayed the same multiple times. This is here because
|
|
|
|
// a render failure will leave the tag as-is and thus the next iteration of the
|
|
|
|
// loop will try to re-render it again. We don't want to keep erroring on these
|
|
|
|
// because renders are expensive and potentially not idempotent.
|
|
|
|
if (skipNextTime[key]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderResult = await render(
|
2017-11-27 15:52:35 +00:00
|
|
|
finalRenderContext[key],
|
|
|
|
finalRenderContext,
|
|
|
|
null,
|
|
|
|
KEEP_ON_ERROR,
|
2018-12-12 17:36:11 +00:00
|
|
|
'Environment',
|
2017-11-27 15:52:35 +00:00
|
|
|
);
|
2019-05-15 20:01:36 +00:00
|
|
|
|
|
|
|
// Result didn't change, so skip
|
|
|
|
if (renderResult === finalRenderContext[key]) {
|
|
|
|
skipNextTime[key] = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
finalRenderContext[key] = renderResult;
|
2017-11-27 15:52:35 +00:00
|
|
|
}
|
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
|
2017-05-15 20:55:05 +00:00
|
|
|
* @param {*} obj - object to render
|
2017-02-20 18:32:27 +00:00
|
|
|
* @param {object} context - context to render against
|
2017-03-28 22:45:23 +00:00
|
|
|
* @param blacklistPathRegex - don't render these paths
|
2017-07-11 01:05:54 +00:00
|
|
|
* @param errorMode - how to handle errors
|
|
|
|
* @param name - name to include in error message
|
2017-02-20 18:32:27 +00:00
|
|
|
* @return {Promise.<*>}
|
|
|
|
*/
|
2018-06-25 17:42:50 +00:00
|
|
|
export async function render<T>(
|
2017-07-20 01:55:40 +00:00
|
|
|
obj: T,
|
|
|
|
context: Object = {},
|
|
|
|
blacklistPathRegex: RegExp | null = null,
|
|
|
|
errorMode: string = THROW_ON_ERROR,
|
2018-12-12 17:36:11 +00:00
|
|
|
name: string = '',
|
2017-07-20 01:55:40 +00:00
|
|
|
): Promise<T> {
|
2017-05-15 20:55:05 +00:00
|
|
|
// Make a deep copy so no one gets mad :)
|
|
|
|
const newObj = clone(obj);
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2018-07-25 20:58:01 +00:00
|
|
|
async function next(x: any, path: string, first: boolean = false): Promise<any> {
|
2017-03-28 22:45:23 +00:00
|
|
|
if (blacklistPathRegex && path.match(blacklistPathRegex)) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2017-05-15 20:55:05 +00:00
|
|
|
const asStr = Object.prototype.toString.call(x);
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
// Leave these types alone
|
|
|
|
if (
|
2017-05-15 20:55:05 +00:00
|
|
|
asStr === '[object Date]' ||
|
|
|
|
asStr === '[object RegExp]' ||
|
|
|
|
asStr === '[object Error]' ||
|
|
|
|
asStr === '[object Boolean]' ||
|
|
|
|
asStr === '[object Number]' ||
|
|
|
|
asStr === '[object Null]' ||
|
|
|
|
asStr === '[object Undefined]'
|
2017-02-20 18:32:27 +00:00
|
|
|
) {
|
|
|
|
// Do nothing to these types
|
2017-07-20 01:55:40 +00:00
|
|
|
} else if (typeof x === 'string') {
|
2017-02-20 18:32:27 +00:00
|
|
|
try {
|
2018-06-25 17:42:50 +00:00
|
|
|
x = await templating.render(x, { context, path });
|
2017-05-15 20:55:05 +00:00
|
|
|
|
|
|
|
// If the variable outputs a tag, render it again. This is a common use
|
|
|
|
// case for environment variables:
|
|
|
|
// {{ foo }} => {% uuid 'v4' %} => dd265685-16a3-4d76-a59c-e8264c16835a
|
|
|
|
if (x.includes('{%')) {
|
2018-06-25 17:42:50 +00:00
|
|
|
x = await templating.render(x, { context, path });
|
2017-05-15 20:55:05 +00:00
|
|
|
}
|
2017-02-20 18:32:27 +00:00
|
|
|
} catch (err) {
|
2017-07-11 01:05:54 +00:00
|
|
|
if (errorMode !== KEEP_ON_ERROR) {
|
|
|
|
throw err;
|
|
|
|
}
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
|
|
|
} else if (Array.isArray(x)) {
|
|
|
|
for (let i = 0; i < x.length; i++) {
|
2017-03-28 22:45:23 +00:00
|
|
|
x[i] = await next(x[i], `${path}[${i}]`);
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
2017-07-20 01:55:40 +00:00
|
|
|
} else if (typeof x === 'object' && x !== null) {
|
2017-03-28 22:45:23 +00:00
|
|
|
// Don't even try rendering disabled objects
|
2017-05-15 20:55:05 +00:00
|
|
|
// Note, this logic probably shouldn't be here, but w/e for now
|
2017-03-28 22:45:23 +00:00
|
|
|
if (x.disabled) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
const keys = Object.keys(x);
|
|
|
|
for (const key of keys) {
|
2017-12-21 14:01:51 +00:00
|
|
|
if (first && key.indexOf('_') === 0) {
|
|
|
|
x[key] = await next(x[key], path);
|
|
|
|
} else {
|
|
|
|
const pathPrefix = path ? path + '.' : '';
|
|
|
|
x[key] = await next(x[key], `${pathPrefix}${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;
|
|
|
|
}
|
|
|
|
|
2017-12-21 14:01:51 +00:00
|
|
|
return next(newObj, name, true);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-08-15 22:31:30 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export async function getRenderContext(
|
2017-07-20 01:55:40 +00:00
|
|
|
request: Request,
|
2019-05-04 20:34:52 +00:00
|
|
|
environmentId: string | null,
|
2017-12-21 14:01:51 +00:00
|
|
|
ancestors: Array<BaseModel> | null = null,
|
2019-04-27 04:34:15 +00:00
|
|
|
purpose: RenderPurpose | null = null,
|
2019-05-15 20:01:36 +00:00
|
|
|
extraInfo: ExtraRenderInfo | null = null,
|
2017-07-20 01:55:40 +00:00
|
|
|
): Promise<Object> {
|
2017-02-13 08:12:02 +00:00
|
|
|
if (!ancestors) {
|
2017-04-21 04:30:52 +00:00
|
|
|
ancestors = await db.withAncestors(request, [
|
2017-11-10 10:50:39 +00:00
|
|
|
models.request.type,
|
2017-04-21 04:30:52 +00:00
|
|
|
models.requestGroup.type,
|
2018-12-12 17:36:11 +00:00
|
|
|
models.workspace.type,
|
2017-04-21 04:30:52 +00:00
|
|
|
]);
|
2017-02-13 08:12:02 +00:00
|
|
|
}
|
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);
|
2017-11-10 10:50:39 +00:00
|
|
|
if (!workspace) {
|
|
|
|
throw new Error('Failed to render. Could not find workspace');
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
const rootEnvironment = await models.environment.getOrCreateForWorkspaceId(
|
2018-12-12 17:36:11 +00:00
|
|
|
workspace ? workspace._id : 'n/a',
|
2018-06-25 17:42:50 +00:00
|
|
|
);
|
2019-05-04 20:34:52 +00:00
|
|
|
const subEnvironment = await models.environment.getById(environmentId || 'n/a');
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2019-08-16 21:19:25 +00:00
|
|
|
const keySource = {};
|
2019-05-15 20:01:36 +00:00
|
|
|
|
2019-08-16 20:54:01 +00:00
|
|
|
// Function that gets Keys and stores their Source location
|
|
|
|
function getKeySource(subObject, inKey, inSource) {
|
2019-08-16 21:19:25 +00:00
|
|
|
// Add key to map if it's not root
|
|
|
|
if (inKey) {
|
|
|
|
keySource[inKey] = inSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recurse down for Objects and Arrays
|
|
|
|
const typeStr = Object.prototype.toString.call(subObject);
|
|
|
|
if (typeStr === '[object Object]') {
|
|
|
|
for (const key of Object.keys(subObject)) {
|
|
|
|
getKeySource(subObject[key], inKey ? `${inKey}.${key}` : key, inSource);
|
|
|
|
}
|
|
|
|
} else if (typeStr === '[object Array]') {
|
|
|
|
for (let i = 0; i < subObject.length; i++) {
|
|
|
|
getKeySource(subObject[i], `${inKey}[${i}]`, inSource);
|
2018-11-30 05:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 20:01:36 +00:00
|
|
|
|
2019-08-16 20:54:01 +00:00
|
|
|
// Get Keys from root environment
|
|
|
|
getKeySource((rootEnvironment || {}).data, '', 'root');
|
|
|
|
|
|
|
|
// Get Keys from sub environment
|
2018-11-30 05:50:30 +00:00
|
|
|
if (subEnvironment) {
|
2019-08-16 20:54:01 +00:00
|
|
|
getKeySource(subEnvironment.data || {}, '', subEnvironment.name || '');
|
2018-11-30 05:50:30 +00:00
|
|
|
}
|
2019-05-15 20:01:36 +00:00
|
|
|
|
2019-08-16 20:54:01 +00:00
|
|
|
// Get Keys from ancestors (e.g. Folders)
|
2018-11-30 05:50:30 +00:00
|
|
|
if (ancestors) {
|
|
|
|
for (let idx = 0; idx < ancestors.length; idx++) {
|
|
|
|
let ancestor: any = ancestors[idx] || {};
|
|
|
|
if (
|
|
|
|
ancestor.type === 'RequestGroup' &&
|
|
|
|
ancestor.hasOwnProperty('environment') &&
|
|
|
|
ancestor.hasOwnProperty('name')
|
|
|
|
) {
|
2019-08-16 20:54:01 +00:00
|
|
|
getKeySource(ancestor.environment || {}, '', ancestor.name || '');
|
2018-11-30 05:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
// Add meta data helper function
|
|
|
|
const baseContext = {};
|
|
|
|
baseContext.getMeta = () => ({
|
2018-06-25 19:58:03 +00:00
|
|
|
requestId: request ? request._id : null,
|
2018-12-12 17:36:11 +00:00
|
|
|
workspaceId: workspace ? workspace._id : 'n/a',
|
2017-06-09 01:10:12 +00:00
|
|
|
});
|
|
|
|
|
2018-11-30 05:50:30 +00:00
|
|
|
baseContext.getKeysContext = () => ({
|
2018-12-12 17:36:11 +00:00
|
|
|
keyContext: keySource,
|
2018-11-30 05:50:30 +00:00
|
|
|
});
|
|
|
|
|
2017-12-21 14:01:51 +00:00
|
|
|
baseContext.getPurpose = () => purpose;
|
2019-05-15 20:01:36 +00:00
|
|
|
baseContext.getExtraInfo = (key: string) => {
|
|
|
|
if (!Array.isArray(extraInfo)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const p = extraInfo.find(v => v.name === key);
|
|
|
|
return p ? p.value : null;
|
|
|
|
};
|
2017-12-21 14:01:51 +00:00
|
|
|
|
2019-04-27 04:34:15 +00:00
|
|
|
baseContext.getEnvironmentId = () => environmentId;
|
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
// Generate the context we need to render
|
2018-07-25 20:58:01 +00:00
|
|
|
return buildRenderContext(ancestors, rootEnvironment, subEnvironment, baseContext);
|
2017-02-13 08:12:02 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export async function getRenderedRequestAndContext(
|
2017-07-20 01:55:40 +00:00
|
|
|
request: Request,
|
2019-05-04 20:34:52 +00:00
|
|
|
environmentId: string | null,
|
2019-04-27 04:34:15 +00:00
|
|
|
purpose?: RenderPurpose,
|
2019-05-15 20:01:36 +00:00
|
|
|
extraInfo?: ExtraRenderInfo,
|
2018-06-25 17:42:50 +00:00
|
|
|
): Promise<{ request: RenderedRequest, context: Object }> {
|
2017-04-21 04:30:52 +00:00
|
|
|
const ancestors = await db.withAncestors(request, [
|
2017-11-10 10:50:39 +00:00
|
|
|
models.request.type,
|
2017-04-21 04:30:52 +00:00
|
|
|
models.requestGroup.type,
|
2018-12-12 17:36:11 +00:00
|
|
|
models.workspace.type,
|
2017-04-21 04:30:52 +00:00
|
|
|
]);
|
2017-02-13 08:12:02 +00:00
|
|
|
const workspace = ancestors.find(doc => doc.type === models.workspace.type);
|
2017-08-22 22:30:57 +00:00
|
|
|
const parentId = workspace ? workspace._id : 'n/a';
|
|
|
|
const cookieJar = await models.cookieJar.getOrCreateForParentId(parentId);
|
2017-02-13 08:12:02 +00:00
|
|
|
|
2019-05-15 20:01:36 +00:00
|
|
|
const renderContext = await getRenderContext(
|
|
|
|
request,
|
|
|
|
environmentId,
|
|
|
|
ancestors,
|
|
|
|
purpose,
|
|
|
|
extraInfo || null,
|
|
|
|
);
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2018-06-28 03:49:40 +00:00
|
|
|
// HACK: Switch '#}' to '# }' to prevent Nunjucks from barfing
|
2019-12-17 17:16:08 +00:00
|
|
|
// https://github.com/kong/insomnia/issues/895
|
2018-06-28 03:43:38 +00:00
|
|
|
try {
|
|
|
|
if (request.body.text && request.body.mimeType === CONTENT_TYPE_GRAPHQL) {
|
|
|
|
const o = JSON.parse(request.body.text);
|
|
|
|
o.query = o.query.replace(/#}/g, '# }');
|
|
|
|
request.body.text = JSON.stringify(o);
|
|
|
|
}
|
|
|
|
} catch (err) {}
|
|
|
|
|
2019-05-07 18:24:04 +00:00
|
|
|
// Render description separately because it's lower priority
|
|
|
|
const description = request.description;
|
|
|
|
request.description = '';
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
// Render all request properties
|
2017-12-21 14:01:51 +00:00
|
|
|
const renderResult = await render(
|
2018-06-25 17:42:50 +00:00
|
|
|
{ _request: request, _cookieJar: cookieJar },
|
2017-03-28 22:45:23 +00:00
|
|
|
renderContext,
|
2018-12-12 17:36:11 +00:00
|
|
|
request.settingDisableRenderRequestBody ? /^body.*/ : null,
|
2017-03-28 22:45:23 +00:00
|
|
|
);
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2017-12-21 14:01:51 +00:00
|
|
|
const renderedRequest = renderResult._request;
|
|
|
|
const renderedCookieJar = renderResult._cookieJar;
|
2019-05-07 18:24:04 +00:00
|
|
|
renderedRequest.description = await render(description, renderContext, null, KEEP_ON_ERROR);
|
2017-08-18 21:17:18 +00:00
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
// Remove disabled params
|
2018-07-25 20:58:01 +00:00
|
|
|
renderedRequest.parameters = renderedRequest.parameters.filter(p => !p.disabled);
|
2016-11-22 19:42:10 +00:00
|
|
|
|
|
|
|
// Remove disabled headers
|
|
|
|
renderedRequest.headers = renderedRequest.headers.filter(p => !p.disabled);
|
|
|
|
|
|
|
|
// Remove disabled body params
|
|
|
|
if (renderedRequest.body && Array.isArray(renderedRequest.body.params)) {
|
2018-07-25 20:58:01 +00:00
|
|
|
renderedRequest.body.params = renderedRequest.body.params.filter(p => !p.disabled);
|
2016-11-22 19:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove disabled authentication
|
2018-07-25 20:58:01 +00:00
|
|
|
if (renderedRequest.authentication && renderedRequest.authentication.disabled) {
|
2017-03-03 20:09:08 +00:00
|
|
|
renderedRequest.authentication = {};
|
2016-11-22 19:42:10 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
// Default the proto if it doesn't exist
|
|
|
|
renderedRequest.url = setDefaultProtocol(renderedRequest.url);
|
|
|
|
|
2017-07-20 01:55:40 +00:00
|
|
|
return {
|
2017-12-21 14:01:51 +00:00
|
|
|
context: renderContext,
|
|
|
|
request: {
|
|
|
|
// Add the yummy cookies
|
|
|
|
cookieJar: renderedCookieJar,
|
|
|
|
cookies: [],
|
2018-03-06 03:15:09 +00:00
|
|
|
isPrivate: false,
|
2017-12-21 14:01:51 +00:00
|
|
|
|
|
|
|
// NOTE: Flow doesn't like Object.assign, so we have to do each property manually
|
|
|
|
// for now to convert Request to RenderedRequest.
|
|
|
|
_id: renderedRequest._id,
|
|
|
|
authentication: renderedRequest.authentication,
|
|
|
|
body: renderedRequest.body,
|
|
|
|
created: renderedRequest.created,
|
|
|
|
modified: renderedRequest.modified,
|
|
|
|
description: renderedRequest.description,
|
|
|
|
headers: renderedRequest.headers,
|
|
|
|
metaSortKey: renderedRequest.metaSortKey,
|
|
|
|
method: renderedRequest.method,
|
|
|
|
name: renderedRequest.name,
|
|
|
|
parameters: renderedRequest.parameters,
|
|
|
|
parentId: renderedRequest.parentId,
|
2018-07-25 20:58:01 +00:00
|
|
|
settingDisableRenderRequestBody: renderedRequest.settingDisableRenderRequestBody,
|
2017-12-21 14:01:51 +00:00
|
|
|
settingEncodeUrl: renderedRequest.settingEncodeUrl,
|
|
|
|
settingSendCookies: renderedRequest.settingSendCookies,
|
|
|
|
settingStoreCookies: renderedRequest.settingStoreCookies,
|
2018-03-26 17:43:42 +00:00
|
|
|
settingRebuildPath: renderedRequest.settingRebuildPath,
|
2019-10-31 16:02:03 +00:00
|
|
|
settingFollowRedirects: renderedRequest.settingFollowRedirects,
|
2017-12-21 14:01:51 +00:00
|
|
|
type: renderedRequest.type,
|
2018-12-12 17:36:11 +00:00
|
|
|
url: renderedRequest.url,
|
|
|
|
},
|
2017-07-20 01:55:40 +00:00
|
|
|
};
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2017-11-27 15:52:35 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export async function getRenderedRequest(
|
2017-12-21 14:01:51 +00:00
|
|
|
request: Request,
|
|
|
|
environmentId: string,
|
2019-04-27 04:34:15 +00:00
|
|
|
purpose?: RenderPurpose,
|
2017-12-21 14:01:51 +00:00
|
|
|
): Promise<RenderedRequest> {
|
2018-07-25 20:58:01 +00:00
|
|
|
const result = await getRenderedRequestAndContext(request, environmentId, purpose);
|
2017-12-21 14:01:51 +00:00
|
|
|
return result.request;
|
|
|
|
}
|
|
|
|
|
2017-11-27 15:52:35 +00:00
|
|
|
/**
|
|
|
|
* Sort the keys that may have Nunjucks last, so that other keys get
|
|
|
|
* defined first. Very important if env variables defined in same obj
|
|
|
|
* (eg. {"foo": "{{ bar }}", "bar": "Hello World!"})
|
|
|
|
*
|
|
|
|
* @param v
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
2018-06-25 17:42:50 +00:00
|
|
|
function _nunjucksSortValue(v) {
|
2017-11-27 15:52:35 +00:00
|
|
|
if (v && v.match && v.match(/({%)/)) {
|
|
|
|
return 3;
|
|
|
|
} else if (v && v.match && v.match(/({{)/)) {
|
|
|
|
return 2;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
function _getOrderedEnvironmentKeys(finalRenderContext: Object): Array<string> {
|
2017-11-27 15:52:35 +00:00
|
|
|
return Object.keys(finalRenderContext).sort((k1, k2) => {
|
|
|
|
const k1Sort = _nunjucksSortValue(finalRenderContext[k1]);
|
|
|
|
const k2Sort = _nunjucksSortValue(finalRenderContext[k2]);
|
|
|
|
return k1Sort - k2Sort;
|
|
|
|
});
|
|
|
|
}
|