mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 06:39:48 +00:00
Prompt tag now only prompts once per request maximum
This commit is contained in:
parent
15e230c77a
commit
8a8c4b3979
@ -51,6 +51,10 @@ export async function removeByKey(plugin: string, key: string): Promise<void> {
|
|||||||
return db.removeWhere(type, { plugin, key });
|
return db.removeWhere(type, { plugin, key });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function removeAll(plugin: string): Promise<void> {
|
||||||
|
return db.removeWhere(type, { plugin });
|
||||||
|
}
|
||||||
|
|
||||||
export async function getByKey(
|
export async function getByKey(
|
||||||
plugin: string,
|
plugin: string,
|
||||||
key: string
|
key: string
|
||||||
|
@ -18,6 +18,9 @@ export function init(plugin: Plugin) {
|
|||||||
},
|
},
|
||||||
async removeItem(key: string): Promise<void> {
|
async removeItem(key: string): Promise<void> {
|
||||||
await models.pluginData.removeByKey(plugin.name, key);
|
await models.pluginData.removeByKey(plugin.name, key);
|
||||||
|
},
|
||||||
|
async clear(key: string): Promise<void> {
|
||||||
|
await models.pluginData.removeAll(plugin.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -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 = [
|
module.exports.templateTags = [
|
||||||
{
|
{
|
||||||
displayName: 'Prompt',
|
displayName: 'Prompt',
|
||||||
@ -30,10 +38,17 @@ module.exports.templateTags = [
|
|||||||
'something else.'
|
'something else.'
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
async run(context, title, label, defaultValue, storageKey) {
|
async run(context, title, label, defaultValue, explicitStorageKey) {
|
||||||
if (STORE[storageKey]) {
|
// 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}`);
|
console.log(`[prompt] Used cached value under ${storageKey}`);
|
||||||
return STORE[storageKey];
|
return cachedValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const value = await context.app.prompt(title || 'Enter Value', {
|
const value = await context.app.prompt(title || 'Enter Value', {
|
||||||
@ -41,10 +56,9 @@ module.exports.templateTags = [
|
|||||||
defaultValue
|
defaultValue
|
||||||
});
|
});
|
||||||
|
|
||||||
// Store if a key is set
|
|
||||||
if (storageKey) {
|
if (storageKey) {
|
||||||
console.log(`[prompt] Stored value under ${storageKey}`);
|
console.log(`[prompt] Stored value under ${storageKey}`);
|
||||||
STORE[storageKey] = value;
|
await context.store.setItem(storageKey, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
|
Loading…
Reference in New Issue
Block a user