Skip to content

Commit 23dca7c

Browse files
committed
return raw json value from extension-api-caller
1 parent 5d8a0bd commit 23dca7c

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

packages/extension-api-caller/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,26 @@ To pass the path parameters:
2323
2424
```sql
2525
{% set a_variable_you_can_define = { "path": { "id": 1 } } %}
26-
SELECT {{ a_variable_you_can_define | rest_api(url='https://dummyjson.com/products/:id') }}
26+
SELECT {{ (a_variable_you_can_define | rest_api(url='https://dummyjson.com/products/:id')).id }}
2727
```
2828

2929
To pass the query parameters:
3030

3131
```sql
3232
{% set a_variable_you_can_define = { "query": { "q": "phone" } } %}
33-
SELECT {{ a_variable_you_can_define | rest_api(url='https://dummyjson.com/products/search') }}
33+
SELECT {{ a_variable_you_can_define | rest_api(url='https://dummyjson.com/products/search') | dump }}
3434
```
3535

3636
To issue the POST request:
3737

3838
```sql
3939
{% set a_variable_you_can_define = { "body": { "title": "BMW Pencil" } } %}
40-
SELECT {{ a_variable_you_can_define | rest_api(url='https://dummyjson.com/products/add', method='POST') }}
40+
SELECT {{ a_variable_you_can_define | rest_api(url='https://dummyjson.com/products/add', method='POST') | dump }}
4141
```
4242

4343
To pass the headers and multiple fields:
4444

4545
```sql
4646
{% set a_variable_you_can_define = { "headers": { "Content-Type": "application/json" }, "body": { "title": "BMW Pencil" } } %}
47-
SELECT {{ a_variable_you_can_define | rest_api(url='https://dummyjson.com/products/add', method='POST') }}
47+
SELECT {{ a_variable_you_can_define | rest_api(url='https://dummyjson.com/products/add', method='POST') | dump }}
4848
```

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const RestApiCallerFilter: FunctionalFilter = async ({
4848
logger.info('API request:', options);
4949
const results = await axios(options);
5050
logger.info('API response:', results.data);
51-
return JSON.stringify(results.data);
51+
return results.data;
5252
} catch (error: any) {
5353
const message = error.response
5454
? `response status: ${error.response.status}, response data: ${JSON.stringify(error.response.data)}`

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

+31-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('Test "rest_api" filter', () => {
5050
extensions: { rest_api: path.join(__dirname, '..', 'src') },
5151
});
5252

53-
const sql = `{% set value = { "path": { "id": 1 } } %}SELECT {{ value | rest_api(url='https://dummyjson.com/products/:id') }}`;
53+
const sql = `{% set value = { "path": { "id": 1 } } %}SELECT {{ value | rest_api(url='https://dummyjson.com/products/:id') | dump }}`;
5454

5555
// Act
5656
await compileAndLoad(sql);
@@ -155,7 +155,7 @@ describe('Test "rest_api" filter', () => {
155155
extensions: { rest_api: path.join(__dirname, '..', 'src') },
156156
});
157157

158-
const sql = `{% set value = { "query": { "q": "phone" } } %}SELECT {{ value | rest_api(url='https://dummyjson.com/products/search') }}`;
158+
const sql = `{% set value = { "query": { "q": "phone" } } %}SELECT {{ value | rest_api(url='https://dummyjson.com/products/search') | dump }}`;
159159

160160
// Act
161161
await compileAndLoad(sql);
@@ -183,7 +183,35 @@ describe('Test "rest_api" filter', () => {
183183
extensions: { rest_api: path.join(__dirname, '..', 'src') },
184184
});
185185

186-
const sql = `{% set value = { "body": { "title": "BMW Pencil" }, "headers": { "Content-Type": "application/json" } } %}SELECT {{ value | rest_api(url='https://dummyjson.com/products/add', method='POST') }}`;
186+
const sql = `{% set value = { "body": { "title": "BMW Pencil" }, "headers": { "Content-Type": "application/json" } } %}SELECT {{ value | rest_api(url='https://dummyjson.com/products/add', method='POST') | dump }}`;
187+
188+
// Act
189+
await compileAndLoad(sql);
190+
await execute({});
191+
192+
// Assert
193+
const queries = await getExecutedQueries();
194+
const bindings = await getCreatedBinding();
195+
196+
expect(queries[0]).toBe('SELECT $1');
197+
expect(bindings[0].get('$1')).toEqual(expected);
198+
},
199+
50 * 1000
200+
)
201+
202+
it(
203+
'Should work with template engine with field access',
204+
async () => {
205+
const expected = {
206+
id: 101,
207+
title: 'BMW Pencil'
208+
}.id;
209+
210+
const { compileAndLoad, execute, getExecutedQueries, getCreatedBinding } = await getTestCompiler({
211+
extensions: { rest_api: path.join(__dirname, '..', 'src') },
212+
});
213+
214+
const sql = `{% set value = { "body": { "title": "BMW Pencil" }, "headers": { "Content-Type": "application/json" } } %}SELECT {{ (value | rest_api(url='https://dummyjson.com/products/add', method='POST')).id }}`;
187215

188216
// Act
189217
await compileAndLoad(sql);

0 commit comments

Comments
 (0)