From 2b4901cee06bc2627adae4ad2820079b2434d1f3 Mon Sep 17 00:00:00 2001 From: Romain Lenzotti Date: Wed, 27 Nov 2024 08:55:18 +0100 Subject: [PATCH] feat(cli): update template to use new Functional API when it's possible --- packages/cli/templates/generate/factory.hbs | 40 ++------- packages/cli/templates/generate/server.hbs | 10 +-- packages/cli/templates/generate/value.hbs | 33 +------ .../init.integration.spec.ts.snap | 90 ++++++------------- .../init/init.integration.spec.ts | 18 ++-- 5 files changed, 49 insertions(+), 142 deletions(-) diff --git a/packages/cli/templates/generate/factory.hbs b/packages/cli/templates/generate/factory.hbs index b4d258f6c..1a4746bbe 100644 --- a/packages/cli/templates/generate/factory.hbs +++ b/packages/cli/templates/generate/factory.hbs @@ -1,35 +1,11 @@ -import {Configuration, Inject, registerProvider} from "@tsed/di"; - -/** - * Inject {{symbolName}} to the service, controller, etc... - * - * ## Usage - * - * ```typescript - * import {Injectable} from "@tsed/di"; - * import {{{symbolName}}} from "./{{symbolName}}"; - * - * @Injectable() - * class MyService { - * @{{symbolName}}() - * private factory: {{symbolName}}; - * - * // or - * constructor(@{{symbolName}}() private factory: {{symbolName}}){} - * } - * - * @decorator - */ -export function {{symbolName}}() { - return Inject({{symbolName}}); -} +import {constant, injectable} from "@tsed/di"; export type {{symbolName}} = any; // implement type or interface for type checking +export const {{symbolName}} = injectable(Symbol.for("{{symbolName}}") + .factory(() => { + // retrieve custom configuration from settings (e.g.: `@Configuration({hello: "value"})`) + const hello = constant("hello"); -registerProvider({ - provide: {{symbolName}}, - deps: [Configuration], - useFactory(settings: Configuration) { - return {}; - } -}); + return {hello}; + }) + .token(); diff --git a/packages/cli/templates/generate/server.hbs b/packages/cli/templates/generate/server.hbs index 85d83094f..654310ebf 100644 --- a/packages/cli/templates/generate/server.hbs +++ b/packages/cli/templates/generate/server.hbs @@ -1,6 +1,6 @@ import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; {{#forEach imports}}{{#if tsIngore}} // @ts-ignore {{/if}}import {{symbols}}{{#if symbols}} from {{/if}}"{{from}}";{{comment}} @@ -53,9 +53,5 @@ import {PlatformApplication} from "@tsed/platform-http"; ] }) export class {{symbolName}} { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } diff --git a/packages/cli/templates/generate/value.hbs b/packages/cli/templates/generate/value.hbs index be8555d26..0ac52c3c2 100644 --- a/packages/cli/templates/generate/value.hbs +++ b/packages/cli/templates/generate/value.hbs @@ -1,32 +1,3 @@ -import {Configuration, Inject, registerProvider} from "@tsed/di"; +import {injectable} from "@tsed/di"; -/** - * Inject {{symbolName}} to the service, controller, etc... - * - * ## Usage - * - * ```typescript - * import {Injectable} from "@tsed/di"; - * import {{{symbolName}}} from "./{{symbolName}}"; - * - * @Injectable() - * class MyService { - * @{{symbolName}}() - * private value: {{symbolName}}; - * - * // or - * constructor(@{{symbolName}}() private value: {{symbolName}}){} - * } - * - * @decorator - */ -export function {{symbolName}}() { - return Inject({{symbolName}}); -} - -export type {{symbolName}} = any; // implement type or interface for type checking - -registerProvider({ - provide: {{symbolName}}, - useValue: {} -}); +export const {{symbolName}} = injectable(Symbol.for("{{symbolName}}")).value({}).token(); diff --git a/packages/cli/test/integrations/init/__snapshots__/init.integration.spec.ts.snap b/packages/cli/test/integrations/init/__snapshots__/init.integration.spec.ts.snap index de14ed6f9..bb642e808 100644 --- a/packages/cli/test/integrations/init/__snapshots__/init.integration.spec.ts.snap +++ b/packages/cli/test/integrations/init/__snapshots__/init.integration.spec.ts.snap @@ -2,8 +2,8 @@ exports[`Init cmd > Express.js > should generate a project with Arch FEATURE 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -54,19 +54,15 @@ import * as pages from "./pages/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; exports[`Init cmd > Express.js > should generate a project with Babel 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -106,19 +102,15 @@ import * as rest from "./controllers/rest/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; exports[`Init cmd > Express.js > should generate a project with Bun 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -158,19 +150,15 @@ import * as rest from "./controllers/rest/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; exports[`Init cmd > Express.js > should generate a project with Convention ANGULAR 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -221,19 +209,15 @@ import * as pages from "./controllers/pages/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; exports[`Init cmd > Express.js > should generate a project with NPM 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -273,19 +257,15 @@ import * as rest from "./controllers/rest/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; exports[`Init cmd > Express.js > should generate a project with Webpack 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -325,19 +305,15 @@ import * as rest from "./controllers/rest/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; exports[`Init cmd > Express.js > should generate a project with swagger 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -388,19 +364,15 @@ import * as pages from "./controllers/pages/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; exports[`Init cmd > Express.js > should generate a project with the right options 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -440,19 +412,15 @@ import * as rest from "./controllers/rest/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; exports[`Init cmd > Koa.js > should generate a project with the right options 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-koa"; // /!\\ keep this import import "@tsed/ajv"; @@ -490,11 +458,7 @@ import * as rest from "./controllers/rest/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; diff --git a/packages/cli/test/integrations/init/init.integration.spec.ts b/packages/cli/test/integrations/init/init.integration.spec.ts index 374a73893..620a473eb 100644 --- a/packages/cli/test/integrations/init/init.integration.spec.ts +++ b/packages/cli/test/integrations/init/init.integration.spec.ts @@ -69,7 +69,7 @@ describe("Init cmd", () => { `); const content = FakeCliFs.entries.get("project-name/src/Server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-express"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toMatchSnapshot(); @@ -178,7 +178,7 @@ describe("Init cmd", () => { const content = FakeCliFs.entries.get("project-name/src/Server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-express"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toContain('import * as pages from "./controllers/pages/index.js"'); @@ -270,7 +270,7 @@ describe("Init cmd", () => { `); const content = FakeCliFs.entries.get("project-name/src/Server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-express"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toMatchSnapshot(); @@ -362,7 +362,7 @@ describe("Init cmd", () => { `); const content = FakeCliFs.entries.get("project-name/src/Server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-express"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toMatchSnapshot(); @@ -455,7 +455,7 @@ describe("Init cmd", () => { `); const content = FakeCliFs.entries.get("project-name/src/Server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-express"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toMatchSnapshot(); @@ -548,7 +548,7 @@ describe("Init cmd", () => { `); const content = FakeCliFs.entries.get("project-name/src/Server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-express"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toMatchSnapshot(); @@ -649,7 +649,7 @@ describe("Init cmd", () => { `); const content = FakeCliFs.entries.get("project-name/src/server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-express"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toMatchSnapshot(); @@ -715,7 +715,7 @@ describe("Init cmd", () => { `); const content = FakeCliFs.entries.get("project-name/src/server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-express"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toMatchSnapshot(); @@ -777,7 +777,7 @@ describe("Init cmd", () => { const content = FakeCliFs.entries.get("project-name/src/Server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-koa"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toMatchSnapshot();