forked from HydraCG/Heracles.ts
/
OperationsCollection.ts
108 lines (94 loc) · 3.39 KB
/
OperationsCollection.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { hydra } from "../../namespaces";
import { IClass } from "../IClass";
import { IOperation } from "../IOperation";
import ResourceFilterableCollection from "./ResourceFilterableCollection";
/**
* Provides a collection of {@link IOperation} that can be filtered with relevant criteria.
* @class
*/
export default class OperationsCollection extends ResourceFilterableCollection<IOperation> {
/**
* Defines an empty operations collection.
* @constant {OperationsCollection}
*/
public static readonly empty = new OperationsCollection();
/**
* Initializes a new instance of the {@link OperationsCollection}
* class with initial collections of operations to filter.
* @param {Iterable<IOperation>} [operations] Initial collection of operations to filter.
*/
public constructor(operations?: Iterable<IOperation>) {
super(operations);
}
/**
* Obtains a collection of operations using a given method.
* @param {string} method Method to match.
* @returns {OperationsCollection}
*/
public ofMethod(method: string): OperationsCollection {
let result: OperationsCollection = this;
if (typeof method === "string" && method.length > 0) {
result = this.narrowFiltersWith("method", method) as OperationsCollection;
}
return result;
}
/**
* Obtains a collection of operations expecting a given type.
* @param {string} iri Expected type.
* @returns {OperationsCollection}
*/
public expecting(iri: string): OperationsCollection {
let result: OperationsCollection = this;
if (typeof iri === "string" && iri.length > 0) {
result = this.narrowFiltersWith<IClass>("expects", value => value.iri === iri) as OperationsCollection;
}
return result;
}
/**
* Obtains a collection of operations returning a given type.
* @param {string} iri Returned type.
* @returns {OperationsCollection}
*/
public returning(iri: string): OperationsCollection {
let result: OperationsCollection = this;
if (typeof iri === "string" && iri.length > 0) {
result = this.narrowFiltersWith<IClass>("returns", value => value.iri === iri) as OperationsCollection;
}
return result;
}
/**
* Obtains a collection of operations expecting a heaer.
* @param {string} name Expected header name.
* @returns {OperationsCollection}
*/
public expectingHeader(name: string): OperationsCollection {
let result: OperationsCollection = this;
if (typeof name === "string" && name.length > 0) {
result = this.narrowFiltersWith("expectedHeaders", name) as OperationsCollection;
}
return result;
}
/**
* Obtains a collection of operations returning a header.
* @param {string} name Returned header name.
* @returns {OperationsCollection}
*/
public returningHeader(name: string): OperationsCollection {
let result: OperationsCollection = this;
if (typeof name === "string" && name.length > 0) {
result = this.narrowFiltersWith("returnedHeaders", name) as OperationsCollection;
}
return result;
}
/**
* Obtains a collection of operations being an hydra:IriTemplate.
* @returns {OperationsCollection}
*/
public withTemplate(): OperationsCollection {
return this.ofType(hydra.IriTemplate) as OperationsCollection;
}
/** @inheritdoc */
protected createInstance(items: Iterable<IOperation>): OperationsCollection {
return new OperationsCollection(items);
}
}