The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as HttpApi from "effect/http-api/HttpApi"; |
| 2 | import * as HttpApiEndpoint from "effect/http-api/HttpApiEndpoint"; |
| 3 | import * as HttpApiGroup from "effect/http-api/HttpApiGroup"; |
| 4 | import * as Schema from "effect/Schema"; |
| 5 | |
| 6 | /** |
| 7 | * One API, defined once. Each group is served by its own Worker (see |
| 8 | * `alchemy.run.ts`); clients only ever see `StoreApi` at a single URL. |
| 9 | */ |
| 10 | |
| 11 | export class Product extends Schema.Class<Product>("Product")({ |
| 12 | id: Schema.String, |
| 13 | name: Schema.String, |
| 14 | price: Schema.Number, |
| 15 | }) {} |
| 16 | |
| 17 | export class Order extends Schema.Class<Order>("Order")({ |
| 18 | id: Schema.String, |
| 19 | productId: Schema.String, |
| 20 | quantity: Schema.Number, |
| 21 | total: Schema.Number, |
| 22 | }) {} |
| 23 | |
| 24 | export class ProductNotFound extends Schema.TaggedError<ProductNotFound>()( |
| 25 | "ProductNotFound", |
| 26 | { id: Schema.String }, |
| 27 | { httpApiStatus: 404 }, |
| 28 | ) {} |
| 29 | |
| 30 | export class OrderNotFound extends Schema.TaggedError<OrderNotFound>()( |
| 31 | "OrderNotFound", |
| 32 | { id: Schema.String }, |
| 33 | { httpApiStatus: 404 }, |
| 34 | ) {} |
| 35 | |
| 36 | const Id = Schema.Struct({ id: Schema.String }); |
| 37 | |
| 38 | /** `GET /products/*` — read-only catalog. */ |
| 39 | export class ProductsGroup extends HttpApiGroup.make("products") |
| 40 | .add(HttpApiEndpoint.get("list", "/", { success: Schema.Array(Product) })) |
| 41 | .add(HttpApiEndpoint.get("get", "/:id", { params: Id, success: Product, error: ProductNotFound })) |
| 42 | .prefix("/products") {} |
| 43 | |
| 44 | /** `/orders/*` — places orders against the catalog. */ |
| 45 | export class OrdersGroup extends HttpApiGroup.make("orders") |
| 46 | .add( |
| 47 | HttpApiEndpoint.post("create", "/", { |
| 48 | payload: Schema.Struct({ productId: Schema.String, quantity: Schema.Number }), |
| 49 | success: Order, |
| 50 | error: ProductNotFound, |
| 51 | }), |
| 52 | ) |
| 53 | .add(HttpApiEndpoint.get("get", "/:id", { params: Id, success: Order, error: OrderNotFound })) |
| 54 | .prefix("/orders") {} |
| 55 | |
| 56 | /** `/admin/*` — catalog writes. */ |
| 57 | export class AdminGroup extends HttpApiGroup.make("admin") |
| 58 | .add( |
| 59 | HttpApiEndpoint.put("putProduct", "/products/:id", { |
| 60 | params: Id, |
| 61 | payload: Schema.Struct({ name: Schema.String, price: Schema.Number }), |
| 62 | success: Product, |
| 63 | }), |
| 64 | ) |
| 65 | .prefix("/admin") {} |
| 66 | |
| 67 | /** The public surface — what a client sees at the one URL. */ |
| 68 | export class StoreApi extends HttpApi.make("StoreApi") |
| 69 | .add(ProductsGroup) |
| 70 | .add(OrdersGroup) |
| 71 | .add(AdminGroup) {} |
| 72 | |
| 73 | /** Each Worker serves exactly one group. */ |
| 74 | export class ProductsApi extends HttpApi.make("ProductsApi").add(ProductsGroup) {} |
| 75 | export class OrdersApi extends HttpApi.make("OrdersApi").add(OrdersGroup) {} |
| 76 | export class AdminApi extends HttpApi.make("AdminApi").add(AdminGroup) {} |