Repository: solid/community-server
Pull request #460 (2021-01-02 08:44:47)

refactor: Add HttpHandlerInput interface


Patch:
diff --git a/src/ldp/AuthenticatedLdpHandler.ts b/src/ldp/AuthenticatedLdpHandler.ts
index a304fc1c9..f6289ca6a 100644
--- a/src/ldp/AuthenticatedLdpHandler.ts
+++ b/src/ldp/AuthenticatedLdpHandler.ts
@@ -2,6 +2,7 @@ import type { Credentials } from '../authentication/Credentials';
 import type { CredentialsExtractor } from '../authentication/CredentialsExtractor';
 import type { Authorizer } from '../authorization/Authorizer';
 import { getLoggerFor } from '../logging/LogUtil';
+import type { HttpHandlerInput } from '../server/HttpHandler';
 import { HttpHandler } from '../server/HttpHandler';
 import type { HttpRequest } from '../server/HttpRequest';
 import type { HttpResponse } from '../server/HttpResponse';
@@ -71,7 +72,7 @@ export class AuthenticatedLdpHandler extends HttpHandler {
    *
    * @returns A promise resolving if this request can be handled, otherwise rejecting with an Error.
    */
-  public async canHandle(input: { request: HttpRequest; response: HttpResponse }): Promise<void> {
+  public async canHandle(input: HttpHandlerInput): Promise<void> {
     return this.requestParser.canHandle(input.request);
   }
 
@@ -88,7 +89,7 @@ export class AuthenticatedLdpHandler extends HttpHandler {
    *
    * @returns A promise resolving when the handling is finished.
    */
-  public async handle(input: { request: HttpRequest; response: HttpResponse }): Promise<void> {
+  public async handle(input: HttpHandlerInput): Promise<void> {
     let writeData: { response: HttpResponse; result: ResponseDescription | Error };
 
     try {
diff --git a/src/server/HttpHandler.ts b/src/server/HttpHandler.ts
index c0441ce2b..7505b232a 100644
--- a/src/server/HttpHandler.ts
+++ b/src/server/HttpHandler.ts
@@ -2,7 +2,12 @@ import { AsyncHandler } from '../util/AsyncHandler';
 import type { HttpRequest } from './HttpRequest';
 import type { HttpResponse } from './HttpResponse';
 
+export interface HttpHandlerInput {
+  request: HttpRequest;
+  response: HttpResponse;
+}
+
 /**
  * An HTTP request handler.
  */
-export abstract class HttpHandler extends AsyncHandler<{ request: HttpRequest; response: HttpResponse }> {}
+export abstract class HttpHandler extends AsyncHandler<HttpHandlerInput> {}
diff --git a/src/server/middleware/CorsHandler.ts b/src/server/middleware/CorsHandler.ts
index f16a40a1d..1c66f6c57 100644
--- a/src/server/middleware/CorsHandler.ts
+++ b/src/server/middleware/CorsHandler.ts
@@ -1,9 +1,8 @@
 import cors from 'cors';
 import type { CorsOptions } from 'cors';
 import type { RequestHandler } from 'express';
+import type { HttpHandlerInput } from '../HttpHandler';
 import { HttpHandler } from '../HttpHandler';
-import type { HttpRequest } from '../HttpRequest';
-import type { HttpResponse } from '../HttpResponse';
 
 const defaultOptions: CorsOptions = {
   origin: (origin: any, callback: any): void => callback(null, origin ?? '*'),
@@ -32,7 +31,7 @@ export class CorsHandler extends HttpHandler {
     this.corsHandler = cors({ ...defaultOptions, ...options });
   }
 
-  public async handle(input: { request: HttpRequest; response: HttpResponse }): Promise<void> {
+  public async handle(input: HttpHandlerInput): Promise<void> {
     return new Promise((resolve): void => {
       this.corsHandler(input.request as any, input.response as any, (): void => resolve());
     });


Test patch:
diff --git a/test/integration/Middleware.test.ts b/test/integration/Middleware.test.ts
index 93a85e137..f99bac47e 100644
--- a/test/integration/Middleware.test.ts
+++ b/test/integration/Middleware.test.ts
@@ -1,16 +1,15 @@
 import type { Server } from 'http';
 import request from 'supertest';
 import type { ExpressHttpServerFactory } from '../../src/server/ExpressHttpServerFactory';
+import type { HttpHandlerInput } from '../../src/server/HttpHandler';
 import { HttpHandler } from '../../src/server/HttpHandler';
-import type { HttpRequest } from '../../src/server/HttpRequest';
-import type { HttpResponse } from '../../src/server/HttpResponse';
 import { StaticAsyncHandler } from '../util/StaticAsyncHandler';
 import { instantiateFromConfig } from './Config';
 
 const port = 6002;
 
 class SimpleHttpHandler extends HttpHandler {
-  public async handle(input: { request: HttpRequest; response: HttpResponse }): Promise<void> {
+  public async handle(input: HttpHandlerInput): Promise<void> {
     input.response.writeHead(200, { location: '/' });
     input.response.end('Hello World');
   }
diff --git a/test/unit/server/WebSocketServerFactory.test.ts b/test/unit/server/WebSocketServerFactory.test.ts
index 9a1abe1d9..a85844bd1 100644
--- a/test/unit/server/WebSocketServerFactory.test.ts
+++ b/test/unit/server/WebSocketServerFactory.test.ts
@@ -2,14 +2,14 @@ import type { Server } from 'http';
 import request from 'supertest';
 import WebSocket from 'ws';
 import { ExpressHttpServerFactory } from '../../../src/server/ExpressHttpServerFactory';
+import type { HttpHandlerInput } from '../../../src/server/HttpHandler';
 import { HttpHandler } from '../../../src/server/HttpHandler';
 import type { HttpRequest } from '../../../src/server/HttpRequest';
-import type { HttpResponse } from '../../../src/server/HttpResponse';
 import { WebSocketHandler } from '../../../src/server/WebSocketHandler';
 import { WebSocketServerFactory } from '../../../src/server/WebSocketServerFactory';
 
 class SimpleHttpHandler extends HttpHandler {
-  public async handle(input: { request: HttpRequest; response: HttpResponse }): Promise<void> {
+  public async handle(input: HttpHandlerInput): Promise<void> {
     input.response.end('SimpleHttpHandler');
   }
 }