Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

1.5 KB
Raw Blame
import { Videos } from "@cap/web-backend";
import { HttpAuthMiddleware, Video } from "@cap/web-domain";
import {
	HttpApi,
	HttpApiBuilder,
	HttpApiEndpoint,
	HttpApiError,
	HttpApiGroup,
} from "@effect/platform";
import { Effect, Layer, Schema } from "effect";
import { apiToHandler } from "@/lib/server";

class Api extends HttpApi.make("Api").add(
	HttpApiGroup.make("root").add(
		HttpApiEndpoint.del("deleteVideo")`/api/video/delete`
			.setUrlParams(Schema.Struct({ videoId: Video.VideoId }))
			.middleware(HttpAuthMiddleware)
			.addError(HttpApiError.Forbidden)
			.addError(HttpApiError.NotFound),
	),
) {}

const ApiLive = HttpApiBuilder.api(Api).pipe(
	Layer.provide(
		HttpApiBuilder.group(Api, "root", (handlers) =>
			Effect.gen(function* () {
				const videos = yield* Videos;

				return handlers.handle("deleteVideo", ({ urlParams }) =>
					videos.delete(urlParams.videoId).pipe(
						Effect.catchTags({
							VideoNotFoundError: () =>
								Effect.fail(new HttpApiError.NotFound()),
							PolicyDenied: () => Effect.fail(new HttpApiError.Unauthorized()),
							DatabaseError: (e) =>
								Effect.logError(e).pipe(
									Effect.andThen(() =>
										Effect.fail(new HttpApiError.InternalServerError()),
									),
								),
							StorageError: (e) =>
								Effect.logError(e).pipe(
									Effect.andThen(() =>
										Effect.fail(new HttpApiError.InternalServerError()),
									),
								),
						}),
					),
				);
			}),
		),
	),
);

const handler = apiToHandler(ApiLive);

export const DELETE = handler;