mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
refactor: Add HttpHandlerInput interface
This commit is contained in:
parent
0bd48f0dc5
commit
a73936f522
@ -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 {
|
||||
|
@ -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> {}
|
||||
|
@ -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());
|
||||
});
|
||||
|
@ -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');
|
||||
}
|
||||
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user