-
-
Notifications
You must be signed in to change notification settings - Fork 469
/
glossary.ts
265 lines (237 loc) · 7.21 KB
/
glossary.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { FlatHeadersObject } from 'headers-polyfill'
import { StrictEventEmitter } from 'strict-event-emitter'
import {
LifeCycleEventEmitter,
LifeCycleEventsMap,
SharedOptions,
} from '../sharedOptions'
import { ServiceWorkerMessage } from './start/utils/createMessageChannel'
import {
DefaultBodyType,
RequestHandler,
RequestHandlerDefaultInfo,
} from '../handlers/RequestHandler'
import type { HttpRequestEventMap, Interceptor } from '@mswjs/interceptors'
import { Path } from '../utils/matching/matchRequestUrl'
import { RequiredDeep } from '../typeUtils'
import { MockedRequest } from '../utils/request/MockedRequest'
export type ResolvedPath = Path | URL
type RequestWithoutMethods = Omit<
Request,
| 'text'
| 'body'
| 'json'
| 'blob'
| 'arrayBuffer'
| 'formData'
| 'clone'
| 'signal'
| 'isHistoryNavigation'
| 'isReloadNavigation'
>
/**
* Request representation received from the worker message event.
*/
export interface ServiceWorkerIncomingRequest extends RequestWithoutMethods {
/**
* Unique UUID of the request generated once the request is
* captured by the "fetch" event in the Service Worker.
*/
id: string
/**
* Text response body.
*/
body?: string
}
export type ServiceWorkerIncomingResponse = Pick<
Response,
'type' | 'ok' | 'status' | 'statusText' | 'body' | 'headers' | 'redirected'
> & {
requestId: string
}
/**
* Map of the events that can be received from the Service Worker.
*/
export interface ServiceWorkerIncomingEventsMap {
MOCKING_ENABLED: boolean
INTEGRITY_CHECK_RESPONSE: string
KEEPALIVE_RESPONSE: never
REQUEST: ServiceWorkerIncomingRequest
RESPONSE: ServiceWorkerIncomingResponse
}
/**
* Map of the events that can be sent to the Service Worker
* from any execution context.
*/
export type ServiceWorkerOutgoingEventTypes =
| 'MOCK_ACTIVATE'
| 'MOCK_DEACTIVATE'
| 'INTEGRITY_CHECK_REQUEST'
| 'KEEPALIVE_REQUEST'
| 'CLIENT_CLOSED'
/**
* Map of the events that can be sent to the Service Worker
* only as a part of a single `fetch` event handler.
*/
export interface ServiceWorkerFetchEventMap {
MOCK_RESPONSE(payload: SerializedResponse): void
MOCK_RESPONSE_START(payload: SerializedResponse): void
MOCK_NOT_FOUND(): void
NETWORK_ERROR(payload: { name: string; message: string }): void
INTERNAL_ERROR(payload: { status: number; body: string }): void
}
export interface ServiceWorkerBroadcastChannelMessageMap {
MOCK_RESPONSE_CHUNK(payload: Uint8Array): void
MOCK_RESPONSE_END(): void
}
export type WorkerLifecycleEventsMap = LifeCycleEventsMap<Response>
export interface SetupWorkerInternalContext {
isMockingEnabled: boolean
startOptions: RequiredDeep<StartOptions>
worker: ServiceWorker | null
registration: ServiceWorkerRegistration | null
requestHandlers: RequestHandler[]
emitter: StrictEventEmitter<WorkerLifecycleEventsMap>
keepAliveInterval?: number
workerChannel: {
/**
* Adds a Service Worker event listener.
*/
on<EventType extends keyof ServiceWorkerIncomingEventsMap>(
eventType: EventType,
callback: (
event: MessageEvent,
message: ServiceWorkerMessage<
EventType,
ServiceWorkerIncomingEventsMap[EventType]
>,
) => void,
): void
send<EventType extends ServiceWorkerOutgoingEventTypes>(
eventType: EventType,
): void
}
events: {
/**
* Adds an event listener on the given target.
* Returns a clean-up function that removes that listener.
*/
addListener<EventType extends Event = Event>(
target: EventTarget,
eventType: string,
listener: (event: EventType) => void,
): () => void
/**
* Removes all currently attached listeners.
*/
removeAllListeners(): void
/**
* Awaits a given message type from the Service Worker.
*/
once<EventType extends keyof ServiceWorkerIncomingEventsMap>(
eventType: EventType,
): Promise<
ServiceWorkerMessage<EventType, ServiceWorkerIncomingEventsMap[EventType]>
>
}
useFallbackMode: boolean
fallbackInterceptor?: Interceptor<HttpRequestEventMap>
}
export type ServiceWorkerInstanceTuple = [
ServiceWorker | null,
ServiceWorkerRegistration,
]
export type FindWorker = (
scriptUrl: string,
mockServiceWorkerUrl: string,
) => boolean
export interface StartOptions extends SharedOptions {
/**
* Service Worker registration options.
*/
serviceWorker?: {
/**
* Custom url to the worker script.
* @default "/mockServiceWorker.js"
*/
url?: string
options?: RegistrationOptions
}
/**
* Disables the logging of captured requests
* into browser's console.
* @default false
*/
quiet?: boolean
/**
* Defers any network requests until the Service Worker
* instance is activated.
* @default true
*/
waitUntilReady?: boolean
/**
* A custom lookup function to find a Mock Service Worker in the list
* of all registered Service Workers on the page.
*/
findWorker?: FindWorker
}
export interface SerializedResponse<BodyType extends DefaultBodyType = any> {
status: number
statusText: string
headers: FlatHeadersObject
body: BodyType
delay?: number
}
export type StartReturnType = Promise<ServiceWorkerRegistration | undefined>
export type StartHandler = (
options: RequiredDeep<StartOptions>,
initialOptions: StartOptions,
) => StartReturnType
export type StopHandler = () => void
export interface SetupWorkerApi {
/**
* Registers and activates the mock Service Worker.
* @see {@link https://mswjs.io/docs/api/setup-worker/start `worker.start()`}
*/
start: (options?: StartOptions) => StartReturnType
/**
* Stops requests interception for the current client.
* @see {@link https://mswjs.io/docs/api/setup-worker/stop `worker.stop()`}
*/
stop: StopHandler
/**
* Prepends given request handlers to the list of existing handlers.
* @param {RequestHandler[]} handlers List of runtime request handlers.
* @see {@link https://mswjs.io/docs/api/setup-worker/use `worker.use()`}
*/
use: (...handlers: RequestHandler[]) => void
/**
* Marks all request handlers that respond using `res.once()` as unused.
* @see {@link https://mswjs.io/docs/api/setup-worker/restore-handlers `worker.restoreHandlers()`}
*/
restoreHandlers: () => void
/**
* Resets request handlers to the initial list given to the `setupWorker` call, or to the explicit next request handlers list, if given.
* @param {RequestHandler[]} nextHandlers List of the new initial request handlers.
* @see {@link https://mswjs.io/docs/api/setup-worker/reset-handlers `worker.resetHandlers()`}
*/
resetHandlers: (...nextHandlers: RequestHandler[]) => void
/**
* Returns a readonly list of currently active request handlers.
* @see {@link https://mswjs.io/docs/api/setup-worker/list-handlers `worker.listHandlers()`}
*/
listHandlers(): ReadonlyArray<
RequestHandler<
RequestHandlerDefaultInfo,
MockedRequest<DefaultBodyType>,
any,
MockedRequest<DefaultBodyType>
>
>
/**
* Lists all active request handlers.
* @see {@link https://mswjs.io/docs/api/setup-worker/print-handlers `worker.printHandlers()`}
*/
printHandlers: () => void
events: LifeCycleEventEmitter<WorkerLifecycleEventsMap>
}