-
Notifications
You must be signed in to change notification settings - Fork 121
/
PutOperationHandler.ts
48 lines (43 loc) · 2.19 KB
/
PutOperationHandler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { getLoggerFor } from '../../logging/LogUtil';
import type { ResourceStore } from '../../storage/ResourceStore';
import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError';
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
import { CreatedResponseDescription } from '../output/response/CreatedResponseDescription';
import { ResetResponseDescription } from '../output/response/ResetResponseDescription';
import type { ResponseDescription } from '../output/response/ResponseDescription';
import type { OperationHandlerInput } from './OperationHandler';
import { OperationHandler } from './OperationHandler';
/**
* Handles PUT {@link Operation}s.
* Calls the setRepresentation function from a {@link ResourceStore}.
*/
export class PutOperationHandler extends OperationHandler {
protected readonly logger = getLoggerFor(this);
private readonly store: ResourceStore;
public constructor(store: ResourceStore) {
super();
this.store = store;
}
public async canHandle({ operation }: OperationHandlerInput): Promise<void> {
if (operation.method !== 'PUT') {
throw new NotImplementedHttpError('This handler only supports PUT operations');
}
}
public async handle({ operation }: OperationHandlerInput): Promise<ResponseDescription> {
// Solid, §2.1: "A Solid server MUST reject PUT, POST and PATCH requests
// without the Content-Type header with a status code of 400."
// https://solid.github.io/specification/protocol#http-server
if (!operation.body.metadata.contentType) {
this.logger.warn('PUT requests require the Content-Type header to be set');
throw new BadRequestHttpError('PUT requests require the Content-Type header to be set');
}
// A more efficient approach would be to have the server return metadata indicating if a resource was new
// See https://github.com/solid/community-server/issues/632
const exists = await this.store.hasResource(operation.target);
await this.store.setRepresentation(operation.target, operation.body, operation.conditions);
if (exists) {
return new ResetResponseDescription();
}
return new CreatedResponseDescription(operation.target);
}
}