This repository has been archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(query): add functions to wrap api calls with typings (#555)
* feat: add function to wrap api calls * feat: add function to wrap api calls with returned type * fix: remove deprecated * fix: rename * test: add unit test * test: add unit tests
- Loading branch information
Showing
9 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/superset-ui-query/src/api/legacy/fetchExploreJson.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
url, | ||
postPayload: { form_data: formData }, | ||
} as RequestConfig).then(({ json }) => json as LegacyChartDataResponse); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* eslint-disable camelcase */ | ||
export interface V1ChartDataResponseResult { | ||
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[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
54 changes: 54 additions & 0 deletions
54
packages/superset-ui-query/test/api/legacy/fetchExploreJson.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
packages/superset-ui-query/test/api/v1/postChartData.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |