Skip to content

Commit

Permalink
return raw json value from extension-api-caller
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryosuke839 committed Oct 30, 2023
1 parent f0d7b1b commit 62b6df1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
8 changes: 4 additions & 4 deletions packages/extension-api-caller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ To pass the path parameters:

```sql
{% set a_variable_you_can_define = { "path": { "id": 1 } } %}
SELECT {{ a_variable_you_can_define | rest_api(url='https://dummyjson.com/products/:id') }}
SELECT {{ (a_variable_you_can_define | rest_api(url='https://dummyjson.com/products/:id')).id }}
```

To pass the query parameters:

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

To issue the POST request:

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

To pass the headers and multiple fields:

```sql
{% set a_variable_you_can_define = { "headers": { "Content-Type": "application/json" }, "body": { "title": "BMW Pencil" } } %}
SELECT {{ a_variable_you_can_define | rest_api(url='https://dummyjson.com/products/add', method='POST') }}
SELECT {{ a_variable_you_can_define | rest_api(url='https://dummyjson.com/products/add', method='POST') | dump }}
```
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const RestApiCallerFilter: FunctionalFilter = async ({
logger.info('API request:', options);
const results = await axios(options);
logger.info('API response:', results.data);
return JSON.stringify(results.data);
return results.data;
} catch (error: any) {
const message = error.response
? `response status: ${error.response.status}, response data: ${JSON.stringify(error.response.data)}`
Expand Down
34 changes: 31 additions & 3 deletions packages/extension-api-caller/test/restApiCaller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Test "rest_api" filter', () => {
extensions: { rest_api: path.join(__dirname, '..', 'src') },
});

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

// Act
await compileAndLoad(sql);
Expand Down Expand Up @@ -155,7 +155,7 @@ describe('Test "rest_api" filter', () => {
extensions: { rest_api: path.join(__dirname, '..', 'src') },
});

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

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

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') }}`;
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 }}`;

// Act
await compileAndLoad(sql);
await execute({});

// Assert
const queries = await getExecutedQueries();
const bindings = await getCreatedBinding();

expect(queries[0]).toBe('SELECT $1');
expect(bindings[0].get('$1')).toEqual(expected);
},
50 * 1000
)

it(
'Should work with template engine with field access',
async () => {
const expected = {
id: 101,
title: 'BMW Pencil'
}.id;

const { compileAndLoad, execute, getExecutedQueries, getCreatedBinding } = await getTestCompiler({
extensions: { rest_api: path.join(__dirname, '..', 'src') },
});

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 }}`;

// Act
await compileAndLoad(sql);
Expand Down

0 comments on commit 62b6df1

Please sign in to comment.