/** * DI-native Webiny API handler for the AWS Lambda transport — storage-agnostic BASE. * * The ROOT container wires the AWS transport (API Gateway HTTP + auth/tenant loaders, background-task * and WebSocket Lambda invocations, storage). Everything that is not * transport-specific — identity providers, storage, and the transport-AGNOSTIC per-request * feature stack — lives in `composition/`, shared with the response-streaming handler * (`createWebinyStreamApiHandler`) so the two roots cannot drift. The storage variant, database * included, is injected via `registerRootStorage` by a thin variant package * (`@webiny/api-composition-aws-ddb`, `-aws-ddb-os`). Keeping the wiring in real packages (not an app * template) is what makes it unit/integration testable. */ import { createLambdaHandler, ApiGatewayFeature } from "@webiny/api-kernel-aws"; import { ApiGatewayIdentityLoaderDecorator } from "~/handlers/ApiGatewayIdentityLoaderDecorator.js"; import { ApiGatewayTenantLoaderDecorator } from "~/handlers/ApiGatewayTenantLoaderDecorator.js"; import { ApiGatewayAssumePermissionsDecorator } from "~/handlers/ApiGatewayAssumePermissionsDecorator.js"; import { registerInboundInputTypes, registerWebinyApiChild, registerWebinyApiRoot } from "~/composition/index.js"; import type { WebinyApiCompositionConfig } from "~/composition/index.js"; export type CreateWebinyApiHandlerConfig = WebinyApiCompositionConfig; export function createWebinyApiHandler(config: CreateWebinyApiHandlerConfig) { return createLambdaHandler({ root: async container => { // ── Transport ────────────────────────────────────────────── // ApiGatewayFeature registers the HTTP transport (event type + router + HttpFeature). ApiGatewayFeature.register(container); // ── Tenant + auth (extract → shared load) ────────────────── // These decorators depend on api-core (RequestTenantLoader/RequestIdentityLoader), so // they live in this composition layer, not api-kernel-aws. registerDecorator applies // LATER registrations as the OUTER wrapper (whose execute() runs first). TENANT must be // established before IDENTITY: API-key authentication resolves the key by tenant partition // (ApiKeysRepository reads TenantContext.getTenant()), so identity establishment depends on // the tenant. The reverse is not true — RequestTenantLoader has no identity dependency. So // register identity first (inner) and tenant last (outer) → tenant runs, then identity, // then the router. container.registerDecorator(ApiGatewayIdentityLoaderDecorator); container.registerDecorator(ApiGatewayTenantLoaderDecorator); /* * Preview-as. Outermost, so the header lands in RawAssumePermissions before anything * can ask for permissions. Order is not load-bearing beyond that: the holder is read * lazily, and AssumedPermissions checks the caller's real permissions before it * honours the header. */ container.registerDecorator(ApiGatewayAssumePermissionsDecorator); // Every non-HTTP invocation shape (background tasks, EventBridge, scheduled actions, // WebSockets) with its handler. Kept in one function so the set is testable and an // inbound transport can't be half-wired. registerInboundInputTypes(container); await registerWebinyApiRoot(container, config); }, child: async container => { await registerWebinyApiChild(container, config); } }); }