From b8f76deff9a0599d10a0c01285eb5cedbbad5ec1 Mon Sep 17 00:00:00 2001 From: Opender Singh Date: Fri, 26 Feb 2021 10:10:21 +1300 Subject: [PATCH] Add plugin bundle to default config (#3092) --- .../app/__tests__/install.test.js | 6 ++++ .../common/__tests__/local-storage.test.js | 6 ++++ .../app/common/migrate-from-designer.js | 9 +++++ .../insomnia-app/app/common/send-request.js | 34 +++++++++++-------- packages/insomnia-app/app/models/response.js | 2 +- .../plugins/context/__tests__/request.test.js | 5 +++ packages/insomnia-app/app/plugins/index.js | 19 +++++++++++ packages/insomnia-app/app/templating/index.js | 10 +++++- .../dropdowns/document-card-dropdown.js | 7 ++-- packages/insomnia-app/config/config.json | 3 +- packages/insomnia-app/package.json | 1 + 11 files changed, 83 insertions(+), 19 deletions(-) diff --git a/packages/insomnia-app/app/__tests__/install.test.js b/packages/insomnia-app/app/__tests__/install.test.js index 12b9e8253b..6da6b00164 100644 --- a/packages/insomnia-app/app/__tests__/install.test.js +++ b/packages/insomnia-app/app/__tests__/install.test.js @@ -3,6 +3,8 @@ import { containsOnlyDeprecationWarnings, isDeprecatedDependencies } from '../pl describe('install.js', () => { describe('containsOnlyDeprecationWarning', () => { it('should return true when all lines in stderr are deprecation warnings', () => { + const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); + const stderr = // Warning #1 'warning insomnia-plugin-xxx-yyy > xyz > xyz > xyz > xyz > xyz: ' + @@ -17,8 +19,11 @@ describe('install.js', () => { 'xyz is no longer maintained and not recommended for usage due to the number of issues. ' + 'Please, upgrade your dependencies to the actual version of xyz.'; expect(containsOnlyDeprecationWarnings(stderr)).toBe(true); + expect(consoleWarnSpy).toHaveBeenCalledTimes(3); }); it('should return false when stderr contains a deprecation warning and an error', () => { + const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); + const stderr = // Warning #1 'warning insomnia-plugin-xxx-yyy > xyz > xyz > xyz > xyz > xyz: ' + @@ -33,6 +38,7 @@ describe('install.js', () => { 'xyz is no longer maintained and not recommended for usage due to the number of issues. ' + 'Please, upgrade your dependencies to the actual version of xyz.'; expect(containsOnlyDeprecationWarnings(stderr)).toBe(false); + expect(consoleWarnSpy).toHaveBeenCalledTimes(2); }); }); describe('isDeprecatedDependencies', () => { diff --git a/packages/insomnia-app/app/common/__tests__/local-storage.test.js b/packages/insomnia-app/app/common/__tests__/local-storage.test.js index ac680b5d6e..e21a4f2871 100644 --- a/packages/insomnia-app/app/common/__tests__/local-storage.test.js +++ b/packages/insomnia-app/app/common/__tests__/local-storage.test.js @@ -43,6 +43,8 @@ describe('LocalStorage()', () => { }); it('does handles malformed files', () => { + const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + const basePath = `/tmp/insomnia-localstorage-${Math.random()}`; const localStorage = new LocalStorage(basePath); @@ -53,9 +55,12 @@ describe('LocalStorage()', () => { // Assert that writing our file actually works fs.writeFileSync(path.join(basePath, 'key'), '{"good": "JSON"}'); expect(localStorage.getItem('key', 'default')).toEqual({ good: 'JSON' }); + expect(consoleErrorSpy).toHaveBeenCalled(); }); it('does handles failing to write file', () => { + const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + const basePath = `/tmp/insomnia-localstorage-${Math.random()}`; const localStorage = new LocalStorage(basePath); fs.rmdirSync(basePath); @@ -66,6 +71,7 @@ describe('LocalStorage()', () => { // Since the above operation failed to write, we should now get back // the default value expect(localStorage.getItem('key', 'different')).toBe('different'); + expect(consoleErrorSpy).toHaveBeenCalled(); }); it('stores a key', () => { diff --git a/packages/insomnia-app/app/common/migrate-from-designer.js b/packages/insomnia-app/app/common/migrate-from-designer.js index bf8c0d263f..23cf424012 100644 --- a/packages/insomnia-app/app/common/migrate-from-designer.js +++ b/packages/insomnia-app/app/common/migrate-from-designer.js @@ -103,6 +103,15 @@ async function migratePlugins(designerDataDir: string, coreDataDir: string) { await removeDirs(designerPlugins, corePluginDir); await copyDirs(designerPlugins, designerPluginDir, corePluginDir); + + // Remove plugin bundle from installed plugins because it's included with the app now + const pluginsToDelete = [ + 'insomnia-plugin-kong-bundle', + 'insomnia-plugin-kong-declarative-config', + 'insomnia-plugin-kong-kubernetes-config', + 'insomnia-plugin-kong-portal', + ]; + await removeDirs(pluginsToDelete, corePluginDir); } async function readDirs(srcDir: string): Array { diff --git a/packages/insomnia-app/app/common/send-request.js b/packages/insomnia-app/app/common/send-request.js index bb02fb9137..96da3cef91 100644 --- a/packages/insomnia-app/app/common/send-request.js +++ b/packages/insomnia-app/app/common/send-request.js @@ -2,6 +2,7 @@ import * as db from './database'; import { types as modelTypes, stats } from '../models'; import { send } from '../network/network'; import { getBodyBuffer } from '../models/response'; +import * as plugins from '../plugins'; export async function getSendRequestCallbackMemDb(environmentId, memDB) { // Initialize the DB in-memory and fill it with data if we're given one @@ -30,19 +31,24 @@ export function getSendRequestCallback(environmentId) { } async function sendAndTransform(requestId, environmentId) { - const res = await send(requestId, environmentId); - const headersObj = {}; - for (const h of res.headers || []) { - const name = h.name || ''; - headersObj[name.toLowerCase()] = h.value || ''; + try { + plugins.ignorePlugin('insomnia-plugin-kong-bundle'); + const res = await send(requestId, environmentId); + const headersObj = {}; + for (const h of res.headers || []) { + const name = h.name || ''; + headersObj[name.toLowerCase()] = h.value || ''; + } + + const bodyBuffer = await getBodyBuffer(res); + + return { + status: res.statusCode, + statusMessage: res.statusMessage, + data: bodyBuffer ? bodyBuffer.toString('utf8') : undefined, + headers: headersObj, + }; + } finally { + plugins.clearIgnores(); } - - const bodyBuffer = await getBodyBuffer(res); - - return { - status: res.statusCode, - statusMessage: res.statusMessage, - data: bodyBuffer ? bodyBuffer.toString('utf8') : undefined, - headers: headersObj, - }; } diff --git a/packages/insomnia-app/app/models/response.js b/packages/insomnia-app/app/models/response.js index b27d6ef582..bb4dc8d468 100644 --- a/packages/insomnia-app/app/models/response.js +++ b/packages/insomnia-app/app/models/response.js @@ -86,7 +86,7 @@ export async function migrate(doc: Object) { } export function hookDatabaseInit(consoleLog: () => void = console.log) { - consoleLog('Init responses DB'); + consoleLog('[db] Init responses DB'); process.nextTick(async () => { await models.response.cleanDeletedResponses(); }); diff --git a/packages/insomnia-app/app/plugins/context/__tests__/request.test.js b/packages/insomnia-app/app/plugins/context/__tests__/request.test.js index 3264292cb0..9849d70c53 100644 --- a/packages/insomnia-app/app/plugins/context/__tests__/request.test.js +++ b/packages/insomnia-app/app/plugins/context/__tests__/request.test.js @@ -110,12 +110,17 @@ describe('request.*', () => { }); it('works for basic getters', async () => { + const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); + const result = plugin.init(await models.request.getById('req_1'), CONTEXT); expect(result.request.getId()).toBe('req_1'); expect(result.request.getName()).toBe('My Request'); expect(result.request.getUrl()).toBe(''); expect(result.request.getMethod()).toBe('GET'); expect(result.request.getBodyText()).toBe('body'); + expect(consoleWarnSpy).toHaveBeenCalledWith( + 'request.getBodyText() is deprecated. Use request.getBody() instead.', + ); expect(result.request.getAuthentication()).toEqual({ type: 'oauth2' }); }); diff --git a/packages/insomnia-app/app/plugins/index.js b/packages/insomnia-app/app/plugins/index.js index ddb5abe395..7a166272ed 100644 --- a/packages/insomnia-app/app/plugins/index.js +++ b/packages/insomnia-app/app/plugins/index.js @@ -105,10 +105,23 @@ export type Theme = { let plugins: ?Array = null; +let ignorePlugins: Array = []; + export async function init(): Promise { + clearIgnores(); await reloadPlugins(); } +export function ignorePlugin(name: string) { + if (!ignorePlugins.includes(name)) { + ignorePlugins.push(name); + } +} + +export function clearIgnores() { + ignorePlugins = []; +} + async function _traversePluginPath( pluginMap: Object, allPaths: Array, @@ -201,7 +214,13 @@ export async function getPlugins(force: boolean = false): Promise> }; for (const p of appConfig().plugins) { + if (ignorePlugins.includes(p)) { + continue; + } const pluginJson = global.require(`${p}/package.json`); + if (ignorePlugins.includes(pluginJson.name)) { + continue; + } const pluginModule = global.require(p); pluginMap[pluginJson.name] = _initPlugin(pluginJson, pluginModule, allConfigs); } diff --git a/packages/insomnia-app/app/templating/index.js b/packages/insomnia-app/app/templating/index.js index face6c2d20..97f3a0277f 100644 --- a/packages/insomnia-app/app/templating/index.js +++ b/packages/insomnia-app/app/templating/index.js @@ -3,6 +3,7 @@ import nunjucks from 'nunjucks'; import BaseExtension from './base-extension'; import type { NunjucksParsedTag } from './utils'; import * as plugins from '../plugins/index'; +import type { TemplateTag } from '../plugins/index'; export class RenderError extends Error { message: string; @@ -152,7 +153,14 @@ async function getNunjucks(renderMode: string) { const nj = nunjucks.configure(config); - const allTemplateTagPlugins = await plugins.getTemplateTags(); + let allTemplateTagPlugins: Array; + try { + plugins.ignorePlugin('insomnia-plugin-kong-bundle'); + allTemplateTagPlugins = await plugins.getTemplateTags(); + } finally { + plugins.clearIgnores(); + } + const allExtensions = allTemplateTagPlugins; for (let i = 0; i < allExtensions.length; i++) { const { templateTag, plugin } = allExtensions[i]; diff --git a/packages/insomnia-app/app/ui/components/dropdowns/document-card-dropdown.js b/packages/insomnia-app/app/ui/components/dropdowns/document-card-dropdown.js index bdcec73433..94da37512e 100644 --- a/packages/insomnia-app/app/ui/components/dropdowns/document-card-dropdown.js +++ b/packages/insomnia-app/app/ui/components/dropdowns/document-card-dropdown.js @@ -105,8 +105,11 @@ class DocumentCardDropdown extends React.PureComponent { } async _onOpen() { - const plugins = await getDocumentActions(); - this.setState({ actionPlugins: plugins }); + // Only load document plugins if the scope is designer, for now + if (this.props.workspace.scope === 'designer') { + const plugins = await getDocumentActions(); + this.setState({ actionPlugins: plugins }); + } } async _handlePluginClick(p: DocumentAction) { diff --git a/packages/insomnia-app/config/config.json b/packages/insomnia-app/config/config.json index ce518d38a1..01257e0265 100644 --- a/packages/insomnia-app/config/config.json +++ b/packages/insomnia-app/config/config.json @@ -27,6 +27,7 @@ "insomnia-plugin-response", "insomnia-plugin-jsonpath", "insomnia-plugin-cookie-jar", - "insomnia-plugin-core-themes" + "insomnia-plugin-core-themes", + "insomnia-plugin-kong-bundle" ] } diff --git a/packages/insomnia-app/package.json b/packages/insomnia-app/package.json index 0f55b8a730..d5e8fd6bb9 100644 --- a/packages/insomnia-app/package.json +++ b/packages/insomnia-app/package.json @@ -131,6 +131,7 @@ "insomnia-plugin-file": "^2.2.24", "insomnia-plugin-hash": "^2.2.24", "insomnia-plugin-jsonpath": "^2.2.24", + "insomnia-plugin-kong-bundle": "^2.2.26", "insomnia-plugin-now": "^2.2.24", "insomnia-plugin-os": "^2.2.24", "insomnia-plugin-prompt": "^2.2.24",