import { TECH_LOG_FEATURE_ID, type TechLogFeatureInput, } from "../application/tech-log-feature-input.ts"; import type { Asset } from "../contracts/studio/contract.ts"; import { createTechLogStudioApplication } from "../application/studio-application.ts"; import type { InstalledHttpOperationExecutor } from "../../../adapters/http/index.ts"; import { createTechLogHttpBinding } from "./http/tech-log-http-binding.ts"; import type { CsrfTokenProvider } from "./http/studio-session-csrf.ts"; import { createHttpPublicContentGateway } from "./http/http-public-content-gateway.ts"; import { deferPublicContentQueries, deferStudioGateways, } from "./deferred-tech-log-ports.ts"; /** * The HTTP gateway builds no headers of its own — `Idempotency-Key` comes * from the execution intent and credentials (including CSRF) come from the * platform's `attachCredentials` collaborator at the composition root. This * context only has to say which adapter to construct and hand it the * composed contract executor. * * CSRF has exactly one provider per Studio session, owned by the composition * root — it is shared with the platform's credential collaborator, so it is * threaded in here rather than constructed locally. */ export type TechLogInstallContext = Readonly<{ studioSource: "MOCK" | "HTTP"; publicSource: "MOCK" | "HTTP"; http: InstalledHttpOperationExecutor; apiBaseUrl: string; requestTimeoutMs: number; csrf: CsrfTokenProvider; }>; export function createTechLogFeatureInstalledInput( context: TechLogInstallContext, ) { // Fix round 1 (I2). MOCK `studioSource` has no real backend behind either // gateway, so — unlike HTTP, where the real server is shared, external // authority for both — the mock document gateway and a mock asset gateway // must share ONE in-memory Asset store, or the mock's own `validateDocument` // could never learn about an Asset the Picker just loaded/uploaded. This map // is scoped to this one `createTechLogFeatureInstalledInput` call (the // composition root calls it once per app instance), so every // `createStudioGateway`/`createStudioAssetGateway` pair produced from this // same `input` — across every `StudioProvider` mount over the app's // lifetime — shares it, the same way a real backend's asset store outlives // any one editing session. It is never read on the HTTP branch. const mockAssets = new Map(); const http = createTechLogHttpBinding(context.http); // The public read source switches independently of Studio: the two are // different services, and the combination that matters today is an authoring // backend that is live while the public read API is not. // // FE-GATE-012. Both sources ship in every build (the runtime document picks // one), so the MOCK content is loaded on its first query instead of riding // the initial JavaScript of a production page that reads HTTP. const publicContent = context.publicSource === "MOCK" ? deferPublicContentQueries( async () => (await import("./static/public-query.ts")).publicContentQueries, ) : createHttpPublicContentGateway({ http }); // MOCK: one in-memory Studio state per application, shared by the document // and management gateways, so deleting a draft from the management surface // also takes it out of the document list. Management used to be HTTP even // here, and a MOCK build (local development, the e2e and visual builds) // asked an API that is not there for its topics and projects. // // FE-GATE-012. Studio's gateways, MOCK or HTTP, are a chunk of their own. // StudioProvider calls this when Studio mounts, so the chunk starts loading // then, next to the page chunk, rather than on the first query. Each call // still gets its own set (and, for MOCK, its own state). const createStudioApplication = () => createTechLogStudioApplication( deferStudioGateways( async () => context.studioSource === "MOCK" ? (await import("./mock/mock-studio-gateways.ts")).createMockStudioGateways( mockAssets, ) : (await import("./http/http-studio-gateways.ts")).createHttpStudioGateways({ http, csrf: context.csrf, apiBaseUrl: context.apiBaseUrl, requestTimeoutMs: context.requestTimeoutMs, }), { startNow: true }, ), ); const input: TechLogFeatureInput = Object.freeze({ publicContent, createStudioApplication, }); return Object.freeze({ featureId: TECH_LOG_FEATURE_ID, input, }); }