1import * as Neon from "alchemy/Neon";
2import * as Output from "alchemy/Output";
3import * as Effect from "effect/Effect";
4import { resources } from "./resources.ts";
5
6export const website = Effect.fn(function* (api: Neon.Function) {
7 const { project, branch, uploads, auth, publicAssets } = yield* resources;
8 const web = yield* Neon.Website.Vite("Web", {
9 branch,
10 rootDir: "./web",
11 env: { VITE_API_URL: api.url, VITE_NEON_AUTH_URL: auth.baseUrl },
12 assets: { notFoundHandling: "single-page-application" },
13 });
14 if (!web.url)
15 return yield* Effect.die(new Error("The website did not return a URL"));
16 const siteUrl =
17 typeof web.url === "string" ? Output.literal(web.url) : web.url;
18 const origin = Output.map(
19 siteUrl,
20 (url: string | undefined) => new URL(url ?? "").origin,
21 );
22 yield* Neon.AuthTrustedDomain("WebOrigin", { auth, domain: origin });
23 return {
24 url: web.url,
25 apiUrl: api.url,
26 authUrl: auth.baseUrl,
27 projectId: project.projectId,
28 branchId: branch.branchId,
29 bucketName: uploads.bucketName,
30 publicBucketName: publicAssets.bucketName,
31 };
32});