Skip to content

Commit

Permalink
Refine RequestHandler signature
Browse files Browse the repository at this point in the history
Allow it to return arbitrary promise instead of awaiting it.
  • Loading branch information
surol committed Jun 24, 2020
1 parent 8d48d05 commit d77f4ea
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 31 deletions.
10 changes: 3 additions & 7 deletions src/core/request-capability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,9 @@ export abstract class RequestCapability<TInput, TExt = object>
* @returns New request processing handler.
*/
for<TMeans extends TInput>(handler: RequestHandler<TMeans & TExt>): RequestHandler<TMeans> {
return async ({ next, modifiedBy }) => {
if (modifiedBy(this[RequestModifier__symbol])) {
await next(handler);
} else {
await next(handler, this);
}
};
return async ({ next, modifiedBy }) => modifiedBy(this[RequestModifier__symbol])
? next(handler)
: next(handler, this);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/core/request-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('requestHandler', () => {
const error = new Error('test');
const calls: number[] = [];

const call = async (): Promise<void> => await requestHandler([
const call = async (): Promise<unknown> => await requestHandler([
() => { calls.push(1); },
() => { calls.push(2); throw error; },
() => { calls.push(3); },
Expand All @@ -57,7 +57,7 @@ describe('requestHandler', () => {
it('stops handlers execution once response written', async () => {

const calls: number[] = [];
const call = async (): Promise<void> => await requestHandler([
const call = async (): Promise<unknown> => await requestHandler([
() => { calls.push(1); },
() => { calls.push(2); (response as any).writableEnded = true; },
() => { calls.push(3); },
Expand Down
4 changes: 2 additions & 2 deletions src/core/request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type RequestHandler<TMeans> =
(
this: void,
context: RequestContext<TMeans>,
) => PromiseLike<void> | void;
) => PromiseLike<unknown> | void;

/**
* Request processing method signature.
Expand All @@ -45,7 +45,7 @@ export type RequestHandlerMethod<TThis, TMeans> =
(
this: TThis,
context: RequestContext<TMeans>,
) => PromiseLike<void> | void;
) => PromiseLike<unknown> | void;

/**
* Builds a request processing handler that delegates request processing to other handlers.
Expand Down
12 changes: 4 additions & 8 deletions src/core/request-modifier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,10 @@ describe('RequestModifier', () => {
expect(JSON.parse(await response.body())).toEqual({ test: 'modified' });
});
it('alters subsequent requests', async () => {
server.listener.mockImplementation(httpListener(Rendering.for(async ({ next }) => {
await next(
async ({ next }) => {
await next(({ renderJson, test }) => renderJson({ test }), { test: 'another' });
},
modifier,
);
})));
server.listener.mockImplementation(httpListener(Rendering.for(async ({ next }) => next(
({ next }) => next(({ renderJson, test }) => renderJson({ test }), { test: 'another' }),
modifier,
))));

const response = await server.get('/');

Expand Down
6 changes: 3 additions & 3 deletions src/http/dispatch/dispatch-by-accepted.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export function dispatchByAccepted<TMeans extends HttpMeans>(

if (handler) {
addResponseHeader(response, 'Vary', 'Accept');
await next(handler.bind(mimeTypes));
} else {
await next(fallback);
return next(handler.bind(mimeTypes));
}

return next(fallback);
};
}
4 changes: 2 additions & 2 deletions src/http/dispatch/dispatch-by-language.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function dispatchByLanguage<TMeans extends HttpMeans>(

const negotiator = httpLanguageNegotiator(languages);

return async ({ request, response, next }) => {
return ({ request, response, next }) => {

const { 'accept-language': acceptLanguage = '*' } = request.headers;
const handler = negotiator(acceptLanguage);
Expand All @@ -65,6 +65,6 @@ export function dispatchByLanguage<TMeans extends HttpMeans>(

addResponseHeader(response, 'Vary', 'Accept-Language');

await next(handler.bind(languages));
return next(handler.bind(languages));
};
}
14 changes: 7 additions & 7 deletions src/http/http-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function incomingHttpHandler<TRequest extends IncomingMessage, TResponse extends
onError = reject;
});

const incomingHandler: RequestHandler<IncomingHttpMeans<TRequest, TResponse>> = async ({
const incomingHandler: RequestHandler<IncomingHttpMeans<TRequest, TResponse>> = ({
request,
response,
next,
Expand All @@ -173,7 +173,7 @@ function incomingHttpHandler<TRequest extends IncomingMessage, TResponse extends
return new URL(url, `${proto}://${host}`);
});

await next(
return next(
handler,
{
requestAddresses: {
Expand All @@ -197,14 +197,14 @@ function incomingHttpHandler<TRequest extends IncomingMessage, TResponse extends
function fullHttpHandler<TRequest extends IncomingMessage, TResponse extends ServerResponse>(
config: HttpConfig<HttpMeans<TRequest, TResponse>>,
handler: RequestHandler<HttpMeans<TRequest, TResponse>>,
):
RequestHandler<HttpMeans<TRequest, TResponse>> {
): RequestHandler<HttpMeans<TRequest, TResponse>> {

const defaultHandler = defaultHttpHandler(config);
const errorHandler = httpErrorHandler(config);

return async (
{ next }: RequestContext<HttpMeans<TRequest, TResponse>>,
): Promise<void> => {
) => {
try {
if (!await next(handler)) {
await next(defaultHandler);
Expand Down Expand Up @@ -246,9 +246,9 @@ function httpErrorHandler<TMeans extends HttpMeans>(

const onError = errorHandler === true ? renderHttpError : errorHandler;

return async context => {
return context => {
logHttpError(context);
await context.next(onError);
return context.next(onError);
};
}

Expand Down

0 comments on commit d77f4ea

Please sign in to comment.