Skip to content

Commit

Permalink
feat(di): add configuration fn api
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Oct 7, 2024
1 parent 35cdff0 commit 6c43b1a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 13 deletions.
28 changes: 28 additions & 0 deletions packages/di/src/common/fn/configuration.spec.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
6 changes: 6 additions & 0 deletions packages/di/src/common/fn/configuration.ts
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions packages/di/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
10 changes: 5 additions & 5 deletions packages/graphql/apollo/src/services/ApolloService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -177,14 +177,14 @@ export class ApolloService {
}, new Map<string, Provider>());

return async () => {
const $ctx = useContext<PlatformContext>();
const context: ApolloContext = {
const $ctx = context<PlatformContext>();
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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
};
5 changes: 4 additions & 1 deletion packages/platform/common/src/builder/PlatformBuilder.spec.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -105,6 +105,9 @@ class ServerModule implements BeforeInit, AfterInit, BeforeRoutesInit, AfterRout
}

describe("PlatformBuilder", () => {
beforeEach(() => {
injector().destroy();
});
describe("loadStatics()", () => {
it("should loadStatics", async () => {
// WHEN
Expand Down
5 changes: 0 additions & 5 deletions packages/third-parties/components-scan/src/isTsEnv.ts
Original file line number Diff line number Diff line change
@@ -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") ||
Expand Down

0 comments on commit 6c43b1a

Please sign in to comment.