-
Notifications
You must be signed in to change notification settings - Fork 0
/
versioningBehavior.ts
90 lines (75 loc) · 2.88 KB
/
versioningBehavior.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
import { ContractDescription, ContractBehavior, BehaviorAnnotation } from "../description/contractDescription";
import { VersionMessageFilter } from "../dispatcher/versionMessageFilter";
import { VersionMessageInspector } from "../dispatcher/versionMessageInspector";
import { MessageFilter } from "../dispatcher/messageFilter";
import { EndpointDescription, EndpointBehavior } from "../description/endpointDescription";
import { DispatchEndpoint } from "../dispatcher/dispatchEndpoint";
/**
* Contract and endpoint behavior that allows an endpoint to be selected by version number as specified by the client.
* The version of the contract must be provided in [semver](http://semver.org/) format. The behavior can be applied to
* a contract using the [[Versioning]] decorator or to a specific endpoint by adding an instance of VersioningBehavior
* to the list of behaviors for the endpoint.
*
* <uml>
* hide members
* hide circle
* ContractBehavior <|.. VersioningBehavior
* EndpointBehavior <|.. VersioningBehavior
* </uml>
*
* ### Example
*
* ```typescript
* @Contract("Calculator")
* @Versioning({ version: "1.0.0" })
* export class CalculatorService {
* ...
* }
* ```
*/
export class VersioningBehavior implements ContractBehavior, EndpointBehavior, BehaviorAnnotation {
/**
* The name of the contract that is the target of the versioning behavior. This is required when the service has
* more than one contract defined.
*/
contract: string;
/**
* The version of the service contract in [semver](http://semver.org/) format.
*/
version: string;
/**
* Constructs a VersioningBehavior object.
* @param options Options for the behvaior.
*/
constructor(options: VersioningOptions) {
if(!options) {
throw new Error("Missing required argument 'options'.");
}
if(!options.version) {
throw new Error("Missing required argument 'options.version'.");
}
this.version = options.version;
this.contract = options.contract;
}
applyContractBehavior (description: ContractDescription, endpoint: DispatchEndpoint): void {
endpoint.filter = new VersionMessageFilter(this.version).and(endpoint.filter);
endpoint.messageInspectors.push(new VersionMessageInspector());
}
applyEndpointBehavior(description: EndpointDescription, endpoint: DispatchEndpoint): void {
this.applyContractBehavior(description.contract, endpoint);
}
}
/**
* Options for a [[VersioningBehavior]].
*/
export interface VersioningOptions {
/**
* The version of the service contract in [semver](http://semver.org/) format.
*/
version: string;
/**
* The name of the contract that is the target of the versioning behavior. This is required for when the service has
* more than one contract defined.
*/
contract?: string;
}