diff --git a/packages/di/src/common/fn/configuration.spec.ts b/packages/di/src/common/fn/configuration.spec.ts new file mode 100644 index 00000000000..a94e68da2ea --- /dev/null +++ b/packages/di/src/common/fn/configuration.spec.ts @@ -0,0 +1,28 @@ +import {beforeEach} from "vitest"; + +import {DITest} from "../../node/index.js"; +import {Injectable} from "../decorators/injectable.js"; +import {configuration} from "./configuration.js"; +import {inject} from "./inject.js"; +import {injector} from "./injector.js"; + +@Injectable() +class Test { + public config = configuration(); +} + +describe("configuration()", () => { + beforeEach(() => + DITest.create({ + feature: "feature" + }) + ); + afterEach(() => DITest.reset()); + + it("should inject configuration", async () => { + const instance = inject(Test); + + expect(instance.config).toEqual(injector().settings); + expect(instance.config.get("feature")).toEqual("feature"); + }); +}); diff --git a/packages/di/src/common/fn/configuration.ts b/packages/di/src/common/fn/configuration.ts new file mode 100644 index 00000000000..37ffe9b50ea --- /dev/null +++ b/packages/di/src/common/fn/configuration.ts @@ -0,0 +1,6 @@ +import {DIConfiguration} from "../services/DIConfiguration.js"; +import {injector} from "./injector.js"; + +export function configuration() { + return injector().settings as TsED.Configuration & DIConfiguration; +} diff --git a/packages/di/src/common/index.ts b/packages/di/src/common/index.ts index 473d95df7cc..b198bdbd67a 100644 --- a/packages/di/src/common/index.ts +++ b/packages/di/src/common/index.ts @@ -27,6 +27,7 @@ export * from "./domain/ProviderScope.js"; export * from "./domain/ProviderType.js"; export * from "./errors/InjectionError.js"; export * from "./errors/InvalidPropertyTokenError.js"; +export * from "./fn/configuration.js"; export * from "./fn/constant.js"; export * from "./fn/inject.js"; export * from "./fn/injectable.js"; diff --git a/packages/graphql/apollo/src/services/ApolloService.ts b/packages/graphql/apollo/src/services/ApolloService.ts index 6787f9026e7..19f3dc840f5 100644 --- a/packages/graphql/apollo/src/services/ApolloService.ts +++ b/packages/graphql/apollo/src/services/ApolloService.ts @@ -3,8 +3,8 @@ import {ApolloServerPluginLandingPageDisabled} from "@apollo/server/plugin/disab import {ApolloServerPluginDrainHttpServer} from "@apollo/server/plugin/drainHttpServer"; import {ApolloServerPluginLandingPageLocalDefault} from "@apollo/server/plugin/landingPage/default"; import type {IExecutableSchemaDefinition} from "@graphql-tools/schema"; -import {InjectorService, LocalsContainer, PlatformApplication, PlatformContext, Provider, useContext} from "@tsed/common"; -import {Constant, Inject, Service} from "@tsed/di"; +import {PlatformApplication, PlatformContext} from "@tsed/common"; +import {Constant, context, Inject, InjectorService, LocalsContainer, Provider, Service} from "@tsed/di"; import {Logger} from "@tsed/logger"; import type {GraphQLSchema} from "graphql"; import Http from "http"; @@ -177,14 +177,14 @@ export class ApolloService { }, new Map()); return async () => { - const $ctx = useContext(); - const context: ApolloContext = { + const $ctx = context(); + const apolloContext: ApolloContext = { dataSources: { ...(settings.dataSources?.() || {}) } }; - const alteredContext = await this.injector.alterAsync("$alterApolloContext", context, $ctx); + const alteredContext = await this.injector.alterAsync("$alterApolloContext", apolloContext, $ctx); $ctx!.set(APOLLO_CONTEXT, alteredContext); diff --git a/packages/graphql/typegraphql/src/middlewares/ContextMiddleware.ts b/packages/graphql/typegraphql/src/middlewares/ContextMiddleware.ts index 2de5635e25b..4e096e4e274 100644 --- a/packages/graphql/typegraphql/src/middlewares/ContextMiddleware.ts +++ b/packages/graphql/typegraphql/src/middlewares/ContextMiddleware.ts @@ -1,7 +1,7 @@ -import {type DIContext, runInContext, useContext} from "@tsed/di"; +import {type DIContext, getContext, runInContext} from "@tsed/di"; import {MiddlewareFn} from "type-graphql"; export const ContextMiddleware: MiddlewareFn<{req: {$ctx: DIContext}}> = (action, next) => { - const $ctx = useContext(action.context?.req?.$ctx); + const $ctx = getContext(action.context?.req?.$ctx); return runInContext($ctx, next); }; diff --git a/packages/platform/common/src/builder/PlatformBuilder.spec.ts b/packages/platform/common/src/builder/PlatformBuilder.spec.ts index cfa80f0489e..469f61add51 100644 --- a/packages/platform/common/src/builder/PlatformBuilder.spec.ts +++ b/packages/platform/common/src/builder/PlatformBuilder.spec.ts @@ -1,5 +1,5 @@ import {catchAsyncError, Type} from "@tsed/core"; -import {Configuration, Controller, Injectable, InjectorService, Module} from "@tsed/di"; +import {Configuration, Controller, Injectable, injector, InjectorService, Module} from "@tsed/di"; import {AfterInit} from "../interfaces/AfterInit.js"; import {AfterListen} from "../interfaces/AfterListen.js"; @@ -105,6 +105,9 @@ class ServerModule implements BeforeInit, AfterInit, BeforeRoutesInit, AfterRout } describe("PlatformBuilder", () => { + beforeEach(() => { + injector().destroy(); + }); describe("loadStatics()", () => { it("should loadStatics", async () => { // WHEN diff --git a/packages/third-parties/components-scan/src/isTsEnv.ts b/packages/third-parties/components-scan/src/isTsEnv.ts index 2fb440d5482..6822b7c76ea 100644 --- a/packages/third-parties/components-scan/src/isTsEnv.ts +++ b/packages/third-parties/components-scan/src/isTsEnv.ts @@ -1,9 +1,4 @@ export function isTsEnv() { - try { - if (require && require.extensions && require.extensions[".ts"]) { - return true; - } - } catch (er) {} return ( process.env["TS_NODE_DEV"] || process.env["_"]?.includes("ts-node") ||