Skip to content

Commit 9a301cc

Browse files
author
eladcon
authored
feat(eventbridge)!: new eventbridge api (#122)
* Optional name property * changed `subscribeFunction` to `onEvent` * `putEvents` is now variadic
1 parent fa129e9 commit 9a301cc

File tree

10 files changed

+31
-25
lines changed

10 files changed

+31
-25
lines changed

eventbridge/README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,36 @@ bring eventbridge;
2020

2121
let bus = new eventbridge.Bus(name: "my-bus");
2222

23-
bus.subscribeFunction("github.pull-request.created", inflight (event) => {
23+
bus.onEvent("github.pull-request.created", inflight (event) => {
2424
log("subscribed event received {Json.stringify(event)}");
2525
}, {
2626
"detail-type": [{"prefix": "pull-request."}],
2727
"source": ["github.com"],
2828
});
2929

3030
new cloud.Function(inflight () => {
31-
bus.putEvents([{
31+
bus.putEvents({
3232
detailType: "pull-request.created",
3333
resources: ["test"],
3434
source: "github.com",
3535
version: "0",
3636
detail: {
3737
"test": "test",
3838
},
39-
}]);
39+
});
4040
});
4141
```
4242

4343
## Parameters
4444

4545
* eventBridgeName - `str` - Optional. Name of an existing EventBridge to reference.
4646

47+
#### Usage
48+
49+
```sh
50+
wing compile -t @winglang/platform-awscdk -v eventBridgeName="my-bus" main.w
51+
```
52+
4753
## License
4854

4955
This library is licensed under the [MIT License](./LICENSE).

eventbridge/lib.test.w

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class InboundGithubEvents {
1313
this.bucket = new cloud.Bucket();
1414
let counter = new cloud.Counter();
1515

16-
eventBridge.subscribeFunction("github.pull-request.created", inflight (event) => {
16+
eventBridge.onEvent("github.pull-request.created", inflight (event) => {
1717
log("subscribed event received {Json.stringify(event)}");
1818
this.bucket.put("test-{counter.inc()}", Json.stringify(event));
1919
}, {
@@ -46,15 +46,15 @@ let env = new Environments();
4646

4747
test "publish to eventbridge" {
4848
log("publishing to eventbridge");
49-
eventBridge.putEvents([{
49+
eventBridge.putEvents({
5050
detailType: "pull-request.created",
5151
resources: ["test"],
5252
source: "github.com",
5353
version: "0",
5454
detail: {
5555
"test": "test",
5656
},
57-
}]);
57+
});
5858

5959
log("published");
6060

@@ -73,15 +73,15 @@ test "publish to eventbridge" {
7373

7474
expect.equal(0, env.bucket.list().length);
7575

76-
eventBridge.putEvents([{
76+
eventBridge.putEvents({
7777
detailType: "myTest.check",
7878
resources: ["test"],
7979
source: "myTest",
8080
version: "0",
8181
detail: {
8282
"fake": "env",
8383
},
84-
}]);
84+
});
8585

8686
log("published 2nd event");
8787

eventbridge/lib.w

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ bring "./platform/awscdk" as awscdk;
1111
pub class Bus impl types.IBus {
1212
inner: types.IBus;
1313

14-
new(props: types.BusProps) {
14+
new(props: types.BusProps?) {
1515
let target = util.env("WING_TARGET");
1616
if target == "sim" {
1717
this.inner = new sim.Bus(props) as "sim";
@@ -24,12 +24,12 @@ pub class Bus impl types.IBus {
2424
}
2525
}
2626

27-
pub inflight putEvents(events: Array<types.PublishEvent>): void {
27+
pub inflight putEvents(...events: Array<types.PublishEvent>): void {
2828
this.inner.putEvents(events);
2929
}
3030

31-
pub subscribeFunction(name: str, handler: inflight (types.Event): void, pattern: Json): void {
32-
this.inner.subscribeFunction(name, handler, pattern);
31+
pub onEvent(name: str, handler: inflight (types.Event): void, pattern: Json): void {
32+
this.inner.onEvent(name, handler, pattern);
3333
}
3434
pub subscribeQueue(name: str, queue: cloud.Queue, pattern: Json): void {
3535
this.inner.subscribeQueue(name, queue, pattern);

eventbridge/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eventbridge/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@winglibs/eventbridge",
33
"description": "Amazon EventBridge library for Wing",
4-
"version": "0.0.4",
4+
"version": "0.1.0",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/winglang/winglibs.git",

eventbridge/platform/awscdk/eventbridge.w

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ pub class Bus impl types.IBus {
99

1010
eventBridge: cdk.aws_events.IEventBus;
1111

12-
new(props: types.BusProps) {
12+
new(props: types.BusProps?) {
1313
let app = nodeof(this).app;
1414
// TODO: use typed properties when its available
1515
if let eventBridgeName = app.parameters.value("eventBridgeName") {
1616
this.eventBridge = cdk.aws_events.EventBus.fromEventBusName(this, "EventBridge", eventBridgeName);
1717
} else {
18-
this.eventBridge = new cdk.aws_events.EventBus(eventBusName: props.name) as "EventBridge";
18+
this.eventBridge = new cdk.aws_events.EventBus(eventBusName: props?.name ?? "eventbridge-{this.node.addr.substring(0, 8)}") as "EventBridge";
1919
}
2020
}
2121

22-
pub subscribeFunction(name: str, handler: inflight (types.Event): void, pattern: Json): void {
22+
pub onEvent(name: str, handler: inflight (types.Event): void, pattern: Json): void {
2323
// event will be json of type `types.Event`
2424
let funk = new cloud.Function(inflight (event) => {
2525
// since wing structs don't supoort custom serialization we need to do it manually

eventbridge/platform/sim/bus.w

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub class EventBridgeBus {
77
topic: cloud.Topic;
88
var handlerCount: num;
99

10-
new(props: types.BusProps) {
10+
new(props: types.BusProps?) {
1111
this.topic = new cloud.Topic() as "EventBridge";
1212

1313
this.handlerCount = 0;

eventbridge/platform/sim/eventbridge.w

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ bring "./bus.w" as bus;
55
pub class Bus impl types.IBus {
66
bus: bus.EventBridgeBus;
77

8-
new(props: types.BusProps) {
8+
new(props: types.BusProps?) {
99
this.bus = new bus.EventBridgeBus(props);
1010
}
1111

12-
pub subscribeFunction(name: str, handler: inflight (types.Event): void, pattern: Json): void {
12+
pub onEvent(name: str, handler: inflight (types.Event): void, pattern: Json): void {
1313
class FnRule {
1414
new(bus: bus.EventBridgeBus) {
1515
let onMessageHandler = bus.subscribe(inflight (event) => {

eventbridge/platform/tfaws/eventbridge.w

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub class Bus impl types.IBus {
99
busName: str;
1010
busArn: str;
1111

12-
new(props: types.BusProps) {
12+
new(props: types.BusProps?) {
1313
let app = nodeof(this).app;
1414
// TODO: use typed properties when its available
1515
if let eventBridgeName = app.parameters.value("eventBridgeName") {
@@ -19,13 +19,13 @@ pub class Bus impl types.IBus {
1919
this.busName = bus.name;
2020
this.busArn = bus.arn;
2121
} else {
22-
let bus = new tfAws.cloudwatchEventBus.CloudwatchEventBus(name: props.name) as "EventBridge";
22+
let bus = new tfAws.cloudwatchEventBus.CloudwatchEventBus(name: props?.name ?? "eventbridge-{this.node.addr.substring(0, 8)}") as "EventBridge";
2323
this.busName = bus.name;
2424
this.busArn = bus.arn;
2525
}
2626
}
2727

28-
pub subscribeFunction(name: str, handler: inflight (types.Event): void, pattern: Json): void {
28+
pub onEvent(name: str, handler: inflight (types.Event): void, pattern: Json): void {
2929
let rule = new tfAws.cloudwatchEventRule.CloudwatchEventRule(
3030
name: name,
3131
eventBusName: this.busName,

eventbridge/types.w

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ pub struct PublishEvent {
2727

2828
pub interface IBus extends std.IResource {
2929
inflight putEvents(events: Array<PublishEvent>): void;
30-
subscribeFunction(name: str, handler: inflight (Event): void, pattern: Json): void;
30+
onEvent(name: str, handler: inflight (Event): void, pattern: Json): void;
3131
subscribeQueue(name: str, queue: cloud.Queue, pattern: Json): void;
3232
}

0 commit comments

Comments
 (0)