-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
67 lines (59 loc) · 2.13 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { GetStaticProps, NextComponentType, NextPageContext } from 'next';
import { createWrapper, MakeStore } from 'next-redux-wrapper';
import { applyMiddleware, createStore, Store } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import thunkMiddleware, {
ThunkDispatch as ReduxThunkDispatch,
} from 'redux-thunk';
import promiseMiddleware from 'redux-promise-middleware';
import reduxInmutableStateInvariant from 'redux-immutable-state-invariant';
import { GetServerSideProps } from '@_app';
import { State } from './model';
import { Action } from './actions';
import { reducer } from './reducers';
// tslint:disable: no-any
export type ThunkDispatch<A extends Action> = ReduxThunkDispatch<
State,
null,
A
>;
export type ActionCreator<A extends Action> = (...args: any[]) => A;
export type ThunkActionCreator<A extends Action, T extends any[] = any[]> = (
...args: T
) => (dispatch: ThunkDispatch<A>, getState: () => State) => void;
const makeStore: MakeStore<Store<State, Action>> = (context) => {
const middleware = [thunkMiddleware, promiseMiddleware];
if (!IS_PRODUCTION) {
middleware.unshift(reduxInmutableStateInvariant());
}
return createStore(
reducer,
composeWithDevTools(applyMiddleware(...middleware))
);
};
const w = createWrapper<Store<State, Action>>(makeStore, {
debug: !IS_PRODUCTION,
});
type GetInitialPageProps<P> = NextComponentType<
NextPageContext,
any,
P
>['getInitialProps'];
type AppStore = Omit<
typeof w,
'getServerSideProps' | 'getStaticProps' | 'getInitialPageProps'
> & {
getServerSideProps: <P extends {} = any>(
callback: (store: Store<State, Action>) => GetServerSideProps<P>
) => void;
getStaticProps: <P extends {} = any>(
callback: (store: Store<State, Action>) => GetStaticProps<P>
) => void;
getInitialPageProps: <P extends {} = any>(
callback: (store: Store<State, Action>) => GetInitialPageProps<P>
) => void;
};
// `wrapper` is required to be named like this
// but the alias `store` is provided as well to make the code clearer
export const wrapper = (w as unknown) as AppStore;
export const store = (w as unknown) as AppStore;