feat: add response description interface

This commit is contained in:
Joachim Van Herwegen 2020-06-23 10:04:51 +02:00
parent cf258d0317
commit e0343fca54
5 changed files with 21 additions and 11 deletions

View File

@ -9,6 +9,7 @@ import { OperationHandler } from './operations/OperationHandler';
import { PermissionSet } from './permissions/PermissionSet';
import { PermissionsExtractor } from './permissions/PermissionsExtractor';
import { RequestParser } from './http/RequestParser';
import { ResponseDescription } from './operations/ResponseDescription';
import { ResponseWriter } from './http/ResponseWriter';
/**
@ -87,15 +88,15 @@ export class AuthenticatedLdpHandler extends HttpHandler {
*/
public async handle(input: { request: HttpRequest; response: HttpResponse }): Promise<void> {
let err: Error;
let operation: Operation;
let description: ResponseDescription;
try {
operation = await this.runHandlers(input.request);
description = await this.runHandlers(input.request);
} catch (error) {
err = error;
}
const writeData = { response: input.response, operation, error: err };
const writeData = { response: input.response, description, error: err };
return this.responseWriter.handleSafe(writeData);
}
@ -107,13 +108,11 @@ export class AuthenticatedLdpHandler extends HttpHandler {
*
* @returns A promise resolving to the generated Operation.
*/
private async runHandlers(request: HttpRequest): Promise<Operation> {
private async runHandlers(request: HttpRequest): Promise<ResponseDescription> {
const op: Operation = await this.requestParser.handleSafe(request);
const credentials: Credentials = await this.credentialsExtractor.handleSafe(request);
const permissions: PermissionSet = await this.permissionsExtractor.handleSafe(op);
await this.authorizer.handleSafe({ credentials, identifier: op.target, permissions });
await this.operationHandler.handleSafe(op);
return op;
return this.operationHandler.handleSafe(op);
}
}

View File

@ -1,9 +1,9 @@
import { AsyncHandler } from '../../util/AsyncHandler';
import { HttpResponse } from '../../server/HttpResponse';
import { Operation } from '../operations/Operation';
import { ResponseDescription } from '../operations/ResponseDescription';
/**
* Writes to the HttpResponse.
* Response depends on the operation result and potentially which errors was thrown.
*/
export type ResponseWriter = AsyncHandler<{ response: HttpResponse; operation: Operation; error?: Error }>;
export abstract class ResponseWriter extends AsyncHandler<{ response: HttpResponse; description?: ResponseDescription; error?: Error }> {}

View File

@ -1,7 +1,8 @@
import { AsyncHandler } from '../../util/AsyncHandler';
import { Operation } from './Operation';
import { ResponseDescription } from './ResponseDescription';
/**
* Handler for a specific operation type.
*/
export abstract class OperationHandler extends AsyncHandler<Operation> {}
export abstract class OperationHandler extends AsyncHandler<Operation, ResponseDescription> {}

View File

@ -0,0 +1,10 @@
import { Representation } from '../representation/Representation';
import { ResourceIdentifier } from '../representation/ResourceIdentifier';
/**
* The result of executing an operation.
*/
export interface ResponseDescription {
identifier: ResourceIdentifier;
body?: Representation;
}

View File

@ -44,7 +44,7 @@ describe('An AuthenticatedLdpHandler', (): void => {
await expect(handler.handle({ request: 'request' as any, response: 'response' as any })).resolves.toEqual('response');
expect(responseFn).toHaveBeenCalledTimes(1);
expect(responseFn).toHaveBeenLastCalledWith({ response: 'response', operation: 'parser' as any });
expect(responseFn).toHaveBeenLastCalledWith({ response: 'response', description: 'operation' as any });
});
it('sends an error to the output if a handler does not support the input.', async(): Promise<void> => {