Skip to content

Commit

Permalink
feat(cli): update template to use new Functional API when it's possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Nov 27, 2024
1 parent 5ebc03a commit 2b4901c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 142 deletions.
40 changes: 8 additions & 32 deletions packages/cli/templates/generate/factory.hbs
Original file line number Diff line number Diff line change
@@ -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();
10 changes: 3 additions & 7 deletions packages/cli/templates/generate/server.hbs
Original file line number Diff line number Diff line change
@@ -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}}
Expand Down Expand Up @@ -53,9 +53,5 @@ import {PlatformApplication} from "@tsed/platform-http";
]
})
export class {{symbolName}} {
@Inject()
protected app: PlatformApplication;

@Configuration()
protected settings: Configuration;
protected app = application();
}
33 changes: 2 additions & 31 deletions packages/cli/templates/generate/value.hbs
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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();
}
"
`;
18 changes: 9 additions & 9 deletions packages/cli/test/integrations/init/init.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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"');
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 2b4901c

Please sign in to comment.