-
Notifications
You must be signed in to change notification settings - Fork 121
/
RoutingAuxiliaryStrategy.ts
55 lines (48 loc) · 2.27 KB
/
RoutingAuxiliaryStrategy.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
49
50
51
52
53
54
55
import type { Representation } from '../representation/Representation';
import type { RepresentationMetadata } from '../representation/RepresentationMetadata';
import type { ResourceIdentifier } from '../representation/ResourceIdentifier';
import type { AuxiliaryStrategy } from './AuxiliaryStrategy';
import { RoutingAuxiliaryIdentifierStrategy } from './RoutingAuxiliaryIdentifierStrategy';
/**
* An {@link AuxiliaryStrategy} that combines multiple AuxiliaryStrategies into one.
* Uses `isAuxiliaryIdentifier` to know which strategy to call for which call.
*
* `addMetadata` will either call all strategies if the input is the subject identifier,
* or only the matching strategy if the input is an auxiliary identifier.
*/
export class RoutingAuxiliaryStrategy extends RoutingAuxiliaryIdentifierStrategy implements AuxiliaryStrategy {
protected readonly sources: AuxiliaryStrategy[] = [];
public constructor(sources: AuxiliaryStrategy[]) {
super(sources);
this.sources = sources;
}
public usesOwnAuthorization(identifier: ResourceIdentifier): boolean {
const source = this.getMatchingSource(identifier);
return source.usesOwnAuthorization(identifier);
}
public isRequiredInRoot(identifier: ResourceIdentifier): boolean {
const source = this.getMatchingSource(identifier);
return source.isRequiredInRoot(identifier);
}
public async addMetadata(metadata: RepresentationMetadata): Promise<void> {
const identifier = { path: metadata.identifier.value };
// Make sure unrelated auxiliary strategies don't add metadata to another auxiliary resource
const match = this.sources.find((source): boolean => source.isAuxiliaryIdentifier(identifier));
if (match) {
await match.addMetadata(metadata);
} else {
for (const source of this.sources) {
await source.addMetadata(metadata);
}
}
}
public async validate(representation: Representation): Promise<void> {
const identifier = { path: representation.metadata.identifier.value };
const source = this.getMatchingSource(identifier);
return source.validate(representation);
}
// Updated with new source typings
protected getMatchingSource(identifier: ResourceIdentifier): AuxiliaryStrategy {
return super.getMatchingSource(identifier) as AuxiliaryStrategy;
}
}