-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
184 lines (160 loc) · 5.02 KB
/
index.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
// Copyright (C) 2019 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/aws-cdk-pure
//
import { App, Construct, Stack } from '@aws-cdk/core'
//
//
export type IaaC<A> = (parent: Construct) => A
export interface IPure<A> {
(parent: Construct): A
effect: (f: (x: A) => void) => IPure<A>
map: <B>(f: (x: A) => B) => IPure<B>
flatMap: <B>(f: (x: A) => IaaC<B>) => IPure<B>
}
/**
* Lifts IaaC type to IPure interface
*/
export function unit<A>(f: IaaC<A>): IPure<A> {
const pure: IPure<A> = f as IPure<A>
pure.effect = (eff: (x: A) => void) =>
unit(
(scope: any) => {
const node = f(scope)
eff(node)
return node
}
)
pure.map = <B>(fmap: (x: A) => B) =>
unit((scope: any) => fmap(f(scope)))
pure.flatMap = <B>(fmap: (x: A) => IaaC<B>) =>
unit((scope: any) => fmap(f(scope))(scope))
return pure
}
//
//
type Node<Prop, Type> = new (scope: Construct, id: string, props: Prop) => Type
/**
* type safe cloud component factory. It takes a class constructor of "cloud component"
* as input and returns another function, which builds a type-safe association between
* "cloud component" and its property.
*
* @param f "cloud component" class constructor
* @param pure purely functional definition of the component
*/
export function iaac<Prop, Type>(f: Node<Prop, Type>): (pure: IaaC<Prop>, name?: string) => IPure<Type> {
return (pure, name) => unit(
(scope) => new f(scope, name || pure.name, pure(scope))
)
}
//
//
type Wrap<Prop, TypeA, TypeB> = new (scope: TypeA, props?: Prop) => TypeB
/**
* type safe cloud component factory for integrations
*
* @param f "cloud component" class constructor
* @param pure purely functional definition of the component
*/
export function wrap<Prop, TypeA, TypeB>(f: Wrap<Prop, TypeA, TypeB>): (pure: IaaC<TypeA>) => IPure<TypeB> {
return (pure) => unit(
(scope) => new f(pure(scope))
)
}
//
//
type Include<Prop, Type> = (scope: Construct, id: string, props: Prop) => Type
/**
* type safe cloud component factory. It takes a fromXXX lookup function of "cloud component"
* as input and returns another function, which builds a type-safe association between
* "cloud component" and its property.
*
* @param f lookup function
* @param pure purely functional definition of the component
*/
export function include<Prop, Type>(f: Include<Prop, Type>): (pure: IaaC<Prop>, name?: string) => IPure<Type> {
return (pure, name) => unit(
(scope) => f(scope, name || pure.name, pure(scope))
)
}
//
//
type Product<T> = {[K in keyof T]: IaaC<T[K]>}
type Pairs<T> = {[K in keyof T]: T[K]}
export interface IEffect<T extends Pairs<T>> {
(parent: Construct): T
effect: (f: (x: T) => void) => IEffect<T>
flatMap: <B>(f: (x: T) => Product<B>) => IEffect<T & B>
yield: <K extends keyof T>(k: K) => IPure<T[K]>
}
function effect<T>(f: IaaC<T>): IEffect<T> {
const pure: IEffect<T> = f as IEffect<T>
pure.flatMap = <B>(fmap: (x: T) => Product<B>) =>
effect(
(scope) => {
const node = f(scope)
const object = fmap(node)
const value = {} as B
const keys = Reflect.ownKeys(object) as (keyof B)[]
for (const key of keys) {
value[key] = object[key](scope)
}
return { ...node, ...value }
}
)
pure.effect = (eff: (x: T) => void) =>
effect(
(scope) => {
const node = f(scope)
eff(node)
return node
}
)
pure.yield = <K extends keyof T>(k: K) => unit((node) => f(node)[k])
return pure
}
function compose<T extends Pairs<T>>(product: Product<T>): IaaC<Pairs<T>> {
return (scope) => {
const value = {} as T
const keys = Reflect.ownKeys(product) as (keyof T)[]
for (const key of keys) {
value[key] = product[key](scope)
}
return value
}
}
/**
* The effect is a type-class that operates with product of individual `IaaC<T>`.
* It implements methods to apply effects to product of "cloud components" and
* yields the result back. The effect function operates with pure types `T`.
* The effect returns always `IaaC<T>`.
*
* @param resources product of `IaaC<T>` components
*/
export function use<T extends Pairs<T>>(resources: Product<T>): IEffect<T> {
return effect(compose(resources))
}
/**
* attaches the pure definition of resource to the stack nodes
*
* @param scope the "parent" context
* @param iaac purely functional definition of the component
*/
export function join<T>(scope: Construct, fn: IaaC<T>): T {
const x = fn(scope) as any
return (typeof x === 'function') ? join(scope, x) : x
}
/**
* Attaches the pure stack components to the root of CDK application.
*
* @param root the root of an entire CDK application
* @param iaac purely functional definition of the stack
* @param name optionally the logical of the stack
*/
export function root<T>(scope: App, fn: IaaC<T>, name?: string): App {
fn(new Stack(scope, name || fn.name))
return scope
}