-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
56 lines (43 loc) · 1.88 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
import { PetrusError, config, storageDrivers } from 'config';
import { configure as oAuth } from 'modules/oAuth';
import { configure as authSession } from 'modules/auth-session';
import { configure as tokens, TokensPersistence } from 'modules/tokens';
import { createRootReducer } from 'services/reducers';
import rootSaga from 'services/sagas';
import type { PetrusCredentials, PetrusCustomConfig, PetrusUser } from 'types';
export function configure<
User extends PetrusUser = PetrusUser,
Credentials extends PetrusCredentials = PetrusCredentials,
AppState extends Record<string, any> = Record<string, any>,
>(customConfig: PetrusCustomConfig<User, Credentials>) {
if (config.initialized) {
throw new PetrusError(`'configure' method can be called only once.`);
}
const oAuthConfig = oAuth(customConfig.oAuth);
const { rootReducer, entitiesInitState } = createRootReducer<User>(customConfig.initialState);
Object.assign(config, {
initialized: true,
logger: customConfig.logger || console,
selector: (state: AppState) => state.auth,
oAuth: oAuthConfig,
tokens: tokens.options(customConfig.tokens),
handlers: {
...authSession.handlers(customConfig.handlers, {
oAuthEnabled: oAuthConfig.enabled,
tokensPersistence: entitiesInitState.tokensPersistence,
}),
...tokens.handlers(customConfig.handlers),
},
mapStorageDriverToTokensPersistence: {
[TokensPersistence.SESSION]: storageDrivers.sessionStorage,
[TokensPersistence.LOCAL]: storageDrivers.indexedDB,
[TokensPersistence.NONE]: storageDrivers.resetStorage,
...customConfig.mapStorageDriverToTokensPersistence,
},
});
Object.freeze(config);
return {
reducer: rootReducer,
saga: rootSaga,
} as const;
}