A missing capability reached the feature factory as an anonymous TypeError. The composer now names the feature and the capability, and the IndexedDB provider example moves from a browser spec into an opt-in recipe with a test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
209 lines
6.7 KiB
TypeScript
209 lines
6.7 KiB
TypeScript
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 extends FeatureIndexedDbRepositoryId>(
|
|
repositoryId: RepositoryId,
|
|
): FeatureIndexedDbRepositories[RepositoryId];
|
|
}>;
|
|
|
|
export function createIndexedDbRepositoryProvider(
|
|
repositories: Readonly<Partial<FeatureIndexedDbRepositories>>,
|
|
): IndexedDbRepositoryProvider {
|
|
const snapshot = Object.freeze({ ...repositories });
|
|
return Object.freeze({
|
|
get<RepositoryId extends FeatureIndexedDbRepositoryId>(
|
|
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<FeaturePlatformCapabilities, Needs[number]>;
|
|
|
|
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<Needs>,
|
|
): Readonly<{
|
|
featureId: FeatureId;
|
|
input: ApplicationFeatureInputs[FeatureId];
|
|
}>;
|
|
}>;
|
|
|
|
export type FeatureAdapterContribution<
|
|
FeatureId extends ApplicationFeatureId,
|
|
Needs extends readonly FeaturePlatformCapabilityId[],
|
|
> = FeatureAdapterContributionDefinition<FeatureId, Needs> &
|
|
Readonly<{
|
|
[FEATURE_ADAPTER_CONTRIBUTION]: true;
|
|
}>;
|
|
|
|
export function defineFeatureAdapterContribution<
|
|
const FeatureId extends ApplicationFeatureId,
|
|
const Needs extends readonly FeaturePlatformCapabilityId[],
|
|
>(
|
|
contribution: FeatureAdapterContributionDefinition<FeatureId, Needs>,
|
|
): FeatureAdapterContribution<FeatureId, Needs> {
|
|
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> =
|
|
Contribution extends Readonly<{
|
|
needs: readonly (infer Capability)[];
|
|
}>
|
|
? Extract<Capability, FeaturePlatformCapabilityId>
|
|
: never;
|
|
|
|
export type ContributionsCapabilityContext<
|
|
Contributions extends readonly ContributionShape[],
|
|
> = Pick<
|
|
FeaturePlatformCapabilities,
|
|
ContributionNeeds<Contributions[number]>
|
|
>;
|
|
|
|
export type InstalledFeatureInputs = Readonly<
|
|
Partial<ApplicationFeatureInputs>
|
|
>;
|
|
|
|
function setFeatureInput<FeatureId extends ApplicationFeatureId>(
|
|
target: Partial<ApplicationFeatureInputs>,
|
|
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<FeatureId, Needs>,
|
|
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<Contributions>,
|
|
): InstalledFeatureInputs {
|
|
const enabled = new Set<string>(enabledFeatureIds);
|
|
const seen = new Set<string>();
|
|
const result: Partial<ApplicationFeatureInputs> = {};
|
|
|
|
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<Record<FeaturePlatformCapabilityId, unknown>>
|
|
>;
|
|
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);
|
|
}
|