Skip to content

Commit 0f9bbfd

Browse files
committed
squash
1 parent 4f143c9 commit 0f9bbfd

22 files changed

+72
-88
lines changed

packages/insomnia/scripts/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const start = async () => {
4343
await copyFiles('../bin', buildFolder);
4444
await copyFiles('../src/static', path.join(buildFolder, 'static'));
4545
await copyFiles('../src/icons', buildFolder);
46-
await copyFiles('../src/main/lint-process.mjs', path.join(buildFolder, 'main/lint-process.mjs'));
46+
await copyFiles('../src/entry.utility.spectral.mjs', path.join(buildFolder, 'entry.utility.spectral.mjs'));
4747
await copyFiles('../src/hidden-window.html', path.join(buildFolder, 'hidden-window.html'));
4848

4949
console.log('[build] Complete!');

packages/insomnia/src/common/render.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import type { SocketIORequest } from '../models/socket-io-request';
1616
import type { WebSocketRequest } from '../models/websocket-request';
1717
import { isWorkspace, type Workspace } from '../models/workspace';
1818
import { getOrInheritAuthentication, getOrInheritHeaders } from '../network/network';
19-
import * as templating from '../templating';
2019
import { RenderError } from '../templating/render-error';
20+
import { render as templateRendering } from '../templating/template-rendering';
2121
import type {
2222
BaseRenderContext,
2323
BaseRenderContextOptions,
@@ -230,7 +230,7 @@ export async function buildRenderContext({
230230
return finalRenderContext;
231231
}
232232
const renderInThisProcess = async (input: RenderInputType) => {
233-
return templating.render(input.input, {
233+
return templateRendering(input.input, {
234234
context: input.context,
235235
path: input.path,
236236
ignoreUndefinedEnvVariable: input.ignoreUndefinedEnvVariable,
@@ -300,7 +300,7 @@ export async function render<T>(
300300
const currentProcessIsRendererAndPluginsAreRestricted =
301301
process.type === 'renderer' && pluginsAreRestrictedToRunInWorker;
302302
const renderFork = currentProcessIsRendererAndPluginsAreRestricted
303-
? (await import('../ui/worker/templating-handler')).renderInWorker
303+
? (await import('../ui/worker/create-templating-worker')).renderInWorker
304304
: renderInThisProcess;
305305

306306
// @ts-expect-error -- TSCONVERSION
@@ -442,7 +442,7 @@ export async function getRenderContext({
442442
}
443443
}
444444

445-
const inKey = templating.NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME;
445+
const inKey = '_';
446446

447447
if (rootGlobalEnvironment) {
448448
getKeySource(rootGlobalEnvironment.data || {}, inKey, 'rootGlobal');
@@ -580,7 +580,7 @@ export async function getRenderedRequestAndContext({
580580
o.query = o.query.replace(/#}/g, '# }');
581581
request.body.text = JSON.stringify(o);
582582
}
583-
} catch (err) { }
583+
} catch (err) {}
584584

585585
// Render description separately because it's lower priority
586586
const description = request.description;

packages/insomnia/src/main/lint-process.mjs renamed to packages/insomnia/src/entry.utility.spectral.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-undef */
2-
console.log('[lint-process] Lint worker started');
2+
console.log('[utility] Spectral Lint worker started');
33
import fs from 'node:fs';
44

55
import Spectral from '@stoplight/spectral-core';
@@ -16,14 +16,14 @@ process.parentPort.on('message', async ({ data: { documentContent, rulesetPath }
1616
try {
1717
(await fs.promises.stat(rulesetPath)).isFile();
1818
hasValidCustomRuleset = true;
19-
} catch {}
19+
} catch { }
2020
}
2121
try {
2222
const spectral = new Spectral.Spectral();
2323
const { fetch } = spectralRuntime;
2424
const ruleset = hasValidCustomRuleset ? await bundleAndLoadRuleset(rulesetPath, { fs, fetch }) : oas;
2525
spectral.setRuleset(ruleset);
26-
console.log('[lint-process] Ruleset loaded:', rulesetPath || 'default OAS ruleset');
26+
console.log('[utility] Ruleset loaded:', rulesetPath || 'default OAS ruleset');
2727
const diagnostics = await spectral.run(documentContent);
2828
process.parentPort.postMessage({ diagnostics });
2929
} catch (err) {

packages/insomnia/src/main/ipc/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@ export function registerMainHandlers() {
173173
lintProcess.kill();
174174
}
175175

176-
lintProcess = utilityProcess.fork(path.join(__dirname, 'main/lint-process.mjs'));
176+
lintProcess = utilityProcess.fork(path.join(__dirname, 'entry.utility.spectral.mjs'));
177177

178178
let process: UtilityProcess | null = lintProcess!;
179179

180180
process.on('exit', code => {
181-
console.log('[lint-process] exited with code:', code);
181+
console.log('[main] utilityProcess exited with code:', code);
182182
resolve({ cancelled: true });
183183
});
184184

@@ -189,7 +189,7 @@ export function registerMainHandlers() {
189189
});
190190

191191
process.on('error', err => {
192-
console.error('[lint-process] error:', err);
192+
console.error('[main] utilityProcess error:', err);
193193
reject({ error: err.toString() });
194194
});
195195

packages/insomnia/src/templating/local-template-tags.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { format } from 'date-fns';
2-
import type { TemplateTag } from 'insomnia/src/plugins';
3-
import type { PluginTemplateTag } from 'insomnia/src/templating/types';
4-
import { invariant } from 'insomnia/src/utils/invariant';
52
import { JSONPath } from 'jsonpath-plus';
63

4+
import type { TemplateTag } from '~/plugins';
5+
import type { PluginTemplateTag } from '~/templating/types';
6+
import { invariant } from '~/utils/invariant';
7+
78
import { fakerFunctions } from './faker-functions';
89
const localTemplatePlugins: { templateTag: PluginTemplateTag }[] = [
910
{

packages/insomnia/src/templating/base-extension.ts renamed to packages/insomnia/src/templating/nunjucks-extension.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import * as pluginApp from '../plugins/context/app';
1414
import * as pluginNetwork from '../plugins/context/network';
1515
import * as pluginStore from '../plugins/context/store';
1616
import type { Plugin } from '../plugins/index';
17-
import * as templating from './index';
17+
import { render as templateRendering } from './template-rendering';
1818
import type { BaseRenderContext, PluginTemplateTag, PluginTemplateTagContext } from './types';
1919
import { decodeEncoding } from './utils';
2020

2121
const EMPTY_ARG = '__EMPTY_NUNJUCKS_ARG__';
2222

23-
export default class BaseExtension {
23+
export class NunjucksExtension {
2424
_ext: PluginTemplateTag | null = null;
2525
_plugin: Plugin | null = null;
2626
tags: PluginTemplateTag['name'][] = [];
@@ -120,19 +120,21 @@ export default class BaseExtension {
120120
};
121121
},
122122
readFile: async (path: string, encoding = 'utf8') => {
123-
const allowed = renderContext?.getSettings().dataFolders.some((folder: string) => folder !== '' && path.startsWith(folder));
123+
const allowed = renderContext
124+
?.getSettings()
125+
.dataFolders.some((folder: string) => folder !== '' && path.startsWith(folder));
124126
if (!allowed) {
125127
throw `Insomnia cannot access the file ‘${path}’. You can adjust this in Preferences → Security.`;
126128
}
127-
129+
128130
const content = await fs.promises.readFile(path);
129131
return encoding === 'utf8' ? content.toString(encoding) : content;
130132
},
131133
decode: async (buffer: Buffer, encoding = 'utf8') => iconv.decode(buffer, encoding),
132134
encode: async (input: string, encoding: BinaryToTextEncoding) =>
133135
crypto.createHash('md5').update(input).digest(encoding),
134136
render: (str: string) =>
135-
templating.render(str, {
137+
templateRendering(str, {
136138
context: renderContext,
137139
}),
138140
openInBrowser: (url: string) => window.main.openInBrowser(url),

packages/insomnia/src/templating/index.ts renamed to packages/insomnia/src/templating/template-rendering.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { localTemplateTags } from 'insomnia/src/templating/local-template-tags';
21
import type { Environment } from 'nunjucks';
32

4-
import BaseExtension from './base-extension';
3+
import { localTemplateTags } from '~/templating/local-template-tags';
4+
55
import { nunjucks } from './nunjucks.client';
6+
import { NunjucksExtension } from './nunjucks-extension';
67
import { extractUndefinedVariableKey, RenderError } from './render-error';
78

89
// Some constants
910
export const RENDER_ALL = 'all';
1011
export const RENDER_VARS = 'variables';
1112
export const RENDER_TAGS = 'tags';
12-
export const NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME = '_';
1313

1414
type NunjucksEnvironment = Environment & {
1515
extensions: Record<string, any>;
@@ -47,7 +47,7 @@ export function render(
4747
// context needs to exist on the root for the old templating syntax, and in _ for the new templating syntax
4848
// old: {{ arr[0].prop }}
4949
// new: {{ _['arr-name-with-dash'][0].prop }}
50-
const templatingContext = { ...context, [NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME]: context };
50+
const templatingContext = { ...context, ['_']: context };
5151
const path = config.path || null;
5252
const renderMode = config.renderMode || RENDER_ALL;
5353
return new Promise<string | null>(async (resolve, reject) => {
@@ -186,7 +186,7 @@ async function getNunjucks(renderMode: string, ignoreUndefinedEnvVariable?: bool
186186
for (const extension of allExtensions) {
187187
const { templateTag, plugin } = extension;
188188
templateTag.priority = templateTag.priority || allExtensions.indexOf(extension);
189-
const instance = new BaseExtension(templateTag, plugin);
189+
const instance = new NunjucksExtension(templateTag, plugin);
190190
nunjucksEnvironment.addExtension(instance.getTag() || '', instance);
191191
// Hidden helper filter to debug complicated things
192192
// eg. `{{ foo | urlencode | debug | upper }}`

packages/insomnia/src/ui/components/.client/codemirror/code-editor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import * as misc from '~/common/misc';
2121
import type { KeyCombination } from '~/common/settings';
2222
import { getTemplateTags } from '~/plugins';
2323
import { useRootLoaderData } from '~/root';
24-
import { getTagDefinitions } from '~/templating/index';
24+
import { getTagDefinitions } from '~/templating/template-rendering';
2525
import { type NunjucksParsedTag, type nunjucksTagContextMenuOptions } from '~/templating/types';
2626
import { extractNunjucksTagFromCoords } from '~/templating/utils';
2727
import { Icon } from '~/ui/components/icon';

packages/insomnia/src/ui/components/.client/codemirror/extensions/nunjucks-tags.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import CodeMirror, { type Token } from 'codemirror';
22

33
import * as misc from '~/common/misc';
4-
import { getTagDefinitions } from '~/templating/index';
4+
import { getTagDefinitions } from '~/templating/template-rendering';
55
import type { HandleRender, RenderContextAndKeys } from '~/templating/types';
66
import { tokenizeTag } from '~/templating/utils';
77
import { showModal } from '~/ui/components/modals/index';

packages/insomnia/src/ui/components/.client/codemirror/lint/json-lint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'codemirror/addon/lint/json-lint';
88
import CodeMirror from 'codemirror';
99
import * as jsonlint from 'jsonlint-mod-fixed';
1010

11-
import { render } from '~/templating/index';
11+
import { render } from '~/templating/template-rendering';
1212
CodeMirror.registerHelper('lint', 'json', validator);
1313

1414
interface ValidationError {

0 commit comments

Comments
 (0)