winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
4.75k stars 186 forks source link

rfc: generate TypeScript clients #6614

Open MarkMcCulloh opened 3 weeks ago

MarkMcCulloh commented 3 weeks ago

For https://github.com/winglang/wing/issues/6538

Rendered RFC

By submitting this pull request, I confirm that my contribution is made under the terms of the Wing Cloud Contribution License.

github-actions[bot] commented 3 weeks ago

Thanks for opening this pull request! :tada: Please consult the contributing guidelines for details on how to contribute to this project. If you need any assistance, don't hesitate to ping the relevant owner over Discord.

Topic Owner
Wing SDK and utility APIs @chriscbr
Wing Console @ainvoner, @skyrpex, @polamoros
JSON, structs, primitives and collections @hasanaburayyan
Platforms and plugins @hasanaburayyan
Frontend resources (website, react, etc) @tsuf239
Language design @chriscbr
VSCode extension and language server @markmcculloh
Compiler architecture, inflights, lifting @yoav-steinberg
Wing Testing Framework @tsuf239
Wing CLI @markmcculloh
Build system, dev environment, releases @markmcculloh
Library Ecosystem @chriscbr
Documentation @hasanaburayyan
SDK test suite @tsuf239
Examples @hasanaburayyan
Wing Playground @eladcon
MarkMcCulloh commented 3 weeks ago

Closing for now to rework

skyrpex commented 1 week ago

Here's an additional use case: I'd like to use Wing code for inflights, but I'd like to use the AWS CDK constructs.

bring "aws-cdk-lib" as cdk;
bring "@aws-sdk/client" as sdk;

// This is a Wing construct wrapping a CDK Bucket.
class CustomBucket {
   bucket: cdk.aws_s3.Bucket;

   new() {
      this.bucket = new cdk.aws_s3.Bucket();
   }

   inflight client: sdk.Client;

   inflight new() {
      this.client = new sdk.S3();
   }

   inflight putObject(key: string, body: string) {
      this.client.send(new PutObject(
         Bucket: this.bucket.bucketName,
         Key: key,
         Body: body,
      ));
   }
}

// This is a Wing construct wrapping a CDK Lambda.
class CustomFunction {
   func: cdk.aws_lambda.Function;

   new(handler: inflight (): any) {
      // This lift method will generate a compiled JS file for `handler` along with its permissions.
      let code = lift(handler);

      // Here we create a CDK Lambda and manually bundle the generated JS code for the `handler` inflight.
      this.func = new cdk.aws_lambda.Function(
         code: bundleWithEsbuild(
            filename: code.filename,
            handler: code.handler,
         ),
      );

      // Here we can generate the CDK policies by iterating the permission objects.
      for permission in code.permissions {
         // In this example, the `handler` inflight only interacted with our Bucket class.
         permission.resource; // typeof Bucket
         permission.action; // "putObject"

         // new cdk.aws_iam.PolicyStatement(...);
      }
   }
}

let bucket = new CustomBucket();
new CustomFunction(inflight () => {
   bucket.putObject("hello.txt", "world!");
});

Long story short, I'm asking for a built-in lift(handler: inflight (...any):any)) method that gives me both a JS file or tree, and the resources&methods that the handler interacted with. Developers will be able to easily integrate with any cloud provider. AFAIK there's currently no way to use Wing for inflights without using cloud.Function.