| 1 | import * as Schema from "effect/Schema"; |
| 2 | import * as HttpApi from "effect/http-api/HttpApi"; |
| 3 | import * as HttpApiEndpoint from "effect/http-api/HttpApiEndpoint"; |
| 4 | import * as HttpApiGroup from "effect/http-api/HttpApiGroup"; |
| 5 | import * as HttpApiSchema from "effect/http-api/HttpApiSchema"; |
| 6 | import { Note, NotesList } from "./BackendApi"; |
| 7 | |
| 8 | export class Hello extends Schema.Class<Hello>("Hello")({ |
| 9 | message: Schema.String, |
| 10 | noteCount: Schema.Number, |
| 11 | }) {} |
| 12 | |
| 13 | export class KvValue extends Schema.Class<KvValue>("KvValue")({ |
| 14 | value: Schema.NullOr(Schema.String), |
| 15 | }) {} |
| 16 | |
| 17 | export class KvOk extends Schema.Class<KvOk>("KvOk")({ |
| 18 | ok: Schema.Boolean, |
| 19 | }) {} |
| 20 | |
| 21 | export class FrontendApi extends HttpApi.make("FrontendApi") |
| 22 | .add( |
| 23 | HttpApiGroup.make("frontend", { topLevel: true }).add( |
| 24 | HttpApiEndpoint.get("hello", "/hello", { |
| 25 | success: Hello, |
| 26 | }), |
| 27 | HttpApiEndpoint.get("listNotes", "/notes", { |
| 28 | success: NotesList, |
| 29 | }), |
| 30 | HttpApiEndpoint.post("createNote", "/notes", { |
| 31 | payload: Schema.Struct({ |
| 32 | title: Schema.String, |
| 33 | }), |
| 34 | success: Note.pipe(HttpApiSchema.status(201)), |
| 35 | }), |
| 36 | HttpApiEndpoint.delete("deleteNote", "/notes/:id", { |
| 37 | params: Schema.Struct({ |
| 38 | id: Schema.String, |
| 39 | }), |
| 40 | success: HttpApiSchema.NoContent, |
| 41 | }), |
| 42 | HttpApiEndpoint.get("getKv", "/kv/:key", { |
| 43 | params: Schema.Struct({ |
| 44 | key: Schema.String, |
| 45 | }), |
| 46 | success: KvValue, |
| 47 | }), |
| 48 | HttpApiEndpoint.put("putKv", "/kv/:key", { |
| 49 | params: Schema.Struct({ |
| 50 | key: Schema.String, |
| 51 | }), |
| 52 | payload: Schema.String.pipe(HttpApiSchema.asText()), |
| 53 | success: KvOk.pipe(HttpApiSchema.status(201)), |
| 54 | }), |
| 55 | ), |
| 56 | ) |
| 57 | .prefix("/api") {} |