diff --git a/packages/insomnia-app/app/models/plugin-data.js b/packages/insomnia-app/app/models/plugin-data.js index beebb3af9..0320d48fd 100644 --- a/packages/insomnia-app/app/models/plugin-data.js +++ b/packages/insomnia-app/app/models/plugin-data.js @@ -51,6 +51,10 @@ export async function removeByKey(plugin: string, key: string): Promise { return db.removeWhere(type, { plugin, key }); } +export async function removeAll(plugin: string): Promise { + return db.removeWhere(type, { plugin }); +} + export async function getByKey( plugin: string, key: string diff --git a/packages/insomnia-app/app/plugins/context/store.js b/packages/insomnia-app/app/plugins/context/store.js index 6661975fa..402ad799b 100644 --- a/packages/insomnia-app/app/plugins/context/store.js +++ b/packages/insomnia-app/app/plugins/context/store.js @@ -18,6 +18,9 @@ export function init(plugin: Plugin) { }, async removeItem(key: string): Promise { await models.pluginData.removeByKey(plugin.name, key); + }, + async clear(key: string): Promise { + await models.pluginData.removeAll(plugin.name); } } }; diff --git a/plugins/insomnia-plugin-prompt/index.js b/plugins/insomnia-plugin-prompt/index.js index 4f0b79b41..901a65e64 100644 --- a/plugins/insomnia-plugin-prompt/index.js +++ b/plugins/insomnia-plugin-prompt/index.js @@ -1,4 +1,12 @@ -const STORE = {}; +module.exports.responseHooks = [ + context => { + const requestId = context.response.getRequestId(); + + // Delete cached value so it will prompt again on the next request + context.store.removeItem(requestId); + } +]; + module.exports.templateTags = [ { displayName: 'Prompt', @@ -30,10 +38,17 @@ module.exports.templateTags = [ 'something else.' } ], - async run(context, title, label, defaultValue, storageKey) { - if (STORE[storageKey]) { + async run(context, title, label, defaultValue, explicitStorageKey) { + // If we don't have a key, default to request ID. + // We do this because we may render the prompt multiple times per request. + // We cache it under the requestId so it only prompts once. We then clear + // the cache in a response hook when the request is sent. + const storageKey = explicitStorageKey || context.meta.requestId; + const cachedValue = await context.store.getItem(storageKey); + + if (cachedValue) { console.log(`[prompt] Used cached value under ${storageKey}`); - return STORE[storageKey]; + return cachedValue; } const value = await context.app.prompt(title || 'Enter Value', { @@ -41,10 +56,9 @@ module.exports.templateTags = [ defaultValue }); - // Store if a key is set if (storageKey) { console.log(`[prompt] Stored value under ${storageKey}`); - STORE[storageKey] = value; + await context.store.setItem(storageKey, value); } return value;