insomnia/app/templating/extensions/uuid-extension.js
Gregory Schier 79b3f3fe7c Better Nunjucks Tag Editor (#234)
* Helper to tokenize Nunjucks tag

* More granular types

* Add tag definitions

* Improve editor to be more WYSIWYG

* Fixed tests

* Added raw response tag
2017-05-23 15:05:31 -07:00

52 lines
971 B
JavaScript

import uuid from 'uuid';
import BaseExtension from './base/base-extension';
export default class UuidExtension extends BaseExtension {
getName () {
return 'UUID';
}
getTag () {
return 'uuid';
}
getDefaultFill () {
return "uuid 'v4'";
}
getDescription () {
return 'generate v1 or v4 UUIDs';
}
getArguments () {
return [{
key: 'version',
label: 'Version',
type: 'enum',
options: [
{value: 'v4', name: 'Version 4'},
{value: 'v1', name: 'Version 1'}
]
}];
}
run (context, uuidType = 'v4') {
if (typeof uuidType === 'number') {
uuidType += '';
} else if (typeof uuidType === 'string') {
uuidType = uuidType.toLowerCase();
}
switch (uuidType) {
case '1':
case 'v1':
return uuid.v1();
case '4':
case 'v4':
return uuid.v4();
default:
throw new Error(`Invalid UUID type "${uuidType}"`);
}
}
}