-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #320 from inversify/feat/update-next-container-mod…
…ule-api-docs Update next container module api docs
- Loading branch information
Showing
3 changed files
with
98 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
.../docs/tools/inversify-code-examples/src/examples/v7/containerModuleApiExample.int.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
|
||
import { Ninja, ninjaPromise, Shuriken } from './containerModuleApiExample'; | ||
|
||
describe('ContainerModule API', () => { | ||
it('should provide expected service', async () => { | ||
const ninja: Ninja = await ninjaPromise; | ||
|
||
expect(ninja).toBeInstanceOf(Ninja); | ||
expect(ninja.weapon).toBeInstanceOf(Shuriken); | ||
}); | ||
}); |
59 changes: 59 additions & 0 deletions
59
packages/docs/tools/inversify-code-examples/src/examples/v7/containerModuleApiExample.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { | ||
Container, | ||
ContainerModule, | ||
ContainerModuleLoadOptions, | ||
inject, | ||
injectable, | ||
named, | ||
} from 'inversify7'; | ||
|
||
interface Weapon { | ||
damage: number; | ||
} | ||
|
||
@injectable() | ||
export class Katana implements Weapon { | ||
public readonly damage: number = 10; | ||
} | ||
|
||
@injectable() | ||
export class Shuriken implements Weapon { | ||
public readonly damage: number = 5; | ||
} | ||
|
||
@injectable() | ||
export class Ninja { | ||
@inject('Weapon') | ||
@named('Ranged') | ||
public readonly weapon!: Weapon; | ||
} | ||
|
||
const container: Container = new Container(); | ||
|
||
const scriptExecution: Promise<void> = (async () => { | ||
// Begin-example | ||
const warriorsModule: ContainerModule = new ContainerModule( | ||
(options: ContainerModuleLoadOptions) => { | ||
options.bind<Ninja>('Ninja').to(Ninja); | ||
}, | ||
); | ||
|
||
const weaponsModule: ContainerModule = new ContainerModule( | ||
(options: ContainerModuleLoadOptions) => { | ||
options.bind<Katana>('Weapon').to(Katana).whenNamed('Melee'); | ||
options.bind<Shuriken>('Weapon').to(Shuriken).whenNamed('Ranged'); | ||
}, | ||
); | ||
|
||
await container.load(warriorsModule, weaponsModule); | ||
|
||
const ninja: Ninja = container.get('Ninja'); | ||
// End-example | ||
})(); | ||
|
||
const ninjaPromise: Promise<Ninja> = scriptExecution.then(() => | ||
container.get('Ninja'), | ||
); | ||
|
||
export { ninjaPromise }; |