Skip to content

Commit

Permalink
Merge pull request #422 from tsedio/feat-update-index-ts
Browse files Browse the repository at this point in the history
Feat update index ts
  • Loading branch information
Romakita authored Nov 28, 2024
2 parents 92336fd + 784eaeb commit 551a6d1
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ exports[`Init OIDC Provider project > should generate a project with oidc 1`] =

exports[`Init OIDC Provider project > should generate a project with oidc 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 @@ -109,11 +109,7 @@ import {InteractionsController} from "./controllers/oidc/InteractionsController.
]
})
export class Server {
@Inject()
protected app: PlatformApplication;
@Configuration()
protected settings: Configuration;
protected app = application();
}
"
`;
Expand Down Expand Up @@ -178,8 +174,8 @@ exports[`Init OIDC Provider project > should generate a project with oidc and sw

exports[`Init OIDC Provider project > should generate a project with oidc and 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 @@ -233,11 +229,7 @@ import {InteractionsController} from "./controllers/oidc/InteractionsController.
]
})
export class Server {
@Inject()
protected app: PlatformApplication;
@Configuration()
protected settings: Configuration;
protected app = application();
}
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

exports[`Init TypeGraphQL project > should generate a project with typegraphql 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 @@ -46,11 +46,7 @@ import * as rest from "./controllers/rest/index.js";
]
})
export class Server {
@Inject()
protected app: PlatformApplication;
@Configuration()
protected settings: Configuration;
protected app = application();
}
"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

exports[`TypeORM: Init cmd > 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 @@ -43,11 +43,7 @@ import * as rest from "./controllers/rest/index.js";
]
})
export class Server {
@Inject()
protected app: PlatformApplication;
@Configuration()
protected settings: Configuration;
protected app = application();
}
"
`;
Expand Down
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();
40 changes: 29 additions & 11 deletions packages/cli/templates/init/src/index.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,35 @@ import {$log} from "@tsed/logger";
import { {{platformSymbol}} } from "@tsed/platform-{{platform}}";
import {Server} from "./{{entryServer}}.js";

async function bootstrap() {
try {
const platform = await {{platformSymbol}}.bootstrap(Server);
await platform.listen();
const SIG_EVENTS = [
"beforeExit",
"SIGHUP",
"SIGINT",
"SIGQUIT",
"SIGILL",
"SIGTRAP",
"SIGABRT",
"SIGBUS",
"SIGFPE",
"SIGUSR1",
"SIGSEGV",
"SIGUSR2",
"SIGTERM"
];

process.on("SIGINT", () => {
platform.stop();
});
} catch (error) {
$log.error({event: "SERVER_BOOTSTRAP_ERROR", message: error.message, stack: error.stack});
}
try {
const platform = await {{platformSymbol}}.bootstrap(Server);
await platform.listen();

SIG_EVENTS.forEach((evt) => process.on(evt, close));

["uncaughtException", "unhandledRejection"].forEach((evt) =>
process.on(evt, async (error) => {
$log.error({event: "SERVER_" + evt.toUpperCase(), message: error.message, stack: error.stack});
await stop();
})
);
} catch (error) {
$log.error({event: "SERVER_BOOTSTRAP_ERROR", message: error.message, stack: error.stack});
}

bootstrap();
Loading

0 comments on commit 551a6d1

Please sign in to comment.