diff --git a/eventbridge/README.md b/eventbridge/README.md index f24c94e8..35d69289 100644 --- a/eventbridge/README.md +++ b/eventbridge/README.md @@ -20,7 +20,7 @@ bring eventbridge; let bus = new eventbridge.Bus(name: "my-bus"); -bus.subscribeFunction("github.pull-request.created", inflight (event) => { +bus.onEvent("github.pull-request.created", inflight (event) => { log("subscribed event received {Json.stringify(event)}"); }, { "detail-type": [{"prefix": "pull-request."}], @@ -28,7 +28,7 @@ bus.subscribeFunction("github.pull-request.created", inflight (event) => { }); new cloud.Function(inflight () => { - bus.putEvents([{ + bus.putEvents({ detailType: "pull-request.created", resources: ["test"], source: "github.com", @@ -36,7 +36,7 @@ new cloud.Function(inflight () => { detail: { "test": "test", }, - }]); + }); }); ``` @@ -44,6 +44,12 @@ new cloud.Function(inflight () => { * eventBridgeName - `str` - Optional. Name of an existing EventBridge to reference. +#### Usage + +```sh +wing compile -t @winglang/platform-awscdk -v eventBridgeName="my-bus" main.w +``` + ## License This library is licensed under the [MIT License](./LICENSE). diff --git a/eventbridge/lib.test.w b/eventbridge/lib.test.w index 5fe6833d..3e607059 100644 --- a/eventbridge/lib.test.w +++ b/eventbridge/lib.test.w @@ -13,7 +13,7 @@ class InboundGithubEvents { this.bucket = new cloud.Bucket(); let counter = new cloud.Counter(); - eventBridge.subscribeFunction("github.pull-request.created", inflight (event) => { + eventBridge.onEvent("github.pull-request.created", inflight (event) => { log("subscribed event received {Json.stringify(event)}"); this.bucket.put("test-{counter.inc()}", Json.stringify(event)); }, { @@ -46,7 +46,7 @@ let env = new Environments(); test "publish to eventbridge" { log("publishing to eventbridge"); - eventBridge.putEvents([{ + eventBridge.putEvents({ detailType: "pull-request.created", resources: ["test"], source: "github.com", @@ -54,7 +54,7 @@ test "publish to eventbridge" { detail: { "test": "test", }, - }]); + }); log("published"); @@ -73,7 +73,7 @@ test "publish to eventbridge" { expect.equal(0, env.bucket.list().length); - eventBridge.putEvents([{ + eventBridge.putEvents({ detailType: "myTest.check", resources: ["test"], source: "myTest", @@ -81,7 +81,7 @@ test "publish to eventbridge" { detail: { "fake": "env", }, - }]); + }); log("published 2nd event"); diff --git a/eventbridge/lib.w b/eventbridge/lib.w index 737f64ed..0d7180d5 100644 --- a/eventbridge/lib.w +++ b/eventbridge/lib.w @@ -11,7 +11,7 @@ bring "./platform/awscdk" as awscdk; pub class Bus impl types.IBus { inner: types.IBus; - new(props: types.BusProps) { + new(props: types.BusProps?) { let target = util.env("WING_TARGET"); if target == "sim" { this.inner = new sim.Bus(props) as "sim"; @@ -24,12 +24,12 @@ pub class Bus impl types.IBus { } } - pub inflight putEvents(events: Array): void { + pub inflight putEvents(...events: Array): void { this.inner.putEvents(events); } - pub subscribeFunction(name: str, handler: inflight (types.Event): void, pattern: Json): void { - this.inner.subscribeFunction(name, handler, pattern); + pub onEvent(name: str, handler: inflight (types.Event): void, pattern: Json): void { + this.inner.onEvent(name, handler, pattern); } pub subscribeQueue(name: str, queue: cloud.Queue, pattern: Json): void { this.inner.subscribeQueue(name, queue, pattern); diff --git a/eventbridge/package-lock.json b/eventbridge/package-lock.json index 7c874dd7..774bbb2e 100644 --- a/eventbridge/package-lock.json +++ b/eventbridge/package-lock.json @@ -1,12 +1,12 @@ { "name": "@winglibs/eventbridge", - "version": "0.0.2", + "version": "0.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@winglibs/eventbridge", - "version": "0.0.2", + "version": "0.1.0", "license": "MIT", "devDependencies": { "@cdktf/provider-aws": "^18.2.0", diff --git a/eventbridge/package.json b/eventbridge/package.json index 8949205f..91161b3c 100644 --- a/eventbridge/package.json +++ b/eventbridge/package.json @@ -1,7 +1,7 @@ { "name": "@winglibs/eventbridge", "description": "Amazon EventBridge library for Wing", - "version": "0.0.4", + "version": "0.1.0", "repository": { "type": "git", "url": "https://github.com/winglang/winglibs.git", diff --git a/eventbridge/platform/awscdk/eventbridge.w b/eventbridge/platform/awscdk/eventbridge.w index 517a54eb..eb0f1fc6 100644 --- a/eventbridge/platform/awscdk/eventbridge.w +++ b/eventbridge/platform/awscdk/eventbridge.w @@ -9,17 +9,17 @@ pub class Bus impl types.IBus { eventBridge: cdk.aws_events.IEventBus; - new(props: types.BusProps) { + new(props: types.BusProps?) { let app = nodeof(this).app; // TODO: use typed properties when its available if let eventBridgeName = app.parameters.value("eventBridgeName") { this.eventBridge = cdk.aws_events.EventBus.fromEventBusName(this, "EventBridge", eventBridgeName); } else { - this.eventBridge = new cdk.aws_events.EventBus(eventBusName: props.name) as "EventBridge"; + this.eventBridge = new cdk.aws_events.EventBus(eventBusName: props?.name ?? "eventbridge-{this.node.addr.substring(0, 8)}") as "EventBridge"; } } - pub subscribeFunction(name: str, handler: inflight (types.Event): void, pattern: Json): void { + pub onEvent(name: str, handler: inflight (types.Event): void, pattern: Json): void { // event will be json of type `types.Event` let funk = new cloud.Function(inflight (event) => { // since wing structs don't supoort custom serialization we need to do it manually diff --git a/eventbridge/platform/sim/bus.w b/eventbridge/platform/sim/bus.w index ba3cd07a..f31e9a8a 100644 --- a/eventbridge/platform/sim/bus.w +++ b/eventbridge/platform/sim/bus.w @@ -7,7 +7,7 @@ pub class EventBridgeBus { topic: cloud.Topic; var handlerCount: num; - new(props: types.BusProps) { + new(props: types.BusProps?) { this.topic = new cloud.Topic() as "EventBridge"; this.handlerCount = 0; diff --git a/eventbridge/platform/sim/eventbridge.w b/eventbridge/platform/sim/eventbridge.w index 2833c53a..29a08dce 100644 --- a/eventbridge/platform/sim/eventbridge.w +++ b/eventbridge/platform/sim/eventbridge.w @@ -5,11 +5,11 @@ bring "./bus.w" as bus; pub class Bus impl types.IBus { bus: bus.EventBridgeBus; - new(props: types.BusProps) { + new(props: types.BusProps?) { this.bus = new bus.EventBridgeBus(props); } - pub subscribeFunction(name: str, handler: inflight (types.Event): void, pattern: Json): void { + pub onEvent(name: str, handler: inflight (types.Event): void, pattern: Json): void { class FnRule { new(bus: bus.EventBridgeBus) { let onMessageHandler = bus.subscribe(inflight (event) => { diff --git a/eventbridge/platform/tfaws/eventbridge.w b/eventbridge/platform/tfaws/eventbridge.w index f2bcdaf0..c239f519 100644 --- a/eventbridge/platform/tfaws/eventbridge.w +++ b/eventbridge/platform/tfaws/eventbridge.w @@ -9,7 +9,7 @@ pub class Bus impl types.IBus { busName: str; busArn: str; - new(props: types.BusProps) { + new(props: types.BusProps?) { let app = nodeof(this).app; // TODO: use typed properties when its available if let eventBridgeName = app.parameters.value("eventBridgeName") { @@ -19,13 +19,13 @@ pub class Bus impl types.IBus { this.busName = bus.name; this.busArn = bus.arn; } else { - let bus = new tfAws.cloudwatchEventBus.CloudwatchEventBus(name: props.name) as "EventBridge"; + let bus = new tfAws.cloudwatchEventBus.CloudwatchEventBus(name: props?.name ?? "eventbridge-{this.node.addr.substring(0, 8)}") as "EventBridge"; this.busName = bus.name; this.busArn = bus.arn; } } - pub subscribeFunction(name: str, handler: inflight (types.Event): void, pattern: Json): void { + pub onEvent(name: str, handler: inflight (types.Event): void, pattern: Json): void { let rule = new tfAws.cloudwatchEventRule.CloudwatchEventRule( name: name, eventBusName: this.busName, diff --git a/eventbridge/types.w b/eventbridge/types.w index ecfa82c2..5ba96877 100644 --- a/eventbridge/types.w +++ b/eventbridge/types.w @@ -27,6 +27,6 @@ pub struct PublishEvent { pub interface IBus extends std.IResource { inflight putEvents(events: Array): void; - subscribeFunction(name: str, handler: inflight (Event): void, pattern: Json): void; + onEvent(name: str, handler: inflight (Event): void, pattern: Json): void; subscribeQueue(name: str, queue: cloud.Queue, pattern: Json): void; }