From 9782cf862e24ccdb8afcabd21e936651eea2bc72 Mon Sep 17 00:00:00 2001 From: Omar Sotillo Date: Wed, 15 Sep 2021 13:17:43 +0200 Subject: [PATCH] fix: removes header and use post request with body (#12) * fix: removes header and use post request with body * release: 0.4.0 --- package.json | 2 +- src/executor/index.spec.ts | 8 +++++--- src/executor/index.ts | 22 +++++++++++++++------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 24f4e97..ad944c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@factorialco/tentaclesql", - "version": "0.3.0", + "version": "0.4.0", "description": "SQL engine from multiple sources", "bin": { "tentaclesql": "dist/cli.js" diff --git a/src/executor/index.spec.ts b/src/executor/index.spec.ts index 71eaffc..bf676d2 100644 --- a/src/executor/index.spec.ts +++ b/src/executor/index.spec.ts @@ -57,12 +57,14 @@ test('executor', async () => { const sql = 'SELECT employees.id + goal_configs.id as value FROM goal_configs JOIN employees ON (employees.id + ?) == goal_configs.id' const result = await executor(sql, [5], headers) - const ast = JSON.stringify(parseSql(sql)) + const body = JSON.stringify( + { query_ast: parseSql(sql) } + ) expect(mockedFetch).toHaveBeenCalledTimes(3) expect(mockedFetch).toHaveBeenCalledWith('https://api.example.com/schema', { headers }) - expect(mockedFetch).toHaveBeenCalledWith('https://api.example.com/tables/goal_configs', { headers: { ...headers, 'x-tentacle-query-ast': ast } }) - expect(mockedFetch).toHaveBeenCalledWith('https://api.example.com/tables/employees', { headers: { ...headers, 'x-tentacle-query-ast': ast } }) + expect(mockedFetch).toHaveBeenCalledWith('https://api.example.com/tables/goal_configs', { headers: headers, method: 'POST', body: body }) + expect(mockedFetch).toHaveBeenCalledWith('https://api.example.com/tables/employees', { headers: headers, method: 'POST', body: body }) expect(result).toEqual([{ value: 25 }]) }) diff --git a/src/executor/index.ts b/src/executor/index.ts index c1c7074..8513621 100644 --- a/src/executor/index.ts +++ b/src/executor/index.ts @@ -39,8 +39,15 @@ function mutateDataframe ( return df.forEach(row => { Object.keys(row).forEach(k => fn(row, k)) }) } -async function fetchTableData (tableDefinition: TableDefinition, headers: any) { - const res = await fetch(tableDefinition.url, { headers }) +async function fetchTableData (tableDefinition: TableDefinition, headers: any, queryAst: any) { + const res = await fetch(tableDefinition.url, { + headers: headers, + method: 'POST', + body: JSON.stringify({ + query_ast: queryAst + }) + } + ) if (!res.ok) { return Promise.reject(new Error(`Error with the request. Status code: ${res.status}`)) @@ -53,7 +60,8 @@ async function populateTables ( db: DatabaseType, usedTables: Array, headers: any, - schema: any + schema: any, + queryAst: any ) { const filteredTableDefinition = schema.filter(( tableDefinition: TableDefinition @@ -62,7 +70,7 @@ async function populateTables ( const promises = filteredTableDefinition.map(async (tableDefinition: TableDefinition) => { createTable(db, tableDefinition) - const data = await fetchTableData(tableDefinition, headers) + const data = await fetchTableData(tableDefinition, headers, queryAst) if (data.length === 0) return @@ -125,10 +133,10 @@ async function executor ( usedTables, { ...headers, host: getHost(), - 'user-agent': `tentaclesql/${version}`, - 'x-tentacle-query-ast': JSON.stringify(ast) + 'user-agent': `tentaclesql/${version}` }, - schema + schema, + ast ) }