import type { ApplicationFeatureId, ApplicationFeatureInputs, } from "../application/ports/in/application-api.ts"; import type { InstalledHttpOperationExecutor, } from "../adapters/http/index.ts"; import type { TelemetryPort } from "../platform/telemetry/telemetry-port.ts"; /** * Features augment this registry with the repository identities they consume. * The provider is a composition capability; feature use cases never import it. */ export interface FeatureIndexedDbRepositories {} export type FeatureIndexedDbRepositoryId = Extract< keyof FeatureIndexedDbRepositories, string >; export type IndexedDbRepositoryProvider = Readonly<{ get( repositoryId: RepositoryId, ): FeatureIndexedDbRepositories[RepositoryId]; }>; export function createIndexedDbRepositoryProvider( repositories: Readonly>, ): IndexedDbRepositoryProvider { const snapshot = Object.freeze({ ...repositories }); return Object.freeze({ get( repositoryId: RepositoryId, ): FeatureIndexedDbRepositories[RepositoryId] { const repository = snapshot[repositoryId]; if (!repository) { throw new TypeError( `IndexedDB repository is not installed: ${repositoryId}`, ); } return repository as FeatureIndexedDbRepositories[RepositoryId]; }, }); } export type FeaturePlatformCapabilities = Readonly<{ http: InstalledHttpOperationExecutor; indexedDb: IndexedDbRepositoryProvider; /** Spec ยง24. Provider-neutral, bounded, fail-open feature telemetry. */ telemetry: TelemetryPort; }>; export type FeaturePlatformCapabilityId = keyof FeaturePlatformCapabilities; export type FeatureCapabilitySelection< Needs extends readonly FeaturePlatformCapabilityId[], > = Pick; const FEATURE_ADAPTER_CONTRIBUTION = Symbol( "feature-adapter-contribution", ); type FeatureAdapterContributionDefinition< FeatureId extends ApplicationFeatureId, Needs extends readonly FeaturePlatformCapabilityId[], > = Readonly<{ featureId: FeatureId; needs: Needs; createInput( context: FeatureCapabilitySelection, ): Readonly<{ featureId: FeatureId; input: ApplicationFeatureInputs[FeatureId]; }>; }>; export type FeatureAdapterContribution< FeatureId extends ApplicationFeatureId, Needs extends readonly FeaturePlatformCapabilityId[], > = FeatureAdapterContributionDefinition & Readonly<{ [FEATURE_ADAPTER_CONTRIBUTION]: true; }>; export function defineFeatureAdapterContribution< const FeatureId extends ApplicationFeatureId, const Needs extends readonly FeaturePlatformCapabilityId[], >( contribution: FeatureAdapterContributionDefinition, ): FeatureAdapterContribution { return Object.freeze({ ...contribution, needs: Object.freeze([...contribution.needs]) as unknown as Needs, [FEATURE_ADAPTER_CONTRIBUTION]: true as const, }); } type ContributionShape = Readonly<{ [FEATURE_ADAPTER_CONTRIBUTION]: true; featureId: ApplicationFeatureId; needs: readonly FeaturePlatformCapabilityId[]; createInput: (...args: never[]) => unknown; }>; type ContributionNeeds = Contribution extends Readonly<{ needs: readonly (infer Capability)[]; }> ? Extract : never; export type ContributionsCapabilityContext< Contributions extends readonly ContributionShape[], > = Pick< FeaturePlatformCapabilities, ContributionNeeds >; export type InstalledFeatureInputs = Readonly< Partial >; function setFeatureInput( target: Partial, featureId: FeatureId, input: ApplicationFeatureInputs[FeatureId], ): void { const writable = target as Record< ApplicationFeatureId, ApplicationFeatureInputs[ApplicationFeatureId] >; writable[featureId] = input; } function invokeContribution< FeatureId extends ApplicationFeatureId, Needs extends readonly FeaturePlatformCapabilityId[], >( contribution: FeatureAdapterContribution, context: FeaturePlatformCapabilities, ) { return contribution.createInput(context); } /** * Dynamic catalog assembly stays centralized here. Type authority lives at each * feature contribution; the only dynamic write is isolated in setFeatureInput. */ export function composeFeatureAdapterInputs< const Contributions extends readonly ContributionShape[], >( contributions: Contributions, enabledFeatureIds: readonly string[], context: ContributionsCapabilityContext, ): InstalledFeatureInputs { const enabled = new Set(enabledFeatureIds); const seen = new Set(); const result: Partial = {}; for (const contribution of contributions) { if (contribution[FEATURE_ADAPTER_CONTRIBUTION] !== true) { throw new TypeError( "Feature adapter contribution must be defined through defineFeatureAdapterContribution()", ); } // Q-6.9-6. Route codecs and message catalogs throw on a duplicate; this // registry silently let the later contribution win, so a feature copied // without renaming its id replaced the original's API. if (seen.has(contribution.featureId)) { throw new TypeError( `duplicate feature adapter contribution: ${contribution.featureId}`, ); } seen.add(contribution.featureId); if (!enabled.has(contribution.featureId)) continue; // 09-05. The context type already asks for every declared need, but a // host that widens it (or JavaScript) used to reach the feature's factory // and fail there as an anonymous TypeError. const provided = context as Readonly< Partial> >; const missing = contribution.needs.find( (need) => provided[need] === undefined, ); if (missing !== undefined) { throw new TypeError( `feature ${contribution.featureId} needs ${missing}, which the host did not provide`, ); } const installed = invokeContribution( contribution as unknown as FeatureAdapterContribution< ApplicationFeatureId, readonly FeaturePlatformCapabilityId[] >, context as FeaturePlatformCapabilities, ); if (installed.featureId !== contribution.featureId) { throw new TypeError( `feature adapter contribution ${contribution.featureId} installed an input for ${String(installed.featureId)}`, ); } setFeatureInput(result, installed.featureId, installed.input); } return Object.freeze(result); }