Skip to content

Commit

Permalink
interpolate url with variables
Browse files Browse the repository at this point in the history
  • Loading branch information
vunb committed Mar 10, 2021
1 parent c45fd2b commit 0663935
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/lib/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { compile } from 'handlebars';

function interpolate(template: string, data: any) {
const temp = compile(template);
return temp(data);
}

export {
interpolate,
};
3 changes: 2 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { evalSync } from 'jexl';
import { Struct, Request } from '../common';
import { TestConditionalCallback, Types } from '../interfaces/types';
import { Logger } from './logger';
import { interpolate } from './template';

const logger = new Logger('Utils');

Expand Down Expand Up @@ -92,7 +93,7 @@ export function callHttpService(command: Struct, req: Request) {
const vIsGetMethod = /^get$/i.test(command.options[0]);
const headers = command.body.map(x => x.split(':'));
const method = vIsGetMethod ? 'GET' : 'POST';
const url = command.options[1];
const url = interpolate(command.options[1], req.variables);
const body = vIsGetMethod ? undefined : req.variables;

logger.info('Send request:', method, url, body);
Expand Down
13 changes: 13 additions & 0 deletions test/lib/template.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect } from 'chai';
import { interpolate } from '../../src/lib/template';

describe('Lib: Template', () => {

describe('Interpolate', () => {
it('should interpolate template with data', async () => {
const param = 'vunb';
const result = interpolate(`/api/url?query={{param}}`, {param});
expect(result).eq('/api/url?query=vunb');
});
});
});
2 changes: 1 addition & 1 deletion test/lib/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai';
import { evaluate } from '../../src/lib/utils';

describe('Utils', () => {
describe('Lib: Utils', () => {

describe('safeEvalCode', () => {
it('should return true when given boolean value', async () => {
Expand Down

0 comments on commit 0663935

Please sign in to comment.