Skip to content

Commit 81582c3

Browse files
authored
Merge pull request #53 from yeuai/dev-http-method
Dev http method
2 parents 98ec77a + f182767 commit 81582c3

7 files changed

Lines changed: 57 additions & 10 deletions

File tree

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@yeuai/botscript",
3-
"version": "1.6.0",
3+
"version": "1.6.1",
44
"description": "A text-based scripting language and bot engine for Conversational User Interfaces (CUI)",
55
"main": "dist/engine",
66
"scripts": {

src/engine/struct.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,17 @@ export class Struct {
122122
struct.name = tokens[0];
123123
struct.options = ['GET', tokens[1]];
124124
} else if (tokens.length === 3) {
125-
const action = /post/i.test(tokens[1]) ? 'post' : 'get';
125+
const method = tokens[1];
126126
struct.name = tokens[0];
127-
struct.options = [action, tokens[2]];
127+
struct.options = [method, tokens[2]];
128128
} else {
129+
// TODO: support new command definition
130+
/**
131+
* @ command_name
132+
* - url: POST https://command-service.url/api/endpoint
133+
* - header: 1
134+
* - header: 2
135+
*/
129136
throw new Error('invalid command');
130137
}
131138
break;

src/lib/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import axios from 'axios';
1+
import axios, { Method } from 'axios';
22
import { evalSync } from 'jexl';
33
import { Struct, Request } from '../common';
44
import { TestConditionalCallback, Types } from '../interfaces/types';
@@ -93,12 +93,12 @@ export function evaluate(expr: string, context: any) {
9393
*/
9494
export function callHttpService(command: Struct, req: Request) {
9595
const vIsGetMethod = /^get$/i.test(command.options[0]);
96+
const url = interpolate(command.options[1], req.variables);
97+
const body = vIsGetMethod ? undefined : req.variables;
98+
const method = command.options[0] as Method;
9699
const headers = command.body
97100
.filter(x => x.split(REGEX_HEADER_SEPARATOR).length === 2)
98101
.map(x => x.split(REGEX_HEADER_SEPARATOR).map(kv => kv.trim()));
99-
const method = vIsGetMethod ? 'GET' : 'POST';
100-
const url = interpolate(command.options[1], req.variables);
101-
const body = vIsGetMethod ? undefined : req.variables;
102102

103103
logger.info(`Send command request @${command.name}: ${method} ${url}${(method === 'POST' && body) ? ', body=' + JSON.stringify(body) : ''}`);
104104

test/command/http-method.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { assert } from 'chai';
2+
import { BotScript, Request } from '../../src/engine';
3+
4+
describe('Command: http method', () => {
5+
6+
it('should support put, delete method', async () => {
7+
const bot = new BotScript();
8+
9+
bot.parse(`
10+
@ service1 put /api/http/put
11+
12+
@ service2 delete /api/http/delete
13+
14+
+ put me
15+
* true @> service1
16+
- Result $message
17+
18+
+ delete me
19+
* true @> service2
20+
- Result $message2
21+
`);
22+
await bot.init();
23+
24+
const req = new Request();
25+
let res = await bot.handleAsync(req.enter('put me'));
26+
assert.match(res.speechResponse, /result ok/i, 'bot response with command executed');
27+
28+
res = await bot.handleAsync(req.enter('delete me'));
29+
assert.match(res.speechResponse, /result ok/i, 'bot response with command executed');
30+
});
31+
32+
});

test/directives/plugin.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ describe('Directive: /plugin', () => {
2424
await bot.init();
2525

2626
it('should load directive /plugin', async () => {
27-
console.log(bot.context.directives);
28-
console.log(bot.context.plugins);
27+
// console.log(bot.context.directives);
28+
// console.log(bot.context.plugins);
2929
assert.isTrue(bot.context.directives.has('plugin:test'), 'contains directive /plugin');
3030
});
3131

test/e2e/delay.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ mock
1616
intent: 'react_positive',
1717
entities: [{ id: 1, name: 'John Smith' }],
1818
})
19+
.onPut('/api/http/put').reply(200, {
20+
error: 0,
21+
message: 'Ok',
22+
})
23+
.onDelete('/api/http/delete').reply(200, {
24+
error: 0,
25+
message2: 'Ok',
26+
})
1927
.onGet('/api/data/list').reply(200, {
2028
people: [{
2129
name: 'Vũ',

0 commit comments

Comments
 (0)