Skip to content

Commit

Permalink
enhance: Don't export action types (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Jan 16, 2020
1 parent c696110 commit 41fedfe
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 32 deletions.
8 changes: 8 additions & 0 deletions packages/rest-hooks/src/actionTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const FETCH_TYPE = 'rest-hooks/fetch' as const;
export const RECEIVE_TYPE = 'rest-hooks/receive' as const;
export const RECEIVE_MUTATE_TYPE = 'rest-hooks/rpc' as const;
export const RECEIVE_DELETE_TYPE = 'rest-hooks/purge' as const;
export const RESET_TYPE = 'rest-hooks/reset' as const;
export const SUBSCRIBE_TYPE = 'rest-hooks/subscribe' as const;
export const UNSUBSCRIBE_TYPE = 'rest-hook/unsubscribe' as const;
export const INVALIDATE_TYPE = 'rest-hooks/invalidate' as const;
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
import { DispatchContext, StateContext } from '../context';
import { useFetcher, useRetrieve, useInvalidator, useResetter } from '../hooks';
import { initialState } from '../../state/reducer';
import { State, ActionTypes, INVALIDATE_TYPE, RESET_TYPE } from '../../types';
import { State, ActionTypes } from '../../types';
import { INVALIDATE_TYPE, RESET_TYPE } from '../../actionTypes';
import { users, articlesPages, payload } from './fixtures';

async function testDispatchFetch(
Expand Down
6 changes: 2 additions & 4 deletions packages/rest-hooks/src/react-integration/hooks/useFetcher.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { useContext, useRef, useCallback } from 'react';

import { FetchAction, UpdateFunction, ReceiveTypes } from '~/types';
import {
FetchAction,
UpdateFunction,
RECEIVE_DELETE_TYPE,
RECEIVE_MUTATE_TYPE,
RECEIVE_TYPE,
ReceiveTypes,
FETCH_TYPE,
} from '~/types';
} from '~/actionTypes';
import {
FetchShape,
DeleteShape,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useContext, useCallback, useRef } from 'react';

import { ReadShape, Schema } from '~/resource';
import { DispatchContext } from '~/react-integration/context';
import { INVALIDATE_TYPE } from '~/types';
import { INVALIDATE_TYPE } from '~/actionTypes';

/** Invalidate a certain item within the cache */
export default function useInvalidator<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext, useCallback } from 'react';

import { DispatchContext } from '~/react-integration/context';
import { RESET_TYPE } from '~/types';
import { RESET_TYPE } from '~/actionTypes';

/** Returns a function to completely clear the cache of all entries */
export default function useResetter(): () => void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useContext, useEffect, useRef } from 'react';

import { DispatchContext } from '~/react-integration/context';
import { ReadShape, Schema } from '~/resource';
import { SUBSCRIBE_TYPE, UNSUBSCRIBE_TYPE } from '~/types';
import { SUBSCRIBE_TYPE, UNSUBSCRIBE_TYPE } from '~/actionTypes';

/** Keeps a resource fresh by subscribing to updates. */
export default function useSubscription<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import CacheProvider from '../CacheProvider';
import NetworkManager from '../../../state/NetworkManager';
import SubscriptionManager from '../../../state/SubscriptionManager';
import PollingSubscription from '../../../state/PollingSubscription';
import { RECEIVE_TYPE } from '~/types';

import { RECEIVE_TYPE } from '~/actionTypes';

describe('<CacheProvider />', () => {
it('should not change dispatch function on re-render', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/rest-hooks/src/state/NetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import {
Manager,
PurgeAction,
Dispatch,
} from '~/types';
import {
RECEIVE_TYPE,
RECEIVE_MUTATE_TYPE,
RECEIVE_DELETE_TYPE,
FETCH_TYPE,
RESET_TYPE,
} from '~/types';
} from '~/actionTypes';
import { RPCAction } from '..';

/** Handles all async network dispatches
Expand Down
3 changes: 2 additions & 1 deletion packages/rest-hooks/src/state/PollingSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Subscription, SubscriptionInit } from './SubscriptionManager';
import isOnline from './isOnline';

import { Schema } from '~/resource';
import { Dispatch, FETCH_TYPE, RECEIVE_TYPE } from '~/types';
import { Dispatch } from '~/types';
import { FETCH_TYPE, RECEIVE_TYPE } from '~/actionTypes';

/**
* PollingSubscription keeps a given resource updated by
Expand Down
3 changes: 1 addition & 2 deletions packages/rest-hooks/src/state/SubscriptionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import {
UnsubscribeAction,
Manager,
Dispatch,
SUBSCRIBE_TYPE,
UNSUBSCRIBE_TYPE,
} from '~/types';
import { SUBSCRIBE_TYPE, UNSUBSCRIBE_TYPE } from '~/actionTypes';
import { Schema } from '~/resource';

type Actions = UnsubscribeAction | SubscribeAction;
Expand Down
5 changes: 2 additions & 3 deletions packages/rest-hooks/src/state/__tests__/networkManager.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { ArticleResource } from '__tests__/common';

import NetworkManager from '../NetworkManager';
import { FetchAction, ResetAction } from '../../types';
import {
FetchAction,
ResetAction,
FETCH_TYPE,
RECEIVE_TYPE,
RECEIVE_MUTATE_TYPE,
RESET_TYPE,
} from '../../types';
} from '../../actionTypes';

describe('NetworkManager', () => {
const manager = new NetworkManager();
Expand Down
4 changes: 3 additions & 1 deletion packages/rest-hooks/src/state/__tests__/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import {
ResetAction,
InvalidateAction,
UpdateFunction,
} from '../../types';
import {
RECEIVE_TYPE,
RECEIVE_MUTATE_TYPE,
RECEIVE_DELETE_TYPE,
INVALIDATE_TYPE,
FETCH_TYPE,
RESET_TYPE,
} from '../../types';
} from '../../actionTypes';

describe('reducer', () => {
describe('singles', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { PollingArticleResource } from '__tests__/common';

import SubscriptionManager, { Subscription } from '../SubscriptionManager';
import { SubscribeAction, UnsubscribeAction } from '../../types';
import {
SubscribeAction,
UnsubscribeAction,
UNSUBSCRIBE_TYPE,
SUBSCRIBE_TYPE,
RECEIVE_TYPE,
} from '../../types';
} from '../../actionTypes';

function onError(e: any) {
e.preventDefault();
Expand Down
5 changes: 2 additions & 3 deletions packages/rest-hooks/src/state/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import mergeDeepCopy from './merge/mergeDeepCopy';
import applyUpdatersToResults from './applyUpdatersToResults';

import { normalize } from '~/resource';
import { ActionTypes, State } from '~/types';
import {
ActionTypes,
State,
RECEIVE_TYPE,
RECEIVE_MUTATE_TYPE,
RECEIVE_DELETE_TYPE,
INVALIDATE_TYPE,
RESET_TYPE,
FETCH_TYPE,
} from '~/types';
} from '~/actionTypes';

export const initialState: State<unknown> = {
entities: {},
Expand Down
19 changes: 10 additions & 9 deletions packages/rest-hooks/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import { FSAWithPayloadAndMeta, FSAWithMeta, FSA } from 'flux-standard-action';

import { ErrorableFSAWithPayloadAndMeta, ErrorableFSAWithMeta } from './fsa';
import { Schema, schemas, Normalize } from './resource';
import {
RECEIVE_TYPE,
RECEIVE_MUTATE_TYPE,
RECEIVE_DELETE_TYPE,
RESET_TYPE,
FETCH_TYPE,
SUBSCRIBE_TYPE,
UNSUBSCRIBE_TYPE,
INVALIDATE_TYPE,
} from './actionTypes';

export type Method = 'get' | 'post' | 'put' | 'patch' | 'delete' | 'options';

export const FETCH_TYPE = 'rest-hooks/fetch' as const;
export const RECEIVE_TYPE = 'rest-hooks/receive' as const;
export const RECEIVE_MUTATE_TYPE = 'rest-hooks/rpc' as const;
export const RECEIVE_DELETE_TYPE = 'rest-hooks/purge' as const;
export const RESET_TYPE = 'rest-hooks/reset' as const;
export const SUBSCRIBE_TYPE = 'rest-hooks/subscribe' as const;
export const UNSUBSCRIBE_TYPE = 'rest-hook/unsubscribe' as const;
export const INVALIDATE_TYPE = 'rest-hooks/invalidate' as const;

export type ReceiveTypes =
| typeof RECEIVE_TYPE
| typeof RECEIVE_MUTATE_TYPE
Expand Down

0 comments on commit 41fedfe

Please sign in to comment.