Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/insomnia/src/common/__tests__/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,4 +737,43 @@ describe('render tests', () => {
);
});
});

describe('getRenderedRequestAndContext()', () => {
it('renders request templates in the main-process path', async () => {
const workspace = await services.workspace.create();
const environment = await services.environment.create({
parentId: workspace._id,
data: {
host: 'example.com',
name: 'world',
},
});
const request = Object.assign(models.request.init(), {
parentId: workspace._id,
name: 'Hello {{ name }}',
url: 'https://{{ host }}/{{ name }}',
});
const originalProcessType = process.type;

Object.defineProperty(process, 'type', {
configurable: true,
value: 'browser',
});

try {
const result = await renderUtils.getRenderedRequestAndContext({
request,
environment: environment._id,
});

expect(result.request.name).toBe('Hello world');
expect(result.request.url).toBe('https://example.com/world');
} finally {
Object.defineProperty(process, 'type', {
configurable: true,
value: originalProcessType,
});
Comment on lines +772 to +775
}
});
});
});
11 changes: 7 additions & 4 deletions packages/insomnia/src/plugins/context/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export const init = (renderPurpose: RenderPurpose = 'general'): { app: AppContex
},

getPath: (name: string) => {
if (!isRenderer) {
return '';
}
invariant(name.toLowerCase() === 'desktop', `Unknown path name ${name}`);
return window.app.getPath('desktop');
},
Expand All @@ -50,7 +53,7 @@ export const init = (renderPurpose: RenderPurpose = 'general'): { app: AppContex

showSaveDialog: async (options = {}) => {
const sendOrNoRender = renderPurpose === 'send' || renderPurpose === 'no-render';
if (!sendOrNoRender) {
if (!sendOrNoRender || !isRenderer) {
return null;
}

Expand All @@ -63,9 +66,9 @@ export const init = (renderPurpose: RenderPurpose = 'general'): { app: AppContex
},

clipboard: {
readText: () => window.clipboard.readText(),
writeText: text => window.clipboard.writeText(text),
clear: () => window.clipboard.clear(),
readText: () => (isRenderer ? window.clipboard.readText() : ''),
writeText: text => { if (isRenderer) { window.clipboard.writeText(text); } },
clear: () => { if (isRenderer) { window.clipboard.clear(); } },
},
},
});
20 changes: 20 additions & 0 deletions packages/insomnia/src/templating/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, it } from 'vitest';

import { render } from '../index';

describe('templating/index (Node environment)', () => {
it('renders a simple variable template', async () => {
const result = await render('{{ name }}', { context: { name: 'world' } });
expect(result).toBe('world');
});

it('renders nested context via _ accessor', async () => {
const result = await render('{{ _.greeting }}', { context: { greeting: 'hello' } });
expect(result).toBe('hello');
});

it('returns plain text unchanged', async () => {
const result = await render('no template here');
expect(result).toBe('no template here');
});
});
13 changes: 11 additions & 2 deletions packages/insomnia/src/templating/base-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,24 @@ export default class BaseExtension {
userInfo: os.userInfo(),
};
},
readFile: async (path: string) => window.main.secureReadFile({ path }),
readFile: async (path: string) => {
if (typeof window !== 'undefined' && window.main?.secureReadFile) {
return window.main.secureReadFile({ path });
}
return '';
},
decode: async (buffer: Buffer, encoding = 'utf8') => iconv.decode(buffer, encoding),
encode: async (input: string, encoding: BinaryToTextEncoding) =>
crypto.createHash('md5').update(input).digest(encoding),
render: (str: string) =>
templating.render(str, {
context: renderContext,
}),
openInBrowser: (url: string) => window.main.openInBrowser(url),
openInBrowser: (url: string) => {
if (typeof window !== 'undefined' && window.main?.openInBrowser) {
window.main.openInBrowser(url);
}
},
models: {
request: {
getById: services.request.getById,
Expand Down
12 changes: 10 additions & 2 deletions packages/insomnia/src/templating/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ import { localTemplateTags } from 'insomnia/src/templating/local-template-tags';
import type { Environment } from 'nunjucks';

import BaseExtension from './base-extension';
import { nunjucks } from './nunjucks.client';
import { extractUndefinedVariableKey, RenderError } from './render-error';

// Some constants
export const NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME = '_';

/** Load the appropriate nunjucks runtime: browser bundle for renderer, Node bundle otherwise. */
async function loadNunjucks() {
if (process.type === 'renderer') {
return (await import('./nunjucks.client')).nunjucks;
}
return (await import('./nunjucks.node')).nunjucks;
}

type NunjucksEnvironment = Environment & {
extensions: Record<string, any>;
};
Expand Down Expand Up @@ -139,7 +146,8 @@ async function getNunjucks(ignoreUndefinedEnvVariable?: boolean): Promise<Nunjuc
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// Create Env with Extensions //
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
const nunjucksEnvironment = nunjucks.configure(config) as NunjucksEnvironment;
const nj = await loadNunjucks();
const nunjucksEnvironment = nj.configure(config) as NunjucksEnvironment;
nunjucksEnvironment.addGlobal('range', () => {});
nunjucksEnvironment.addGlobal('cycler', () => {});
nunjucksEnvironment.addGlobal('joiner', () => {});
Expand Down
1 change: 1 addition & 0 deletions packages/insomnia/src/templating/nunjucks.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as nunjucks } from 'nunjucks';
Loading