From 01196cf45753a19c03d92bb337a26279fb80676d Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Tue, 6 Nov 2018 08:52:55 -0500 Subject: [PATCH] Fix Prompt plugin (Closes #1063) --- packages/insomnia-app/app/models/plugin-data.js | 4 ++++ .../insomnia-app/app/plugins/context/store.js | 9 ++++++++- plugins/insomnia-plugin-prompt/index.js | 17 ++++++++++++++--- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/packages/insomnia-app/app/models/plugin-data.js b/packages/insomnia-app/app/models/plugin-data.js index 3f7461c75..ef21fa28b 100644 --- a/packages/insomnia-app/app/models/plugin-data.js +++ b/packages/insomnia-app/app/models/plugin-data.js @@ -44,6 +44,10 @@ export async function removeByKey(plugin: string, key: string): Promise { return db.removeWhere(type, { plugin, key }); } +export async function all(plugin: string): Promise> { + return db.find(type, { plugin }); +} + export async function removeAll(plugin: string): Promise { return db.removeWhere(type, { plugin }); } diff --git a/packages/insomnia-app/app/plugins/context/store.js b/packages/insomnia-app/app/plugins/context/store.js index 402ad799b..44054548e 100644 --- a/packages/insomnia-app/app/plugins/context/store.js +++ b/packages/insomnia-app/app/plugins/context/store.js @@ -19,8 +19,15 @@ export function init(plugin: Plugin) { async removeItem(key: string): Promise { await models.pluginData.removeByKey(plugin.name, key); }, - async clear(key: string): Promise { + async clear(): Promise { await models.pluginData.removeAll(plugin.name); + }, + async all(): Promise> { + const docs = await models.pluginData.all(plugin.name); + return docs.map(d => ({ + value: d.value, + key: d.key + })); } } }; diff --git a/plugins/insomnia-plugin-prompt/index.js b/plugins/insomnia-plugin-prompt/index.js index 6d587b754..f89bc726d 100644 --- a/plugins/insomnia-plugin-prompt/index.js +++ b/plugins/insomnia-plugin-prompt/index.js @@ -1,7 +1,14 @@ +const crypto = require('crypto'); + module.exports.responseHooks = [ - context => { + async context => { + const items = await context.store.all(); + const requestId = context.request.getId(); + const toRemove = items.filter(v => v.key.indexOf(requestId) === 0); // Delete cached values we prompt again on the next request - context.store.clear(); + for (const { key } of toRemove) { + await context.store.removeItem(key); + } } ]; @@ -53,7 +60,11 @@ module.exports.templateTags = [ // 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}.${title}`; + const titleHash = crypto + .createHash('md5') + .update(title) + .digest('hex'); + const storageKey = explicitStorageKey || `${context.meta.requestId}.${titleHash}`; const cachedValue = await context.store.getItem(storageKey); if (cachedValue) {