From a516cd994ae36092a37b65f7d6dc48bd8750d1a9 Mon Sep 17 00:00:00 2001 From: Michael Solomon Date: Sat, 26 Oct 2024 14:06:11 +0300 Subject: [PATCH 1/3] fix typo --- src/utils/handleRequest.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/handleRequest.ts b/src/utils/handleRequest.ts index 84316e0e..225f8c10 100644 --- a/src/utils/handleRequest.ts +++ b/src/utils/handleRequest.ts @@ -112,7 +112,7 @@ export async function handleRequest( // for that event are finished (e.g. async listeners awaited). // By the end of this promise, the developer cannot affect the // request anymore. - const requestListtenersPromise = emitAsync(options.emitter, 'request', { + const requestListenersPromise = emitAsync(options.emitter, 'request', { requestId: options.requestId, request: options.request, controller: options.controller, @@ -121,7 +121,7 @@ export async function handleRequest( await Promise.race([ // Short-circuit the request handling promise if the request gets aborted. requestAbortPromise, - requestListtenersPromise, + requestListenersPromise, options.controller[kResponsePromise], ]) From c8e1374ace167074bfd636d23c15e0985e491345 Mon Sep 17 00:00:00 2001 From: Michael Solomon Date: Mon, 11 Nov 2024 15:22:45 +0200 Subject: [PATCH 2/3] wip --- src/RequestController.ts | 6 +++--- src/utils/handleRequest.ts | 38 ++++++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/RequestController.ts b/src/RequestController.ts index 8ff48cfc..7df0a740 100644 --- a/src/RequestController.ts +++ b/src/RequestController.ts @@ -13,7 +13,7 @@ export class RequestController { * @note This promise cannot be rejected. It's either infinitely * pending or resolved with whichever Response was passed to `respondWith()`. */ - [kResponsePromise]: DeferredPromise; + [kResponsePromise]: DeferredPromise; /** * Internal flag indicating if this request has been handled. @@ -46,7 +46,7 @@ export class RequestController { this[kResponsePromise].resolve(response) /** - * @note The request conrtoller doesn't do anything + * @note The request controller doesn't do anything * apart from letting the interceptor await the response * provided by the developer through the response promise. * Each interceptor implements the actual respondWith/errorWith @@ -60,7 +60,7 @@ export class RequestController { * controller.errorWith() * controller.errorWith(new Error('Oops!')) */ - public errorWith(error?: Error): void { + public errorWith(error?: Object): void { invariant.as( InterceptorError, !this[kRequestHandled], diff --git a/src/utils/handleRequest.ts b/src/utils/handleRequest.ts index 08d1e5f2..e213e52a 100644 --- a/src/utils/handleRequest.ts +++ b/src/utils/handleRequest.ts @@ -43,20 +43,31 @@ interface HandleRequestOptions { export async function handleRequest( options: HandleRequestOptions ): Promise { - const handleResponse = async (response: Response | Error) => { + const onResolve = async (response: Response): Promise => { + // Handle "Response.error()" instances. if (response instanceof Error) { options.onError(response) - } - - // Handle "Response.error()" instances. - else if (isResponseError(response)) { + } else if (isResponseError(response)) { options.onRequestError(response) } else { - await options.onResponse(response) + await handleResponse(response) } return true } + const onReject = async (error: unknown): Promise => { + // Handle thrown responses. + if (error instanceof Response) { + await handleResponse(error) + return true + } + + return await handleResponseError(error); + } + + const handleResponse = async (response: Response) => { + await options.onResponse(response) + } const handleResponseError = async (error: unknown): Promise => { // Forward the special interceptor error instances @@ -69,12 +80,7 @@ export async function handleRequest( if (isNodeLikeError(error)) { options.onError(error) return true - } - - // Handle thrown responses. - if (error instanceof Response) { - return await handleResponse(error) - } + } return false } @@ -144,7 +150,7 @@ export async function handleRequest( if (result.error) { // Handle the error during the request listener execution. // These can be thrown responses or request errors. - if (await handleResponseError(result.error)) { + if (await onReject(result.error)) { return true } @@ -188,11 +194,11 @@ export async function handleRequest( * emit of the same event. They are forwarded as-is. */ if (nextResult.error) { - return handleResponseError(nextResult.error) + return onReject(nextResult.error) } if (nextResult.data) { - return handleResponse(nextResult.data) + return onResolve(nextResult.data) } } @@ -208,7 +214,7 @@ export async function handleRequest( * unhandled exceptions from intended errors. */ if (result.data) { - return handleResponse(result.data) + return onResolve(result.data) } // In all other cases, consider the request unhandled. From e9efbee6137f9209488d90f34eae5fb47376ca8f Mon Sep 17 00:00:00 2001 From: Michael Solomon Date: Tue, 12 Nov 2024 21:06:43 +0200 Subject: [PATCH 3/3] wip --- src/RequestController.test.ts | 8 ++++++++ src/RequestController.ts | 1 + 2 files changed, 9 insertions(+) diff --git a/src/RequestController.test.ts b/src/RequestController.test.ts index f3adeab2..33119635 100644 --- a/src/RequestController.test.ts +++ b/src/RequestController.test.ts @@ -26,6 +26,14 @@ it('resolves the response promise with the error provided to "errorWith"', async await expect(controller[kResponsePromise]).resolves.toEqual(error) }) +it('resolves the response promise with the object provided to "errorWith"', async () => { + const controller = new RequestController(new Request('http://localhost')) + const error = { message: 'Oops!' } + controller.errorWith(error) + + await expect(controller[kResponsePromise]).resolves.toEqual(error) +}) + it('throws when calling "respondWith" multiple times', () => { const controller = new RequestController(new Request('http://localhost')) controller.respondWith(new Response('hello world')) diff --git a/src/RequestController.ts b/src/RequestController.ts index 7df0a740..9b307c1f 100644 --- a/src/RequestController.ts +++ b/src/RequestController.ts @@ -59,6 +59,7 @@ export class RequestController { * @example * controller.errorWith() * controller.errorWith(new Error('Oops!')) + * controller.errorWith({ message: 'Oops!'}) */ public errorWith(error?: Object): void { invariant.as(