-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathslice_creator.ts
145 lines (137 loc) · 4.62 KB
/
slice_creator.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import type {
CaseReducerDefinition,
CreatorCaseReducers,
PayloadAction,
ReducerCreator,
ReducerCreatorEntry,
} from '@reduxjs/toolkit'
import { reducerCreator } from '../createSlice'
import type { WithRequiredProp } from '../tsHelpers'
import type {
EntityAdapter,
EntityId,
EntityState,
DefaultPlural,
EntityReducers,
} from './models'
import { capitalize } from './utils'
export const entityMethodsCreatorType = /*@__PURE__*/ Symbol()
export interface EntityMethodsCreatorConfig<
T,
Id extends EntityId,
State,
Single extends string,
Plural extends string,
> {
selectEntityState?: (state: State) => EntityState<T, Id>
name?: Single
pluralName?: Plural
}
type EntityMethodsCreator<State> =
State extends EntityState<infer T, infer Id>
? {
<
T,
Id extends EntityId,
Single extends string = '',
Plural extends string = DefaultPlural<Single>,
>(
adapter: EntityAdapter<T, Id>,
config: WithRequiredProp<
EntityMethodsCreatorConfig<T, Id, State, Single, Plural>,
'selectEntityState'
>,
): EntityReducers<T, Id, State, Single, Plural>
<
Single extends string = '',
Plural extends string = DefaultPlural<Single>,
>(
adapter: EntityAdapter<T, Id>,
config?: Omit<
EntityMethodsCreatorConfig<T, Id, State, Single, Plural>,
'selectEntityState'
>,
): EntityReducers<T, Id, State, Single, Plural>
}
: <
T,
Id extends EntityId,
Single extends string = '',
Plural extends string = DefaultPlural<Single>,
>(
adapter: EntityAdapter<T, Id>,
config: WithRequiredProp<
EntityMethodsCreatorConfig<T, Id, State, Single, Plural>,
'selectEntityState'
>,
) => EntityReducers<T, Id, State, Single, Plural>
declare module '@reduxjs/toolkit' {
export interface SliceReducerCreators<
State,
CaseReducers extends CreatorCaseReducers<State>,
Name extends string,
ReducerPath extends string,
> {
[entityMethodsCreatorType]: ReducerCreatorEntry<EntityMethodsCreator<State>>
}
}
const makeWrappedReducerCreator =
<T, Id extends EntityId, State>(
selectEntityState: (state: State) => EntityState<T, Id>,
) =>
<Payload>(
mutator: (
state: EntityState<T, Id>,
action: PayloadAction<Payload>,
) => void,
): CaseReducerDefinition<State, PayloadAction<Payload>> =>
reducerCreator.create<Payload>((state: State, action) => {
mutator(selectEntityState(state), action)
})
export function createEntityMethods<
T,
Id extends EntityId,
State = EntityState<T, Id>,
Single extends string = '',
Plural extends string = DefaultPlural<Single>,
>(
adapter: EntityAdapter<T, Id>,
{
selectEntityState = (state) => state as unknown as EntityState<T, Id>,
name: nameParam = '' as Single,
pluralName: pluralParam = (nameParam && `${nameParam}s`) as Plural,
}: EntityMethodsCreatorConfig<T, Id, State, Single, Plural> = {},
): EntityReducers<T, Id, State, Single, Plural> {
// template literal computed keys don't keep their type if there's an unresolved generic
// so we cast to some intermediate type to at least check we're using the right variables in the right places
const name = nameParam as 's'
const pluralName = pluralParam as 'p'
const reducer = makeWrappedReducerCreator(selectEntityState)
const reducers: EntityReducers<T, Id, State, 's', 'p'> = {
[`addOne${capitalize(name)}` as const]: reducer(adapter.addOne),
[`addMany${capitalize(pluralName)}` as const]: reducer(adapter.addMany),
[`setOne${capitalize(name)}` as const]: reducer(adapter.setOne),
[`setMany${capitalize(pluralName)}` as const]: reducer(adapter.setMany),
[`setAll${capitalize(pluralName)}` as const]: reducer(adapter.setAll),
[`removeOne${capitalize(name)}` as const]: reducer(adapter.removeOne),
[`removeMany${capitalize(pluralName)}` as const]: reducer(
adapter.removeMany,
),
[`removeAll${capitalize(pluralName)}` as const]: reducer(adapter.removeAll),
[`upsertOne${capitalize(name)}` as const]: reducer(adapter.upsertOne),
[`upsertMany${capitalize(pluralName)}` as const]: reducer(
adapter.upsertMany,
),
[`updateOne${capitalize(name)}` as const]: reducer(adapter.updateOne),
[`updateMany${capitalize(pluralName)}` as const]: reducer(
adapter.updateMany,
),
}
return reducers as any
}
export const entityMethodsCreator: ReducerCreator<
typeof entityMethodsCreatorType
> = {
type: entityMethodsCreatorType,
create: createEntityMethods,
}