Skip to content

Commit

Permalink
fix(di): remove default scope configuration on DIConfiguration level
Browse files Browse the repository at this point in the history
BREAKING CHANGE: configuration.scope is removed. This options doesn't make sense since a $ctx exists. ProviderScope.REQUEST must to be declared explicitly on each controller.
  • Loading branch information
Romakita committed Nov 16, 2024
1 parent e3b5c77 commit 5eea6d5
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 88 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ lib-cov

# Coverage directory used by tools like istanbul
coverage
coverage-*


# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
Expand Down
17 changes: 0 additions & 17 deletions docs/docs/configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,23 +265,6 @@ Add providers or modules here. These modules or provider will be built before th
</Tab>
</Tabs>

### scopes

- type: `{[key: string]: ProviderScope}`

Change the default scope for a given provider. See [injection scopes](/docs/injection-scopes) for more details.

```typescript
import {Configuration, ProviderScope, ProviderType} from "@tsed/di";

@Configuration({
scopes: {
[ProviderType.CONTROLLER]: ProviderScope.REQUEST
}
})
export class Server {}
```

### logger

- type: @@PlatformLoggerSettings@@
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/injection-scopes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The scope of a [Provider](/docs/providers.md) defines the lifecycle and visibility of that bean in the context in which it is used.

Ts.ED DI defines 3 types of @@Scope@@ which can be used on injectable classes:
Ts.ED DI defines 3 types of @@ProviderScope@@ which can be used on injectable classes:

- `singleton`: The default scope. The provider is created during server initialization and is shared across all requests.
- `request`: A new instance of the provider is created for each incoming request.
Expand Down
10 changes: 6 additions & 4 deletions packages/di/src/common/domain/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class Provider<T = any> implements ProviderOpts<T> {
return ProviderScope.SINGLETON;
}

return this.get("scope");
return this.get<ProviderScope>("scope", ProviderScope.SINGLETON);
}

/**
Expand All @@ -122,7 +122,7 @@ export class Provider<T = any> implements ProviderOpts<T> {
}

get configuration(): Partial<TsED.Configuration> {
return this.get("configuration");
return this.get("configuration")!;
}

set configuration(configuration: Partial<TsED.Configuration>) {
Expand All @@ -141,8 +141,10 @@ export class Provider<T = any> implements ProviderOpts<T> {
return this.store.get(`${DI_USE_PARAM_OPTIONS}:${index}`);
}

get(key: string) {
return this.store.get(key) || this._tokenStore.get(key);
get<Type = unknown>(key: string): Type | undefined;
get<Type = unknown>(key: string, defaultValue: Type): Type;
get<Type = unknown>(key: string, defaultValue?: Type): Type | undefined {
return this.store.get(key) || this._tokenStore.get(key) || defaultValue;
}

isAsync(): boolean {
Expand Down
13 changes: 1 addition & 12 deletions packages/di/src/common/services/DIConfiguration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,10 @@ describe("DIConfiguration", () => {
expect(obj).toEqual({
imports: [],
logger: {},
routes: [],
scopes: {}
routes: []
});
});
});
describe("scopes()", () => {
it("should get scopes", () => {
// GIVEN
const configuration = new DIConfiguration();

configuration.scopes = {};
expect(configuration.scopes).toEqual({});
});
});

describe("imports()", () => {
it("should get imports", () => {
// GIVEN
Expand Down
17 changes: 0 additions & 17 deletions packages/di/src/common/services/DIConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export class DIConfiguration {

constructor(initialProps = {}) {
Object.entries({
scopes: {},
imports: [],
routes: [],
logger: {},
Expand Down Expand Up @@ -46,22 +45,6 @@ export class DIConfiguration {
this.map.set("env", value);
}

get scopes(): Record<string, ProviderScope> {
return this.map.get("scopes");
}

set scopes(value: Record<string, ProviderScope>) {
this.map.set("scopes", value);
}
//
// get resolvers(): DIResolver[] {
// return this.getRaw("resolvers");
// }
//
// set resolvers(resolvers: DIResolver[]) {
// this.map.set("resolvers", resolvers);
// }

get imports(): (TokenProvider | ImportTokenProviderOpts)[] {
return this.get("imports")!;
}
Expand Down
25 changes: 5 additions & 20 deletions packages/di/src/common/services/InjectorService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,29 +616,18 @@ describe("InjectorService", () => {
// GIVEN
const injector = new InjectorService();

injector.settings.set({
scopes: {
[ProviderType.VALUE]: ProviderScope.SINGLETON
}
});

expect(injector.settings.get("scopes")).toEqual({
[ProviderType.VALUE]: ProviderScope.SINGLETON
});

injector.add(Symbol.for("TOKEN1"), {
configuration: {
custom: "config",
scopes: {
provider_custom: ProviderScope.SINGLETON
custom: {
config: "1"
}
}
});

injector.add(Symbol.for("TOKEN2"), {
configuration: {
scopes: {
provider_custom_2: ProviderScope.SINGLETON
custom: {
config2: "1"
}
}
});
Expand All @@ -647,11 +636,7 @@ describe("InjectorService", () => {
injector.resolveConfiguration();

// THEN
expect(injector.settings.scopes).toEqual({
provider_custom: "singleton",
provider_custom_2: "singleton",
value: "singleton"
});
expect(injector.settings.get("custom")).toEqual({config: "1", config2: "1"});
});
});

Expand Down
16 changes: 2 additions & 14 deletions packages/di/src/common/services/InjectorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,6 @@ export class InjectorService extends Container {
this.#cache.set(InjectorService, this);
}

get scopes() {
return this.settings.scopes || {};
}

/**
* Retrieve default scope for a given provider.
* @param provider
*/
public scopeOf(provider: Provider) {
return provider.scope || this.scopes[String(provider.type)] || ProviderScope.SINGLETON;
}

/**
* Return a list of instance build by the injector.
*/
Expand Down Expand Up @@ -181,7 +169,7 @@ export class InjectorService extends Container {

instance = this.resolve(token, options);

switch (this.scopeOf(provider)) {
switch (provider.scope) {
case ProviderScope.SINGLETON:
if (provider.hooks && !options.rebuild) {
this.registerHooks(provider, instance);
Expand Down Expand Up @@ -233,7 +221,7 @@ export class InjectorService extends Container {
*/
loadSync() {
for (const [, provider] of this) {
if (!this.has(provider.token) && this.scopeOf(provider) === ProviderScope.SINGLETON) {
if (!this.has(provider.token) && provider.scope === ProviderScope.SINGLETON) {
this.invoke(provider.token);
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/platform/platform-params/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export default defineConfig(
coverage: {
...presets.test.coverage,
thresholds: {
statements: 99.55,
statements: 99.4,
branches: 95.45,
functions: 100,
lines: 99.55
lines: 99.4
}
}
}
}
);
);
4 changes: 4 additions & 0 deletions tools/vitest/presets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export const presets = defineConfig({
all: true,
include: ["src/**/*.{tsx,ts}"],
exclude: [
"**/exports.ts",
"**/interfaces/**",
"**/*fixtures.ts",
"**/fixtures/**",
"**/*.spec.{ts,tsx}",
"**/*.stories.{ts,tsx}",
"**/*.d.ts",
Expand Down

0 comments on commit 5eea6d5

Please sign in to comment.