sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ Neon/ fixtures/ backend-http-worker.ts

import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Neon from "@alchemy.run/neon";
import * as Effect from "effect/Effect";
import * as FetchHttpClient from "effect/http/FetchHttpClient";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import * as Layer from "effect/Layer";
import * as Redacted from "effect/Redacted";
import { backendAuth, backendBranch, backendDataApi, backendGateway } from "./backend-resources.ts";

const bindings = Layer.mergeAll(
  Neon.ConnectAuthHttp,
  Neon.QueryDataApiHttp,
  Neon.QueryAIGatewayHttp,
).pipe(Layer.provide(FetchHttpClient.layer));

export default class BackendHttpWorker extends Cloudflare.Worker<BackendHttpWorker>()(
  "BackendHttpWorker",
  {
    main: import.meta.url,
  },
  Effect.gen(function* () {
    const auth = yield* Neon.ConnectAuth(backendAuth);
    const data = yield* Neon.QueryDataApi(backendDataApi);
    const ai = yield* Neon.QueryAIGateway(backendGateway);
    const shared = yield* Neon.QueryAIGateway(
      Effect.gen(function* () {
        return yield* Neon.AIGateway("SharedGateway", {
          branch: yield* backendBranch,
        });
      }),
    );
    return {
      fetch: Effect.gen(function* () {
        return yield* HttpServerResponse.json({
          authUrl: yield* auth.baseUrl,
          jwksUrl: yield* auth.jwksUrl,
          dataUrl: yield* data.baseUrl,
          aiUrl: yield* ai.baseUrl,
          hasToken: Redacted.value(yield* ai.token).length > 0,
          sharedToken: Redacted.value(yield* ai.token) === Redacted.value(yield* shared.token),
        });
      }),
    };
  }).pipe(Effect.provide(bindings)),
) {}