2018-11-06 13:52:55 +00:00
|
|
|
const crypto = require('crypto');
|
|
|
|
|
2018-08-04 18:52:35 +00:00
|
|
|
module.exports.responseHooks = [
|
2018-11-06 13:52:55 +00:00
|
|
|
async context => {
|
|
|
|
const items = await context.store.all();
|
|
|
|
const requestId = context.request.getId();
|
|
|
|
const toRemove = items.filter(v => v.key.indexOf(requestId) === 0);
|
2018-06-27 05:12:43 +00:00
|
|
|
// Delete cached values we prompt again on the next request
|
2018-11-06 13:52:55 +00:00
|
|
|
for (const { key } of toRemove) {
|
|
|
|
await context.store.removeItem(key);
|
|
|
|
}
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
2018-06-27 04:58:34 +00:00
|
|
|
];
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
module.exports.templateTags = [
|
|
|
|
{
|
|
|
|
displayName: 'Prompt',
|
|
|
|
name: 'prompt',
|
|
|
|
description: 'prompt user for input',
|
2019-04-18 21:01:15 +00:00
|
|
|
disablePreview: args => args[4] && args[4].value === true,
|
2018-06-25 17:42:50 +00:00
|
|
|
args: [
|
|
|
|
{
|
|
|
|
displayName: 'Title',
|
2018-06-27 05:12:43 +00:00
|
|
|
type: 'string',
|
|
|
|
help: 'Title is a unique string used to identify the prompt value',
|
2018-12-12 17:36:11 +00:00
|
|
|
validate: v => (v ? '' : 'Required'),
|
2018-06-25 17:42:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Label',
|
2018-12-12 17:36:11 +00:00
|
|
|
type: 'string',
|
2018-06-25 17:42:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Default Value',
|
|
|
|
type: 'string',
|
|
|
|
help:
|
|
|
|
'This value is used to pre-populate the prompt dialog, but is ALSO used ' +
|
|
|
|
'when the app renders preview values (like the one below). This is to prevent the ' +
|
2018-12-12 17:36:11 +00:00
|
|
|
'prompt from displaying too frequently during general app use.',
|
2018-06-25 17:42:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Storage Key',
|
|
|
|
type: 'string',
|
|
|
|
help:
|
|
|
|
'If this is set, the value will be stored in memory under this key until the app is ' +
|
|
|
|
"closed. To force this tag to re-prompt the user, simply change this key's value to " +
|
2018-12-12 17:36:11 +00:00
|
|
|
'something else.',
|
2018-07-05 21:44:45 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Mask Text',
|
|
|
|
type: 'boolean',
|
2018-08-04 18:52:35 +00:00
|
|
|
help: 'If this is enabled, the value when input will be masked like a password field.',
|
2018-12-12 17:36:11 +00:00
|
|
|
defaultValue: false,
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
],
|
2018-08-04 18:52:35 +00:00
|
|
|
async run(context, title, label, defaultValue, explicitStorageKey, maskText) {
|
2018-06-27 05:12:43 +00:00
|
|
|
if (!title) {
|
|
|
|
throw new Error('Title attribute is required for prompt tag');
|
|
|
|
}
|
|
|
|
|
2018-06-27 04:58:34 +00:00
|
|
|
// 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.
|
2018-11-06 13:52:55 +00:00
|
|
|
const titleHash = crypto
|
|
|
|
.createHash('md5')
|
|
|
|
.update(title)
|
|
|
|
.digest('hex');
|
|
|
|
const storageKey = explicitStorageKey || `${context.meta.requestId}.${titleHash}`;
|
2018-06-27 04:58:34 +00:00
|
|
|
const cachedValue = await context.store.getItem(storageKey);
|
|
|
|
|
|
|
|
if (cachedValue) {
|
2018-06-25 17:42:50 +00:00
|
|
|
console.log(`[prompt] Used cached value under ${storageKey}`);
|
2018-06-27 04:58:34 +00:00
|
|
|
return cachedValue;
|
2018-06-25 17:42:50 +00:00
|
|
|
}
|
2018-03-30 14:25:35 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
const value = await context.app.prompt(title || 'Enter Value', {
|
|
|
|
label,
|
2018-07-05 21:44:45 +00:00
|
|
|
defaultValue,
|
2018-12-12 17:36:11 +00:00
|
|
|
inputType: maskText ? 'password' : 'text',
|
2018-06-25 17:42:50 +00:00
|
|
|
});
|
2018-03-30 14:25:35 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
if (storageKey) {
|
|
|
|
console.log(`[prompt] Stored value under ${storageKey}`);
|
2018-06-27 04:58:34 +00:00
|
|
|
await context.store.setItem(storageKey, value);
|
2018-06-25 17:42:50 +00:00
|
|
|
}
|
2018-03-30 14:25:35 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
return value;
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|