From e0343fca54d6a779fd94df9863db2b28bb9ba332 Mon Sep 17 00:00:00 2001 From: Joachim Van Herwegen Date: Tue, 23 Jun 2020 10:04:51 +0200 Subject: [PATCH] feat: add response description interface --- src/ldp/AuthenticatedLdpHandler.ts | 13 ++++++------- src/ldp/http/ResponseWriter.ts | 4 ++-- src/ldp/operations/OperationHandler.ts | 3 ++- src/ldp/operations/ResponseDescription.ts | 10 ++++++++++ test/unit/ldp/AuthenticatedLdpHandler.test.ts | 2 +- 5 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 src/ldp/operations/ResponseDescription.ts diff --git a/src/ldp/AuthenticatedLdpHandler.ts b/src/ldp/AuthenticatedLdpHandler.ts index f5c626a85..57bcf5619 100644 --- a/src/ldp/AuthenticatedLdpHandler.ts +++ b/src/ldp/AuthenticatedLdpHandler.ts @@ -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 { 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 { + private async runHandlers(request: HttpRequest): Promise { 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); } } diff --git a/src/ldp/http/ResponseWriter.ts b/src/ldp/http/ResponseWriter.ts index c6e9769de..ac5bed3d8 100644 --- a/src/ldp/http/ResponseWriter.ts +++ b/src/ldp/http/ResponseWriter.ts @@ -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 }> {} diff --git a/src/ldp/operations/OperationHandler.ts b/src/ldp/operations/OperationHandler.ts index 405be2f10..b58ddf3a2 100644 --- a/src/ldp/operations/OperationHandler.ts +++ b/src/ldp/operations/OperationHandler.ts @@ -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 {} +export abstract class OperationHandler extends AsyncHandler {} diff --git a/src/ldp/operations/ResponseDescription.ts b/src/ldp/operations/ResponseDescription.ts new file mode 100644 index 000000000..3958fdc2c --- /dev/null +++ b/src/ldp/operations/ResponseDescription.ts @@ -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; +} diff --git a/test/unit/ldp/AuthenticatedLdpHandler.test.ts b/test/unit/ldp/AuthenticatedLdpHandler.test.ts index 02bdec362..efa1d61a5 100644 --- a/test/unit/ldp/AuthenticatedLdpHandler.test.ts +++ b/test/unit/ldp/AuthenticatedLdpHandler.test.ts @@ -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 => {