Skip to content

Commit

Permalink
chore: refactor to isHandlerKind
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Nov 6, 2024
1 parent 09ef3ef commit b6c5375
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'
import { SetupWorkerInternalContext, StartOptions } from '../glossary'
import type { RequiredDeep } from '~/core/typeUtils'
import { handleRequest } from '~/core/utils/handleRequest'
import { toRequestHandlersOnly } from '~/core/utils/internal/toRequestHandlersOnly'
import { isHandlerKind } from '~/core/utils/internal/isHandlerKind'

export function createFallbackRequestListener(
context: SetupWorkerInternalContext,
Expand All @@ -25,7 +25,7 @@ export function createFallbackRequestListener(
const response = await handleRequest(
request,
requestId,
context.getRequestHandlers().filter(toRequestHandlersOnly),
context.getRequestHandlers().filter(isHandlerKind('RequestHandler')),
options,
context.emitter,
{
Expand Down
4 changes: 2 additions & 2 deletions src/browser/setupWorker/start/createRequestListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { handleRequest } from '~/core/utils/handleRequest'
import { RequiredDeep } from '~/core/typeUtils'
import { devUtils } from '~/core/utils/internal/devUtils'
import { toResponseInit } from '~/core/utils/toResponseInit'
import { toRequestHandlersOnly } from '~/core/utils/internal/toRequestHandlersOnly'
import { isHandlerKind } from '~/core/utils/internal/isHandlerKind'

export const createRequestListener = (
context: SetupWorkerInternalContext,
Expand Down Expand Up @@ -44,7 +44,7 @@ export const createRequestListener = (
await handleRequest(
request,
requestId,
context.getRequestHandlers().filter(toRequestHandlersOnly),
context.getRequestHandlers().filter(isHandlerKind('RequestHandler')),
options,
context.emitter,
{
Expand Down
3 changes: 2 additions & 1 deletion src/core/handlers/RequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import type { ResponseResolutionContext } from '../utils/executeHandlers'
import type { MaybePromise } from '../typeUtils'
import { StrictRequest, StrictResponse } from '..//HttpResponse'
import type { HandlerKind } from './common'

export type DefaultRequestMultipartBody = Record<
string,
Expand Down Expand Up @@ -117,7 +118,7 @@ export abstract class RequestHandler<
StrictRequest<DefaultBodyType>
>()

private readonly __kind: 'RequestHandler'
private readonly __kind: HandlerKind

public info: HandlerInfo & RequestHandlerInternalInfo
/**
Expand Down
5 changes: 3 additions & 2 deletions src/core/handlers/WebSocketHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
matchRequestUrl,
} from '../utils/matching/matchRequestUrl'
import { getCallFrame } from '../utils/internal/getCallFrame'
import type { HandlerKind } from './common'

type WebSocketHandlerParsedResult = {
match: Match
Expand All @@ -28,7 +29,7 @@ const kStopPropagationPatched = Symbol('kStopPropagationPatched')
const KOnStopPropagation = Symbol('KOnStopPropagation')

export class WebSocketHandler {
private readonly __kind: 'WebSocketHandler'
private readonly __kind: HandlerKind

public id: string
public callFrame?: string
Expand All @@ -40,7 +41,7 @@ export class WebSocketHandler {

this[kEmitter] = new Emitter()
this.callFrame = getCallFrame(new Error())
this.__kind = 'WebSocketHandler'
this.__kind = 'EventHandler'
}

public parse(args: {
Expand Down
1 change: 1 addition & 0 deletions src/core/handlers/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type HandlerKind = 'RequestHandler' | 'EventHandler'
64 changes: 64 additions & 0 deletions src/core/utils/internal/isHandlerKind.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { GraphQLHandler } from '../../handlers/GraphQLHandler'
import { HttpHandler } from '../../handlers/HttpHandler'
import { RequestHandler } from '../../handlers/RequestHandler'
import { WebSocketHandler } from '../../handlers/WebSocketHandler'
import { isHandlerKind } from './isHandlerKind'

it('returns true if expected a request handler and given a request handler', () => {
expect(
isHandlerKind('RequestHandler')(new HttpHandler('*', '*', () => {})),
).toBe(true)

expect(
isHandlerKind('RequestHandler')(
new GraphQLHandler('all', '*', '*', () => {}),
),
).toBe(true)
})

it('returns true if expected a request handler and given a custom request handler', () => {
class MyHandler extends RequestHandler {
constructor() {
super({ info: { header: '*' }, resolver: () => {} })
}
predicate = () => false
log() {}
}

expect(isHandlerKind('RequestHandler')(new MyHandler())).toBe(true)
})

it('returns false if expected a request handler but given event handler', () => {
expect(isHandlerKind('RequestHandler')(new WebSocketHandler('*'))).toBe(false)
})

it('returns false if expected a request handler but given arbitrary object', () => {
expect(isHandlerKind('RequestHandler')(undefined)).toBe(false)
expect(isHandlerKind('RequestHandler')(null)).toBe(false)
expect(isHandlerKind('RequestHandler')({})).toBe(false)
expect(isHandlerKind('RequestHandler')([])).toBe(false)
expect(isHandlerKind('RequestHandler')(123)).toBe(false)
expect(isHandlerKind('RequestHandler')('hello')).toBe(false)
})

it('returns true if expected an event handler and given an event handler', () => {
expect(isHandlerKind('EventHandler')(new WebSocketHandler('*'))).toBe(true)
})

it('returns true if expected an event handler and given a custom event handler', () => {
class MyEventHandler extends WebSocketHandler {
constructor() {
super('*')
}
}
expect(isHandlerKind('EventHandler')(new MyEventHandler())).toBe(true)
})

it('returns false if expected an event handler but given arbitrary object', () => {
expect(isHandlerKind('EventHandler')(undefined)).toBe(false)
expect(isHandlerKind('EventHandler')(null)).toBe(false)
expect(isHandlerKind('EventHandler')({})).toBe(false)
expect(isHandlerKind('EventHandler')([])).toBe(false)
expect(isHandlerKind('EventHandler')(123)).toBe(false)
expect(isHandlerKind('EventHandler')('hello')).toBe(false)
})
21 changes: 21 additions & 0 deletions src/core/utils/internal/isHandlerKind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { HandlerKind } from '../../handlers/common'
import type { RequestHandler } from '../../handlers/RequestHandler'
import type { WebSocketHandler } from '../../handlers/WebSocketHandler'

/**
* A filter function that ensures that the provided argument
* is a handler of the given kind. This helps differentiate
* between different kinds of handlers, e.g. request and event handlers.
*/
export function isHandlerKind<K extends HandlerKind>(kind: K) {
return (
input: unknown,
): input is K extends 'EventHandler' ? WebSocketHandler : RequestHandler => {
return (
input != null &&
typeof input === 'object' &&
'__kind' in input &&
input.__kind === kind
)
}
}
40 changes: 0 additions & 40 deletions src/core/utils/internal/toRequestHandlersOnly.test.ts

This file was deleted.

15 changes: 0 additions & 15 deletions src/core/utils/internal/toRequestHandlersOnly.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/core/ws/handleWebSocketEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
onUnhandledRequest,
UnhandledRequestStrategy,
} from '../utils/request/onUnhandledRequest'
import { isHandlerKind } from '../utils/internal/toRequestHandlersOnly'

Check failure on line 9 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (4.9)

Cannot find module '../utils/internal/toRequestHandlersOnly' or its corresponding type declarations.

Check failure on line 9 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.0)

Cannot find module '../utils/internal/toRequestHandlersOnly' or its corresponding type declarations.

Check failure on line 9 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / exports

Cannot find module '../utils/internal/toRequestHandlersOnly' or its corresponding type declarations.

Check failure on line 9 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (4.8)

Cannot find module '../utils/internal/toRequestHandlersOnly' or its corresponding type declarations.

Check failure on line 9 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.2)

Cannot find module '../utils/internal/toRequestHandlersOnly' or its corresponding type declarations.

Check failure on line 9 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.3)

Cannot find module '../utils/internal/toRequestHandlersOnly' or its corresponding type declarations.

Check failure on line 9 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.1)

Cannot find module '../utils/internal/toRequestHandlersOnly' or its corresponding type declarations.

Check failure on line 9 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.4)

Cannot find module '../utils/internal/toRequestHandlersOnly' or its corresponding type declarations.

Check failure on line 9 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.5)

Cannot find module '../utils/internal/toRequestHandlersOnly' or its corresponding type declarations.

interface HandleWebSocketEventOptions {
getUnhandledRequestStrategy: () => UnhandledRequestStrategy
Expand All @@ -30,7 +31,7 @@ export function handleWebSocketEvent(options: HandleWebSocketEventOptions) {

for (const handler of handlers) {
if (
handler instanceof WebSocketHandler &&
isHandlerKind('EventHandler')(handler) &&
handler.predicate({

Check failure on line 35 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (4.9)

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; parsedResult: WebSocketHandlerParsedResult | Promise<any>; }' is not assignable to parameter of type '{ request: Request; parsedResult: any; resolutionContext?: ResponseResolutionContext | undefined; } & { event: MessageEvent<WebSocketConnectionData>; parsedResult: WebSocketHandlerParsedResult; }'.

Check failure on line 35 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.0)

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; parsedResult: WebSocketHandlerParsedResult | Promise<any>; }' is not assignable to parameter of type '{ request: Request; parsedResult: any; resolutionContext?: ResponseResolutionContext | undefined; } & { event: MessageEvent<WebSocketConnectionData>; parsedResult: WebSocketHandlerParsedResult; }'.

Check failure on line 35 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / exports

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; parsedResult: WebSocketHandlerParsedResult | Promise<any>; }' is not assignable to parameter of type '{ request: Request; parsedResult: any; resolutionContext?: ResponseResolutionContext | undefined; } & { event: MessageEvent<WebSocketConnectionData>; parsedResult: WebSocketHandlerParsedResult; }'.

Check failure on line 35 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (4.8)

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; parsedResult: WebSocketHandlerParsedResult | Promise<any>; }' is not assignable to parameter of type '{ request: Request; parsedResult: any; resolutionContext?: ResponseResolutionContext | undefined; } & { event: MessageEvent<WebSocketConnectionData>; parsedResult: WebSocketHandlerParsedResult; }'.

Check failure on line 35 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.2)

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; parsedResult: Promise<any> | WebSocketHandlerParsedResult; }' is not assignable to parameter of type '{ request: Request; parsedResult: any; resolutionContext?: ResponseResolutionContext | undefined; } & { event: MessageEvent<WebSocketConnectionData>; parsedResult: WebSocketHandlerParsedResult; }'.

Check failure on line 35 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.3)

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; parsedResult: Promise<any> | WebSocketHandlerParsedResult; }' is not assignable to parameter of type '{ request: Request; parsedResult: any; resolutionContext?: ResponseResolutionContext | undefined; } & { event: MessageEvent<WebSocketConnectionData>; parsedResult: WebSocketHandlerParsedResult; }'.

Check failure on line 35 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.1)

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; parsedResult: WebSocketHandlerParsedResult | Promise<any>; }' is not assignable to parameter of type '{ request: Request; parsedResult: any; resolutionContext?: ResponseResolutionContext | undefined; } & { event: MessageEvent<WebSocketConnectionData>; parsedResult: WebSocketHandlerParsedResult; }'.

Check failure on line 35 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.4)

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; parsedResult: WebSocketHandlerParsedResult | Promise<any>; }' is not assignable to parameter of type '{ request: Request; parsedResult: any; resolutionContext?: ResponseResolutionContext | undefined; } & { event: MessageEvent<WebSocketConnectionData>; parsedResult: WebSocketHandlerParsedResult; }'.

Check failure on line 35 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.5)

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; parsedResult: WebSocketHandlerParsedResult | Promise<any>; }' is not assignable to parameter of type '{ request: Request; parsedResult: any; resolutionContext?: ResponseResolutionContext | undefined; } & { event: MessageEvent<WebSocketConnectionData>; parsedResult: WebSocketHandlerParsedResult; }'.
event: connectionEvent,
parsedResult: handler.parse({

Check failure on line 37 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (4.9)

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; }' is not assignable to parameter of type '{ request: Request; resolutionContext?: ResponseResolutionContext | undefined; } & { event: MessageEvent<WebSocketConnectionData>; }'.

Check failure on line 37 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.0)

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; }' is not assignable to parameter of type '{ request: Request; resolutionContext?: ResponseResolutionContext | undefined; } & { event: MessageEvent<WebSocketConnectionData>; }'.

Check failure on line 37 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / exports

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; }' is not assignable to parameter of type '{ request: Request; resolutionContext?: ResponseResolutionContext | undefined; } & { event: MessageEvent<WebSocketConnectionData>; }'.

Check failure on line 37 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (4.8)

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; }' is not assignable to parameter of type '{ request: Request; resolutionContext?: ResponseResolutionContext | undefined; } & { event: MessageEvent<WebSocketConnectionData>; }'.

Check failure on line 37 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.2)

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; }' is not assignable to parameter of type '{ event: MessageEvent<WebSocketConnectionData>; } & { request: Request; resolutionContext?: ResponseResolutionContext | undefined; }'.

Check failure on line 37 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.3)

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; }' is not assignable to parameter of type '{ event: MessageEvent<WebSocketConnectionData>; } & { request: Request; resolutionContext?: ResponseResolutionContext | undefined; }'.

Check failure on line 37 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.1)

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; }' is not assignable to parameter of type '{ request: Request; resolutionContext?: ResponseResolutionContext | undefined; } & { event: MessageEvent<WebSocketConnectionData>; }'.

Check failure on line 37 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.4)

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; }' is not assignable to parameter of type '{ request: Request; resolutionContext?: ResponseResolutionContext | undefined; } & { event: MessageEvent<WebSocketConnectionData>; }'.

Check failure on line 37 in src/core/ws/handleWebSocketEvent.ts

View workflow job for this annotation

GitHub Actions / typescript (5.5)

Argument of type '{ event: MessageEvent<WebSocketConnectionData>; }' is not assignable to parameter of type '{ request: Request; resolutionContext?: ResponseResolutionContext | undefined; } & { event: MessageEvent<WebSocketConnectionData>; }'.
Expand Down
4 changes: 2 additions & 2 deletions src/node/SetupServerCommonApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { InternalError, devUtils } from '~/core/utils/internal/devUtils'
import type { SetupServerCommon } from './glossary'
import { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'
import { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'
import { toRequestHandlersOnly } from '~/core/utils/internal/toRequestHandlersOnly'
import { isHandlerKind } from '~/core/utils/internal/isHandlerKind'

export const DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {
onUnhandledRequest: 'warn',
Expand Down Expand Up @@ -64,7 +64,7 @@ export class SetupServerCommonApi
requestId,
this.handlersController
.currentHandlers()
.filter(toRequestHandlersOnly),
.filter(isHandlerKind('RequestHandler')),
this.resolvedOptions,
this.emitter,
)
Expand Down

0 comments on commit b6c5375

Please sign in to comment.