Skip to content

Commit

Permalink
feat: remove resolutionContext
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Dec 15, 2024
1 parent a6c419c commit bdd2083
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 111 deletions.
12 changes: 2 additions & 10 deletions src/core/handlers/HttpHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ResponseResolutionContext } from '../utils/executeHandlers'
import { devUtils } from '../utils/internal/devUtils'
import { isStringEqual } from '../utils/internal/isStringEqual'
import { getStatusCodeColor } from '../utils/logging/getStatusCodeColor'
Expand Down Expand Up @@ -106,16 +105,9 @@ export class HttpHandler extends RequestHandler<
)
}

async parse(args: {
request: Request
resolutionContext?: ResponseResolutionContext
}) {
async parse(args: { request: Request }) {
const url = new URL(args.request.url)
const match = matchRequestUrl(
url,
this.info.path,
args.resolutionContext?.baseUrl,
)
const match = matchRequestUrl(url, this.info.path)
const cookies = getAllRequestCookies(args.request)

return {
Expand Down
17 changes: 2 additions & 15 deletions src/core/handlers/RequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Iterable,
isIterable,
} from '../utils/internal/isIterable'
import type { ResponseResolutionContext } from '../utils/executeHandlers'
import type { MaybePromise } from '../typeUtils'
import { StrictRequest, StrictResponse } from '..//HttpResponse'
import type { HandlerKind } from './common'
Expand Down Expand Up @@ -163,7 +162,6 @@ export abstract class RequestHandler<
abstract predicate(args: {
request: Request
parsedResult: ParsedResult
resolutionContext?: ResponseResolutionContext
}): boolean

/**
Expand All @@ -179,10 +177,7 @@ export abstract class RequestHandler<
* Parse the intercepted request to extract additional information from it.
* Parsed result is then exposed to other methods of this request handler.
*/
async parse(_args: {
request: Request
resolutionContext?: ResponseResolutionContext
}): Promise<ParsedResult> {
async parse(_args: { request: Request }): Promise<ParsedResult> {
return {} as ParsedResult
}

Expand All @@ -193,19 +188,14 @@ export abstract class RequestHandler<
* as a convenience method for consumers writing custom
* handlers.
*/
public async test(args: {
request: Request
resolutionContext?: ResponseResolutionContext
}): Promise<boolean> {
public async test(args: { request: Request }): Promise<boolean> {
const parsedResult = await this.parse({
request: args.request,
resolutionContext: args.resolutionContext,
})

return this.predicate({
request: args.request,
parsedResult,
resolutionContext: args.resolutionContext,
})
}

Expand Down Expand Up @@ -241,7 +231,6 @@ export abstract class RequestHandler<
public async run(args: {
request: StrictRequest<any>
requestId: string
resolutionContext?: ResponseResolutionContext
}): Promise<RequestHandlerExecutionResult<ParsedResult> | null> {
if (this.isUsed && this.options?.once) {
return null
Expand All @@ -256,12 +245,10 @@ export abstract class RequestHandler<

const parsedResult = await this.parse({
request: args.request,
resolutionContext: args.resolutionContext,
})
const shouldInterceptRequest = this.predicate({
request: args.request,
parsedResult,
resolutionContext: args.resolutionContext,
})

if (!shouldInterceptRequest) {
Expand Down
8 changes: 1 addition & 7 deletions src/core/utils/executeHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ export interface HandlersExecutionResult {
response?: Response
}

export interface ResponseResolutionContext {
baseUrl?: string
}

/**
* Executes the list of request handlers against the given request.
* Returns the execution result object containing any matching request
Expand All @@ -22,18 +18,16 @@ export const executeHandlers = async <Handlers extends Array<RequestHandler>>({
request,
requestId,
handlers,
resolutionContext,
}: {
request: Request
requestId: string
handlers: Handlers
resolutionContext?: ResponseResolutionContext
}): Promise<HandlersExecutionResult | null> => {
let matchingHandler: RequestHandler | null = null
let result: RequestHandlerExecutionResult<any> | null = null

for (const handler of handlers) {
result = await handler.run({ request, requestId, resolutionContext })
result = await handler.run({ request, requestId })

// If the handler produces some result for this request,
// it automatically becomes matching.
Expand Down
64 changes: 0 additions & 64 deletions src/core/utils/handleRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,67 +423,3 @@ it('handles parallel requests with one-time handlers', async () => {
expect(oneTimeHandler.isUsed).toBe(true)
expect(anotherHandler.isUsed).toBe(true)
})

describe('[Private] - resolutionContext - used for extensions', () => {
describe('#baseUrl', () => {
test('when defined, handle requests to that base url only defining pathnames in the handler', async () => {
const { emitter } = setup()

const baseUrl = 'http://this-base-url-works.com'
const handleRequestOptionsWithBaseUrl: HandleRequestOptions = {
...handleRequestOptions,
resolutionContext: { baseUrl },
}

const handler = http.get('/resource', () => {
return HttpResponse.text('Mocked response')
})

const handlers: Array<RequestHandler> = [handler]

const requestId = createRequestId()
const request = new Request(new URL('/resource', baseUrl))
const response = await handleRequest(
request,
requestId,
handlers,
options,
emitter,
handleRequestOptionsWithBaseUrl,
)

expect(await response?.text()).toBe('Mocked response')
})

test('when defined, do not handle requests to different base urls when defining pathnames in the handler', async () => {
const { emitter } = setup()

const baseUrl = 'http://this-base-url-works.com'
const handleRequestOptionsWithBaseUrl: HandleRequestOptions = {
...handleRequestOptions,
resolutionContext: { baseUrl },
}

const handler = http.get('/resource', () => {
return HttpResponse.text('Mocked response')
})

const handlers: Array<RequestHandler> = [handler]

const requestId = createRequestId()
const request = new Request(
new URL('/resource', `http://not-the-base-url.com`),
)
const response = await handleRequest(
request,
requestId,
handlers,
options,
emitter,
handleRequestOptionsWithBaseUrl,
)

expect(response).toBeUndefined()
})
})
})
15 changes: 0 additions & 15 deletions src/core/utils/handleRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@ import { onUnhandledRequest } from './request/onUnhandledRequest'
import { storeResponseCookies } from './request/storeResponseCookies'

export interface HandleRequestOptions {
/**
* `resolutionContext` is not part of the general public api
* but is exposed to aid in creating extensions like
* `@mswjs/http-middleware`.
*/
resolutionContext?: {
/**
* A base url to use when resolving relative urls.
* @note This is primarily used by the `@mswjs/http-middleware`
* to resolve relative urls in the context of the running server
*/
baseUrl?: string
}

/**
* Invoked whenever a request is performed as-is.
*/
Expand Down Expand Up @@ -59,7 +45,6 @@ export async function handleRequest(
request,
requestId,
handlers,
resolutionContext: handleRequestOptions?.resolutionContext,
})
})

Expand Down

0 comments on commit bdd2083

Please sign in to comment.