mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
33 lines
1.1 KiB
TypeScript
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 };
|
|
}
|
|
}
|