-
Notifications
You must be signed in to change notification settings - Fork 272
feat(query): add functions to wrap api calls with typings #555
Changes from all commits
a535838
4021ff5
cca63ef
67a7b2f
09b4346
7483a33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { SupersetClientInterface, SupersetClient, RequestConfig } from '@superset-ui/connection'; | ||
import { QueryFormData } from '../../types/QueryFormData'; | ||
import { LegacyChartDataResponse } from './types'; | ||
|
||
export interface Params { | ||
client?: SupersetClientInterface; | ||
method?: 'GET' | 'POST'; | ||
requestConfig?: Partial<RequestConfig>; | ||
url?: string; | ||
formData: QueryFormData; | ||
} | ||
|
||
export default function fetchExploreJson({ | ||
client = SupersetClient, | ||
method = 'POST', | ||
requestConfig, | ||
url = '/superset/explore_json/', | ||
formData, | ||
}: Params) { | ||
const fetchFunc = method === 'GET' ? client.get : client.post; | ||
|
||
return fetchFunc({ | ||
...requestConfig, | ||
// TODO: Have to transform formData as query string for GET | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have an example for what a query string could look like? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a utility function in |
||
url, | ||
postPayload: { form_data: formData }, | ||
} as RequestConfig).then(({ json }) => json as LegacyChartDataResponse); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { V1ChartDataResponseResult } from '../v1/types'; | ||
|
||
export interface LegacyChartDataResponse extends Omit<V1ChartDataResponseResult, 'data'> { | ||
data: Record<string, unknown>[] | Record<string, unknown>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { SupersetClientInterface, SupersetClient, RequestConfig } from '@superset-ui/connection'; | ||
import { QueryContext } from '../../types/Query'; | ||
import { V1ChartDataResponse } from './types'; | ||
|
||
export interface Params { | ||
client?: SupersetClientInterface; | ||
requestConfig?: Partial<RequestConfig>; | ||
queryContext: QueryContext; | ||
} | ||
|
||
export default function postChartData({ | ||
client = SupersetClient, | ||
requestConfig, | ||
queryContext, | ||
}: Params) { | ||
return client | ||
.post({ | ||
...requestConfig, | ||
endpoint: '/api/v1/chart/data', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(queryContext), | ||
} as RequestConfig) | ||
.then(({ json }) => json as V1ChartDataResponse); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* eslint-disable camelcase */ | ||
export interface V1ChartDataResponseResult { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree with the doubts about variable names. This was the hardest part about this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think marking it as v1 is useful in cases where the interface specifically relates to behavior of the v1 api. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still don't feel strongly either way so I will merge and adjust later if anybody has more comments. |
||
cache_key: string | null; | ||
cache_timeout: number | null; | ||
cache_dttm: string | null; | ||
data: Record<string, unknown>[]; | ||
error: string | null; | ||
is_cached: boolean; | ||
query: string; | ||
rowcount: number; | ||
stacktrace: string | null; | ||
status: 'stopped' | 'failed' | 'pending' | 'running' | 'scheduled' | 'success' | 'timed_out'; | ||
} | ||
|
||
export interface V1ChartDataResponse { | ||
result: V1ChartDataResponseResult[]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const LOGIN_GLOB = 'glob:*superset/csrf_token/*'; // eslint-disable-line import/prefer-default-export |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import fetchMock from 'fetch-mock'; | ||
import { SupersetClient } from '@superset-ui/connection'; | ||
import { LOGIN_GLOB } from '../fixtures/constants'; | ||
import { fetchExploreJson } from '../../../src'; | ||
|
||
describe('fetchExploreJson()', () => { | ||
beforeAll(() => { | ||
fetchMock.get(LOGIN_GLOB, { csrf_token: '1234' }); | ||
SupersetClient.reset(); | ||
SupersetClient.configure().init(); | ||
}); | ||
|
||
afterEach(fetchMock.restore); | ||
|
||
it('returns a promise of LegacyChartDataResponse', () => { | ||
fetchMock.post('glob:*/superset/explore_json/', { | ||
field1: 'abc', | ||
field2: 'def', | ||
}); | ||
|
||
return expect( | ||
fetchExploreJson({ | ||
formData: { | ||
granularity: 'minute', | ||
viz_type: 'word_cloud', | ||
datasource: '1__table', | ||
}, | ||
}), | ||
).resolves.toEqual({ | ||
field1: 'abc', | ||
field2: 'def', | ||
}); | ||
}); | ||
it('uses GET when specified', () => { | ||
fetchMock.get('glob:*/superset/explore_json/', { | ||
field1: 'abc', | ||
field2: 'def', | ||
}); | ||
|
||
return expect( | ||
fetchExploreJson({ | ||
method: 'GET', | ||
formData: { | ||
granularity: 'minute', | ||
viz_type: 'word_cloud', | ||
datasource: '1__table', | ||
}, | ||
}), | ||
).resolves.toEqual({ | ||
field1: 'abc', | ||
field2: 'def', | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import fetchMock from 'fetch-mock'; | ||
import { SupersetClient } from '@superset-ui/connection'; | ||
import { LOGIN_GLOB } from '../fixtures/constants'; | ||
import { postChartData, buildQueryContext } from '../../../src'; | ||
|
||
describe('postChartData()', () => { | ||
beforeAll(() => { | ||
fetchMock.get(LOGIN_GLOB, { csrf_token: '1234' }); | ||
SupersetClient.reset(); | ||
SupersetClient.configure().init(); | ||
}); | ||
|
||
afterEach(fetchMock.restore); | ||
|
||
it('returns a promise of ChartDataResponse', () => { | ||
fetchMock.post('glob:*/api/v1/chart/data', { | ||
result: [ | ||
{ | ||
field1: 'abc', | ||
field2: 'def', | ||
}, | ||
], | ||
}); | ||
|
||
return expect( | ||
postChartData({ | ||
queryContext: buildQueryContext({ | ||
granularity: 'minute', | ||
viz_type: 'word_cloud', | ||
datasource: '1__table', | ||
}), | ||
}), | ||
).resolves.toEqual({ | ||
result: [ | ||
{ | ||
field1: 'abc', | ||
field2: 'def', | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank god for this custom type... i have mild ptsd from
formData
lol