Skip to content

Commit

Permalink
enhance: Allow 'false' as Endpoint.sideEffect (#2452)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Feb 24, 2023
1 parent bdbed37 commit 69b6a04
Show file tree
Hide file tree
Showing 16 changed files with 66 additions and 18 deletions.
12 changes: 10 additions & 2 deletions packages/core/src/controller/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,11 @@ export default class Controller<
* @see https://resthooks.io/docs/api/Controller#subscribe
*/
subscribe = <
E extends EndpointInterface<FetchFunction, Schema | undefined, undefined>,
E extends EndpointInterface<
FetchFunction,
Schema | undefined,
undefined | false
>,
>(
endpoint: E,
...args: readonly [...Parameters<E>] | readonly [null]
Expand All @@ -270,7 +274,11 @@ export default class Controller<
* @see https://resthooks.io/docs/api/Controller#unsubscribe
*/
unsubscribe = <
E extends EndpointInterface<FetchFunction, Schema | undefined, undefined>,
E extends EndpointInterface<
FetchFunction,
Schema | undefined,
undefined | false
>,
>(
endpoint: E,
...args: readonly [...Parameters<E>] | readonly [null]
Expand Down
6 changes: 5 additions & 1 deletion packages/experimental/src/hooks/useSubscription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { useEffect } from 'react';
* @see https://resthooks.io/docs/api/useSubscription
*/
export default function useSubscription<
E extends EndpointInterface<FetchFunction, Schema | undefined, undefined>,
E extends EndpointInterface<
FetchFunction,
Schema | undefined,
undefined | false
>,
Args extends readonly [...Parameters<E>] | readonly [null],
>(endpoint: E, ...args: Args) {
const controller = useController();
Expand Down
4 changes: 2 additions & 2 deletions packages/normalizr/src/endpoint/EndpointInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Normalize } from '../types.js';
export interface EndpointInterface<
F extends FetchFunction = FetchFunction,
S extends Schema | undefined = Schema | undefined,
M extends true | undefined = true | undefined,
M extends boolean | undefined = boolean | undefined,
> extends EndpointExtraOptions<F> {
(...args: Parameters<F>): InferReturn<F, S>;
key(...args: Parameters<F>): string;
Expand Down Expand Up @@ -70,4 +70,4 @@ export interface MutateEndpoint<
export type ReadEndpoint<
F extends FetchFunction = FetchFunction,
S extends Schema | undefined = Schema | undefined,
> = EndpointInterface<F, S, undefined>;
> = EndpointInterface<F, S, undefined | false>;
6 changes: 5 additions & 1 deletion packages/react/src/hooks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
export type CondNull<P, A, B> = P extends null ? A : B;

export type SuspenseReturn<
E extends EndpointInterface<FetchFunction, Schema | undefined, undefined>,
E extends EndpointInterface<
FetchFunction,
Schema | undefined,
undefined | false
>,
Args extends readonly [...Parameters<E>] | readonly [null],
> = CondNull<
Args[0],
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/useCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import useController from '../hooks/useController.js';
*/
export default function useCache<
E extends Pick<
EndpointInterface<FetchFunction, Schema | undefined, undefined>,
EndpointInterface<FetchFunction, Schema | undefined, undefined | false>,
'key' | 'schema' | 'invalidIfStale'
>,
Args extends readonly [...Parameters<E['key']>] | readonly [null],
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/hooks/useDLE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ type StatefulReturn<S extends Schema | undefined, P> = CondNull<
* @see https://resthooks.io/docs/api/useDLE
*/
export default function useDLE<
E extends EndpointInterface<FetchFunction, Schema | undefined, undefined>,
E extends EndpointInterface<
FetchFunction,
Schema | undefined,
undefined | false
>,
Args extends readonly [...Parameters<E>] | readonly [null],
>(
endpoint: E,
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/hooks/useFetch.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import useFocusEffect from './useFocusEffect.native.js';
* @see https://resthooks.io/docs/api/useFetch
*/
export default function useFetch<
E extends EndpointInterface<FetchFunction, Schema | undefined, undefined>,
E extends EndpointInterface<
FetchFunction,
Schema | undefined,
undefined | false
>,
Args extends readonly [...Parameters<E>] | readonly [null],
>(endpoint: E, ...args: Args) {
const state = useCacheState();
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/hooks/useFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import useController from '../hooks/useController.js';
* @see https://resthooks.io/docs/api/useFetch
*/
export default function useFetch<
E extends EndpointInterface<FetchFunction, Schema | undefined, undefined>,
E extends EndpointInterface<
FetchFunction,
Schema | undefined,
undefined | false
>,
Args extends readonly [...Parameters<E>] | readonly [null],
>(endpoint: E, ...args: Args) {
const state = useCacheState();
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/hooks/useLive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import useSuspense from './useSuspense.js';
* @throws {NetworkError} If fetch fails.
*/
export default function useLive<
E extends EndpointInterface<FetchFunction, Schema | undefined, undefined>,
E extends EndpointInterface<
FetchFunction,
Schema | undefined,
undefined | false
>,
Args extends readonly [...Parameters<E>] | readonly [null],
>(endpoint: E, ...args: Args): SuspenseReturn<E, Args> {
useSubscription(endpoint, ...args);
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/hooks/useSubscription.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import useFocusEffect from './useFocusEffect.native.js';
* @see https://resthooks.io/docs/api/useSubscription
*/
export default function useSubscription<
E extends EndpointInterface<FetchFunction, Schema | undefined, undefined>,
E extends EndpointInterface<
FetchFunction,
Schema | undefined,
undefined | false
>,
Args extends readonly [...Parameters<E>] | readonly [null],
>(endpoint: E, ...args: Args) {
const controller = useController();
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/hooks/useSubscription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import useController from './useController.js';
* @see https://resthooks.io/docs/api/useSubscription
*/
export default function useSubscription<
E extends EndpointInterface<FetchFunction, Schema | undefined, undefined>,
E extends EndpointInterface<
FetchFunction,
Schema | undefined,
undefined | false
>,
Args extends readonly [...Parameters<E>] | readonly [null],
>(endpoint: E, ...args: Args) {
const controller = useController();
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/hooks/useSuspense.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ import useFocusEffect from './useFocusEffect.native.js';
* @throws {NetworkError} If fetch fails.
*/
export default function useSuspense<
E extends EndpointInterface<FetchFunction, Schema | undefined, undefined>,
E extends EndpointInterface<
FetchFunction,
Schema | undefined,
undefined | false
>,
Args extends readonly [...Parameters<E>] | readonly [null],
>(endpoint: E, ...args: Args): SuspenseReturn<E, Args> {
const state = useCacheState();
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/hooks/useSuspense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import useController from '../hooks/useController.js';
* @throws {NetworkError} If fetch fails.
*/
export default function useSuspense<
E extends EndpointInterface<FetchFunction, Schema | undefined, undefined>,
E extends EndpointInterface<
FetchFunction,
Schema | undefined,
undefined | false
>,
Args extends readonly [...Parameters<E>] | readonly [null],
>(endpoint: E, ...args: Args): SuspenseReturn<E, Args> {
const state = useCacheState();
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-hooks/src/hooks/useCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { ReadShape, ParamsFromShape } from '../endpoint/index.js';
export default function useCache<
E extends
| Pick<
EndpointInterface<FetchFunction, Schema | undefined, undefined>,
EndpointInterface<FetchFunction, Schema | undefined, undefined | false>,
'key' | 'schema' | 'invalidIfStale'
>
| Pick<ReadShape<any, any>, 'getFetchKey' | 'schema' | 'options'>,
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-hooks/src/hooks/useError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type UseErrorReturn<P> = P extends [null] ? undefined : ErrorTypes | undefined;
export default function useError<
E extends
| Pick<
EndpointInterface<FetchFunction, Schema | undefined, undefined>,
EndpointInterface<FetchFunction, Schema | undefined, undefined | false>,
'key' | 'schema' | 'invalidIfStale'
>
| Pick<ReadShape<any, any>, 'getFetchKey' | 'schema' | 'options'>,
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-hooks/src/hooks/useSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ReadShape, ParamsFromShape } from '../endpoint/index.js';
*/
export default function useSubscription<
E extends
| EndpointInterface<FetchFunction, Schema | undefined, undefined>
| EndpointInterface<FetchFunction, Schema | undefined, undefined | false>
| ReadShape<any, any>,
Args extends
| (E extends (...args: any) => any
Expand Down

1 comment on commit 69b6a04

@vercel
Copy link

@vercel vercel bot commented on 69b6a04 Feb 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.