Skip to content

Commit 2d643a2

Browse files
committed
allow extension-api-caller to accept other https methods, headers, body
1 parent fbdb880 commit 2d643a2

4 files changed

Lines changed: 49 additions & 9 deletions

File tree

packages/extension-api-caller/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@vulcan-sql/extension-api-caller",
33
"description": "Calling APIs to get data from other sources",
4-
"version": "0.8.0",
4+
"version": "0.8.1",
55
"type": "commonjs",
66
"publishConfig": {
77
"access": "public"

packages/extension-api-caller/src/lib/filters/restApiCaller.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,22 @@ export const RestApiCallerFilter: FunctionalFilter = async ({
1111
value,
1212
}) => {
1313
if (!args['url']) throw new InternalError('url is required');
14+
1415
const url = args['arg'] && args['arg'] === ':id' ? `${args['url']}/${value}` : args['url'];
15-
const results = await axios.get<Array<any>>(url, {
16+
const httpMethod = args['method'] || 'get';
17+
let options: any = {
18+
url: url,
19+
method: httpMethod,
1620
params: args['arg'] && args['arg'] !== ':id' ? { [args['arg']]: value } : {},
17-
});
21+
}
22+
if (args['body']) {
23+
options = {...options, body: JSON.parse(args['body'])}
24+
}
25+
if (args['headers']) {
26+
options = {...options, headers: JSON.parse(args['headers'])}
27+
}
28+
29+
const results = await axios(options);
1830

1931
return JSON.stringify(results.data);
2032
};

packages/extension-api-caller/test/restApiCaller.spec.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ describe('Test "rest_api" filter', () => {
99
extensions: { rest_api: path.join(__dirname, '..', 'src') },
1010
});
1111

12-
const sql = `{% set result = 1990 %}SELECT {{ result |
13-
rest_api(url='', arg='begin_date') }}`;
12+
const sql = `{% set result = 1990 %}SELECT {{ result | rest_api(url='', arg='begin_date') }}`;
1413

1514
// Act
1615
await compileAndLoad(sql);
@@ -24,8 +23,22 @@ describe('Test "rest_api" filter', () => {
2423
);
2524

2625
it(
27-
'The call API extensions created by createFilter function should work with template engine',
26+
'The rest_api function should work with template engine',
2827
async () => {
28+
const { compileAndLoad, execute, getExecutedQueries, getCreatedBinding } = await getTestCompiler({
29+
extensions: { rest_api: path.join(__dirname, '..', 'src') },
30+
});
31+
32+
const sql = `{% set result = 1990 %}SELECT {{ result | rest_api(url='http://localhost:3000/api/artists', arg='begin_date') }}`;
33+
34+
// Act
35+
await compileAndLoad(sql);
36+
await execute({});
37+
38+
// Assert
39+
const queries = await getExecutedQueries();
40+
const bindings = await getCreatedBinding();
41+
2942
const expected = JSON.stringify(
3043
[
3144
{
@@ -141,12 +154,20 @@ describe('Test "rest_api" filter', () => {
141154
]
142155
);
143156

157+
expect(queries[0]).toBe('SELECT $1');
158+
expect(bindings[0].get('$1')).toEqual(expected);
159+
},
160+
50 * 1000
161+
);
162+
163+
it(
164+
'The rest_api function should work with HTTP Post request',
165+
async () => {
144166
const { compileAndLoad, execute, getExecutedQueries, getCreatedBinding } = await getTestCompiler({
145167
extensions: { rest_api: path.join(__dirname, '..', 'src') },
146168
});
147169

148-
const sql = `{% set result = 1990 %}SELECT {{ result |
149-
rest_api(url='http://localhost:3000/api/artists', arg='begin_date') }}`;
170+
const sql = `{% set body = '{"title": "BMW Pencil"}' %}SELECT {{ body | rest_api(url='https://dummyjson.com/products/add', method='POST', arg='body') }}`;
150171

151172
// Act
152173
await compileAndLoad(sql);
@@ -156,9 +177,13 @@ describe('Test "rest_api" filter', () => {
156177
const queries = await getExecutedQueries();
157178
const bindings = await getCreatedBinding();
158179

180+
const expected = JSON.stringify({
181+
"id": 101
182+
});
183+
159184
expect(queries[0]).toBe('SELECT $1');
160185
expect(bindings[0].get('$1')).toEqual(expected);
161186
},
162187
50 * 1000
163-
);
188+
)
164189
});

tsconfig.base.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@
104104
"@vulcan-sql/extension-store-canner": [
105105
"packages/extension-store-canner/src/index.ts"
106106
],
107+
"@vulcan-sql/extension-api-caller": [
108+
"packages/extension-api-caller/src/index.ts"
109+
],
107110
"@vulcan-sql/integration-testing": [
108111
"packages/integration-testing/src/index"
109112
],

0 commit comments

Comments
 (0)