Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
"outvariant": "^1.4.3",
"path-to-regexp": "^6.3.0",
"picocolors": "^1.1.1",
"strict-event-emitter": "^0.5.1",
"rettime": "^0.6.2",
"type-fest": "^4.26.1",
"yargs": "^17.7.2"
},
Expand Down
11 changes: 8 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions src/browser/setupWorker/glossary.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Emitter } from 'strict-event-emitter'
import { Emitter } from 'rettime'
import {
LifeCycleEventEmitter,
LifeCycleEventsMap,
Expand Down Expand Up @@ -95,7 +95,7 @@ export interface SetupWorkerInternalContext {
/**
* Adds a Service Worker event listener.
*/
on<EventType extends keyof ServiceWorkerIncomingEventsMap>(
on: <EventType extends keyof ServiceWorkerIncomingEventsMap>(
eventType: EventType,
callback: (
event: MessageEvent,
Expand All @@ -104,10 +104,10 @@ export interface SetupWorkerInternalContext {
ServiceWorkerIncomingEventsMap[EventType]
>,
) => void,
): void
send<EventType extends ServiceWorkerOutgoingEventTypes>(
) => void
send: <EventType extends ServiceWorkerOutgoingEventTypes>(
eventType: EventType,
): void
) => void
}
events: {
/**
Expand All @@ -122,13 +122,13 @@ export interface SetupWorkerInternalContext {
/**
* Removes all currently attached listeners.
*/
removeAllListeners(): void
removeAllListeners: () => void
/**
* Awaits a given message type from the Service Worker.
*/
once<EventType extends keyof ServiceWorkerIncomingEventsMap>(
once: <EventType extends keyof ServiceWorkerIncomingEventsMap>(
eventType: EventType,
): Promise<
) => Promise<
ServiceWorkerMessage<EventType, ServiceWorkerIncomingEventsMap[EventType]>
>
}
Expand Down Expand Up @@ -235,7 +235,7 @@ export interface SetupWorker {
*
* @see {@link https://mswjs.io/docs/api/setup-worker/list-handlers `worker.listHandlers()` API reference}
*/
listHandlers(): ReadonlyArray<RequestHandler | WebSocketHandler>
listHandlers: () => ReadonlyArray<RequestHandler | WebSocketHandler>

/**
* Life-cycle events.
Expand Down
40 changes: 21 additions & 19 deletions src/core/SetupApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { invariant } from 'outvariant'
import { EventMap, Emitter } from 'strict-event-emitter'
import { Emitter, type DefaultEventMap } from 'rettime'
import { RequestHandler } from './handlers/RequestHandler'
import { LifeCycleEventEmitter } from './sharedOptions'
import { devUtils } from './utils/internal/devUtils'
Expand All @@ -12,8 +12,10 @@ export abstract class HandlersController {
abstract prepend(
runtimeHandlers: Array<RequestHandler | WebSocketHandler>,
): void
abstract reset(nextHandles: Array<RequestHandler | WebSocketHandler>): void
abstract currentHandlers(): Array<RequestHandler | WebSocketHandler>
abstract reset: (
nextHandles: Array<RequestHandler | WebSocketHandler>,
) => void
abstract currentHandlers: () => Array<RequestHandler | WebSocketHandler>
}

export class InMemoryHandlersController implements HandlersController {
Expand Down Expand Up @@ -44,12 +46,14 @@ export class InMemoryHandlersController implements HandlersController {
/**
* Generic class for the mock API setup.
*/
export abstract class SetupApi<EventsMap extends EventMap> extends Disposable {
export abstract class SetupApi<
EventMap extends DefaultEventMap,
> extends Disposable {
protected handlersController: HandlersController
protected readonly emitter: Emitter<EventsMap>
protected readonly publicEmitter: Emitter<EventsMap>
protected readonly emitter: Emitter<EventMap>
protected readonly publicEmitter: Emitter<EventMap>

public readonly events: LifeCycleEventEmitter<EventsMap>
public readonly events: LifeCycleEventEmitter<EventMap>

constructor(...initialHandlers: Array<RequestHandler | WebSocketHandler>) {
super()
Expand All @@ -63,8 +67,8 @@ export abstract class SetupApi<EventsMap extends EventMap> extends Disposable {

this.handlersController = new InMemoryHandlersController(initialHandlers)

this.emitter = new Emitter<EventsMap>()
this.publicEmitter = new Emitter<EventsMap>()
this.emitter = new Emitter<EventMap>()
this.publicEmitter = new Emitter<EventMap>()
pipeEvents(this.emitter, this.publicEmitter)

this.events = this.createLifeCycleEvents()
Expand Down Expand Up @@ -111,17 +115,15 @@ export abstract class SetupApi<EventsMap extends EventMap> extends Disposable {
return toReadonlyArray(this.handlersController.currentHandlers())
}

private createLifeCycleEvents(): LifeCycleEventEmitter<EventsMap> {
private createLifeCycleEvents(): LifeCycleEventEmitter<EventMap> {
return {
on: (...args: any[]) => {
return (this.publicEmitter.on as any)(...args)
},
removeListener: (...args: any[]) => {
return (this.publicEmitter.removeListener as any)(...args)
},
removeAllListeners: (...args: any[]) => {
return this.publicEmitter.removeAllListeners(...args)
},
on: this.publicEmitter.on.bind(this.publicEmitter),
removeListener: this.publicEmitter.removeListener.bind(
this.publicEmitter,
),
removeAllListeners: this.publicEmitter.removeAllListeners.bind(
this.publicEmitter,
),
}
}
}
10 changes: 6 additions & 4 deletions src/core/handlers/WebSocketHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Emitter } from 'strict-event-emitter'
import { type DefaultEventMap, Emitter, TypedEvent } from 'rettime'
import { createRequestId } from '@mswjs/interceptors'
import type {
WebSocketClientConnectionProtocol,
Expand All @@ -18,8 +18,8 @@ type WebSocketHandlerParsedResult = {
match: Match
}

export type WebSocketHandlerEventMap = {
connection: [args: WebSocketHandlerConnection]
export interface WebSocketHandlerEventMap extends DefaultEventMap {
connection: TypedEvent<WebSocketHandlerConnection>
}

export interface WebSocketHandlerConnection {
Expand Down Expand Up @@ -136,7 +136,9 @@ export class WebSocketHandler {

// Emit the connection event on the handler.
// This is what the developer adds listeners for.
return this[kEmitter].emit('connection', connection)
return this[kEmitter].emit(
new TypedEvent('connection', { data: connection }),
)
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/core/sharedOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Emitter } from 'strict-event-emitter'
import type { Emitter, DefaultEventMap } from 'rettime'
import type { UnhandledRequestStrategy } from './utils/request/onUnhandledRequest'

export interface SharedOptions {
Expand All @@ -13,46 +13,46 @@
onUnhandledRequest?: UnhandledRequestStrategy
}

export type LifeCycleEventsMap = {
export interface LifeCycleEventsMap extends DefaultEventMap {
'request:start': [

Check failure on line 17 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / build (20)

Property ''request:start'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 17 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / publish

Property ''request:start'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 17 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / build (22)

Property ''request:start'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 17 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / exports

Property ''request:start'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 17 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / publish

Property ''request:start'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 17 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.1)

Property ''request:start'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 17 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.8)

Property ''request:start'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 17 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.2)

Property ''request:start'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 17 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.6)

Property ''request:start'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 17 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.0)

Property ''request:start'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 17 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.7)

Property ''request:start'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 17 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.5)

Property ''request:start'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.
args: {
request: Request
requestId: string
},
]
'request:match': [

Check failure on line 23 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / build (20)

Property ''request:match'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 23 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / publish

Property ''request:match'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 23 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / build (22)

Property ''request:match'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 23 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / exports

Property ''request:match'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 23 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / publish

Property ''request:match'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 23 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.1)

Property ''request:match'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 23 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.8)

Property ''request:match'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 23 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.2)

Property ''request:match'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 23 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.6)

Property ''request:match'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 23 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.0)

Property ''request:match'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 23 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.7)

Property ''request:match'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 23 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.5)

Property ''request:match'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.
args: {
request: Request
requestId: string
},
]
'request:unhandled': [

Check failure on line 29 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / build (20)

Property ''request:unhandled'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 29 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / publish

Property ''request:unhandled'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 29 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / build (22)

Property ''request:unhandled'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 29 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / exports

Property ''request:unhandled'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 29 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / publish

Property ''request:unhandled'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 29 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.1)

Property ''request:unhandled'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 29 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.8)

Property ''request:unhandled'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 29 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.2)

Property ''request:unhandled'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 29 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.6)

Property ''request:unhandled'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 29 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.0)

Property ''request:unhandled'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 29 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.7)

Property ''request:unhandled'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 29 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.5)

Property ''request:unhandled'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.
args: {
request: Request
requestId: string
},
]
'request:end': [

Check failure on line 35 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / build (20)

Property ''request:end'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 35 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / publish

Property ''request:end'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 35 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / build (22)

Property ''request:end'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 35 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / exports

Property ''request:end'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 35 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / publish

Property ''request:end'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 35 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.1)

Property ''request:end'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 35 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.8)

Property ''request:end'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 35 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.2)

Property ''request:end'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 35 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.6)

Property ''request:end'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 35 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.0)

Property ''request:end'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 35 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.7)

Property ''request:end'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 35 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.5)

Property ''request:end'' of type '[args: { request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.
args: {
request: Request
requestId: string
},
]
'response:mocked': [

Check failure on line 41 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / build (20)

Property ''response:mocked'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 41 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / publish

Property ''response:mocked'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 41 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / build (22)

Property ''response:mocked'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 41 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / exports

Property ''response:mocked'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 41 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / publish

Property ''response:mocked'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 41 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.1)

Property ''response:mocked'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 41 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.8)

Property ''response:mocked'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 41 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.2)

Property ''response:mocked'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 41 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.6)

Property ''response:mocked'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 41 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.0)

Property ''response:mocked'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 41 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.7)

Property ''response:mocked'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 41 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.5)

Property ''response:mocked'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.
args: {
response: Response
request: Request
requestId: string
},
]
'response:bypass': [

Check failure on line 48 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / build (20)

Property ''response:bypass'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 48 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / publish

Property ''response:bypass'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 48 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / build (22)

Property ''response:bypass'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 48 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / exports

Property ''response:bypass'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 48 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / publish

Property ''response:bypass'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 48 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.1)

Property ''response:bypass'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 48 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.8)

Property ''response:bypass'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 48 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.2)

Property ''response:bypass'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 48 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.6)

Property ''response:bypass'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 48 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.0)

Property ''response:bypass'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 48 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.7)

Property ''response:bypass'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 48 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.5)

Property ''response:bypass'' of type '[args: { response: Response; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.
args: {
response: Response
request: Request
requestId: string
},
]
unhandledException: [

Check failure on line 55 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / build (20)

Property 'unhandledException' of type '[args: { error: Error; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 55 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / publish

Property 'unhandledException' of type '[args: { error: Error; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 55 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / build (22)

Property 'unhandledException' of type '[args: { error: Error; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 55 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / exports

Property 'unhandledException' of type '[args: { error: Error; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 55 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / publish

Property 'unhandledException' of type '[args: { error: Error; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 55 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.1)

Property 'unhandledException' of type '[args: { error: Error; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 55 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.8)

Property 'unhandledException' of type '[args: { error: Error; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 55 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.2)

Property 'unhandledException' of type '[args: { error: Error; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 55 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.6)

Property 'unhandledException' of type '[args: { error: Error; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 55 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.0)

Property 'unhandledException' of type '[args: { error: Error; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 55 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.7)

Property 'unhandledException' of type '[args: { error: Error; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.

Check failure on line 55 in src/core/sharedOptions.ts

View workflow job for this annotation

GitHub Actions / typescript (5.5)

Property 'unhandledException' of type '[args: { error: Error; request: Request; requestId: string; }]' is not assignable to 'string' index type 'TypedEvent<any, any, string>'.
args: {
error: Error
request: Request
Expand All @@ -61,6 +61,7 @@
]
}

export type LifeCycleEventEmitter<
EventsMap extends Record<string | symbol, any>,
> = Pick<Emitter<EventsMap>, 'on' | 'removeListener' | 'removeAllListeners'>
export type LifeCycleEventEmitter<EventMap extends DefaultEventMap> = Pick<
Emitter<EventMap>,
'on' | 'removeListener' | 'removeAllListeners'
>
2 changes: 1 addition & 1 deletion src/core/utils/handleRequest.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @vitest-environment jsdom
import { Emitter } from 'strict-event-emitter'
import { Emitter } from 'rettime'
import { createRequestId } from '@mswjs/interceptors'
import { LifeCycleEventsMap, SharedOptions } from '../sharedOptions'
import { RequestHandler } from '../handlers/RequestHandler'
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils/handleRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { until } from '@open-draft/until'
import { Emitter } from 'strict-event-emitter'
import { Emitter } from 'rettime'
import { LifeCycleEventsMap, SharedOptions } from '../sharedOptions'
import { RequiredDeep } from '../typeUtils'
import type { RequestHandler } from '../handlers/RequestHandler'
Expand Down Expand Up @@ -44,11 +44,11 @@
emitter: Emitter<LifeCycleEventsMap>,
handleRequestOptions?: HandleRequestOptions,
): Promise<Response | undefined> {
emitter.emit('request:start', { request, requestId })

Check failure on line 47 in src/core/utils/handleRequest.ts

View workflow job for this annotation

GitHub Actions / typescript (5.3)

Expected 1 arguments, but got 2.

// Perform requests wrapped in "bypass()" as-is.
if (request.headers.get('accept')?.includes('msw/passthrough')) {
emitter.emit('request:end', { request, requestId })

Check failure on line 51 in src/core/utils/handleRequest.ts

View workflow job for this annotation

GitHub Actions / typescript (5.3)

Expected 1 arguments, but got 2.
handleRequestOptions?.onPassthroughResponse?.(request)
return
}
Expand All @@ -65,7 +65,7 @@

if (lookupResult.error) {
// Allow developers to react to unhandled exceptions in request handlers.
emitter.emit('unhandledException', {

Check failure on line 68 in src/core/utils/handleRequest.ts

View workflow job for this annotation

GitHub Actions / typescript (5.3)

Expected 1 arguments, but got 2.
error: lookupResult.error,
request,
requestId,
Expand All @@ -77,8 +77,8 @@
// matching this request. Report the request as unhandled.
if (!lookupResult.data) {
await onUnhandledRequest(request, options.onUnhandledRequest)
emitter.emit('request:unhandled', { request, requestId })

Check failure on line 80 in src/core/utils/handleRequest.ts

View workflow job for this annotation

GitHub Actions / typescript (5.3)

Expected 1 arguments, but got 2.
emitter.emit('request:end', { request, requestId })

Check failure on line 81 in src/core/utils/handleRequest.ts

View workflow job for this annotation

GitHub Actions / typescript (5.3)

Expected 1 arguments, but got 2.
handleRequestOptions?.onPassthroughResponse?.(request)
return
}
Expand All @@ -88,7 +88,7 @@
// When the handled request returned no mocked response, warn the developer,
// as it may be an oversight on their part. Perform the request as-is.
if (!response) {
emitter.emit('request:end', { request, requestId })

Check failure on line 91 in src/core/utils/handleRequest.ts

View workflow job for this annotation

GitHub Actions / typescript (5.3)

Expected 1 arguments, but got 2.
handleRequestOptions?.onPassthroughResponse?.(request)
return
}
Expand All @@ -99,7 +99,7 @@
response.status === 302 &&
response.headers.get('x-msw-intention') === 'passthrough'
) {
emitter.emit('request:end', { request, requestId })

Check failure on line 102 in src/core/utils/handleRequest.ts

View workflow job for this annotation

GitHub Actions / typescript (5.3)

Expected 1 arguments, but got 2.
handleRequestOptions?.onPassthroughResponse?.(request)
return
}
Expand All @@ -107,7 +107,7 @@
// Store all the received response cookies in the cookie jar.
storeResponseCookies(request, response)

emitter.emit('request:match', { request, requestId })

Check failure on line 110 in src/core/utils/handleRequest.ts

View workflow job for this annotation

GitHub Actions / typescript (5.3)

Expected 1 arguments, but got 2.

const requiredLookupResult =
lookupResult.data as RequiredDeep<HandlersExecutionResult>
Expand Down
7 changes: 4 additions & 3 deletions src/core/utils/internal/pipeEvents.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Emitter } from 'strict-event-emitter'
import { Emitter, TypedEvent } from 'rettime'
import { pipeEvents } from './pipeEvents'

it('pipes events from the source emitter to the destination emitter', () => {
Expand All @@ -9,6 +9,7 @@ it('pipes events from the source emitter to the destination emitter', () => {
const callback = vi.fn()
destination.on('hello', callback)

source.emit('hello', 'world', { data: true })
expect(callback).toHaveBeenNthCalledWith(1, 'world', { data: true })
const event = new TypedEvent('hello', { data: 'world' })
source.emit(event)
expect(callback).toHaveBeenNthCalledWith(1, event)
})
14 changes: 7 additions & 7 deletions src/core/utils/internal/pipeEvents.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Emitter, EventMap } from 'strict-event-emitter'
import type { Emitter, DefaultEventMap } from 'rettime'

/**
* Pipes all emitted events from one emitter to another.
*/
export function pipeEvents<Events extends EventMap>(
source: Emitter<Events>,
destination: Emitter<Events>,
export function pipeEvents<EventMap extends DefaultEventMap>(
source: Emitter<EventMap>,
destination: Emitter<EventMap>,
): void {
const rawEmit: typeof source.emit & { _isPiped?: boolean } = source.emit

Expand All @@ -14,9 +14,9 @@ export function pipeEvents<Events extends EventMap>(
}

const sourceEmit: typeof source.emit & { _isPiped?: boolean } =
function sourceEmit(this: typeof source, event, ...data) {
destination.emit(event, ...data)
return rawEmit.call(this, event, ...data)
function sourceEmit(this: typeof source, event) {
destination.emit(event)
return rawEmit.call(this, event)
}

sourceEmit._isPiped = true
Expand Down
4 changes: 2 additions & 2 deletions src/core/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

export type WebSocketEventListener<
EventType extends keyof WebSocketHandlerEventMap,
> = (...args: WebSocketHandlerEventMap[EventType]) => void

Check failure on line 30 in src/core/ws.ts

View workflow job for this annotation

GitHub Actions / typescript (5.4)

A rest parameter must be of an array type.

export type WebSocketLink = {
/**
Expand Down Expand Up @@ -116,15 +116,15 @@
// handler matches and emits a connection event.
// When that happens, store that connection in the
// set of all connections for reference.
handler[kEmitter].on('connection', async ({ client }) => {
await clientManager.addConnection(client)
handler[kEmitter].on('connection', async (event) => {
await clientManager.addConnection(event.data.client)
})

// The "handleWebSocketEvent" function will invoke
// the "run()" method on the WebSocketHandler.
// If the handler matches, it will emit the "connection"
// event. Attach the user-defined listener to that event.
handler[kEmitter].on(event, listener)

Check failure on line 127 in src/core/ws.ts

View workflow job for this annotation

GitHub Actions / typescript (5.4)

Argument of type 'keyof WebSocketHandlerEventMap' is not assignable to parameter of type 'string'.

return handler
},
Expand Down
8 changes: 4 additions & 4 deletions test/browser/setup/workerConsole.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { format } from 'outvariant'
import { Emitter } from 'strict-event-emitter'
import { type DefaultEventMap, Emitter, TypedEvent } from 'rettime'
import { type Page } from '@playwright/test'

type WorkerConsoleMessageType =
Expand Down Expand Up @@ -33,8 +33,8 @@ type WorkerConsoleMessageType =
| 'context'
| 'memory'

type WorkerConsoleEventMap = {
[MessageType in WorkerConsoleMessageType]: [message: string]
type WorkerConsoleEventMap = DefaultEventMap & {
[MessageType in WorkerConsoleMessageType]: TypedEvent<string>
}

type InternalWorkerConsoleMessageData = {
Expand Down Expand Up @@ -85,7 +85,7 @@ export class WorkerConsole extends Emitter<WorkerConsoleEventMap> {
const formattedMessage = format(template, ...positionals)

this.addMessage(messageType, formattedMessage)
this.emit(messageType, formattedMessage)
this.emit(new TypedEvent(messageType, { data: formattedMessage }))
},
)

Expand Down
9 changes: 4 additions & 5 deletions test/support/WebSocketServer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { invariant } from 'outvariant'
import { Emitter } from 'strict-event-emitter'
import { type DefaultEventMap, Emitter, TypedEvent } from 'rettime'
import fastify, { FastifyInstance } from 'fastify'
import fastifyWebSocket, { SocketStream } from '@fastify/websocket'

type FastifySocket = SocketStream['socket']

type WebSocketEventMap = {
connection: [client: FastifySocket]
interface WebSocketEventMap extends DefaultEventMap {
connection: TypedEvent<FastifySocket>
}

export class WebSocketServer extends Emitter<WebSocketEventMap> {
Expand All @@ -24,8 +24,7 @@ export class WebSocketServer extends Emitter<WebSocketEventMap> {
fastify.get('/', { websocket: true }, ({ socket }) => {
this.clients.add(socket)
socket.once('close', () => this.clients.delete(socket))

this.emit('connection', socket)
this.emit(new TypedEvent('connection', { data: socket }))
})
})
}
Expand Down
Loading