mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: add response description interface
This commit is contained in:
parent
cf258d0317
commit
e0343fca54
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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 }> {}
|
||||
|
@ -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
src/ldp/operations/ResponseDescription.ts
Normal file
10
src/ldp/operations/ResponseDescription.ts
Normal 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;
|
||||
}
|
@ -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> => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user