-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: introduced tsdoc eslint plugin
- Loading branch information
Showing
15 changed files
with
185 additions
and
73 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
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
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
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
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
File renamed without changes.
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,42 @@ | ||
/** | ||
* Container for storing objects | ||
* Can handle hierarchy as well | ||
* | ||
* @experimental | ||
*/ | ||
export class Container<T, K = T> implements Iterable<T> { | ||
private readonly _values = new Map<K, T>(); | ||
|
||
constructor(private _parent?: Container<T, K>) {} | ||
|
||
public *[Symbol.iterator](): IterableIterator<T> { | ||
if (this._parent) yield* this._parent; | ||
yield* this._values.values(); | ||
} | ||
|
||
public setParent(parent: Container<T, K>): void { | ||
this._parent = parent; | ||
} | ||
|
||
public set(key: K, value: T): void { | ||
this._values.set(key, value); | ||
} | ||
|
||
public get(key: K): T | null { | ||
if (this._values.has(key)) { | ||
return this._values.get(key) ?? null; | ||
} | ||
return this._parent?.get(key) ?? null; | ||
} | ||
|
||
public push(...items: T[]): void { | ||
items.forEach((item) => this._values.set(item as unknown as K, item)); | ||
} | ||
|
||
public get length(): number { | ||
let length = 0; | ||
if (this._parent) length += this._parent.length; | ||
length += this._values.size; | ||
return length; | ||
} | ||
} |
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,17 @@ | ||
import { AfterControllerCreationHook, AppDestroyHook, AppInitHook, AppReadyHook, BeforeControllerCreationHook } from '../plugins/life-cycle.js'; | ||
|
||
export interface HooksRegistry { | ||
appInit: AppInitHook[]; | ||
beforeControllerCreation: BeforeControllerCreationHook[]; | ||
afterControllerCreation: AfterControllerCreationHook[]; | ||
appReady: AppReadyHook[]; | ||
appDestroy: AppDestroyHook[]; | ||
} | ||
|
||
export const hooksRegistry: HooksRegistry = { | ||
appInit: [], | ||
beforeControllerCreation: [], | ||
afterControllerCreation: [], | ||
appReady: [], | ||
appDestroy: [], | ||
}; |
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 |
---|---|---|
@@ -1,34 +1,30 @@ | ||
import { FastifyInstance } from 'fastify'; | ||
import { hooksRegistry, Registrable } from 'fastify-decorators/plugins'; | ||
import { Container } from 'typedi'; | ||
import { fastify } from 'fastify'; | ||
import { bootstrap, Controller } from 'fastify-decorators'; | ||
import { Container, Service } from 'typedi'; | ||
import { useContainer } from './index.js'; | ||
import { CLASS_LOADER } from 'fastify-decorators/plugins'; | ||
|
||
describe('Use container', () => { | ||
beforeEach(() => { | ||
hooksRegistry.beforeControllerCreation = []; | ||
hooksRegistry.appInit = []; | ||
Container.reset(); | ||
}); | ||
|
||
it('should register before controller creation hook', () => { | ||
beforeAll(() => { | ||
useContainer(Container); | ||
|
||
expect(hooksRegistry.beforeControllerCreation).toHaveLength(1); | ||
}); | ||
|
||
it('should register controller in container on beforeControllerCreation', () => { | ||
useContainer(Container); | ||
|
||
class Test {} | ||
it('should create controller with injected dependency', async () => { | ||
const instance = fastify(); | ||
instance.register(bootstrap, { controllers: [SampleController] }); | ||
|
||
hooksRegistry.beforeControllerCreation[0]({} as FastifyInstance, Test as Registrable); | ||
await instance.ready(); | ||
|
||
expect(Container.get(Test)).toBeInstanceOf(Test); | ||
expect(Container.has(SampleController)).toBeTruthy(); | ||
expect(instance[CLASS_LOADER](SampleController, instance)).toBeInstanceOf(SampleController); | ||
expect(instance[CLASS_LOADER](SampleController, instance).dependency).toBeInstanceOf(Dependency); | ||
}); | ||
}); | ||
|
||
it('should register after controller creation hook', () => { | ||
useContainer(Container); | ||
@Service() | ||
class Dependency {} | ||
|
||
expect(hooksRegistry.appInit).toHaveLength(1); | ||
}); | ||
}); | ||
@Controller() | ||
class SampleController { | ||
constructor(public dependency: Dependency) {} | ||
} |
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