Skip to content

Commit

Permalink
chore: review coderabbitai
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Nov 19, 2024
1 parent d6bf276 commit 721179f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/di/src/common/decorators/autoInjectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function resolveAutoInjectableArgs(token: Type, args: unknown[]) {
list.push(args[i]);
} else {
const value = deps[i];
const instance = isArray(value) ? inj!.getMany(value[0], {locals, parent: token}) : inj!.invoke(value, {locals, parent: token});
const instance = isArray(value) ? inj.getMany(value[0], {locals, parent: token}) : inj.invoke(value, {locals, parent: token});

list.push(instance);
}
Expand Down
12 changes: 11 additions & 1 deletion packages/di/src/common/domain/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,18 @@ export class Provider<T = any> implements ProviderOpts<T> {
getArgOpts(index: number) {
return this.store.get(`${DI_USE_PARAM_OPTIONS}:${index}`);
}

/**
* Retrieves a value from the provider's store.
* @param key The key to look up
* @returns The value if found, undefined otherwise
*/
get<Type = unknown>(key: string): Type | undefined;
/**
* Retrieves a value from the provider's store with a default fallback.
* @param key The key to look up
* @param defaultValue The value to return if key is not found
* @returns The found value or defaultValue
*/
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;
Expand Down
11 changes: 10 additions & 1 deletion packages/di/src/common/fn/injectMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import type {InvokeOptions} from "../interfaces/InvokeOptions.js";
import {injector} from "./injector.js";
import {localsContainer} from "./localsContainer.js";

/**
* Injects multiple instances of a given token using the injector service.
* @param token - The injection token to resolve
* @param opts - Optional configuration for the injection
* @param opts.useOpts - Options for instance creation
* @param opts.rebuild - Whether to rebuild the instance
* @param opts.locals - Local container overrides
* @returns Array of resolved instances
*/
export function injectMany<T>(token: string | symbol, opts?: Partial<Pick<InvokeOptions, "useOpts" | "rebuild" | "locals">>): T[] {
return injector().getMany<T>(token, {...opts, locals: opts?.locals || localsContainer()});
return injector().getMany<T>(token, {...opts, locals: opts?.locals || localsContainer()} as InvokeOptions);
}
10 changes: 5 additions & 5 deletions packages/di/src/common/services/InjectorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {createContainer} from "../utils/createContainer.js";
import {getConstructorDependencies} from "../utils/getConstructorDependencies.js";
import {DIConfiguration} from "./DIConfiguration.js";

const EXCLUDED_CONFIGURATION_KEYS = ["mount", "imports"];

/**
* This service contain all services collected by `@Service` or services declared manually with `InjectorService.factory()` or `InjectorService.service()`.
*
Expand Down Expand Up @@ -89,7 +91,7 @@ export class InjectorService extends Container {
*/
getMany<Type = any>(type: any, options?: Partial<InvokeOptions>): Type[] {
return this.getProviders(type).map((provider) => {
return this.invoke(provider.token, options)!;
return this.invoke<Type>(provider.token, options);
});
}

Expand Down Expand Up @@ -194,9 +196,7 @@ export class InjectorService extends Container {
if (options.locals) {
options.locals.set(token, instance);

if (provider.hooks && provider.hooks.$onDestroy) {
options.locals.hooks.on("$onDestroy", (...args: any[]) => provider.hooks!.$onDestroy(instance, ...args));
}
options.locals?.hooks.on("$onDestroy", (...args: unknown[]) => provider.hooks?.$onDestroy(instance, ...args));
}

return instance;
Expand Down Expand Up @@ -257,7 +257,7 @@ export class InjectorService extends Container {
super.forEach((provider) => {
if (provider.configuration && provider.type !== "server:module") {
Object.entries(provider.configuration).forEach(([key, value]) => {
if (!["mount", "imports"].includes(key)) {
if (!EXCLUDED_CONFIGURATION_KEYS.includes(key)) {
value = mergedConfiguration.has(key) ? deepMerge(mergedConfiguration.get(key), value) : deepClone(value);
mergedConfiguration.set(key, value);
}
Expand Down
12 changes: 5 additions & 7 deletions packages/orm/adapters/src/services/Adapters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Type} from "@tsed/core";
import {Inject, Injectable, InjectorService} from "@tsed/di";
import {constant, inject, injectable} from "@tsed/di";

import {MemoryAdapter} from "../adapters/MemoryAdapter.js";
import {Adapter, AdapterConstructorOptions} from "../domain/Adapter.js";
Expand All @@ -8,16 +8,14 @@ export interface AdapterInvokeOptions<Model = any> extends AdapterConstructorOpt
adapter?: Type<Adapter<Model>>;
}

@Injectable()
export class Adapters {
@Inject()
injector: InjectorService;

invokeAdapter<T = any>(options: AdapterInvokeOptions): Adapter<T> {
const {adapter = this.injector.settings.get("adapters.Adapter", MemoryAdapter), ...props} = options;
const {adapter = constant("adapters.Adapter", MemoryAdapter), ...props} = options;

return this.injector.invoke<Adapter<T>>(adapter, {
return inject<Adapter<T>>(adapter, {
useOpts: props
});
}
}

injectable(Adapters);

0 comments on commit 721179f

Please sign in to comment.