DI Bag API / index / Module
Interface: Module<ExportedServices extends object, RequiredServices extends object, Constraints extends NeedConstraint = never, PublicProviders extends Registrations = PublicRegistrations<ExportedServices>>
Defined in: module.ts:45
A sealed, non-resolving module with private providers and selected public exports. Create modules through DiBagApi.createBuilder and Builder.buildModule; this type-only class has no public constructor.
See
https://dany-fedorov.github.io/di-bag/guides/tutorial.html#reuse-named-modules
Type Parameters
| Type Parameter | Description |
|---|---|
ExportedServices | The services this module exports, keyed by export name or token symbol. |
RequiredServices | The services the installing builder must provide. |
Constraints | The checks retained from the sealed graph and applied again at installation. |
PublicProviders | The provider contract of each export, as the installing builder sees it. |
Methods
withRenamedExport()
withRenamedExport<const CurrentExportKey extends string, const NewExportKey extends string>(options: {
readonly currentExportKey: CurrentExportKey & RenameKeys<ExportedServices, CurrentExportKey, NewExportKey, 'withRenamedExport'>;
readonly newExportKey: NewExportKey & RenameKeys<ExportedServices, CurrentExportKey, NewExportKey, 'withRenamedExport'>;
}): Module<Renamed<ExportedServices, CurrentExportKey, NewExportKey>, RequiredServices, RenamedConstraints<Constraints, CurrentExportKey, NewExportKey>, RenamedProviders<PublicProviders, CurrentExportKey, NewExportKey>>;Defined in: module.ts:73
Return a module view with one string-named export renamed through an options object.
Type Parameters
| Type Parameter | Description |
|---|---|
CurrentExportKey | - |
NewExportKey | - |
Parameters
| Parameter | Description |
|---|---|
options | The current export and its noncolliding new name. |
Returns
A new sealed module, or the same instance when both names are equal.
Throws
DI_BAG_INVALID_ARGUMENT for a malformed options object or export name, DI_BAG_UNKNOWN_SERVICE_KEY for an unknown current export.
Example
const feature = DiBag.createBuilder().withServices({ service: () => 1 })
.buildModule({ exportedServiceKeys: ['service'] });
const renamed = feature.withRenamedExport({ currentExportKey: 'service', newExportKey: 'featureService' });withRenamedRequirement()
withRenamedRequirement<const CurrentRequirementKey extends string, const NewRequirementKey extends string>(options: {
readonly currentRequirementKey: CurrentRequirementKey & CurrentRequirementKeyAdmission<RequiredServices, CurrentRequirementKey>;
readonly newRequirementKey: NewRequirementKey & NewRequirementKeyAdmission<ExportedServices, RequiredServices, CurrentRequirementKey, NewRequirementKey>;
}): Module<ExportedServices, Renamed<RequiredServices, CurrentRequirementKey, NewRequirementKey>, RenamedRequirementConstraints<Constraints, CurrentRequirementKey, NewRequirementKey>, RenamedRequirementProviders<PublicProviders, CurrentRequirementKey, NewRequirementKey>>;Defined in: module.ts:132
Return a module view that asks its host for a requirement under a new name. Factory parameter names and private bindings retain their lexical meaning.
Type Parameters
| Type Parameter | Description |
|---|---|
CurrentRequirementKey | - |
NewRequirementKey | - |
Parameters
| Parameter | Description |
|---|---|
options | The current requirement and its noncolliding new host key. |
Returns
A new sealed module, or this module when both keys are equal.
Throws
DI_BAG_INVALID_ARGUMENT for malformed options; DI_BAG_UNKNOWN_SERVICE_KEY for a known non-requirement; DI_BAG_DUPLICATE_SERVICE_KEY for a known collision.
Remarks
Type checking rejects unknown requirements and all name collisions. Runtime checks cover only facts available without executing a factory.
Example
const feature = DiBag.createBuilder()
.withServices({ answer: ({ config }: { config: number }) => config })
.buildModule({ exportedServiceKeys: ['answer'] });
const app = DiBag.createBuilder()
.withInstalledModules([feature.withRenamedRequirement({ currentRequirementKey: 'config', newRequirementKey: 'featureConfig' })])
.withServices({ featureConfig: () => 42 }).buildContainer();
console.log(app.resolve('answer'));
await app.close();