From 15b590ae36ef3e8f20ca5104fbbb0e2603c9bbd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Pintos=20L=C3=B3pez?= Date: Mon, 30 Dec 2024 21:37:37 +0100 Subject: [PATCH] feat(inversify-code-examples): add container api examples --- .../src/examples/containerApiGet.spec.ts | 9 +++ .../src/examples/containerApiGet.ts | 18 +++++ .../examples/containerApiGetAsync.int.spec.ts | 9 +++ .../src/examples/containerApiGetAsync.ts | 18 +++++ .../examples/containerApiGetNamed.int.spec.ts | 10 +++ .../src/examples/containerApiGetNamed.ts | 24 +++++++ .../containerApiGetNamedAsync.int.spec.ts | 15 +++++ .../src/examples/containerApiGetNamedAsync.ts | 36 ++++++++++ .../containerApiGetTagged.int.spec.ts | 10 +++ .../src/examples/containerApiGetTagged.ts | 38 +++++++++++ .../containerApiGetTaggedAsync.int.spec.ts | 15 +++++ .../examples/containerApiGetTaggedAsync.ts | 38 +++++++++++ .../examples/containerApiMerge.int.spec.ts | 28 ++++++++ .../src/examples/containerApiMerge.ts | 65 +++++++++++++++++++ ...erApiOptionsAutoBindInjectable.int.spec.ts | 13 ++++ .../containerApiOptionsAutoBindInjectable.ts | 19 ++++++ ...nsAutoBindInjectablePrecedence.int.spec.ts | 13 ++++ ...rApiOptionsAutoBindInjectablePrecedence.ts | 16 +++++ ...ontainerApiOptionsDefaultScope.int.spec.ts | 16 +++++ .../containerApiOptionsDefaultScope.ts | 17 +++++ 20 files changed, 427 insertions(+) create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiGet.spec.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiGet.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiGetAsync.int.spec.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiGetAsync.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiGetNamed.int.spec.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiGetNamed.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiGetNamedAsync.int.spec.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiGetNamedAsync.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiGetTagged.int.spec.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiGetTagged.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiGetTaggedAsync.int.spec.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiGetTaggedAsync.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiMerge.int.spec.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiMerge.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsAutoBindInjectable.int.spec.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsAutoBindInjectable.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsAutoBindInjectablePrecedence.int.spec.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsAutoBindInjectablePrecedence.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsDefaultScope.int.spec.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsDefaultScope.ts diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiGet.spec.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGet.spec.ts new file mode 100644 index 00000000..06843cb5 --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGet.spec.ts @@ -0,0 +1,9 @@ +import { describe, expect, it } from '@jest/globals'; + +import { Katana, katana } from './containerApiGet'; + +describe('Container API (get)', () => { + it('should provide Katana weapon', () => { + expect(katana).toBeInstanceOf(Katana); + }); +}); diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiGet.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGet.ts new file mode 100644 index 00000000..013db91f --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGet.ts @@ -0,0 +1,18 @@ +import { Container } from 'inversify'; + +interface Weapon { + damage: number; +} + +export class Katana implements Weapon { + public readonly damage: number = 10; +} + +// Begin-example +const container: Container = new Container(); +container.bind('Weapon').to(Katana); + +const katana: Weapon = container.get('Weapon'); +// End-example + +export { katana }; diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetAsync.int.spec.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetAsync.int.spec.ts new file mode 100644 index 00000000..f6233e87 --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetAsync.int.spec.ts @@ -0,0 +1,9 @@ +import { describe, expect, it } from '@jest/globals'; + +import { Level1, level1 } from './containerApiGetAsync'; + +describe('Container API (getAsync)', () => { + it('should provide async service', async () => { + expect(await level1).toBeInstanceOf(Level1); + }); +}); diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetAsync.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetAsync.ts new file mode 100644 index 00000000..5d4b788c --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetAsync.ts @@ -0,0 +1,18 @@ +import { Container } from 'inversify'; + +export class Level1 {} + +// Begin-example +async function buildLevel1(): Promise { + return new Level1(); +} + +const container: Container = new Container(); +container + .bind('Level1') + .toDynamicValue(async (): Promise => buildLevel1()); + +const level1: Promise = container.getAsync('Level1'); // Returns Promise +// End-example + +export { level1 }; diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetNamed.int.spec.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetNamed.int.spec.ts new file mode 100644 index 00000000..60f091ac --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetNamed.int.spec.ts @@ -0,0 +1,10 @@ +import { describe, expect, it } from '@jest/globals'; + +import { Katana, katana, Shuriken, shuriken } from './containerApiGetNamed'; + +describe('Container API (getNamed)', () => { + it('should provide Katana weapon', () => { + expect(katana).toBeInstanceOf(Katana); + expect(shuriken).toBeInstanceOf(Shuriken); + }); +}); diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetNamed.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetNamed.ts new file mode 100644 index 00000000..2aa6c863 --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetNamed.ts @@ -0,0 +1,24 @@ +import { Container } from 'inversify'; + +interface Weapon { + readonly damage: number; +} + +export class Katana implements Weapon { + public readonly damage: number = 10; +} + +export class Shuriken implements Weapon { + public readonly damage: number = 5; +} + +// Begin-example +const container: Container = new Container(); +container.bind('Weapon').to(Katana).whenTargetNamed('japanese'); +container.bind('Weapon').to(Shuriken).whenTargetNamed('chinese'); + +const katana: Weapon = container.getNamed('Weapon', 'japanese'); +const shuriken: Weapon = container.getNamed('Weapon', 'chinese'); +// End-example + +export { katana, shuriken }; diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetNamedAsync.int.spec.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetNamedAsync.int.spec.ts new file mode 100644 index 00000000..87327496 --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetNamedAsync.int.spec.ts @@ -0,0 +1,15 @@ +import { describe, expect, it } from '@jest/globals'; + +import { + Katana, + katana, + Shuriken, + shuriken, +} from './containerApiGetNamedAsync'; + +describe('Container API (getNamed)', () => { + it('should provide Katana weapon', async () => { + expect(await katana).toBeInstanceOf(Katana); + expect(await shuriken).toBeInstanceOf(Shuriken); + }); +}); diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetNamedAsync.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetNamedAsync.ts new file mode 100644 index 00000000..918e64a6 --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetNamedAsync.ts @@ -0,0 +1,36 @@ +import { Container } from 'inversify'; + +interface Weapon { + damage: number; +} + +export class Katana implements Weapon { + public readonly damage: number = 10; +} + +export class Shuriken implements Weapon { + public readonly damage: number = 5; +} + +// Begin-example +const container: Container = new Container(); +container + .bind('Weapon') + .toDynamicValue(async () => new Katana()) + .whenTargetNamed('melee'); +container + .bind('Weapon') + .toDynamicValue(async () => new Shuriken()) + .whenTargetNamed('ranged'); + +const katana: Promise = container.getNamedAsync( + 'Weapon', + 'melee', +); +const shuriken: Promise = container.getNamedAsync( + 'Weapon', + 'ranged', +); +// End-example + +export { katana, shuriken }; diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetTagged.int.spec.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetTagged.int.spec.ts new file mode 100644 index 00000000..2e660784 --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetTagged.int.spec.ts @@ -0,0 +1,10 @@ +import { describe, expect, it } from '@jest/globals'; + +import { Katana, katana, Shuriken, shuriken } from './containerApiGetTagged'; + +describe('Container API (getTagged)', () => { + it('should provide weapon services', () => { + expect(katana).toBeInstanceOf(Katana); + expect(shuriken).toBeInstanceOf(Shuriken); + }); +}); diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetTagged.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetTagged.ts new file mode 100644 index 00000000..64978fd0 --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetTagged.ts @@ -0,0 +1,38 @@ +import { Container } from 'inversify'; + +interface Weapon { + readonly damage: number; +} + +export class Katana implements Weapon { + public readonly damage: number = 10; +} + +export class Shuriken implements Weapon { + public readonly damage: number = 5; +} + +// Begin-example +const container: Container = new Container(); +container + .bind('Weapon') + .to(Katana) + .whenTargetTagged('faction', 'samurai'); +container + .bind('Weapon') + .to(Shuriken) + .whenTargetTagged('faction', 'ninja'); + +const katana: Weapon = container.getTagged( + 'Weapon', + 'faction', + 'samurai', +); +const shuriken: Weapon = container.getTagged( + 'Weapon', + 'faction', + 'ninja', +); +// End-example + +export { katana, shuriken }; diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetTaggedAsync.int.spec.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetTaggedAsync.int.spec.ts new file mode 100644 index 00000000..e5f285b3 --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetTaggedAsync.int.spec.ts @@ -0,0 +1,15 @@ +import { describe, expect, it } from '@jest/globals'; + +import { + Katana, + katana, + Shuriken, + shuriken, +} from './containerApiGetTaggedAsync'; + +describe('Container API (getTagged)', () => { + it('should provide weapon services', async () => { + expect(await katana).toBeInstanceOf(Katana); + expect(await shuriken).toBeInstanceOf(Shuriken); + }); +}); diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetTaggedAsync.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetTaggedAsync.ts new file mode 100644 index 00000000..bbcb70ae --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiGetTaggedAsync.ts @@ -0,0 +1,38 @@ +import { Container } from 'inversify'; + +interface Weapon { + readonly damage: number; +} + +export class Katana implements Weapon { + public readonly damage: number = 10; +} + +export class Shuriken implements Weapon { + public readonly damage: number = 5; +} + +// Begin-example +const container: Container = new Container(); +container + .bind('Weapon') + .toDynamicValue(async () => new Katana()) + .whenTargetTagged('faction', 'samurai'); +container + .bind('Weapon') + .toDynamicValue(async () => new Shuriken()) + .whenTargetTagged('faction', 'ninja'); + +const katana: Promise = container.getTaggedAsync( + 'Weapon', + 'faction', + 'samurai', +); +const shuriken: Promise = container.getTaggedAsync( + 'Weapon', + 'faction', + 'ninja', +); +// End-example + +export { katana, shuriken }; diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiMerge.int.spec.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiMerge.int.spec.ts new file mode 100644 index 00000000..5e1e104d --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiMerge.int.spec.ts @@ -0,0 +1,28 @@ +import { describe, expect, it } from '@jest/globals'; + +import { + gameContainer, + Katana, + Ninja, + NINJA_EXPANSION_TYPES, + Samurai, + SAMURAI_EXPANSION_TYPES, + Shuriken, +} from './containerApiMerge'; + +describe('Container API (merge)', () => { + it('should provide expected services', () => { + expect( + gameContainer.get(NINJA_EXPANSION_TYPES.Ninja), + ).toBeInstanceOf(Ninja); + expect( + gameContainer.get(NINJA_EXPANSION_TYPES.Shuriken), + ).toBeInstanceOf(Shuriken); + expect( + gameContainer.get(SAMURAI_EXPANSION_TYPES.Samurai), + ).toBeInstanceOf(Samurai); + expect( + gameContainer.get(SAMURAI_EXPANSION_TYPES.Katana), + ).toBeInstanceOf(Katana); + }); +}); diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiMerge.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiMerge.ts new file mode 100644 index 00000000..07b37ca4 --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiMerge.ts @@ -0,0 +1,65 @@ +/* eslint-disable @typescript-eslint/typedef */ +import { Container, injectable, interfaces } from 'inversify'; + +// Begin-example +@injectable() +class Ninja { + public readonly name: string = 'Ninja'; +} + +@injectable() +class Shuriken { + public readonly name: string = 'Shuriken'; +} + +const NINJA_EXPANSION_TYPES = { + Ninja: 'Ninja', + Shuriken: 'Shuriken', +} satisfies Record; + +const ninjaExpansionContainer: Container = new Container(); + +ninjaExpansionContainer.bind(NINJA_EXPANSION_TYPES.Ninja).to(Ninja); +ninjaExpansionContainer + .bind(NINJA_EXPANSION_TYPES.Shuriken) + .to(Shuriken); + +@injectable() +class Samurai { + public readonly name: string = 'Samurai'; +} + +@injectable() +class Katana { + public name = 'Katana'; +} + +const SAMURAI_EXPANSION_TYPES = { + Katana: 'Katana', + Samurai: 'Samurai', +} satisfies Record; + +const samuraiExpansionContainer: Container = new Container(); +samuraiExpansionContainer + .bind(SAMURAI_EXPANSION_TYPES.Samurai) + .to(Samurai); +samuraiExpansionContainer + .bind(SAMURAI_EXPANSION_TYPES.Katana) + .to(Katana); + +const gameContainer: interfaces.Container = Container.merge( + ninjaExpansionContainer, + samuraiExpansionContainer, +); + +// End-example + +export { + gameContainer, + Katana, + Ninja, + NINJA_EXPANSION_TYPES, + Samurai, + SAMURAI_EXPANSION_TYPES, + Shuriken, +}; diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsAutoBindInjectable.int.spec.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsAutoBindInjectable.int.spec.ts new file mode 100644 index 00000000..e7090324 --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsAutoBindInjectable.int.spec.ts @@ -0,0 +1,13 @@ +import { describe, expect, it } from '@jest/globals'; + +import { + isBoundAfterGet, + isBoundBeforeGet, +} from './containerApiOptionsAutoBindInjectable'; + +describe('Container API options (autoBindInjectable)', () => { + it('should auto bind ninja', () => { + expect(isBoundAfterGet).toBe(true); + expect(isBoundBeforeGet).toBe(false); + }); +}); diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsAutoBindInjectable.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsAutoBindInjectable.ts new file mode 100644 index 00000000..e01d01c9 --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsAutoBindInjectable.ts @@ -0,0 +1,19 @@ +import { Container } from 'inversify'; + +class Ninja {} + +// Begin-example +const container: Container = new Container({ autoBindInjectable: true }); + +// Exclude-from-example +export const isBoundBeforeGet: boolean = container.isBound(Ninja); + +// returns false +container.isBound(Ninja); +// returns a Ninja +container.get(Ninja); +// returns true +container.isBound(Ninja); +// End-example + +export const isBoundAfterGet: boolean = container.isBound(Ninja); diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsAutoBindInjectablePrecedence.int.spec.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsAutoBindInjectablePrecedence.int.spec.ts new file mode 100644 index 00000000..7e4f7490 --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsAutoBindInjectablePrecedence.int.spec.ts @@ -0,0 +1,13 @@ +import { describe, expect, it } from '@jest/globals'; + +import { + container, + Ninja, + NinjaMaster, +} from './containerApiOptionsAutoBindInjectablePrecedence'; + +describe('Container API options (autoBindInjectable, precedence)', () => { + it('should provide ninja according to manual binding', () => { + expect(container.get(Ninja)).toBeInstanceOf(NinjaMaster); + }); +}); diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsAutoBindInjectablePrecedence.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsAutoBindInjectablePrecedence.ts new file mode 100644 index 00000000..7c14df68 --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsAutoBindInjectablePrecedence.ts @@ -0,0 +1,16 @@ +import { Container } from 'inversify'; + +export class Ninja {} + +export class NinjaMaster extends Ninja {} + +// Begin-example +const container: Container = new Container({ autoBindInjectable: true }); + +// returns a Ninja +container.bind(Ninja).to(NinjaMaster); +// returns NinjaMaster +container.get(Ninja); +// End-example + +export { container }; diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsDefaultScope.int.spec.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsDefaultScope.int.spec.ts new file mode 100644 index 00000000..01a4335a --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsDefaultScope.int.spec.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from '@jest/globals'; + +import { + container, + Ninja, + warriorServiceId, +} from './containerApiOptionsDefaultScope'; + +describe('Container API options (defaultScope)', () => { + it('should provide a container with ninja binding in transient scope', () => { + expect(container.get(warriorServiceId)).toBeInstanceOf(Ninja); + expect(container.get(warriorServiceId)).not.toBe( + container.get(warriorServiceId), + ); + }); +}); diff --git a/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsDefaultScope.ts b/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsDefaultScope.ts new file mode 100644 index 00000000..d284ee87 --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/containerApiOptionsDefaultScope.ts @@ -0,0 +1,17 @@ +import { Container } from 'inversify'; + +type Warrior = unknown; + +class Ninja {} + +const warriorServiceId: symbol = Symbol.for('WarriorServiceId'); + +// Begin-example +const container: Container = new Container({ defaultScope: 'Singleton' }); + +// You can configure the scope when declaring bindings: +container.bind(warriorServiceId).to(Ninja).inTransientScope(); + +// End-example + +export { container, Ninja, warriorServiceId };