mirror of
https://github.com/Kong/insomnia
synced 2024-11-07 22:30:15 +00:00
More control over the prompt plugin (#1916)
* more control of storage/default values for prompt plugin * fix eslint
This commit is contained in:
parent
32a958ab95
commit
c7b0617767
@ -40,23 +40,35 @@ module.exports.templateTags = [
|
|||||||
defaultValue: false,
|
defaultValue: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Default to Last Value',
|
displayName: 'Prompt for a Value...',
|
||||||
type: 'boolean',
|
type: 'enum',
|
||||||
help:
|
help:
|
||||||
'If this is enabled, the input field will be pre-filled with this value. This option is ' +
|
'Controls when this prompt should be shown and what value should be used to ' +
|
||||||
'ignored when the storage key is set.',
|
'pre-populate the prompt dialog',
|
||||||
defaultValue: true,
|
options: [
|
||||||
|
{ displayName: 'on each request', value: 'always-fresh' },
|
||||||
|
{ displayName: 'on each request, using the default value as default', value: 'always-default' },
|
||||||
|
{ displayName: 'on each request, using the stored value as default', value: 'always-storage' },
|
||||||
|
{ displayName: 'at most once, storing the value for further requests', value: 'once-storage' },
|
||||||
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
async run(context, title, label, defaultValue, explicitStorageKey, maskText, saveLastValue) {
|
async run(context, title, label, defaultValue, explicitStorageKey, maskText, showCondition) {
|
||||||
if (!title) {
|
if (!title) {
|
||||||
throw new Error('Title attribute is required for prompt tag');
|
throw new Error('Title attribute is required for prompt tag');
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we don't have a key, default to request ID.
|
// Backward compatibility with "Default to Last Value" #1597
|
||||||
|
if (showCondition === true) {
|
||||||
|
showCondition = 'always-storage';
|
||||||
|
} else if (showCondition === false) {
|
||||||
|
showCondition = 'always-fresh';
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we don't have a key, default to request ID and title
|
||||||
// We do this because we may render the prompt multiple times per request.
|
// 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
|
// We cache it under the requestId.title so it only prompts once. We then
|
||||||
// the cache in a response hook when the request is sent.
|
// clear the cache in a response hook when the request is sent.
|
||||||
const titleHash = crypto
|
const titleHash = crypto
|
||||||
.createHash('md5')
|
.createHash('md5')
|
||||||
.update(title)
|
.update(title)
|
||||||
@ -64,21 +76,20 @@ module.exports.templateTags = [
|
|||||||
const storageKey = explicitStorageKey || `${context.meta.requestId}.${titleHash}`;
|
const storageKey = explicitStorageKey || `${context.meta.requestId}.${titleHash}`;
|
||||||
const cachedValue = await context.store.getItem(storageKey);
|
const cachedValue = await context.store.getItem(storageKey);
|
||||||
|
|
||||||
// Directly return cached value if using explicitly defined storage key
|
if (showCondition === 'once-storage' && cachedValue !== null) {
|
||||||
if (explicitStorageKey && cachedValue) {
|
console.log(`[prompt] Used cached value under ${storageKey}`);
|
||||||
console.log(`[prompt] Used cached value under ${storageKey}`);
|
return cachedValue;
|
||||||
return cachedValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use cached value as default value
|
if (showCondition === 'always-fresh') {
|
||||||
if (cachedValue && saveLastValue) {
|
defaultValue = '';
|
||||||
defaultValue = cachedValue;
|
} else if (showCondition === 'always-storage') {
|
||||||
console.log(`[prompt] Used cached value under ${storageKey}`);
|
defaultValue = cachedValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only prompt when we're actually sending
|
// Only prompt when we're actually sending
|
||||||
if (context.renderPurpose !== 'send') {
|
if (context.renderPurpose !== 'send') {
|
||||||
if (cachedValue !== null) {
|
if (showCondition === 'once-storage' && cachedValue !== null) {
|
||||||
return cachedValue;
|
return cachedValue;
|
||||||
} else {
|
} else {
|
||||||
return defaultValue || '';
|
return defaultValue || '';
|
||||||
@ -88,6 +99,7 @@ module.exports.templateTags = [
|
|||||||
const value = await context.app.prompt(title || 'Enter Value', {
|
const value = await context.app.prompt(title || 'Enter Value', {
|
||||||
label,
|
label,
|
||||||
defaultValue,
|
defaultValue,
|
||||||
|
selectText: true,
|
||||||
inputType: maskText ? 'password' : 'text',
|
inputType: maskText ? 'password' : 'text',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user