/
package.ts
124 lines (116 loc) · 3.27 KB
/
package.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Interfaces and classes for package authors to use and extend.
*
* @module
*/
import { Result } from 'neverthrow';
import {
ComponentConfig,
GeneralConfig,
InfraConfig,
NetworkConfig,
RelationConfig,
ServiceConfig,
} from './config';
import { PlanError } from './domain';
/**
* Interface representing all resources included as part of the infra baseline generated by a package.
*
* It is expected to be implemented in a package to include **all* underlying resources which are to be augmented by custom code in a project using the package.
*
* @group For package authors
*/
export interface InfraPlanConstructs {}
/**
* Short-hand type representing outputs from the infra baseline generated by a package.
* It is commonly used for information (such as displaying) purposes.
*
* It should be used as-is. The `map` structure with `any` value offers flexibility.
*
* @group For both project creators and package authors
*/
export type InfraPlanOutputs = Map<string, any>;
/**
* Concrete class that carries both the generated infra baseline and all the outputs as a result.
*
* @sealed Usually there is no need to extend this class.
* @group For both project creators and package authors
*/
export class InfraPlan<IPC extends InfraPlanConstructs> {
constructor(
public readonly constructs: IPC,
public readonly outputs: InfraPlanOutputs
) {}
}
/**
* Abstract class to be extended by packages to generate infra baseline.
*
* @group For package authors
*/
export abstract class Planner<
IPC extends InfraPlanConstructs,
GC extends GeneralConfig,
NC extends NetworkConfig,
CC extends ComponentConfig,
SC extends ServiceConfig,
RC extends RelationConfig
> {
/**
* Provisioning infra according to the configuration.
*
* @param config The project configuration in full.
* @param scope Optional parent construct.
* @returns Result that encapsulates either the infra baseline {@link InfraPlan} if successful, or error {@link PlanError} if failed.
*/
abstract runWith(
config: InfraConfig<GC, NC, CC, SC, RC>,
scope: any
): Result<InfraPlan<IPC>, PlanError>;
}
/**
* Abstract class for packages to implement in order to handle outputs or errors from the result.
*
* @group For package authors
*/
export class ResultHandler {
/**
* The default handler.
*/
public static DEFAULT = new ResultHandler();
constructor(
/**
* Function to handle results.
*/
readonly onOk?: (outputs: InfraPlanOutputs) => void,
/**
* Function to to handle errors.
*/
readonly onErr?: (error: PlanError) => void
) {}
/**
* Function that handles infra provisioning result by delegating to `onErr` and `onOk` functions to be implemented.
*
* @param result The result of infra provisioning.
*/
handle(result?: Result<InfraPlanOutputs, PlanError>): void {
if (result) {
result
.map((outputs) => {
if (this.onOk) {
this.onOk(outputs);
}
})
.mapErr((error) => {
if (this.onErr) {
this.onErr(error);
} else {
throw new Error(error.message);
}
});
} else {
throw new Error(
'Unknown error when provisioning infra, please check your configurations!'
);
}
}
}