-
-
Notifications
You must be signed in to change notification settings - Fork 620
Expand file tree
/
Copy pathhandlers-controller.ts
More file actions
159 lines (127 loc) · 4.38 KB
/
Copy pathhandlers-controller.ts
File metadata and controls
159 lines (127 loc) · 4.38 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { invariant } from 'outvariant'
import { type RequestHandler } from '../handlers/RequestHandler'
import { type WebSocketHandler } from '../handlers/WebSocketHandler'
import { devUtils } from '../utils/internal/devUtils'
export type AnyHandler = RequestHandler | WebSocketHandler
export type HandlersMap = Partial<Record<AnyHandler['kind'], Array<AnyHandler>>>
export function groupHandlersByKind(handlers: Array<AnyHandler>): HandlersMap {
const groups: HandlersMap = {}
/**
* @note `Object.groupBy` is not implemented in Node.js v20.
*/
for (const handler of handlers) {
;(groups[handler.kind] ||= []).push(handler)
}
return groups
}
export interface HandlersControllerState {
initialHandlers: HandlersMap
handlers: HandlersMap
}
export abstract class HandlersController {
protected getInitialState(
initialHandlers: Array<AnyHandler>,
): HandlersControllerState {
invariant(
this.#validateHandlers(initialHandlers),
devUtils.formatMessage(
'Failed to apply given request handlers: invalid input. Did you forget to spread the request handlers Array?',
),
)
const normalizedInitialHandlers = groupHandlersByKind(initialHandlers)
return {
initialHandlers: normalizedInitialHandlers,
handlers: { ...normalizedInitialHandlers },
}
}
protected abstract getState(): HandlersControllerState
protected abstract setState(nextState: Partial<HandlersControllerState>): void
public currentHandlers(): Array<AnyHandler> {
return Object.values(this.getState().handlers)
.flat()
.filter((handler) => handler != null)
}
public getHandlersByKind(kind: AnyHandler['kind']): Array<AnyHandler> {
return this.getState().handlers[kind] || []
}
public use(nextHandlers: Array<AnyHandler>): void {
invariant(
this.#validateHandlers(nextHandlers),
devUtils.formatMessage(
'[MSW] Failed to call "use()" with the given request handlers: invalid input. Did you forget to spread the array of request handlers?',
),
)
if (nextHandlers.length === 0) {
return
}
const { handlers } = this.getState()
// Iterate over next handlers and prepend them to their respective lists.
// Iterate in a reverse order to the keep the order of the runtime handlers as provided.
for (let i = nextHandlers.length - 1; i >= 0; i--) {
const handler = nextHandlers[i]
handlers[handler.kind] = handlers[handler.kind]
? [handler, ...handlers[handler.kind]!]
: [handler]
}
this.setState({ handlers })
}
public reset(nextHandlers: Array<AnyHandler>): void {
invariant(
nextHandlers.length > 0 ? this.#validateHandlers(nextHandlers) : true,
devUtils.formatMessage(
'Failed to replace initial handlers during reset: invalid handlers. Did you forget to spread the handlers array?',
),
)
for (const handler of this.currentHandlers()) {
if ('reset' in handler) {
handler['reset']()
}
}
const { initialHandlers } = this.getState()
if (nextHandlers.length === 0) {
this.setState({
handlers: { ...initialHandlers },
})
return
}
const normalizedNextHandlers = groupHandlersByKind(nextHandlers)
this.setState({
initialHandlers: normalizedNextHandlers,
handlers: { ...normalizedNextHandlers },
})
}
public restore(): void {
for (const handler of this.currentHandlers()) {
if ('restore' in handler) {
handler['restore']()
}
}
}
#validateHandlers(handlers: Array<AnyHandler>): boolean {
return handlers.every((handler) => !Array.isArray(handler))
}
}
export class InMemoryHandlersController extends HandlersController {
#handlers: HandlersMap
#initialHandlers: HandlersMap
constructor(initialHandlers: Array<AnyHandler>) {
super()
const initialState = this.getInitialState(initialHandlers)
this.#initialHandlers = initialState.initialHandlers
this.#handlers = initialState.handlers
}
protected getState(): HandlersControllerState {
return {
initialHandlers: this.#initialHandlers,
handlers: this.#handlers,
}
}
protected setState(nextState: Partial<HandlersControllerState>): void {
if (nextState.initialHandlers) {
this.#initialHandlers = nextState.initialHandlers
}
if (nextState.handlers) {
this.#handlers = nextState.handlers
}
}
}