Skip to content

Commit

Permalink
fix: useStatefulResource() return type
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Mar 3, 2021
1 parent 0add0e9 commit 6a7b5e9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export {
useResetter,
hasUsableData,
} from './react-integration';
export type { SyntheticError } from './react-integration';
export type { SyntheticError, ErrorTypes } from './react-integration';
export {
StateContext,
DispatchContext,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/react-integration/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import useResetter from './useResetter';
import useFetchDispatcher from './useFetchDispatcher';
import useInvalidateDispatcher from './useInvalidateDispatcher';
export { default as hasUsableData } from './hasUsableData';
export type { SyntheticError } from './useError';
export type { SyntheticError, ErrorTypes } from './useError';

export {
useFetcher,
Expand Down
11 changes: 5 additions & 6 deletions packages/core/src/react-integration/hooks/useError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ export interface SyntheticError extends Error {
synthetic: true;
}

type UseErrorReturn<P> = P extends null
? undefined
:
| ((NetworkError | UnknownError) & { synthetic?: undefined | false })
| SyntheticError
| undefined;
export type ErrorTypes =
| ((NetworkError | UnknownError) & { synthetic?: undefined | false })
| SyntheticError;

type UseErrorReturn<P> = P extends null ? undefined : ErrorTypes | undefined;

/** Access a resource or error if failed to get it */
export default function useError<
Expand Down
2 changes: 1 addition & 1 deletion packages/legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"url": "https://github.com/coinbase/rest-hooks/issues"
},
"peerDependencies": {
"@rest-hooks/core": "^1.0.5",
"@rest-hooks/core": "^1.0.7",
"@types/react": "^16.8.4 || ^17.0.0",
"react": "^16.8.4 || ^17.0.0"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/legacy/src/__tests__/useStatefulResource.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
CoolerArticleResource,
InvalidIfStaleArticleResource,
} from '__tests__/common';
} from '__tests__/new';
import { makeRenderRestHook, makeCacheProvider } from '@rest-hooks/test';
import nock from 'nock';

Expand Down Expand Up @@ -90,7 +90,7 @@ describe('useStatefulResource()', () => {

it('should fetch anew with param changes', async () => {
const { result, waitForNextUpdate, rerender } = renderRestHook(
({ id }) => {
({ id }: { id: number }) => {
return useStatefulResource(CoolerArticleResource.detail(), {
id,
});
Expand Down Expand Up @@ -127,7 +127,7 @@ describe('useStatefulResource()', () => {
const { result, rerender, waitForNextUpdate } = renderRestHook(
props => {
return useStatefulResource(
InvalidIfStaleArticleResource.detailShape(),
InvalidIfStaleArticleResource.detail(),
props,
);
},
Expand Down
34 changes: 29 additions & 5 deletions packages/legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,47 @@ import {
useRetrieve,
useError,
Schema,
ReadShape,
useDenormalized,
StateContext,
hasUsableData,
useMeta,
ParamsFromShape,
ReadShape,
__INTERNAL__,
} from '@rest-hooks/core';
import type {
Denormalize,
DenormalizeNullable,
ErrorTypes,
} from '@rest-hooks/core';
import { denormalize } from '@rest-hooks/normalizr';
import { useContext } from 'react';

const { buildInferredResults } = __INTERNAL__;

type CondNull<P, A, B> = P extends null ? A : B;

type StatefulReturn<S extends Schema, P> = CondNull<
P,
{
data: DenormalizeNullable<S>;
loading: false;
error: undefined;
},
| {
data: Denormalize<S>;
loading: false;
error: undefined;
}
| { data: DenormalizeNullable<S>; loading: true; error: undefined }
| { data: DenormalizeNullable<S>; loading: false; error: ErrorTypes }
>;

/** Ensure a resource is available; loading and error returned explicitly. */
export function useStatefulResource<
Params extends Readonly<object>,
S extends Schema
>(fetchShape: ReadShape<S, Params>, params: Params | null) {
Shape extends ReadShape<any, any>,
Params extends ParamsFromShape<Shape> | null
>(fetchShape: Shape, params: Params): StatefulReturn<Shape['schema'], Params> {
const state = useContext(StateContext);
const [denormalized, ready, deleted, entitiesExpireAt] = useDenormalized(
fetchShape,
Expand Down Expand Up @@ -57,5 +81,5 @@ export function useStatefulResource<
data,
loading,
error,
};
} as any;
}

0 comments on commit 6a7b5e9

Please sign in to comment.