2018-06-25 17:42:50 +00:00
|
|
|
import type { Request } from '../models/request';
|
|
|
|
import type { BaseModel } from '../models/index';
|
|
|
|
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';
|
2020-06-09 21:14:05 +00:00
|
|
|
import { CONTENT_TYPE_GRAPHQL, JSON_ORDER_SEPARATOR } from './constants';
|
2021-05-12 06:35:00 +00:00
|
|
|
import { database 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';
|
2020-03-09 18:20:22 +00:00
|
|
|
import orderedJSON from 'json-order';
|
2020-10-01 18:49:05 +00:00
|
|
|
import * as templatingUtils from '../templating/utils';
|
2020-12-17 11:38:21 +00:00
|
|
|
import type { GrpcRequest, GrpcRequestBody } from '../models/grpc-request';
|
|
|
|
import { isRequestGroup } from '../models/helpers/is-model';
|
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';
|
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 */
|
2021-05-12 06:35:00 +00:00
|
|
|
export type ExtraRenderInfo = Array<{
|
|
|
|
name: string;
|
|
|
|
value: any;
|
|
|
|
}>;
|
2019-05-15 20:01:36 +00:00
|
|
|
|
2017-07-20 01:55:40 +00:00
|
|
|
export type RenderedRequest = Request & {
|
2021-05-12 06:35:00 +00:00
|
|
|
cookies: Array<{
|
|
|
|
name: string;
|
|
|
|
value: string;
|
|
|
|
disabled?: boolean;
|
|
|
|
}>;
|
|
|
|
cookieJar: CookieJar;
|
2017-07-20 01:55:40 +00:00
|
|
|
};
|
|
|
|
|
2020-12-17 11:38:21 +00:00
|
|
|
export type RenderedGrpcRequest = GrpcRequest;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2020-12-17 11:38:21 +00:00
|
|
|
export type RenderedGrpcRequestBody = GrpcRequestBody;
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export interface RenderContextAndKeys {
|
|
|
|
context: Record<string, any>;
|
|
|
|
keys: Array<{
|
|
|
|
name: string;
|
|
|
|
value: any;
|
|
|
|
}>
|
|
|
|
}
|
|
|
|
|
|
|
|
export type HandleGetRenderContext = () => Promise<RenderContextAndKeys>;
|
|
|
|
|
|
|
|
export type HandleRender = <T>(object: T, contextCacheKey?: string | null) => Promise<T>;
|
|
|
|
|
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,
|
2021-05-12 06:35:00 +00:00
|
|
|
baseContext: Record<string, any> = {},
|
|
|
|
) {
|
|
|
|
const envObjects: Array<Record<string, any>> = [];
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2020-03-09 18:20:22 +00:00
|
|
|
// Get root environment keys in correct order
|
|
|
|
// Then get sub environment keys in correct order
|
|
|
|
// Then get ancestor (folder) environment keys in correct order
|
2016-09-03 04:32:45 +00:00
|
|
|
if (rootEnvironment) {
|
2020-03-09 18:20:22 +00:00
|
|
|
const ordered = orderedJSON.order(
|
|
|
|
rootEnvironment.data,
|
|
|
|
rootEnvironment.dataPropertyOrder,
|
2020-06-09 21:14:05 +00:00
|
|
|
JSON_ORDER_SEPARATOR,
|
2020-03-09 18:20:22 +00:00
|
|
|
);
|
|
|
|
envObjects.push(ordered);
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (subEnvironment) {
|
2020-03-09 18:20:22 +00:00
|
|
|
const ordered = orderedJSON.order(
|
|
|
|
subEnvironment.data,
|
|
|
|
subEnvironment.dataPropertyOrder,
|
2020-06-09 21:14:05 +00:00
|
|
|
JSON_ORDER_SEPARATOR,
|
2020-03-09 18:20:22 +00:00
|
|
|
);
|
|
|
|
envObjects.push(ordered);
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 01:55:40 +00:00
|
|
|
for (const doc of (ancestors || []).reverse()) {
|
2020-03-09 18:20:22 +00:00
|
|
|
const ancestor: any = doc;
|
|
|
|
const { environment, environmentPropertyOrder } = ancestor;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2018-03-29 17:59:32 +00:00
|
|
|
if (typeof environment === 'object' && environment !== null) {
|
2020-06-09 21:14:05 +00:00
|
|
|
const ordered = orderedJSON.order(
|
|
|
|
environment,
|
|
|
|
environmentPropertyOrder,
|
|
|
|
JSON_ORDER_SEPARATOR,
|
|
|
|
);
|
2020-03-09 18:20:22 +00:00
|
|
|
envObjects.push(ordered);
|
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
|
2020-03-09 18:20:22 +00:00
|
|
|
// from top-most parent to bottom-most child, and they keys in each environment
|
|
|
|
// ordered by its property map.
|
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
|
2021-05-12 06:35:00 +00:00
|
|
|
async function renderSubContext(
|
|
|
|
subObject: Record<string, any>,
|
|
|
|
subContext: Record<string, any>,
|
|
|
|
) {
|
2019-08-11 09:12:48 +00:00
|
|
|
const keys = _getOrderedEnvironmentKeys(subObject);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-08-11 09:12:48 +00:00
|
|
|
return subContext;
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
for (const envObject of envObjects) {
|
2019-08-11 09:12:48 +00:00
|
|
|
// 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.
|
2020-04-09 17:32:19 +00:00
|
|
|
const finalRenderContext = renderContext;
|
2017-02-07 16:09:12 +00:00
|
|
|
|
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 = {};
|
2021-05-12 06:35:00 +00:00
|
|
|
|
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
|
|
|
}
|
2021-05-12 06:35:00 +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,
|
2021-05-12 06:35:00 +00:00
|
|
|
context: Record<string, any> = {},
|
2017-07-20 01:55:40 +00:00
|
|
|
blacklistPathRegex: RegExp | null = null,
|
|
|
|
errorMode: string = THROW_ON_ERROR,
|
2021-05-12 06:35:00 +00:00
|
|
|
name = '',
|
|
|
|
) {
|
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
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
async function next<T>(x: T, path: string, first = false) {
|
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 {
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION
|
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
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION
|
2017-05-15 20:55:05 +00:00
|
|
|
if (x.includes('{%')) {
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION
|
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
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION
|
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);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
return next<T>(newObj, name, true);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2021-05-17 13:59:43 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export async function getRenderContext(
|
2021-05-12 06:35:00 +00:00
|
|
|
request: Request | GrpcRequest | null,
|
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,
|
2021-05-12 06:35:00 +00:00
|
|
|
): Promise<Record<string, any>> {
|
2017-02-13 08:12:02 +00:00
|
|
|
if (!ancestors) {
|
2020-12-17 11:38:21 +00:00
|
|
|
ancestors = await _getRequestAncestors(request);
|
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);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
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');
|
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) {
|
2020-10-01 18:49:05 +00:00
|
|
|
keySource[templatingUtils.normalizeToDotAndBracketNotation(inKey)] = inSource;
|
2019-08-16 21:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Recurse down for Objects and Arrays
|
|
|
|
const typeStr = Object.prototype.toString.call(subObject);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-08-16 21:19:25 +00:00
|
|
|
if (typeStr === '[object Object]') {
|
|
|
|
for (const key of Object.keys(subObject)) {
|
2020-10-01 18:49:05 +00:00
|
|
|
getKeySource(subObject[key], templatingUtils.forceBracketNotation(inKey, key), inSource);
|
2019-08-16 21:19:25 +00:00
|
|
|
}
|
|
|
|
} else if (typeStr === '[object Array]') {
|
|
|
|
for (let i = 0; i < subObject.length; i++) {
|
2020-10-01 18:49:05 +00:00
|
|
|
getKeySource(subObject[i], templatingUtils.forceBracketNotation(inKey, i), inSource);
|
2018-11-30 05:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 20:01:36 +00:00
|
|
|
|
2020-10-01 18:49:05 +00:00
|
|
|
const inKey = templating.NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME;
|
2019-08-16 20:54:01 +00:00
|
|
|
// Get Keys from root environment
|
2020-10-01 18:49:05 +00:00
|
|
|
getKeySource((rootEnvironment || {}).data, inKey, 'root');
|
2019-08-16 20:54:01 +00:00
|
|
|
|
|
|
|
// Get Keys from sub environment
|
2018-11-30 05:50:30 +00:00
|
|
|
if (subEnvironment) {
|
2020-10-01 18:49:05 +00:00
|
|
|
getKeySource(subEnvironment.data || {}, inKey, 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++) {
|
2020-04-09 17:32:19 +00:00
|
|
|
const ancestor: any = ancestors[idx] || {};
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2018-11-30 05:50:30 +00:00
|
|
|
if (
|
2020-12-17 11:38:21 +00:00
|
|
|
isRequestGroup(ancestor) &&
|
2018-11-30 05:50:30 +00:00
|
|
|
ancestor.hasOwnProperty('environment') &&
|
|
|
|
ancestor.hasOwnProperty('name')
|
|
|
|
) {
|
2020-10-01 18:49:05 +00:00
|
|
|
getKeySource(ancestor.environment || {}, inKey, ancestor.name || '');
|
2018-11-30 05:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
// Add meta data helper function
|
2021-05-12 06:35:00 +00:00
|
|
|
const baseContext: Record<string, any> = {};
|
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
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;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
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
|
|
|
}
|
2021-05-17 13:59:43 +00:00
|
|
|
|
2020-12-17 11:38:21 +00:00
|
|
|
export async function getRenderedGrpcRequest(
|
|
|
|
request: GrpcRequest,
|
|
|
|
environmentId: string | null,
|
|
|
|
purpose?: RenderPurpose,
|
|
|
|
extraInfo?: ExtraRenderInfo,
|
|
|
|
skipBody?: boolean,
|
2021-05-12 06:35:00 +00:00
|
|
|
) {
|
2020-12-17 11:38:21 +00:00
|
|
|
const renderContext = await getRenderContext(
|
|
|
|
request,
|
|
|
|
environmentId,
|
|
|
|
null,
|
|
|
|
purpose,
|
|
|
|
extraInfo || null,
|
|
|
|
);
|
|
|
|
const description = request.description;
|
|
|
|
// Render description separately because it's lower priority
|
|
|
|
request.description = '';
|
|
|
|
// Ignore body by default and only include if specified to
|
|
|
|
const ignorePathRegex = skipBody ? /^body.*/ : null;
|
|
|
|
// Render all request properties
|
|
|
|
const renderedRequest: RenderedGrpcRequest = await render(
|
|
|
|
request,
|
|
|
|
renderContext,
|
|
|
|
ignorePathRegex,
|
|
|
|
);
|
|
|
|
renderedRequest.description = await render(description, renderContext, null, KEEP_ON_ERROR);
|
|
|
|
return renderedRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getRenderedGrpcRequestMessage(
|
|
|
|
request: GrpcRequest,
|
|
|
|
environmentId: string | null,
|
|
|
|
purpose?: RenderPurpose,
|
|
|
|
extraInfo?: ExtraRenderInfo,
|
2021-05-12 06:35:00 +00:00
|
|
|
) {
|
2020-12-17 11:38:21 +00:00
|
|
|
const renderContext = await getRenderContext(
|
|
|
|
request,
|
|
|
|
environmentId,
|
|
|
|
null,
|
|
|
|
purpose,
|
|
|
|
extraInfo || null,
|
|
|
|
);
|
|
|
|
// Render request body
|
|
|
|
const renderedBody: RenderedGrpcRequestBody = await render(request.body, renderContext);
|
|
|
|
return renderedBody;
|
|
|
|
}
|
2021-05-17 13:59:43 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export async function getRenderedRequestAndContext(
|
2017-07-20 01:55:40 +00:00
|
|
|
request: Request,
|
2021-05-17 13:59:43 +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,
|
2021-05-12 06:35:00 +00:00
|
|
|
) {
|
2020-12-17 11:38:21 +00:00
|
|
|
const ancestors = await _getRequestAncestors(request);
|
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);
|
2019-05-15 20:01:36 +00:00
|
|
|
const renderContext = await getRenderContext(
|
|
|
|
request,
|
2021-05-17 13:59:43 +00:00
|
|
|
environmentId || null,
|
2019-05-15 20:01:36 +00:00
|
|
|
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);
|
|
|
|
}
|
2021-05-12 06:35:00 +00:00
|
|
|
} catch (err) { }
|
2018-06-28 03:43:38 +00:00
|
|
|
|
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(
|
2021-05-12 06:35:00 +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
|
|
|
);
|
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);
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2020-03-09 18:20:22 +00:00
|
|
|
return v && v.match && v.match(/({{|{%)/) ? 2 : 1;
|
2017-11-27 15:52:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
function _getOrderedEnvironmentKeys(finalRenderContext: Record<string, any>): Array<string> {
|
2017-11-27 15:52:35 +00:00
|
|
|
return Object.keys(finalRenderContext).sort((k1, k2) => {
|
|
|
|
const k1Sort = _nunjucksSortValue(finalRenderContext[k1]);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-11-27 15:52:35 +00:00
|
|
|
const k2Sort = _nunjucksSortValue(finalRenderContext[k2]);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-11-27 15:52:35 +00:00
|
|
|
return k1Sort - k2Sort;
|
|
|
|
});
|
|
|
|
}
|
2020-12-17 11:38:21 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
async function _getRequestAncestors(request: Request | GrpcRequest | null): Promise<Array<BaseModel>> {
|
2020-12-17 11:38:21 +00:00
|
|
|
return await db.withAncestors(request, [
|
|
|
|
models.request.type,
|
|
|
|
models.grpcRequest.type,
|
|
|
|
models.requestGroup.type,
|
|
|
|
models.workspace.type,
|
|
|
|
]);
|
|
|
|
}
|