Skip to content

Commit

Permalink
chore(sdk): deprecate makeHandler() and convertBetweenHandlers() (#…
Browse files Browse the repository at this point in the history
…6354)

We can now use `lift()` and `inflight()` in TypeScript instead of `makeHandler()` and `convertBetweenHandlers()`.

This is a great exercise for our TypeScript inflight support (and it's awesome :-)).

## Checklist

- [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [x] Docs updated (only required for features)
- [x] Added `pr/e2e-full` label if this feature requires end-to-end testing

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
eladb authored May 1, 2024
1 parent 2546cb9 commit 9883ece
Show file tree
Hide file tree
Showing 146 changed files with 3,582 additions and 3,963 deletions.
385 changes: 12 additions & 373 deletions docs/docs/04-standard-library/aws/api-reference.md

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions docs/docs/04-standard-library/cloud/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,51 @@ The response's status code.
---
### CorsHeaders <a name="CorsHeaders" id="@winglang/sdk.cloud.CorsHeaders"></a>
Type definition for CORS headers which includes default and options headers.
#### Initializer <a name="Initializer" id="@winglang/sdk.cloud.CorsHeaders.Initializer"></a>
```wing
bring cloud;

let CorsHeaders = cloud.CorsHeaders{ ... };
```
#### Properties <a name="Properties" id="Properties"></a>
| **Name** | **Type** | **Description** |
| --- | --- | --- |
| <code><a href="#@winglang/sdk.cloud.CorsHeaders.property.defaultResponse">defaultResponse</a></code> | <code>MutMap&lt;str&gt;</code> | Default CORS response headers. |
| <code><a href="#@winglang/sdk.cloud.CorsHeaders.property.optionsResponse">optionsResponse</a></code> | <code>MutMap&lt;str&gt;</code> | CORS options response headers. |
---
##### `defaultResponse`<sup>Required</sup> <a name="defaultResponse" id="@winglang/sdk.cloud.CorsHeaders.property.defaultResponse"></a>
```wing
defaultResponse: MutMap<str>;
```
- *Type:* MutMap&lt;str&gt;
Default CORS response headers.
---
##### `optionsResponse`<sup>Required</sup> <a name="optionsResponse" id="@winglang/sdk.cloud.CorsHeaders.property.optionsResponse"></a>
```wing
optionsResponse: MutMap<str>;
```
- *Type:* MutMap&lt;str&gt;
CORS options response headers.
---
## Protocols <a name="Protocols" id="Protocols"></a>
### IApiEndpointHandler <a name="IApiEndpointHandler" id="@winglang/sdk.cloud.IApiEndpointHandler"></a>
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/04-standard-library/cloud/service.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ Inflight client for `IServiceHandler`.
##### `handle` <a name="handle" id="@winglang/sdk.cloud.IServiceHandlerClient.handle"></a>

```wing
handle(): IServiceStopHandler?
inflight handle(): IServiceStopHandler?
```

Handler to run when the service starts.
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/04-standard-library/sim/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ Convert this resource to a resource schema for the simulator.
##### `get` <a name="get" id="@winglang/sdk.sim.IStateClient.get"></a>

```wing
get(key: str): Json
inflight get(key: str): Json
```

Gets the runtime state of this object.
Expand All @@ -340,7 +340,7 @@ The object's key.
##### `set` <a name="set" id="@winglang/sdk.sim.IStateClient.set"></a>

```wing
set(key: str, value: Json): void
inflight set(key: str, value: Json): void
```

Sets the state of runtime a runtime object.
Expand All @@ -364,7 +364,7 @@ The object's value.
##### `tryGet` <a name="tryGet" id="@winglang/sdk.sim.IStateClient.tryGet"></a>

```wing
tryGet(key: str): Json?
inflight tryGet(key: str): Json?
```

Checks if runtime state exists for this object and returns it's value.
Expand Down
1 change: 0 additions & 1 deletion libs/awscdk/src/api.onrequest.inflight.ts

This file was deleted.

2 changes: 1 addition & 1 deletion libs/awscdk/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export class Api extends cloud.Api implements IAwsApi {
): cloud.Function {
let handler = this.handlers[inflight._id];
if (!handler) {
const newInflight = ApiEndpointHandler.toFunctionHandler(inflight, Api.renderCorsHeaders(this.corsOptions));
const newInflight = ApiEndpointHandler.toFunctionHandler(inflight, Api.renderCorsHeaders(this.corsOptions)?.defaultResponse);
const prefix = `${method.toLowerCase()}${path.replace(/\//g, "_")}_}`;
handler = new cloud.Function(
this,
Expand Down
17 changes: 0 additions & 17 deletions libs/awscdk/src/bucket.onevent.inflight.ts

This file was deleted.

68 changes: 21 additions & 47 deletions libs/awscdk/src/bucket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { join } from "path";
import { RemovalPolicy } from "aws-cdk-lib";
import {
BlockPublicAccess,
Expand All @@ -11,15 +10,14 @@ import { LambdaDestination } from "aws-cdk-lib/aws-s3-notifications";
import { Construct } from "constructs";
import { App } from "./app";
import { cloud, core, std } from "@winglang/sdk";
import { convertBetweenHandlers } from "@winglang/sdk/lib/shared/convert";
import { calculateBucketPermissions } from "@winglang/sdk/lib/shared-aws/permissions";
import { IAwsBucket } from "@winglang/sdk/lib/shared-aws/bucket";
import {
IAwsCdkFunction,
addPolicyStatements,
isAwsCdkFunction,
} from "./function";
import { LiftMap } from "@winglang/sdk/lib/core";
import { LiftMap, lift } from "@winglang/sdk/lib/core";

const EVENTS = {
[cloud.BucketEventType.DELETE]: EventType.OBJECT_REMOVED,
Expand Down Expand Up @@ -56,20 +54,24 @@ export class Bucket extends cloud.Bucket implements IAwsBucket {
}
}

protected eventHandlerLocation(): string {
return join(__dirname, "bucket.onevent.inflight.js");
}

private onEventFunction(
event: string,
event: cloud.BucketEventType,
inflight: cloud.IBucketEventHandler,
opts?: cloud.BucketOnCreateOptions
): IAwsCdkFunction {
const functionHandler = convertBetweenHandlers(
inflight,
this.eventHandlerLocation(),
`BucketEventHandlerClient`
);
const functionHandler = lift({ handler: inflight, eventType: event }).inflight(async (ctx, event) => {
const record = event.Records[0];
if (!record) {
throw new Error("No record found in the S3 event");
}
try {
await ctx.handler(record.s3.object.key, ctx.eventType);
} catch (error) {
//TODO: change to some sort of warning- console.warn doesn't seems to work
console.log("Error parsing the notification event message: ", error);
console.log("Event: ", event);
}
});

const fn = new cloud.Function(
this.node.scope!, // ok since we're not a tree root
Expand Down Expand Up @@ -110,7 +112,7 @@ export class Bucket extends cloud.Bucket implements IAwsBucket {
inflight: cloud.IBucketEventHandler,
opts?: cloud.BucketOnCreateOptions
): void {
const fn = this.onEventFunction("OnCreate", inflight, opts);
const fn = this.onEventFunction(cloud.BucketEventType.CREATE, inflight, opts);

std.Node.of(this).addConnection({
source: this,
Expand All @@ -128,7 +130,7 @@ export class Bucket extends cloud.Bucket implements IAwsBucket {
inflight: cloud.IBucketEventHandler,
opts?: cloud.BucketOnDeleteOptions
): void {
const fn = this.onEventFunction("OnDelete", inflight, opts);
const fn = this.onEventFunction(cloud.BucketEventType.DELETE, inflight, opts);

std.Node.of(this).addConnection({
source: this,
Expand All @@ -146,7 +148,7 @@ export class Bucket extends cloud.Bucket implements IAwsBucket {
inflight: cloud.IBucketEventHandler,
opts?: cloud.BucketOnUpdateOptions
): void {
const fn = this.onEventFunction("OnUpdate", inflight, opts);
const fn = this.onEventFunction(cloud.BucketEventType.UPDATE, inflight, opts);

std.Node.of(this).addConnection({
source: this,
Expand All @@ -164,37 +166,9 @@ export class Bucket extends cloud.Bucket implements IAwsBucket {
inflight: cloud.IBucketEventHandler,
opts?: cloud.BucketOnEventOptions
) {
const fn = this.onEventFunction("OnEvent", inflight, opts);

std.Node.of(this).addConnection({
source: this,
target: fn,
name: "onCreate()",
});
this.bucket.addEventNotification(
EVENTS[cloud.BucketEventType.CREATE],
new LambdaDestination(fn.awscdkFunction)
);

std.Node.of(this).addConnection({
source: this,
target: fn,
name: "onDelete()",
});
this.bucket.addEventNotification(
EVENTS[cloud.BucketEventType.DELETE],
new LambdaDestination(fn.awscdkFunction)
);

std.Node.of(this).addConnection({
source: this,
target: fn,
name: "onUpdate()",
});
this.bucket.addEventNotification(
EVENTS[cloud.BucketEventType.UPDATE],
new LambdaDestination(fn.awscdkFunction)
);
this.onCreate(inflight, opts);
this.onDelete(inflight, opts);
this.onUpdate(inflight, opts);
}

public onLift(host: std.IInflightHost, ops: string[]): void {
Expand Down
4 changes: 0 additions & 4 deletions libs/awscdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
export * from "./platform";
export * from "./app";
export * from "./bucket";
export * from "./bucket.onevent.inflight";
export * from "./counter";
export * from "./counter.inflight";
export * from "./endpoint";
export * from "./function";
export * from "./queue";
export * from "./queue.setconsumer.inflight";
export * from "./schedule";
export * from "./schedule.ontick.inflight";
export * from "./secret";
export * from "./topic";
export * from "./topic.onmessage.inflight";
export * from "./website";
export * from "./on-deploy";
export * from "./test-runner";
Expand Down
1 change: 0 additions & 1 deletion libs/awscdk/src/queue.setconsumer.inflight.ts

This file was deleted.

1 change: 0 additions & 1 deletion libs/awscdk/src/schedule.ontick.inflight.ts

This file was deleted.

1 change: 0 additions & 1 deletion libs/awscdk/src/topic.onmessage.inflight.ts

This file was deleted.

Loading

0 comments on commit 9883ece

Please sign in to comment.