CommunitySolidServer/src/ldp/operations/HeadOperationHandler.ts
Joachim Van Herwegen bb7e88b137 refactor: Split HttpHandler behaviour over multiple classes
This allows easier reuse of certain reoccurring behaviours,
such as authorization.
The AuthenticatedLdpHandler is no longer required
since it is a combination of parsing and authorization.
This did require a small change to the OperationHandler interface.
2021-10-07 15:51:11 +02:00

35 lines
1.3 KiB
TypeScript

import type { ResourceStore } from '../../storage/ResourceStore';
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
import { OkResponseDescription } from '../http/response/OkResponseDescription';
import type { ResponseDescription } from '../http/response/ResponseDescription';
import type { OperationHandlerInput } from './OperationHandler';
import { OperationHandler } from './OperationHandler';
/**
* Handles HEAD {@link Operation}s.
* Calls the getRepresentation function from a {@link ResourceStore}.
*/
export class HeadOperationHandler extends OperationHandler {
private readonly store: ResourceStore;
public constructor(store: ResourceStore) {
super();
this.store = store;
}
public async canHandle({ operation }: OperationHandlerInput): Promise<void> {
if (operation.method !== 'HEAD') {
throw new NotImplementedHttpError('This handler only supports HEAD operations');
}
}
public async handle({ operation }: OperationHandlerInput): Promise<ResponseDescription> {
const body = await this.store.getRepresentation(operation.target, operation.preferences, operation.conditions);
// Close the Readable as we will not return it.
body.data.destroy();
return new OkResponseDescription(body.metadata);
}
}