Skip to content

Commit

Permalink
feat: add rison request type to makeApi (#843)
Browse files Browse the repository at this point in the history
  • Loading branch information
nytai authored and zhaoyongjie committed Nov 26, 2021
1 parent 6a42164 commit c527f55
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@types/d3-time": "^1.0.9",
"@types/d3-time-format": "^2.1.0",
"@types/lodash": "^4.14.149",
"@types/rison": "0.0.6",
"@vx/responsive": "^0.0.197",
"csstype": "^2.6.4",
"d3-format": "^1.3.2",
Expand All @@ -54,6 +55,7 @@
"pretty-ms": "^7.0.0",
"react-error-boundary": "^1.2.5",
"reselect": "^4.0.0",
"rison": "^0.1.1",
"whatwg-fetch": "^3.0.0"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import rison from 'rison';
import {
SupersetClient,
Payload as SupersetPayload,
Expand All @@ -29,7 +30,7 @@ import {
import handleError, { ErrorInput } from './handleError';
import { SupersetApiRequestOptions, SupersetApiErrorPayload, ParsedResponseType } from './types';

const validRequestTypes = new Set(['form', 'json', 'search']);
const validRequestTypes = new Set(['form', 'json', 'search', 'rison']);

interface SupersetApiFactoryOptions extends Omit<RequestBase, 'url'> {
/**
Expand All @@ -46,7 +47,7 @@ interface SupersetApiFactoryOptions extends Omit<RequestBase, 'url'> {
* - json: as JSON string with request Content-Type header set to application/json
* - search: add to search params
*/
requestType?: 'form' | 'json' | 'search';
requestType?: 'form' | 'json' | 'search' | 'rison';
}

function isPayloadless(method?: Method) {
Expand Down Expand Up @@ -81,7 +82,9 @@ export default function makeApi<
// use `search` payload (searchParams) when it's a GET request
const requestType = requestType_ || (isPayloadless(method) ? 'search' : 'json');
if (!validRequestTypes.has(requestType)) {
throw new Error('Invalid request payload type, choose from: form | json | search');
throw new Error(
`Invalid request payload type, choose from: ${[...validRequestTypes].join(' | ')}`,
);
}

async function request(
Expand All @@ -96,6 +99,8 @@ export default function makeApi<
};
if (requestType === 'search') {
requestConfig.searchParams = payload;
} else if (requestType === 'rison') {
requestConfig.endpoint = `${endpoint}?q=${rison.encode(payload)}`;
} else if (requestType === 'form') {
requestConfig.postPayload = payload;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ describe('makeApi()', () => {
expect(fetchMock.lastUrl()).toContain('/test-get-search?p1=1&p2=2&p3=1%2C2');
});

it('should serialize rison for method=GET, requestType=rison', async () => {
expect.assertions(1);
const api = makeApi({
method: 'GET',
endpoint: '/test-post-search',
requestType: 'rison',
});
fetchMock.get('glob:*/test-post-search*', { rison: 'get' });
await api({ p1: 1, p3: [1, 2] });
expect(fetchMock.lastUrl()).toContain('/test-post-search?q=(p1:1,p3:!(1,2))');
});

it('should use searchParams for method=POST, requestType=search', async () => {
expect.assertions(1);
const api = makeApi({
Expand Down

0 comments on commit c527f55

Please sign in to comment.