mirror of
https://github.com/Kong/insomnia.git
synced 2026-10-25 16:19:20 -04:00
Add custom template tags for timestamp and uuid
This commit is contained in:
1 parent
1fc857cdd4
commit
3ba0074331
5 files changed
+58
-10
No files matched your search
@@ -13,6 +13,7 @@
|
||||
"electron-context-menu": "^0.4.0",
|
||||
"electron-squirrel-startup": "^1.0.0",
|
||||
"httpsnippet": "git@github.com:gschier/httpsnippet.git#39d2fb0449f33711e5cc71a4d42b0e5b808426b4",
|
||||
"json-lint": "^0.1.0",
|
||||
"jsonpath-plus": "^0.15.0",
|
||||
"nedb": "^1.8.0",
|
||||
"node-localstorage": "^1.3.0",
|
||||
|
||||
@@ -9,6 +9,16 @@ describe('render()', () => {
|
||||
expect(rendered).toBe('Hello World!');
|
||||
});
|
||||
|
||||
it('renders custom tag: uuid', () => {
|
||||
const rendered = render.render('Hello {% uuid %}!');
|
||||
expect(rendered).toBe('Hello b5c2f089-2c0c-46b6-a6f1-8d5b86cbc603!');
|
||||
});
|
||||
|
||||
it('renders custom tag: timestamp', () => {
|
||||
const rendered = render.render('Hello {% timestamp %}!');
|
||||
expect(rendered).toMatch(/Hello \d{13}!/);
|
||||
});
|
||||
|
||||
it('fails on invalid template', () => {
|
||||
const fn = () => render.render('Hello {{ msg }!', {msg: 'World'});
|
||||
expect(fn).toThrowError('expected variable end');
|
||||
|
||||
@@ -1,11 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
import render from '../render';
|
||||
import * as querystring from '../querystring';
|
||||
import * as db from '../../database';
|
||||
import {DEBOUNCE_MILLIS, METHOD_GET} from '../constants';
|
||||
import {getRenderedRequest} from '../render';
|
||||
|
||||
const FLAGS = [
|
||||
'cacert', 'capath', 'E', 'cert', 'cert-type', 'ciphers', 'K', 'config',
|
||||
'connect-timeout', 'C', 'continue-at', 'b',
|
||||
|
||||
+45
-2
@@ -1,16 +1,59 @@
|
||||
import nunjucks from 'nunjucks';
|
||||
import traverse from 'traverse';
|
||||
import uuid from 'node-uuid';
|
||||
import * as db from '../database';
|
||||
import {TYPE_WORKSPACE} from '../database/index';
|
||||
import {getBasicAuthHeader, hasAuthHeader, setDefaultProtocol} from './util';
|
||||
|
||||
nunjucks.configure({
|
||||
const nunjucksEnvironment = nunjucks.configure({
|
||||
autoescape: false
|
||||
});
|
||||
|
||||
class NoArgsExtension {
|
||||
parse (parser, nodes, lexer) {
|
||||
const args = parser.parseSignature(null, true);
|
||||
parser.skip(lexer.TOKEN_BLOCK_END);
|
||||
return new nodes.CallExtension(this, 'run', args);
|
||||
}
|
||||
}
|
||||
|
||||
class ArgsExtension {
|
||||
parse (parser, nodes, lexer) {
|
||||
const tok = parser.nextToken();
|
||||
const args = parser.parseSignature(null, true);
|
||||
parser.advanceAfterBlockEnd(tok.value);
|
||||
return new nodes.CallExtension(this, 'run', args);
|
||||
}
|
||||
}
|
||||
|
||||
class TimestampExtension extends NoArgsExtension {
|
||||
constructor () {
|
||||
super();
|
||||
this.tags = ['timestamp'];
|
||||
}
|
||||
|
||||
run (context) {
|
||||
return Date.now();
|
||||
}
|
||||
}
|
||||
|
||||
class UuidExtension extends NoArgsExtension {
|
||||
constructor () {
|
||||
super();
|
||||
this.tags = ['uuid'];
|
||||
}
|
||||
|
||||
run (context) {
|
||||
return uuid.v4();
|
||||
}
|
||||
}
|
||||
|
||||
nunjucksEnvironment.addExtension('uuid', new UuidExtension());
|
||||
nunjucksEnvironment.addExtension('timestamp', new TimestampExtension());
|
||||
|
||||
export function render (template, context = {}) {
|
||||
try {
|
||||
return nunjucks.renderString(template, context);
|
||||
return nunjucksEnvironment.renderString(template, context);
|
||||
} catch (e) {
|
||||
throw new Error(e.message.replace(/\(unknown path\)\s*/, ''));
|
||||
}
|
||||
|
||||
+2
-2
@@ -63,7 +63,6 @@
|
||||
"httpsnippet": "git@github.com:gschier/httpsnippet.git#39d2fb0449f33711e5cc71a4d42b0e5b808426b4",
|
||||
"json-lint": "^0.1.0",
|
||||
"jsonpath-plus": "^0.15.0",
|
||||
"jsonschema": "^1.1.0",
|
||||
"mousetrap": "^1.6.0",
|
||||
"nedb": "^1.8.0",
|
||||
"node-localstorage": "^1.3.0",
|
||||
@@ -82,7 +81,8 @@
|
||||
"tough-cookie": "^2.3.1",
|
||||
"traverse": "^0.6.6",
|
||||
"xml2js": "^0.4.17",
|
||||
"xml2js-xpath": "^0.7.0"
|
||||
"xml2js-xpath": "^0.7.0",
|
||||
"node-uuid": "latest"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-cli": "^6.11.4",
|
||||
|
||||
Reference in new issue
Block a user