forked from tsedio/tsed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(di): export injectable,controller,interceptor functions
BREAKING CHANGE: registerValue and registerController are removed in favor of injectable/controller functions
- Loading branch information
Showing
20 changed files
with
219 additions
and
192 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 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import {DITest, logger} from "../../node/index.js"; | ||
import {ProviderScope} from "../domain/ProviderScope.js"; | ||
import {ProviderType} from "../domain/ProviderType.js"; | ||
import {inject} from "./inject.js"; | ||
import {controller, injectable, interceptor} from "./injectable.js"; | ||
|
||
class Nested { | ||
get() { | ||
return "hello"; | ||
} | ||
} | ||
|
||
class MyClass { | ||
nested = inject(Nested); | ||
logger = logger(); | ||
} | ||
|
||
class MyController {} | ||
|
||
injectable(Nested).scope(ProviderScope.SINGLETON).class(Nested); | ||
injectable(MyClass); | ||
|
||
describe("injectable", () => { | ||
describe("injectable()", () => { | ||
it("should define a singleton scope", async () => { | ||
const instance = await DITest.invoke(MyClass); | ||
|
||
expect(instance.nested).toBeInstanceOf(Nested); | ||
expect(instance.nested.get()).toEqual("hello"); | ||
}); | ||
it("should create a factory", async () => { | ||
const builder = injectable(Symbol.for("Test")).factory(() => "test"); | ||
const provider = builder.inspect(); | ||
|
||
expect(provider.type).toEqual(ProviderType.PROVIDER); | ||
expect(builder.token()).toEqual(Symbol.for("Test")); | ||
}); | ||
it("should create an async factory", async () => { | ||
const builder = injectable(Symbol.for("Test")).asyncFactory(() => Promise.resolve("test")); | ||
const provider = builder.inspect(); | ||
|
||
expect(provider.type).toEqual(ProviderType.PROVIDER); | ||
expect(builder.token()).toEqual(Symbol.for("Test")); | ||
}); | ||
it("should create a value", async () => { | ||
const builder = injectable(Symbol.for("Test")).value({ | ||
test: "test" | ||
}); | ||
const provider = builder.inspect(); | ||
|
||
expect(provider.type).toEqual(ProviderType.VALUE); | ||
expect(builder.token()).toEqual(Symbol.for("Test")); | ||
}); | ||
}); | ||
|
||
describe("controller()", () => { | ||
it("should define a singleton scope", async () => { | ||
const builder = controller(MyController) | ||
.path("/my-controller") | ||
.scope(ProviderScope.REQUEST) | ||
.middlewares({ | ||
use: [() => {}] | ||
}); | ||
|
||
const provider = builder.inspect(); | ||
|
||
expect(provider.type).toEqual(ProviderType.CONTROLLER); | ||
expect(provider.scope).toEqual(ProviderScope.REQUEST); | ||
}); | ||
}); | ||
|
||
describe("interceptor()", () => { | ||
it("should define a singleton scope", async () => { | ||
const builder = interceptor(MyController); | ||
const provider = builder.inspect(); | ||
|
||
expect(provider.type).toEqual(ProviderType.INTERCEPTOR); | ||
}); | ||
}); | ||
}); |
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,6 +1,98 @@ | ||
import {registerProvider} from "../registries/ProviderRegistry.js"; | ||
import "../registries/ProviderRegistry.js"; | ||
|
||
/** | ||
* @alias {registerProvider} Alias of registerProvider | ||
*/ | ||
export const injectable = registerProvider; | ||
import {Store, type Type} from "@tsed/core"; | ||
|
||
import {ControllerProvider} from "../domain/ControllerProvider.js"; | ||
import type {Provider} from "../domain/Provider.js"; | ||
import {ProviderType} from "../domain/ProviderType.js"; | ||
import type {ProviderOpts} from "../interfaces/ProviderOpts.js"; | ||
import type {TokenProvider} from "../interfaces/TokenProvider.js"; | ||
import {GlobalProviders} from "../registries/GlobalProviders.js"; | ||
|
||
type ProviderBuilder<Token extends TokenProvider, BaseProvider, T extends object> = { | ||
[K in keyof T as T[K] extends (...args: any[]) => any ? never : K]: (value: T[K]) => ProviderBuilder<Token, BaseProvider, T>; | ||
} & { | ||
inspect(): BaseProvider; | ||
store(): Store; | ||
token(): Token; | ||
factory(f: (...args: unknown[]) => unknown): ProviderBuilder<Token, BaseProvider, T>; | ||
asyncFactory(f: (...args: unknown[]) => Promise<unknown>): ProviderBuilder<Token, BaseProvider, T>; | ||
value(v: unknown): ProviderBuilder<Token, BaseProvider, T>; | ||
class(c: Type): ProviderBuilder<Token, BaseProvider, T>; | ||
}; | ||
|
||
export function providerBuilder<Provider, Picked extends keyof Provider>(props: string[], baseOpts: Partial<ProviderOpts<Provider>> = {}) { | ||
return <Token extends TokenProvider>( | ||
token: Token, | ||
options: Partial<ProviderOpts<Type>> = {} | ||
): ProviderBuilder<Token, Provider, Pick<Provider, Picked>> => { | ||
const provider = GlobalProviders.merge(token, { | ||
...options, | ||
...baseOpts, | ||
provide: token | ||
}); | ||
|
||
return props.reduce( | ||
(acc, prop) => { | ||
return { | ||
...acc, | ||
[prop]: function (value: any) { | ||
(provider as any)[prop] = value; | ||
return this; | ||
} | ||
}; | ||
}, | ||
{ | ||
factory(factory: any) { | ||
provider.useFactory = factory; | ||
return this; | ||
}, | ||
asyncFactory(asyncFactory: any) { | ||
provider.useAsyncFactory = asyncFactory; | ||
return this; | ||
}, | ||
value(value: any) { | ||
provider.useValue = value; | ||
provider.type = ProviderType.VALUE; | ||
return this; | ||
}, | ||
class(k: any) { | ||
provider.useClass = k; | ||
return this; | ||
}, | ||
store() { | ||
return provider.store; | ||
}, | ||
inspect() { | ||
return provider; | ||
}, | ||
token() { | ||
return provider.token as Token; | ||
} | ||
} as ProviderBuilder<Token, Provider, Pick<Provider, Picked>> | ||
); | ||
}; | ||
} | ||
|
||
type PickedProps = | ||
| "scope" | ||
| "path" | ||
| "alias" | ||
| "useFactory" | ||
| "useAsyncFactory" | ||
| "useValue" | ||
| "useClass" | ||
| "hooks" | ||
| "deps" | ||
| "resolvers" | ||
| "imports" | ||
| "configuration"; | ||
|
||
const Props = ["type", "scope", "path", "alias", "hooks", "deps", "resolvers", "imports", "configuration"]; | ||
export const injectable = providerBuilder<Provider, PickedProps | "type">(Props); | ||
export const interceptor = providerBuilder<Provider, PickedProps | "type">(Props, { | ||
type: ProviderType.INTERCEPTOR | ||
}); | ||
export const controller = providerBuilder<ControllerProvider, PickedProps | "children" | "middlewares">([...Props, "middlewares"], { | ||
type: ProviderType.CONTROLLER | ||
}); |
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
Oops, something went wrong.