Skip to content

Commit

Permalink
Fix executeSQL through POST requests (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josmorsot committed Nov 18, 2022
1 parent 39a1409 commit 631c883
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Not released

- Fix `executeSQL` through **POST** request [#531](https://github.com/CartoDB/carto-react/pull/531)

## 1.4

### 1.4.6 (2022-11-11)
Expand Down
44 changes: 36 additions & 8 deletions packages/react-api/src/api/SQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,27 @@ const DEFAULT_USER_COMPONENT_IN_URL = '{user}';
* @param { Object } props.opts - Additional options for the HTTP request
* @param { import('@deck.gl/carto').QueryParameters } props.queryParameters - SQL query parameters
*/
export const executeSQL = async ({ credentials, query, connection, opts, queryParameters }) => {
export const executeSQL = async ({
credentials,
query,
connection,
opts,
queryParameters
}) => {
let response;

if (!credentials) {
throw new Error('No credentials provided');
}

try {
const request = createRequest({ credentials, connection, query, opts, queryParameters });
const request = createRequest({
credentials,
connection,
query,
opts,
queryParameters
});
response = await fetch(request);
} catch (error) {
if (error.name === 'AbortError') throw error;
Expand All @@ -55,16 +67,21 @@ export const executeSQL = async ({ credentials, query, connection, opts, queryPa
* Create an 'SQL query' request
* (using GET or POST request, depending on url size)
*/
function createRequest({ credentials, connection, query, opts = {}, queryParameters = [] }) {
function createRequest({
credentials,
connection,
query,
opts = {},
queryParameters = []
}) {
const { abortController, ...otherOptions } = opts;

const { apiVersion = API_VERSIONS.V2 } = credentials;

const rawParams = {
client: CLIENT,
q: query?.trim(),
...otherOptions,
queryParameters: queryParameters ? JSON.stringify(queryParameters) : undefined
...otherOptions
};

if (apiVersion === API_VERSIONS.V3) {
Expand All @@ -79,15 +96,21 @@ function createRequest({ credentials, connection, query, opts = {}, queryParamet
}

// Get request
const urlParamsForGet = Object.entries(rawParams).map(([key, value]) =>
const getParams = {
...rawParams,
queryParameters: queryParameters ? JSON.stringify(queryParameters) : undefined
};
const urlParamsForGet = Object.entries(getParams).map(([key, value]) =>
encodeParameter(key, value)
);
const getUrl = generateApiUrl({
credentials,
connection,
parameters: urlParamsForGet
});
if (getUrl.length < REQUEST_GET_MAX_URL_LENGTH) {

const isGet = getUrl.length < REQUEST_GET_MAX_URL_LENGTH;
if (isGet) {
return getRequest(getUrl, requestOpts);
}

Expand All @@ -96,12 +119,17 @@ function createRequest({ credentials, connection, query, opts = {}, queryParamet
apiVersion === API_VERSIONS.V3
? [`access_token=${credentials.accessToken}`, `client=${CLIENT}`]
: null;

const payload = {
...rawParams,
queryParameters
};
const postUrl = generateApiUrl({
credentials,
connection,
parameters: urlParamsForPost
});
return postRequest(postUrl, rawParams, requestOpts);
return postRequest(postUrl, payload, requestOpts);
}

/**
Expand Down

0 comments on commit 631c883

Please sign in to comment.