|
| 1 | +import { |
| 2 | + CurrentUser, |
| 3 | + HttpAuthMiddleware, |
| 4 | + Loom, |
| 5 | + Workflows, |
| 6 | +} from "@cap/web-domain"; |
| 7 | +import { |
| 8 | + HttpApi, |
| 9 | + HttpApiBuilder, |
| 10 | + HttpApiEndpoint, |
| 11 | + HttpApiError, |
| 12 | + HttpApiGroup, |
| 13 | +} from "@effect/platform"; |
| 14 | +import { Effect, Layer, Option, Schema } from "effect"; |
| 15 | +import { apiToHandler } from "@/lib/server"; |
| 16 | + |
| 17 | +export const revalidate = "force-dynamic"; |
| 18 | + |
| 19 | +class Api extends HttpApi.make("CapWebApi") |
| 20 | + .add( |
| 21 | + HttpApiGroup.make("root").add( |
| 22 | + HttpApiEndpoint.post("loom")`/import-video` |
| 23 | + .setPayload( |
| 24 | + Schema.Struct({ |
| 25 | + loom: Schema.Struct({ |
| 26 | + downloadUrl: Schema.String, |
| 27 | + videoId: Schema.String, |
| 28 | + }), |
| 29 | + }), |
| 30 | + ) |
| 31 | + .middleware(HttpAuthMiddleware) |
| 32 | + .addError(HttpApiError.InternalServerError), |
| 33 | + ), |
| 34 | + ) |
| 35 | + .prefix("/api/loom") {} |
| 36 | + |
| 37 | +const ApiLive = HttpApiBuilder.api(Api).pipe( |
| 38 | + Layer.provide( |
| 39 | + HttpApiBuilder.group(Api, "root", (handlers) => |
| 40 | + handlers.handle( |
| 41 | + "loom", |
| 42 | + Effect.fn( |
| 43 | + function* ({ payload }) { |
| 44 | + const { workflows } = yield* Workflows.HttpClient; |
| 45 | + const user = yield* CurrentUser; |
| 46 | + |
| 47 | + yield* workflows.LoomImportVideo({ |
| 48 | + payload: { |
| 49 | + cap: { |
| 50 | + userId: user.id, |
| 51 | + orgId: user.activeOrgId, |
| 52 | + }, |
| 53 | + loom: { |
| 54 | + userId: "loomVideoId123", |
| 55 | + orgId: "loomOrgId123", |
| 56 | + video: { |
| 57 | + id: payload.loom.videoId, |
| 58 | + name: "loom video name", |
| 59 | + downloadUrl: payload.loom.downloadUrl, |
| 60 | + width: Option.none(), |
| 61 | + height: Option.none(), |
| 62 | + durationSecs: Option.none(), |
| 63 | + fps: Option.none(), |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }); |
| 68 | + }, |
| 69 | + (e) => |
| 70 | + e.pipe( |
| 71 | + Effect.tapDefect(Effect.log), |
| 72 | + Effect.catchAll(() => new HttpApiError.InternalServerError()), |
| 73 | + ), |
| 74 | + ), |
| 75 | + ), |
| 76 | + ), |
| 77 | + ), |
| 78 | +); |
| 79 | + |
| 80 | +const { handler } = apiToHandler(ApiLive); |
| 81 | + |
| 82 | +export const GET = handler; |
| 83 | +export const HEAD = handler; |
| 84 | +export const POST = handler; |
0 commit comments