CommunitySolidServer/src/ldp/operations/PutOperationHandler.ts
2020-09-03 09:02:20 +02:00

33 lines
1.1 KiB
TypeScript

import { ResourceStore } from '../../storage/ResourceStore';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { Operation } from './Operation';
import { OperationHandler } from './OperationHandler';
import { ResponseDescription } from './ResponseDescription';
/**
* Handles PUT {@link Operation}s.
* Calls the setRepresentation function from a {@link ResourceStore}.
*/
export class PutOperationHandler extends OperationHandler {
private readonly store: ResourceStore;
public constructor(store: ResourceStore) {
super();
this.store = store;
}
public async canHandle(input: Operation): Promise<void> {
if (input.method !== 'PUT') {
throw new UnsupportedHttpError('This handler only supports PUT operations.');
}
}
public async handle(input: Operation): Promise<ResponseDescription> {
if (typeof input.body !== 'object') {
throw new UnsupportedHttpError('PUT operations require a body.');
}
await this.store.setRepresentation(input.target, input.body);
return { identifier: input.target };
}
}