-
Notifications
You must be signed in to change notification settings - Fork 121
/
GetOperationHandler.ts
41 lines (34 loc) · 1.66 KB
/
GetOperationHandler.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
import { getETag } from '../../storage/Conditions';
import type { ResourceStore } from '../../storage/ResourceStore';
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
import { assertReadConditions } from '../../util/ResourceUtil';
import { HH } from '../../util/Vocabularies';
import { OkResponseDescription } from '../output/response/OkResponseDescription';
import type { ResponseDescription } from '../output/response/ResponseDescription';
import type { OperationHandlerInput } from './OperationHandler';
import { OperationHandler } from './OperationHandler';
/**
* Handles GET {@link Operation}s.
* Calls the getRepresentation function from a {@link ResourceStore}.
*/
export class GetOperationHandler extends OperationHandler {
private readonly store: ResourceStore;
public constructor(store: ResourceStore) {
super();
this.store = store;
}
public async canHandle({ operation }: OperationHandlerInput): Promise<void> {
if (operation.method !== 'GET') {
throw new NotImplementedHttpError('This handler only supports GET operations');
}
}
public async handle({ operation }: OperationHandlerInput): Promise<ResponseDescription> {
const body = await this.store.getRepresentation(operation.target, operation.preferences, operation.conditions);
// Check whether the cached representation is still valid or it is necessary to send a new representation
assertReadConditions(body, operation.conditions);
// Add the ETag of the returned representation
const etag = getETag(body.metadata);
body.metadata.set(HH.terms.etag, etag);
return new OkResponseDescription(body.metadata, body.data);
}
}