This repository has been archived by the owner on Sep 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
storeUtils.ts
52 lines (50 loc) · 1.51 KB
/
storeUtils.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
import Fetchr from "fetchr";
import { createMemoryHistory } from "react-router";
import { routerMiddleware } from "react-router-redux";
import { createStore as create, applyMiddleware } from "redux";
import steps from "redux-effects-steps";
import fetchr from "redux-effects-fetchr";
import uploader from "redux-effects-formdata-uploader";
import authMiddleware from "../../middleware/authMiddleware";
import { sign } from "../../../../server/services/AccessToken";
import reducer from "../../modules/reducer";
import configs from "../../../../server/configs";
export function createWithSignedStore(name: string, aud: any, options: Object) {
return sign(
{
sub: name,
aud,
exp: Math.floor(Date.now() / 1000),
},
configs.auth.key,
).then(token => {
const store = createStore({
initialState: {
app: { auth: { username: name, login: true } },
},
});
return store;
});
}
export function createStore(options: any) {
const history = createMemoryHistory(options.historyRoute || "/");
const initialState = options.initialState || {};
const store = (create as any)(
reducer,
initialState,
(applyMiddleware as any)(
...[
steps,
authMiddleware(),
fetchr(new Fetchr({ ...configs.fetchr, req: {} })),
options.upload &&
uploader({
baseURL: `http://localhost:${options.upload.port}/`,
csrfToken: null,
}),
routerMiddleware(history),
].filter(Boolean),
),
);
return store;
}