better names

This commit is contained in:
David Aaron Suddjian 2021-02-22 16:29:57 -08:00
parent 8f6cfcffc0
commit 2c6fd0abb8
3 changed files with 9 additions and 8 deletions

View File

@ -144,7 +144,7 @@ export function useApiResourceFullBody<RESULT>(
* @param transformFn a callback that transforms the result object into the shape you want. * @param transformFn a callback that transforms the result object into the shape you want.
* Make sure to use a persistent function for this so it doesn't constantly recalculate! * Make sure to use a persistent function for this so it doesn't constantly recalculate!
*/ */
export function useResourceTransform<IN, OUT>( export function useTransformedResource<IN, OUT>(
resource: Resource<IN>, resource: Resource<IN>,
transformFn: (result: IN) => OUT, transformFn: (result: IN) => OUT,
): Resource<OUT> { ): Resource<OUT> {
@ -161,7 +161,8 @@ export function useResourceTransform<IN, OUT>(
} }
// returns the "result" field from a fetched API v1 endpoint // returns the "result" field from a fetched API v1 endpoint
const innerResult = <T>(responseBody: { result: T }) => responseBody.result; const extractInnerResult = <T>(responseBody: { result: T }) =>
responseBody.result;
/** /**
* A general-purpose hook to fetch a Superset resource from a v1 API endpoint. * A general-purpose hook to fetch a Superset resource from a v1 API endpoint.
@ -173,8 +174,8 @@ const innerResult = <T>(responseBody: { result: T }) => responseBody.result;
* @param endpoint The url where the resource is located. * @param endpoint The url where the resource is located.
*/ */
export function useApiV1Resource<RESULT>(endpoint: string): Resource<RESULT> { export function useApiV1Resource<RESULT>(endpoint: string): Resource<RESULT> {
return useResourceTransform( return useTransformedResource(
useApiResourceFullBody<{ result: RESULT }>(endpoint), useApiResourceFullBody<{ result: RESULT }>(endpoint),
innerResult, extractInnerResult,
); );
} }

View File

@ -19,7 +19,7 @@
import rison from 'rison'; import rison from 'rison';
import Chart from 'src/types/Chart'; import Chart from 'src/types/Chart';
import { useApiV1Resource, useResourceTransform } from './apiResources'; import { useApiV1Resource, useTransformedResource } from './apiResources';
function extractOwnerNames({ owners }: Chart) { function extractOwnerNames({ owners }: Chart) {
if (!owners) return null; if (!owners) return null;
@ -32,7 +32,7 @@ const ownerNamesQuery = rison.encode({
}); });
export function useChartOwnerNames(chartId: string) { export function useChartOwnerNames(chartId: string) {
return useResourceTransform( return useTransformedResource(
useApiV1Resource<Chart>(`/api/v1/chart/${chartId}?q=${ownerNamesQuery}`), useApiV1Resource<Chart>(`/api/v1/chart/${chartId}?q=${ownerNamesQuery}`),
extractOwnerNames, extractOwnerNames,
); );

View File

@ -20,10 +20,10 @@
export { export {
useApiResourceFullBody, useApiResourceFullBody,
useApiV1Resource, useApiV1Resource,
useResourceTransform, useTransformedResource,
} from './apiResources'; } from './apiResources';
// A central catalog of API hooks. // A central catalog of API Resource hooks.
// Add new API hooks here, organized under // Add new API hooks here, organized under
// different files for different resource types. // different files for different resource types.
export { useChartOwnerNames } from './charts'; export { useChartOwnerNames } from './charts';