import type { Container } from "@webiny/di"; import { registerBuildParams, registerExtensions } from "@webiny/handler"; import { GraphQLEngineFeature } from "@webiny/api-graphql"; import { ApiCoreFeature, RequestStorage } from "@webiny/api-core"; import { WcpLicenseLoader } from "@webiny/api-core/features/wcp/WcpLicenseLoader.js"; import { HeadlessCmsFeature } from "@webiny/api-headless-cms"; import { AcoHcmsFeature } from "@webiny/api-headless-cms-aco"; import { HcmsTasksFeature } from "@webiny/api-headless-cms-tasks"; import { CmsWorkflowsFeature } from "@webiny/api-headless-cms-workflows"; import { CmsSchedulerFeature } from "@webiny/api-headless-cms-scheduler"; import { MailerFeature } from "@webiny/api-mailer"; import { RecordLockingAppFeature } from "@webiny/api-record-locking"; import { AuditLogsFeature } from "@webiny/api-audit-logs"; import { DashboardAppFeature } from "@webiny/api-dashboard"; import { AcoFeature } from "@webiny/api-aco"; import { BackgroundTasksFeature } from "@webiny/background-tasks/api"; import { FileManagerAppFeature } from "@webiny/api-file-manager"; import { FileManagerAcoFeature } from "@webiny/api-file-manager-aco"; import { WebsiteBuilderFeature } from "@webiny/api-website-builder"; import { WebsiteBuilderWorkflowsFeature } from "@webiny/api-website-builder-workflows"; import { WebsiteBuilderSchedulerFeature } from "@webiny/api-website-builder-scheduler"; import { WebsocketsFeature } from "@webiny/api-websockets"; import { WorkflowsFeature } from "@webiny/api-workflows"; import { SchedulerFeature } from "@webiny/api-scheduler"; /** Installs a hosting-specific transport adapter into the per-request container at its interleave point. */ export type TransportRegistrar = (container: Container) => void | Promise; export interface RegisterApiRequestStackConfig { /** * Project-defined extensions, applied at register() time (so extension features — including * code-defined CMS models — are registered before anything lists/caches the model set). */ extensions: () => Parameters[1]; /** * Hosting-specific transport adapters, each installed at its exact interleave point in the stack. * Every entry follows the same shape: it runs immediately AFTER the transport-agnostic domain * Feature has registered its NULL default, and overrides that default (nearest-container-last-wins) * with the real adapter. Each is optional — omit one for a deployment/transport that lacks that * capability. AWS supplies AWS adapters (API Gateway Management API / EventBridge / S3); the * standalone server supplies in-process adapters (server WebSockets / in-process scheduler / local disk). */ transports?: { /** * Hosting-specific replacements for api-core defaults, run right after `ApiCoreFeature`: * `PublicUrls` (default: every URL unknown), `CdnCache` (default: nothing to invalidate), and * `EncryptionConfig` / `HashingConfig` (default: read from build params). */ hostingConfig?: TransportRegistrar; /** * Real-time (WebSockets) transport, run right after `WebsocketsFeature`. Overrides the * NullWebsocketsTransport, otherwise every server→client send() is a silent no-op. */ realtime?: TransportRegistrar; /** * Scheduler transport, run AFTER `SchedulerFeature` and BEFORE `CmsSchedulerFeature`. */ scheduler?: TransportRegistrar; /** * File-manager storage transport, run AFTER `FileManagerAppFeature` (which registers the * AssetDeliveryRoute + NULL asset-delivery impls). Overrides those nulls with real impls and * adds the file-operation features. */ fileManager?: TransportRegistrar; }; } /** * Registers the transport-AGNOSTIC Webiny API request stack: every domain feature + the GraphQL * engine, in the correct (order-sensitive) sequence. This is shared by all transport composition * packages (`@webiny/api-composition-aws`, and the future `-server`) so the feature set and its * ordering live in ONE place and can't drift between transports. * * Transport- and storage-specific pieces are injected via the hooks in {@link RegisterApiRequestStackConfig} * at their exact interleave points. The caller is responsible for the ROOT-container transport wiring * (HTTP/event transport, auth/tenant loaders, identity provider, DB + storage) before dispatch reaches * this per-request stack. That includes the `Logger`: api-core registers none, so a root without one * fails the first time anything that logs is built. * * ORDER IS LOAD-BEARING — do not reorder. Notably: extensions must be applied before anything that * lists + caches the per-request model set; the GraphQL engine must be registered last. */ export async function registerApiRequestStack( container: Container, config: RegisterApiRequestStackConfig ): Promise { // Refresh the WCP license BEFORE any feature registers — this is the single per-request refresh. // register()-time feature-flag checks (e.g. the private-files gate) and WcpContext.canUse* both // read it via the process cache (`WcpLicenseProvider.get()` / `WcpLicenseLoader.getCached()`). Runs here // — the shared request stack both hosting types call — so no handler wires it. Process-cached // (~5-min TTL) + single-flighted → cheap no-op on warm requests. await WcpLicenseLoader.load(); // ── Storage (before ANY feature) ─────────────────────────── // Every storage that registers per request put a `RequestStorage` in the root; each registers // its storages here, in this request's container. First, because `ApiCoreFeature` resolves the // core storage while it registers. A storage that keeps its storages in the root (the DynamoDB // variants) registers none, and this does nothing. for (const storage of container.resolveAll(RequestStorage)) { await storage.register(container); } // ── Build params (before ANY feature) ────────────────────── // Features gate themselves on FeatureFlags at register() time (AuditLogs, RecordLocking, // Workflows, ...), and FeatureFlags is a BuildParam. Registered with the rest of the extensions // below, it arrived too late: those gates read `{}`, and a license-granted feature couldn't be // disabled from config. const extensions = config.extensions(); await registerBuildParams(container, extensions); // ── Core API (per-request: EventPublisher + tenant/identity/request contexts must bind to the // request child container so per-request event handlers are resolvable) ───────── ApiCoreFeature.register(container, { wcpLicense: undefined }); await config.transports?.hostingConfig?.(container); // ── CMS ──────────────────────────────────────────────────── HeadlessCmsFeature.register(container, { type: "manage" }); AcoFeature.register(container); AcoHcmsFeature.register(container); HcmsTasksFeature.register(container); // ── File Manager (domain) + storage transport ────────────── FileManagerAppFeature.register(container); FileManagerAcoFeature.register(container); await config.transports?.fileManager?.(container); // ── Website Builder ──────────────────────────────────────── WebsiteBuilderFeature.register(container); WebsiteBuilderWorkflowsFeature.register(container); WebsiteBuilderSchedulerFeature.register(container); // ── Websockets (domain) + real-time transport ────────────── WebsocketsFeature.register(container); await config.transports?.realtime?.(container); // ── Supporting services ──────────────────────────────────── MailerFeature.register(container); RecordLockingAppFeature.register(container, {}); AuditLogsFeature.register(container, {}); DashboardAppFeature.register(container); BackgroundTasksFeature.register(container); // ── Workflows ────────────────────────────────────────────── WorkflowsFeature.register(container); CmsWorkflowsFeature.register(container); // ── Scheduler + scheduler transport ──────────────────────── SchedulerFeature.register(container); await config.transports?.scheduler?.(container); CmsSchedulerFeature.register(container); // ── Extensions ───────────────────────────────────────────── // Apply at register() time so extension features — including code-defined CMS models // (ModelFactory), e.g. Languages — are registered before anything populates ModelCache. Anything // reaching the model set through GetModel/ListModels (which go via ModelsFetcher -> // ModelCache.getOrSet) caches it for the rest of the request, so a set built before extensions // register would be missing their models. Every consumer now resolves models at schema-build or // resolver time, i.e. after all registration. await registerExtensions(container, extensions); // ── GraphQL engine (always last) ─────────────────────────── GraphQLEngineFeature.register(container); }