Skip to content

Commit

Permalink
enhance: Infer returntype of useFetcher()'s functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Apr 1, 2020
1 parent 5b360df commit 4b2a19d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
10 changes: 7 additions & 3 deletions packages/rest-hooks/src/react-integration/hooks/useFetcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useContext, useRef, useCallback } from 'react';
import { FetchAction, UpdateFunction, ReceiveTypes } from 'rest-hooks/types';
import {
RECEIVE_DELETE_TYPE,
Expand All @@ -17,6 +16,8 @@ import {
} from 'rest-hooks/resource';
import { DispatchContext } from 'rest-hooks/react-integration/context';

import { useContext, useRef, useCallback } from 'react';

const SHAPE_TYPE_TO_RESPONSE_TYPE: Record<
FetchShape<any, any, any>['type'],
ReceiveTypes
Expand Down Expand Up @@ -46,7 +47,10 @@ export default function useFetcher<
fetchShape: Shape,
throttle = false,
): Shape extends DeleteShape<any, any, any>
? (params: ParamsFromShape<Shape>, body: BodyFromShape<Shape>) => Promise<any>
? (
params: ParamsFromShape<Shape>,
body: BodyFromShape<Shape>,
) => ReturnType<typeof fetchShape['fetch']>
: <
UpdateParams extends OptimisticUpdateParams<
SchemaFromShape<Shape>,
Expand All @@ -56,7 +60,7 @@ export default function useFetcher<
params: ParamsFromShape<Shape>,
body: BodyFromShape<Shape>,
updateParams?: UpdateParams | undefined,
) => Promise<any> {
) => ReturnType<typeof fetchShape['fetch']> {
const dispatch = useContext(DispatchContext);

// we just want the current values when we dispatch, so
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useMemo, useContext } from 'react';
import {
ReadShape,
Denormalize,
Expand All @@ -8,6 +7,8 @@ import {
import { useDenormalized } from 'rest-hooks/state/selectors';
import { StateContext } from 'rest-hooks/react-integration/context';

import { useMemo, useContext } from 'react';

import useRetrieve from './useRetrieve';
import useError from './useError';
import hasUsableData from './hasUsableData';
Expand Down Expand Up @@ -61,7 +62,7 @@ function useManyResources<A extends ResourceArgs<any, any>[]>(
// only wait on promises without results
.map(
(p, i) =>
!hasUsableData(denormalizedValues[i][1], resourceList[i][0]) && p,
!hasUsableData(denormalizedValues[i][1], resourceList[i][0]) && !!p,
);

// throw first valid error
Expand Down
10 changes: 5 additions & 5 deletions packages/rest-hooks/src/react-integration/hooks/useRetrieve.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { ReadShape, Schema } from 'rest-hooks/resource';
import { ReadShape, ParamsFromShape } from 'rest-hooks/resource';

import { useMemo } from 'react';

import useFetcher from './useFetcher';
import useExpiresAt from './useExpiresAt';

/** Request a resource if it is not in cache. */
export default function useRetrieve<
Params extends Readonly<object>,
S extends Schema
>(fetchShape: ReadShape<S, Params>, params: Params | null) {
export default function useRetrieve<Shape extends ReadShape<any, any>>(
fetchShape: Shape,
params: ParamsFromShape<Shape> | null,
) {
const fetch = useFetcher(fetchShape, true);
const expiresAt = useExpiresAt(fetchShape, params);

Expand Down

0 comments on commit 4b2a19d

Please sign in to comment.