/
SetupApi.ts
97 lines (83 loc) · 2.99 KB
/
SetupApi.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
import { invariant } from 'outvariant'
import { EventMapType, StrictEventEmitter } from 'strict-event-emitter'
import {
DefaultBodyType,
RequestHandler,
RequestHandlerDefaultInfo,
} from './handlers/RequestHandler'
import { LifeCycleEventEmitter } from './sharedOptions'
import { devUtils } from './utils/internal/devUtils'
import { pipeEvents } from './utils/internal/pipeEvents'
import { toReadonlyArray } from './utils/internal/toReadonlyArray'
import { MockedRequest } from './utils/request/MockedRequest'
/**
* Generic class for the mock API setup.
*/
export abstract class SetupApi<EventsMap extends EventMapType> {
protected initialHandlers: ReadonlyArray<RequestHandler>
protected currentHandlers: Array<RequestHandler>
protected readonly emitter: StrictEventEmitter<EventsMap>
protected readonly publicEmitter: StrictEventEmitter<EventsMap>
public readonly events: LifeCycleEventEmitter<EventsMap>
constructor(initialHandlers: Array<RequestHandler>) {
this.validateHandlers(initialHandlers)
this.initialHandlers = toReadonlyArray(initialHandlers)
this.currentHandlers = [...initialHandlers]
this.emitter = new StrictEventEmitter<EventsMap>()
this.publicEmitter = new StrictEventEmitter<EventsMap>()
pipeEvents(this.emitter, this.publicEmitter)
this.events = this.createLifeCycleEvents()
}
private validateHandlers(handlers: ReadonlyArray<RequestHandler>): void {
// Guard against incorrect call signature of the setup API.
for (const handler of handlers) {
invariant(
!Array.isArray(handler),
devUtils.formatMessage(
'Failed to construct "%s" given an Array of request handlers. Make sure you spread the request handlers when calling the respective setup function.',
),
this.constructor.name,
)
}
}
protected dispose(): void {
this.emitter.removeAllListeners()
this.publicEmitter.removeAllListeners()
}
public use(...runtimeHandlers: Array<RequestHandler>): void {
this.currentHandlers.unshift(...runtimeHandlers)
}
public restoreHandlers(): void {
this.currentHandlers.forEach((handler) => {
handler.markAsSkipped(false)
})
}
public resetHandlers(...nextHandlers: Array<RequestHandler>): void {
this.currentHandlers =
nextHandlers.length > 0 ? [...nextHandlers] : [...this.initialHandlers]
}
public listHandlers(): ReadonlyArray<
RequestHandler<
RequestHandlerDefaultInfo,
MockedRequest<DefaultBodyType>,
any,
MockedRequest<DefaultBodyType>
>
> {
return toReadonlyArray(this.currentHandlers)
}
private createLifeCycleEvents(): LifeCycleEventEmitter<EventsMap> {
return {
on: (...args) => {
return this.publicEmitter.on(...args)
},
removeListener: (...args) => {
return this.publicEmitter.removeListener(...args)
},
removeAllListeners: (...args: any) => {
return this.publicEmitter.removeAllListeners(...args)
},
}
}
abstract printHandlers(): void
}