Skip to content

Commit

Permalink
feat: add response description interface
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Jun 24, 2020
1 parent cf258d0 commit e0343fc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
13 changes: 6 additions & 7 deletions src/ldp/AuthenticatedLdpHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/ldp/http/ResponseWriter.ts
Original file line number Diff line number Diff line change
@@ -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 }> {}
3 changes: 2 additions & 1 deletion src/ldp/operations/OperationHandler.ts
Original file line number Diff line number Diff line change
@@ -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> {}
10 changes: 10 additions & 0 deletions src/ldp/operations/ResponseDescription.ts
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion test/unit/ldp/AuthenticatedLdpHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> => {
Expand Down

0 comments on commit e0343fc

Please sign in to comment.