pluto-lang / pluto

Pluto provides a unified programming interface that allows you to seamlessly tap into cloud capabilities and develop your cloud and AI applications.
https://pluto-lang.vercel.app
Apache License 2.0
87 stars 8 forks source link

Transfer the values generated by compile-time to runtime. #102

Open jianzs opened 11 months ago

jianzs commented 11 months ago

https://github.com/pluto-lang/pluto/blob/main/docs/documentation/design/capture-value.en.md

jianzs commented 10 months ago

After #120, this feature becomes available on the cloud platform. However, it's not yet supported in the simulation environment. A redesign of simulator is necessary to accommodate for simulation needs.

This is an example that leverages this feature, identical to the app-with-prop-access located in the testapps.

import { Router, Tester, HttpRequest, HttpResponse } from "@plutolang/pluto";

const router = new Router("router");

router.get("/echo", async (req: HttpRequest): Promise<HttpResponse> => {
  const message = req.query["message"] ?? "Hello, Pluto!";
  return {
    statusCode: 200,
    body: message,
  };
});

const tester = new Tester("e2e");

tester.it("test echo", async () => {
  // Verify the correctness of business logic.
  const res = await fetch(router.url + "/echo?message=Hello%20Pluto!");
  const body = await res.text();
  if (res.status !== 200) {
    throw new Error(`Unexpected status code: ${res.status}`);
  }
  if (body !== "Hello Pluto!") {
    throw new Error(`Unexpected body: ${body}`);
  }
});