87 lines
2.6 KiB
TypeScript
87 lines
2.6 KiB
TypeScript
export type FeatureContractIdentity = Readonly<{ featureId: string }>;
|
|
|
|
type EmptyRecord = Readonly<Record<never, never>>;
|
|
|
|
/**
|
|
* Zero-feature products are a supported topology. The fallback contract keeps
|
|
* every composer's property access valid without pretending that any feature
|
|
* route, operation, schema, mapper, or invalidation edge exists.
|
|
*/
|
|
export type EmptyFeatureContract = FeatureContractIdentity &
|
|
Readonly<{
|
|
routes: EmptyRecord;
|
|
routeRuntimeContracts: EmptyRecord;
|
|
apiOperations: EmptyRecord;
|
|
invalidation: Readonly<{
|
|
topics: readonly [];
|
|
namespaces: readonly [];
|
|
edges: readonly [];
|
|
}>;
|
|
topicVersions: readonly [];
|
|
schemas: EmptyRecord;
|
|
runtimeSchemas: EmptyRecord;
|
|
mappers: EmptyRecord;
|
|
}>;
|
|
|
|
export type FeatureMessageCatalogs = Readonly<
|
|
Record<string, Readonly<Record<string, string>>>
|
|
>;
|
|
|
|
type EmptyFeatureMessageCatalogs = Readonly<
|
|
Record<string, Readonly<Record<never, never>>>
|
|
>;
|
|
|
|
type EmptyFeatureRuntimeDescriptor = Readonly<{
|
|
featureId: string;
|
|
routeCodecs: EmptyRecord;
|
|
routeRuntime: EmptyRecord;
|
|
}>;
|
|
|
|
/**
|
|
* The default parameters deliberately describe the empty-product fallback.
|
|
* Concrete generated modules retain their literal types; only an empty
|
|
* generated tuple resolves to this default shape.
|
|
*/
|
|
export type FeatureModule<
|
|
Contract extends FeatureContractIdentity = EmptyFeatureContract,
|
|
Routes extends Readonly<Record<string, unknown>> = EmptyRecord,
|
|
Adapter = never,
|
|
Runtime = EmptyFeatureRuntimeDescriptor,
|
|
Messages extends FeatureMessageCatalogs = EmptyFeatureMessageCatalogs,
|
|
ContractContribution = never,
|
|
> = Readonly<{
|
|
id: string;
|
|
contracts: Contract;
|
|
routes: Routes;
|
|
adapters: readonly Adapter[];
|
|
runtimes: readonly Runtime[];
|
|
messages: Messages;
|
|
contractContributions: readonly ContractContribution[];
|
|
}>;
|
|
|
|
/** Structural supertype used when accepting any concrete feature descriptor. */
|
|
export type AnyFeatureModule = FeatureModule<
|
|
FeatureContractIdentity,
|
|
Readonly<Record<string, unknown>>,
|
|
unknown,
|
|
unknown,
|
|
FeatureMessageCatalogs,
|
|
unknown
|
|
>;
|
|
|
|
export function defineFeatureModule<const Module extends AnyFeatureModule>(
|
|
module: Module,
|
|
): Readonly<Module> {
|
|
if (module.id !== module.contracts.featureId) {
|
|
throw new TypeError(
|
|
`Feature module id ${module.id} does not match contract featureId ${module.contracts.featureId}`,
|
|
);
|
|
}
|
|
return Object.freeze({
|
|
...module,
|
|
adapters: Object.freeze([...module.adapters]),
|
|
runtimes: Object.freeze([...module.runtimes]),
|
|
contractContributions: Object.freeze([...module.contractContributions]),
|
|
}) as unknown as Readonly<Module>;
|
|
}
|